|
1 | | -import { useAtomValue } from "@effect/atom-react"; |
2 | | -import { |
3 | | - EnvironmentId, |
4 | | - ProjectId, |
5 | | - type PullRequestActor, |
6 | | - type PullRequestFilterOptionsInput, |
7 | | -} from "@t3tools/contracts"; |
| 1 | +import { EnvironmentId, ProjectId, type PullRequestActor } from "@t3tools/contracts"; |
8 | 2 | import { |
9 | 3 | detectPullRequestMention, |
10 | 4 | insertPullRequestMention, |
11 | 5 | narrowPullRequestMentions, |
12 | | - pullRequestMentionSearch, |
13 | 6 | } from "@t3tools/shared/pullRequestComposerTrigger"; |
14 | | -import * as Option from "effect/Option"; |
15 | | -import { AsyncResult, Atom } from "effect/reactivity"; |
16 | 7 | import { useState } from "react"; |
17 | 8 | import type { TextInputSelectionChangeEvent } from "react-native"; |
18 | 9 |
|
19 | 10 | import { composerPullRequests } from "../../state/pull-requests"; |
| 11 | +import { useEnvironmentQuery } from "../../state/query"; |
20 | 12 | import { useDebouncedValue } from "../../state/queries"; |
21 | 13 |
|
22 | 14 | const MENTION_SUGGESTION_LIMIT = 6; |
23 | 15 |
|
24 | | -const mentionOptionsFamily = Atom.family((key: string) => { |
25 | | - const target: { |
26 | | - readonly environmentId: EnvironmentId; |
27 | | - readonly input: PullRequestFilterOptionsInput; |
28 | | - } | null = JSON.parse(key); |
29 | | - return Atom.make((get) => { |
30 | | - if (target === null) return { authors: [], waiting: false, failed: false }; |
31 | | - const result = get(composerPullRequests.filterOptions(target)); |
32 | | - return { |
33 | | - authors: Option.match(AsyncResult.value(result), { |
34 | | - onNone: () => [] as ReadonlyArray<PullRequestActor>, |
35 | | - onSome: (options) => options.authors, |
36 | | - }), |
37 | | - waiting: result.waiting, |
38 | | - failed: AsyncResult.isFailure(result), |
39 | | - }; |
40 | | - }); |
41 | | -}); |
42 | | - |
43 | 16 | /** Completes `@login` in a pull request text input from the repository's users. */ |
44 | 17 | export function usePullRequestMentions(input: { |
45 | 18 | readonly environmentId: string; |
46 | 19 | readonly projectId: string; |
47 | 20 | readonly value: string; |
48 | 21 | readonly onChangeValue: (value: string) => void; |
49 | | - readonly enabled?: boolean; |
| 22 | + /** The pull request's host completes mentions (`capabilities.mentions`), and the field takes them. */ |
| 23 | + readonly enabled: boolean; |
50 | 24 | }) { |
51 | 25 | const [selection, setSelection] = useState<{ start: number; end: number } | null>(null); |
52 | 26 | // Set only right after an insertion, so the native input keeps its own caret otherwise. |
53 | 27 | const [placed, setPlaced] = useState<{ start: number; end: number } | null>(null); |
54 | 28 | const trigger = |
55 | | - input.enabled === false || selection === null || selection.start !== selection.end |
| 29 | + !input.enabled || selection === null || selection.start !== selection.end |
56 | 30 | ? null |
57 | 31 | : detectPullRequestMention(input.value, selection.start); |
58 | 32 | const typed = trigger?.query ?? ""; |
59 | | - const search = pullRequestMentionSearch(typed); |
| 33 | + const search = typed === "" ? undefined : typed.slice(0, 100); |
60 | 34 | const sent = useDebouncedValue(search, 250); |
61 | | - const hosted = useAtomValue( |
62 | | - mentionOptionsFamily( |
63 | | - JSON.stringify( |
64 | | - trigger === null |
65 | | - ? null |
66 | | - : { |
67 | | - environmentId: EnvironmentId.make(input.environmentId), |
68 | | - input: { |
69 | | - kind: "authors", |
70 | | - projectId: ProjectId.make(input.projectId), |
71 | | - ...(sent ? { query: sent } : {}), |
72 | | - }, |
73 | | - }, |
74 | | - ), |
75 | | - ), |
| 35 | + const hosted = useEnvironmentQuery( |
| 36 | + trigger === null |
| 37 | + ? null |
| 38 | + : composerPullRequests.filterOptions({ |
| 39 | + environmentId: EnvironmentId.make(input.environmentId), |
| 40 | + input: { |
| 41 | + kind: "authors", |
| 42 | + projectId: ProjectId.make(input.projectId), |
| 43 | + ...(sent ? { query: sent } : {}), |
| 44 | + }, |
| 45 | + }), |
76 | 46 | ); |
77 | 47 | // Older answers stay useful while the debounced search catches up, narrowed to what is typed now. |
78 | | - const candidates = narrowPullRequestMentions(hosted.authors, typed, MENTION_SUGGESTION_LIMIT); |
| 48 | + const candidates = narrowPullRequestMentions( |
| 49 | + hosted.data?.authors ?? [], |
| 50 | + typed, |
| 51 | + MENTION_SUGGESTION_LIMIT, |
| 52 | + ); |
79 | 53 |
|
80 | 54 | return { |
81 | 55 | open: trigger !== null, |
82 | 56 | candidates, |
83 | | - isLoading: hosted.waiting || sent !== search, |
84 | | - failed: hosted.failed, |
| 57 | + isLoading: hosted.isPending || sent !== search, |
| 58 | + failed: hosted.error !== null, |
85 | 59 | selection: placed ?? undefined, |
86 | 60 | onSelectionChange: (event: TextInputSelectionChangeEvent) => { |
87 | 61 | setSelection(event.nativeEvent.selection); |
|
0 commit comments