-
Notifications
You must be signed in to change notification settings - Fork 326
/
SearchIssues.tsx
93 lines (85 loc) · 2.3 KB
/
SearchIssues.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { Form } from '@highlight-run/ui/components'
import { Select } from 'antd'
import React, { useState } from 'react'
import { useSearchIssuesLazyQuery } from '@/graph/generated/hooks'
import { IntegrationType } from '@/graph/generated/schemas'
import { useDebouncedValue } from '@/hooks/useDebouncedValue'
import { IssueTrackerIntegration } from '@/pages/IntegrationsPage/IssueTrackerIntegrations'
import styles from './SearchIssues.module.css'
export interface SearchOption {
value: string
label: string
id: string
url: string
}
export interface SearchIssuesProps {
onSelect: (option: SearchOption) => void
integration: IssueTrackerIntegration
project_id: string
}
export const SearchIssues = ({
onSelect,
integration,
project_id,
}: SearchIssuesProps) => {
const [selectedOption, setSelectOption] = React.useState<
SearchOption | undefined
>({
label: '',
value: '',
id: '',
url: '',
})
const [query, setQuery] = useState<string>('')
const debouncedQuery = useDebouncedValue(query) || ''
const [searchIssues, { data, loading }] = useSearchIssuesLazyQuery()
React.useEffect(() => {
debouncedQuery &&
searchIssues({
variables: {
project_id,
query: debouncedQuery,
integration_type: integration.name as IntegrationType,
},
fetchPolicy: 'no-cache',
})
}, [searchIssues, project_id, debouncedQuery, integration])
const options = React.useMemo(() => {
return (
data?.search_issues.map((s) => ({
id: s.id,
url: s.issue_url,
value: s.issue_url,
label: s.title,
})) || []
)
}, [data]) as SearchOption[]
return (
<Form.NamedSection label="Link an issue" name="issue_id">
<Select
className={styles.select}
// this mode allows using the select component as a single searchable input
// @ts-ignore
placeholder="Search Issues"
autoFocus
size="middle"
// @ts-ignore
onSelect={(newValue: string) => {
const option = options.find((o) => o.value === newValue)
if (option) {
onSelect(option)
setSelectOption(option)
}
}}
defaultValue={selectedOption as unknown as SearchOption}
options={options}
notFoundContent={<span>`No issues found`</span>}
filterOption={false}
loading={loading}
onSearch={setQuery}
showSearch
showArrow={loading}
/>
</Form.NamedSection>
)
}