|
| 1 | +import type { EnvironmentId, LinearIssueSummary } from "@t3tools/contracts"; |
| 2 | +import { useAtomValue } from "@effect/atom-react"; |
| 3 | +import { linearIssueContextRecord } from "@t3tools/client-runtime/state/linear"; |
| 4 | +import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; |
| 5 | +import { formatComposerContextReference } from "@t3tools/shared/composerContextReferences"; |
| 6 | +import * as Cause from "effect/Cause"; |
| 7 | +import * as Option from "effect/Option"; |
| 8 | +import { AsyncResult } from "effect/unstable/reactivity"; |
| 9 | +import { useNavigation } from "@react-navigation/native"; |
| 10 | +import { useState, type ReactNode } from "react"; |
| 11 | +import { ActivityIndicator, Alert, FlatList, Modal, Platform, Pressable, View } from "react-native"; |
| 12 | +import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 13 | + |
| 14 | +import { linearEnvironment } from "../state/linear"; |
| 15 | +import { useDebouncedValue } from "../state/queries"; |
| 16 | +import { useEnvironmentQuery } from "../state/query"; |
| 17 | +import { useAtomCommand } from "../state/use-atom-command"; |
| 18 | +import { |
| 19 | + captureComposerDraftInsertion, |
| 20 | + insertComposerDraftContext, |
| 21 | + type ComposerDraftInsertion, |
| 22 | +} from "../state/use-composer-drafts"; |
| 23 | +import { AppText as Text, AppTextInput } from "./AppText"; |
| 24 | + |
| 25 | +/** Linear allows 30 searches a minute, so typing settles before it queries. */ |
| 26 | +const SEARCH_DEBOUNCE_MS = 300; |
| 27 | + |
| 28 | +/** Linear's failures carry a reason; a rate limit deserves a clearer hint than its detail. */ |
| 29 | +function linearErrorMessage(error: unknown, fallback: string): string { |
| 30 | + if (typeof error === "object" && error !== null && "reason" in error) { |
| 31 | + if (error.reason === "rate-limited") return "Linear is busy. Try again shortly."; |
| 32 | + if (error.reason === "not-connected" || error.reason === "revoked") { |
| 33 | + return "Linear is not connected. Connect it in Settings."; |
| 34 | + } |
| 35 | + } |
| 36 | + return error instanceof Error && error.message.trim() ? error.message : fallback; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * The composer's Linear action. The owner renders `sheet` somewhere that outlives composer |
| 41 | + * focus: opening the sheet blurs the editor, which can unmount a focus-dependent toolbar. |
| 42 | + */ |
| 43 | +export function useLinearIssuePicker( |
| 44 | + target: { readonly environmentId: EnvironmentId; readonly draftKey: string } | null, |
| 45 | +): { readonly open: () => void; readonly sheet: ReactNode } { |
| 46 | + const navigation = useNavigation(); |
| 47 | + const connection = useEnvironmentQuery( |
| 48 | + target |
| 49 | + ? linearEnvironment.connection({ environmentId: target.environmentId, input: {} }) |
| 50 | + : null, |
| 51 | + ); |
| 52 | + const [opened, setOpened] = useState<{ |
| 53 | + readonly environmentId: EnvironmentId; |
| 54 | + readonly draftKey: string; |
| 55 | + readonly insertion: ComposerDraftInsertion; |
| 56 | + } | null>(null); |
| 57 | + const open = () => { |
| 58 | + if (!target) return; |
| 59 | + // Connecting is a settings task; the picker only attaches. |
| 60 | + if (connection.data && connection.data.phase !== "connected") { |
| 61 | + navigation.navigate("SettingsSheet", { |
| 62 | + screen: "SettingsContent", |
| 63 | + params: { screen: "SettingsLinear" }, |
| 64 | + }); |
| 65 | + return; |
| 66 | + } |
| 67 | + setOpened({ ...target, insertion: captureComposerDraftInsertion(target.draftKey) }); |
| 68 | + }; |
| 69 | + return { |
| 70 | + open, |
| 71 | + sheet: opened ? <LinearIssuePickerSheet {...opened} onClose={() => setOpened(null)} /> : null, |
| 72 | + }; |
| 73 | +} |
| 74 | + |
| 75 | +/** Attaches a Linear issue at the caret the composer had when the sheet opened. */ |
| 76 | +function LinearIssuePickerSheet(props: { |
| 77 | + readonly environmentId: EnvironmentId; |
| 78 | + readonly draftKey: string; |
| 79 | + readonly insertion: ComposerDraftInsertion; |
| 80 | + readonly onClose: () => void; |
| 81 | +}) { |
| 82 | + const insets = useSafeAreaInsets(); |
| 83 | + const connection = useEnvironmentQuery( |
| 84 | + linearEnvironment.connection({ environmentId: props.environmentId, input: {} }), |
| 85 | + ); |
| 86 | + const phase = connection.data?.phase; |
| 87 | + return ( |
| 88 | + <Modal presentationStyle="pageSheet" animationType="slide" onRequestClose={props.onClose}> |
| 89 | + <View |
| 90 | + className="flex-1 bg-sheet-solid" |
| 91 | + style={ |
| 92 | + Platform.OS === "android" |
| 93 | + ? { paddingTop: insets.top, paddingBottom: insets.bottom } |
| 94 | + : undefined |
| 95 | + } |
| 96 | + > |
| 97 | + <View className="flex-row items-center justify-between p-4"> |
| 98 | + <Text className="text-lg text-foreground">Linear issue</Text> |
| 99 | + <Pressable accessibilityRole="button" onPress={props.onClose} className="p-3"> |
| 100 | + <Text className="text-foreground">Cancel</Text> |
| 101 | + </Pressable> |
| 102 | + </View> |
| 103 | + {phase === "connected" ? ( |
| 104 | + <LinearIssueSearch {...props} /> |
| 105 | + ) : ( |
| 106 | + <View className="flex-1 items-center justify-center p-6"> |
| 107 | + {phase === undefined && !connection.error ? ( |
| 108 | + <ActivityIndicator /> |
| 109 | + ) : ( |
| 110 | + <Text className="text-center text-foreground-muted"> |
| 111 | + {connection.error ?? "Linear is not connected. Connect it in Settings > Linear."} |
| 112 | + </Text> |
| 113 | + )} |
| 114 | + </View> |
| 115 | + )} |
| 116 | + </View> |
| 117 | + </Modal> |
| 118 | + ); |
| 119 | +} |
| 120 | + |
| 121 | +function LinearIssueSearch(props: { |
| 122 | + readonly environmentId: EnvironmentId; |
| 123 | + readonly draftKey: string; |
| 124 | + readonly insertion: ComposerDraftInsertion; |
| 125 | + readonly onClose: () => void; |
| 126 | +}) { |
| 127 | + const insets = useSafeAreaInsets(); |
| 128 | + const [query, setQuery] = useState(""); |
| 129 | + const trimmed = query.trim(); |
| 130 | + const settled = useDebouncedValue(trimmed, SEARCH_DEBOUNCE_MS); |
| 131 | + // An empty query lists the viewer's open assigned issues. |
| 132 | + const result = useAtomValue( |
| 133 | + linearEnvironment.issues({ |
| 134 | + environmentId: props.environmentId, |
| 135 | + input: settled ? { query: settled } : {}, |
| 136 | + }), |
| 137 | + ); |
| 138 | + const getIssue = useAtomCommand(linearEnvironment.getIssue, { |
| 139 | + label: "linear issue fetch", |
| 140 | + reportFailure: false, |
| 141 | + }); |
| 142 | + const [attaching, setAttaching] = useState<string | null>(null); |
| 143 | + const issues = Option.getOrNull(AsyncResult.value(result))?.issues ?? []; |
| 144 | + const pending = settled !== trimmed || result.waiting; |
| 145 | + const error = |
| 146 | + result._tag === "Failure" |
| 147 | + ? linearErrorMessage(Cause.squash(result.cause), "Could not load Linear issues.") |
| 148 | + : null; |
| 149 | + |
| 150 | + const attach = async (issue: LinearIssueSummary) => { |
| 151 | + if (attaching) return; |
| 152 | + setAttaching(issue.id); |
| 153 | + try { |
| 154 | + const fetched = await getIssue({ |
| 155 | + environmentId: props.environmentId, |
| 156 | + input: { id: issue.id }, |
| 157 | + }); |
| 158 | + if (fetched._tag === "Failure") { |
| 159 | + Alert.alert( |
| 160 | + "Could not attach issue", |
| 161 | + linearErrorMessage(squashAtomCommandFailure(fetched), "Try again."), |
| 162 | + ); |
| 163 | + return; |
| 164 | + } |
| 165 | + const record = linearIssueContextRecord(fetched.value); |
| 166 | + if ( |
| 167 | + !insertComposerDraftContext( |
| 168 | + props.draftKey, |
| 169 | + { |
| 170 | + text: formatComposerContextReference(record), |
| 171 | + context: { version: 1, records: [record] }, |
| 172 | + }, |
| 173 | + props.insertion, |
| 174 | + ) |
| 175 | + ) { |
| 176 | + Alert.alert("Too many context items", "Remove some context from the draft and try again."); |
| 177 | + return; |
| 178 | + } |
| 179 | + props.onClose(); |
| 180 | + } finally { |
| 181 | + setAttaching(null); |
| 182 | + } |
| 183 | + }; |
| 184 | + |
| 185 | + return ( |
| 186 | + <View className="flex-1"> |
| 187 | + <View className="px-4 pb-3"> |
| 188 | + <AppTextInput |
| 189 | + accessibilityLabel="Search Linear issues" |
| 190 | + autoFocus |
| 191 | + autoCapitalize="none" |
| 192 | + autoCorrect={false} |
| 193 | + clearButtonMode="while-editing" |
| 194 | + placeholder="Search, or paste ENG-123 or an issue link" |
| 195 | + returnKeyType="search" |
| 196 | + value={query} |
| 197 | + onChangeText={setQuery} |
| 198 | + /> |
| 199 | + </View> |
| 200 | + <FlatList |
| 201 | + data={issues} |
| 202 | + keyExtractor={(issue) => issue.id} |
| 203 | + keyboardShouldPersistTaps="handled" |
| 204 | + contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 16) }} |
| 205 | + ListHeaderComponent={ |
| 206 | + !trimmed && issues.length > 0 ? ( |
| 207 | + <Text className="px-4 pb-2 text-sm text-foreground-muted">Assigned to you</Text> |
| 208 | + ) : null |
| 209 | + } |
| 210 | + ListEmptyComponent={ |
| 211 | + <View className="items-center p-6"> |
| 212 | + {pending ? ( |
| 213 | + <ActivityIndicator /> |
| 214 | + ) : ( |
| 215 | + <Text |
| 216 | + selectable={error !== null} |
| 217 | + className={ |
| 218 | + error ? "text-center text-danger-foreground" : "text-center text-foreground-muted" |
| 219 | + } |
| 220 | + > |
| 221 | + {error ?? (trimmed ? "No matching issues." : "No open issues are assigned to you.")} |
| 222 | + </Text> |
| 223 | + )} |
| 224 | + </View> |
| 225 | + } |
| 226 | + renderItem={({ item }) => ( |
| 227 | + <Pressable |
| 228 | + accessibilityRole="button" |
| 229 | + accessibilityLabel={`${item.identifier} ${item.title}, ${item.stateName}`} |
| 230 | + disabled={attaching !== null} |
| 231 | + onPress={() => void attach(item)} |
| 232 | + className="flex-row items-center gap-3 px-4 py-3 active:bg-subtle disabled:opacity-60" |
| 233 | + > |
| 234 | + <View className="min-w-0 flex-1 gap-0.5"> |
| 235 | + <Text className="text-base text-foreground" numberOfLines={2}> |
| 236 | + {item.title} |
| 237 | + </Text> |
| 238 | + <Text className="text-sm text-foreground-muted" numberOfLines={1}> |
| 239 | + {item.identifier} · {item.stateName} |
| 240 | + </Text> |
| 241 | + </View> |
| 242 | + {attaching === item.id ? <ActivityIndicator /> : null} |
| 243 | + </Pressable> |
| 244 | + )} |
| 245 | + /> |
| 246 | + </View> |
| 247 | + ); |
| 248 | +} |
0 commit comments