|
| 1 | +import { EnvironmentId, ProjectId, type PullRequestActor } from "@t3tools/contracts"; |
| 2 | +import { |
| 3 | + detectPullRequestMention, |
| 4 | + insertPullRequestMention, |
| 5 | + narrowPullRequestMentions, |
| 6 | +} from "@t3tools/shared/pullRequestComposerTrigger"; |
| 7 | +import { useState } from "react"; |
| 8 | +import type { TextInputSelectionChangeEvent } from "react-native"; |
| 9 | + |
| 10 | +import { composerPullRequests } from "../../state/pull-requests"; |
| 11 | +import { useEnvironmentQuery } from "../../state/query"; |
| 12 | +import { useDebouncedValue } from "../../state/queries"; |
| 13 | + |
| 14 | +const MENTION_SUGGESTION_LIMIT = 6; |
| 15 | + |
| 16 | +/** Completes `@login` in a pull request text input from the repository's users. */ |
| 17 | +export function usePullRequestMentions(input: { |
| 18 | + readonly environmentId: string; |
| 19 | + readonly projectId: string; |
| 20 | + readonly value: string; |
| 21 | + readonly onChangeValue: (value: string) => void; |
| 22 | + /** The pull request's host completes mentions (`capabilities.mentions`), and the field takes them. */ |
| 23 | + readonly enabled: boolean; |
| 24 | +}) { |
| 25 | + const [selection, setSelection] = useState<{ start: number; end: number } | null>(null); |
| 26 | + // Set only right after an insertion, so the native input keeps its own caret otherwise. |
| 27 | + const [placed, setPlaced] = useState<{ start: number; end: number } | null>(null); |
| 28 | + const trigger = |
| 29 | + !input.enabled || selection === null || selection.start !== selection.end |
| 30 | + ? null |
| 31 | + : detectPullRequestMention(input.value, selection.start); |
| 32 | + const typed = trigger?.query ?? ""; |
| 33 | + const search = typed === "" ? undefined : typed.slice(0, 100); |
| 34 | + const sent = useDebouncedValue(search, 250); |
| 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 | + }), |
| 46 | + ); |
| 47 | + // Older answers stay useful while the debounced search catches up, narrowed to what is typed now. |
| 48 | + const candidates = narrowPullRequestMentions( |
| 49 | + hosted.data?.authors ?? [], |
| 50 | + typed, |
| 51 | + MENTION_SUGGESTION_LIMIT, |
| 52 | + ); |
| 53 | + |
| 54 | + return { |
| 55 | + open: trigger !== null, |
| 56 | + candidates, |
| 57 | + isLoading: hosted.isPending || sent !== search, |
| 58 | + failed: hosted.error !== null, |
| 59 | + selection: placed ?? undefined, |
| 60 | + onSelectionChange: (event: TextInputSelectionChangeEvent) => { |
| 61 | + setSelection(event.nativeEvent.selection); |
| 62 | + setPlaced(null); |
| 63 | + }, |
| 64 | + onBlur: () => setSelection(null), |
| 65 | + select: (actor: PullRequestActor) => { |
| 66 | + if (trigger === null) return; |
| 67 | + const next = insertPullRequestMention(input.value, trigger, actor.login); |
| 68 | + input.onChangeValue(next.text); |
| 69 | + setSelection({ start: next.cursor, end: next.cursor }); |
| 70 | + setPlaced({ start: next.cursor, end: next.cursor }); |
| 71 | + }, |
| 72 | + }; |
| 73 | +} |
0 commit comments