-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom autocomplete component with jira features
- Loading branch information
Showing
5 changed files
with
189 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
web/src/jira/JiraAuthLink.tsx → web/src/jira/components/JiraAuthLink.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { Link } from "@mui/material"; | ||
import { keijoJiraApiUrl } from "./jiraConfig"; | ||
import { keijoJiraApiUrl } from "../jiraConfig"; | ||
|
||
export const JiraAuthLink = <Link href={keijoJiraApiUrl}>Authorize Keijo to use Jira</Link>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { Autocomplete, ListItem, TextField } from "@mui/material"; | ||
import { FieldValues, UseFormReturn } from "react-hook-form"; | ||
import { FindDimensionOptionsDocument } from "../../graphql/generated/graphql"; | ||
import { useGetIssues, useSearchIssues } from "../../jira/jiraApi"; | ||
import { issueKeyToSummary } from "../../jira/jiraUtils"; | ||
import { jiraQueryMaxResults } from "../../jira/jiraConfig"; | ||
import useInfiniteScroll from "react-infinite-scroll-hook"; | ||
import { useDebounceValue } from "usehooks-ts"; | ||
import { useQuery } from "@apollo/client"; | ||
|
||
type JiraIssueFieldProps<T extends FieldValues> = { | ||
form: UseFormReturn<T>; | ||
title: string; | ||
options: Array<string>; | ||
value: unknown; | ||
onChange: (val: unknown) => void; | ||
}; | ||
|
||
const JiraIssueAutoComplete = <T extends FieldValues>({ | ||
form, | ||
title, | ||
options, | ||
value, | ||
onChange, | ||
}: JiraIssueFieldProps<T>) => { | ||
const { loading } = useQuery(FindDimensionOptionsDocument); | ||
|
||
const [issueFilter, setIssueFilter] = useDebounceValue("", 300); | ||
const { | ||
data: dataPages, | ||
error, | ||
fetchNextPage, | ||
hasNextPage, | ||
isLoading: pagesLoading, | ||
} = useGetIssues({ issueKeys: options, enabled: !loading }); | ||
|
||
const { data: searchedIssueData, isLoading: searchLoading } = useSearchIssues({ | ||
issueKeys: options, | ||
searchFilter: issueFilter, | ||
}); | ||
|
||
const [sentryRef, { rootRef }] = useInfiniteScroll({ | ||
loading: pagesLoading, | ||
hasNextPage, | ||
disabled: !!error, | ||
onLoadMore: fetchNextPage, | ||
delayInMs: 0, | ||
}); | ||
|
||
const pagedIssueData = dataPages?.pages; | ||
const queriedKeys = [ | ||
...(pagedIssueData || []), | ||
...(searchedIssueData ? [searchedIssueData] : []), | ||
]; | ||
const keyToSummary = { ...issueKeyToSummary(queriedKeys) }; | ||
const queriedOptions = [ | ||
...new Set([ | ||
...options.slice(0, (dataPages?.pages.length || 1) * jiraQueryMaxResults), | ||
...Object.keys(keyToSummary), | ||
]), | ||
]; | ||
const getOptionText = (option: string) => | ||
keyToSummary[option] ? `${option}: ${keyToSummary[option]}` : option; | ||
|
||
return ( | ||
<Autocomplete | ||
value={value} | ||
loading={pagesLoading || searchLoading} | ||
onChange={(_, value) => onChange(value)} | ||
options={queriedOptions} | ||
renderOption={(props, option, state) => { | ||
const shouldLoadMore = (state.index + 1) % jiraQueryMaxResults === 0 && state.index > 0; | ||
return ( | ||
<ListItem {...props} ref={shouldLoadMore ? sentryRef : undefined}> | ||
{getOptionText(option as string)} | ||
</ListItem> | ||
); | ||
}} | ||
filterOptions={(options, state) => { | ||
if ((state.inputValue && state.inputValue !== issueFilter) || searchLoading) return []; | ||
return options.filter((option) => | ||
getOptionText(option as string) | ||
.toLowerCase() | ||
.trim() | ||
.includes(state.inputValue.toLowerCase().trim()), | ||
); | ||
}} | ||
ListboxProps={{ | ||
ref: rootRef, | ||
}} | ||
onInputChange={(_, value, reason) => { | ||
if (reason === "input") setIssueFilter(value); | ||
}} | ||
autoHighlight | ||
renderInput={(params) => ( | ||
<TextField | ||
{...params} | ||
label={title} | ||
onChange={onChange} | ||
error={!!form.formState.errors["issue"]} | ||
helperText={form.formState.errors["issue"]?.message as string} | ||
/> | ||
)} | ||
/> | ||
); | ||
}; | ||
|
||
export default JiraIssueAutoComplete; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters