|
| 1 | +import type { EnvironmentId } from "@t3tools/contracts"; |
| 2 | +import { formatComposerContextReference } from "@t3tools/shared/composerContextReferences"; |
| 3 | +import { |
| 4 | + checkoutAccessForDriver, |
| 5 | + checkoutCandidates, |
| 6 | + checkoutContextRecord, |
| 7 | + checkoutDisplayName, |
| 8 | + checkoutPathKey, |
| 9 | + looksLikeCheckoutPath, |
| 10 | + rankCheckoutCandidates, |
| 11 | +} from "@t3tools/shared/checkouts"; |
| 12 | +import { useMemo, useState, type ReactNode } from "react"; |
| 13 | +import { Alert, FlatList, Pressable, View } from "react-native"; |
| 14 | +import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 15 | + |
| 16 | +import { useEnvironmentServerConfig, useProjects, useThreadShells } from "../state/entities"; |
| 17 | +import { useDebouncedValue } from "../state/queries"; |
| 18 | +import { useEnvironmentQuery } from "../state/query"; |
| 19 | +import { sourceControlEnvironment } from "../state/sourceControl"; |
| 20 | +import { |
| 21 | + captureComposerDraftInsertion, |
| 22 | + insertComposerDraftContext, |
| 23 | + type ComposerDraftInsertion, |
| 24 | +} from "../state/use-composer-drafts"; |
| 25 | +import { AppText as Text, AppTextInput } from "./AppText"; |
| 26 | +import { PickerSheet } from "./PickerSheet"; |
| 27 | + |
| 28 | +export interface CheckoutPickerTarget { |
| 29 | + readonly environmentId: EnvironmentId; |
| 30 | + readonly draftKey: string; |
| 31 | + readonly driver: string | null; |
| 32 | +} |
| 33 | + |
| 34 | +type OpenedCheckoutPicker = CheckoutPickerTarget & { |
| 35 | + readonly insertion: ComposerDraftInsertion; |
| 36 | +}; |
| 37 | + |
| 38 | +/** Picks a local checkout to reference in the draft. The owner renders `sheet`. */ |
| 39 | +export function useCheckoutPicker(target: CheckoutPickerTarget | null): { |
| 40 | + readonly open: () => void; |
| 41 | + readonly sheet: ReactNode; |
| 42 | +} { |
| 43 | + const [opened, setOpened] = useState<OpenedCheckoutPicker | null>(null); |
| 44 | + return { |
| 45 | + open: () => { |
| 46 | + if (target) |
| 47 | + setOpened({ ...target, insertion: captureComposerDraftInsertion(target.draftKey) }); |
| 48 | + }, |
| 49 | + sheet: opened ? <CheckoutPickerSheet {...opened} onClose={() => setOpened(null)} /> : null, |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +function CheckoutPickerSheet(props: OpenedCheckoutPicker & { readonly onClose: () => void }) { |
| 54 | + const insets = useSafeAreaInsets(); |
| 55 | + const projects = useProjects(); |
| 56 | + const threads = useThreadShells(); |
| 57 | + const config = useEnvironmentServerConfig(props.environmentId); |
| 58 | + const [query, setQuery] = useState(""); |
| 59 | + const candidates = useMemo( |
| 60 | + () => |
| 61 | + checkoutCandidates({ |
| 62 | + currentEnvironmentId: props.environmentId, |
| 63 | + projects, |
| 64 | + worktrees: threads.map((thread) => ({ |
| 65 | + environmentId: thread.environmentId, |
| 66 | + worktreePath: thread.worktreePath, |
| 67 | + })), |
| 68 | + environmentLabel: (id) => |
| 69 | + id === props.environmentId |
| 70 | + ? (config?.environment.label ?? "This environment") |
| 71 | + : "Another environment", |
| 72 | + }).filter((candidate) => candidate.selectable), |
| 73 | + [config?.environment.label, projects, props.environmentId, threads], |
| 74 | + ); |
| 75 | + const ranked = useMemo( |
| 76 | + () => rankCheckoutCandidates(candidates, looksLikeCheckoutPath(query) ? "" : query, new Map()), |
| 77 | + [candidates, query], |
| 78 | + ); |
| 79 | + const pasted = looksLikeCheckoutPath(query) ? checkoutPathKey(query.trim()) : null; |
| 80 | + const paths = useMemo(() => { |
| 81 | + const listed = ranked.slice(0, 20).map((candidate) => candidate.path); |
| 82 | + if (pasted !== null && !listed.includes(pasted)) listed.unshift(pasted); |
| 83 | + return listed; |
| 84 | + }, [pasted, ranked]); |
| 85 | + const settled = useDebouncedValue(paths.join("\n"), 200); |
| 86 | + const inspection = useEnvironmentQuery( |
| 87 | + settled.length === 0 |
| 88 | + ? null |
| 89 | + : sourceControlEnvironment.checkouts({ |
| 90 | + environmentId: props.environmentId, |
| 91 | + input: { paths: settled.split("\n") }, |
| 92 | + }), |
| 93 | + ); |
| 94 | + const rows = useMemo(() => { |
| 95 | + const listed = ranked.slice(0, 20); |
| 96 | + if (pasted === null || listed.some((candidate) => checkoutPathKey(candidate.path) === pasted)) { |
| 97 | + return listed; |
| 98 | + } |
| 99 | + return [ |
| 100 | + { |
| 101 | + environmentId: props.environmentId, |
| 102 | + environmentLabel: "", |
| 103 | + selectable: true, |
| 104 | + path: pasted, |
| 105 | + name: checkoutDisplayName(pasted), |
| 106 | + kind: "project" as const, |
| 107 | + }, |
| 108 | + ...listed, |
| 109 | + ]; |
| 110 | + }, [pasted, props.environmentId, ranked]); |
| 111 | + |
| 112 | + const attach = (path: string, name: string) => { |
| 113 | + const entry = inspection.data?.entries.find( |
| 114 | + (candidate) => checkoutPathKey(candidate.path) === checkoutPathKey(path), |
| 115 | + ); |
| 116 | + const record = checkoutContextRecord({ |
| 117 | + path: entry?.path ?? path, |
| 118 | + name, |
| 119 | + branch: entry?.branch ?? null, |
| 120 | + headSha: entry?.headSha ?? null, |
| 121 | + changedFiles: entry?.changedFiles ?? 0, |
| 122 | + ahead: entry?.ahead ?? 0, |
| 123 | + access: checkoutAccessForDriver(props.driver), |
| 124 | + environmentId: props.environmentId, |
| 125 | + }); |
| 126 | + if ( |
| 127 | + !insertComposerDraftContext( |
| 128 | + props.draftKey, |
| 129 | + { |
| 130 | + text: formatComposerContextReference(record), |
| 131 | + context: { version: 1, records: [record] }, |
| 132 | + }, |
| 133 | + props.insertion, |
| 134 | + ) |
| 135 | + ) { |
| 136 | + Alert.alert("Too many context items", "Remove some context from the draft and try again."); |
| 137 | + return; |
| 138 | + } |
| 139 | + props.onClose(); |
| 140 | + }; |
| 141 | + |
| 142 | + return ( |
| 143 | + <PickerSheet title="Attach checkout" onClose={props.onClose}> |
| 144 | + <View className="px-4 pb-3"> |
| 145 | + <AppTextInput |
| 146 | + accessibilityLabel="Search checkouts" |
| 147 | + autoFocus |
| 148 | + autoCapitalize="none" |
| 149 | + autoCorrect={false} |
| 150 | + clearButtonMode="while-editing" |
| 151 | + placeholder="Search by name or paste a path" |
| 152 | + value={query} |
| 153 | + onChangeText={setQuery} |
| 154 | + /> |
| 155 | + </View> |
| 156 | + <FlatList |
| 157 | + data={rows} |
| 158 | + keyExtractor={(row) => row.path} |
| 159 | + keyboardShouldPersistTaps="handled" |
| 160 | + contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 16) }} |
| 161 | + ListEmptyComponent={ |
| 162 | + <View className="items-center p-6"> |
| 163 | + <Text className="text-center text-foreground-muted"> |
| 164 | + {pasted |
| 165 | + ? "Press return to use that path." |
| 166 | + : "No local checkouts yet. Paste a path to use a folder."} |
| 167 | + </Text> |
| 168 | + </View> |
| 169 | + } |
| 170 | + renderItem={({ item }) => { |
| 171 | + const entry = inspection.data?.entries.find( |
| 172 | + (candidate) => checkoutPathKey(candidate.path) === checkoutPathKey(item.path), |
| 173 | + ); |
| 174 | + const status = |
| 175 | + entry === undefined |
| 176 | + ? item.path |
| 177 | + : `${entry.branch ?? "detached"} · ${entry.changedFiles === 0 ? "clean" : `${entry.changedFiles} changed`}`; |
| 178 | + return ( |
| 179 | + <Pressable |
| 180 | + accessibilityRole="button" |
| 181 | + accessibilityLabel={`Attach ${item.name}`} |
| 182 | + onPress={() => attach(item.path, item.name)} |
| 183 | + className="gap-0.5 px-4 py-3 active:bg-subtle" |
| 184 | + > |
| 185 | + <Text className="text-base text-foreground" numberOfLines={1}> |
| 186 | + {item.name} |
| 187 | + </Text> |
| 188 | + <Text className="text-sm text-foreground-muted" numberOfLines={1}> |
| 189 | + {status} |
| 190 | + </Text> |
| 191 | + </Pressable> |
| 192 | + ); |
| 193 | + }} |
| 194 | + /> |
| 195 | + </PickerSheet> |
| 196 | + ); |
| 197 | +} |
0 commit comments