The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import { useNavigation, usePreventRemove, type StaticScreenProps } from "@react-navigation/native"; |
| 3 | import { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 4 | import { |
| 5 | isAtomCommandInterrupted, |
| 6 | squashAtomCommandFailure, |
| 7 | } from "@t3tools/client-runtime/state/runtime"; |
| 8 | import type { EnvironmentId, ThreadGuestShare, ThreadId } from "@t3tools/contracts"; |
| 9 | import { msg, T, useGT, useMessages } from "gt-react-native"; |
| 10 | import * as DateTime from "effect/DateTime"; |
| 11 | import { useMemo, useState } from "react"; |
| 12 | import { Platform, ScrollView, Share, View } from "react-native"; |
| 13 | import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 14 | |
| 15 | import { AppBar } from "../../components/ui/AppBar"; |
| 16 | import { SymbolView } from "../../components/ui/AppSymbol"; |
| 17 | import { AppText as Text } from "../../components/ui/AppText"; |
| 18 | import { Button } from "../../components/ui/Button"; |
| 19 | import * as Dialog from "../../components/ui/Dialog"; |
| 20 | import { ScreenContent } from "../../components/ui/ScreenContent"; |
| 21 | import { SheetHeader } from "../../components/ui/SheetHeader"; |
| 22 | import { useDeadlinePassed } from "../../lib/clock"; |
| 23 | import { tryCopyTextWithHaptic } from "../../lib/copyTextWithHaptic"; |
| 24 | import { NativeStackScreenOptions } from "../../native/StackHeader"; |
| 25 | import { serverEnvironment } from "../../state/server"; |
| 26 | import { environmentThreadDetails } from "../../state/threads"; |
| 27 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 28 | import { useThreadGuestShare } from "./ThreadGuestShareBar"; |
| 29 | |
| 30 | const TITLE = msg("Share with guest"); |
| 31 | const COPY_FEEDBACK_DURATION_MS = 1200; |
| 32 | const PENDING_MESSAGES_NOTICE = msg( |
| 33 | "Send or dismiss pending guest messages in the thread before creating a new link.", |
| 34 | ); |
| 35 | |
| 36 | type CreateState = |
| 37 | | { readonly status: "idle" } |
| 38 | | { readonly status: "creating" } |
| 39 | | { readonly status: "created"; readonly url: string; readonly share: ThreadGuestShare } |
| 40 | | { readonly status: "failed"; readonly message: string }; |
| 41 | |
| 42 | type GuestShareTarget = { readonly environmentId: EnvironmentId; readonly threadId: ThreadId }; |
| 43 | |
| 44 | export function ThreadGuestShareSheet({ route }: StaticScreenProps<GuestShareTarget>) { |
| 45 | const gt = useGT(); |
| 46 | const m = useMessages(); |
| 47 | const navigation = useNavigation(); |
| 48 | const insets = useSafeAreaInsets(); |
| 49 | const { environmentId, threadId } = route.params; |
| 50 | const threadRef = useMemo( |
| 51 | () => scopeThreadRef(environmentId, threadId), |
| 52 | [environmentId, threadId], |
| 53 | ); |
| 54 | const guestShare = useThreadGuestShare(threadRef); |
| 55 | const shareExpired = useDeadlinePassed( |
| 56 | guestShare === null ? null : Date.parse(guestShare.expiresAt), |
| 57 | ); |
| 58 | const activeShare = shareExpired ? null : guestShare; |
| 59 | const hasPendingMessages = |
| 60 | guestShare?.messages.some((message) => message.status === "pending") ?? false; |
| 61 | // From the thread list the detail loads only now; until it does, an active link is unknown and a |
| 62 | // new link would end it without the warning. |
| 63 | const thread = useAtomValue(environmentThreadDetails.threadAtom(threadRef)); |
| 64 | const threadLoaded = thread !== null; |
| 65 | const threadDeleted = useAtomValue( |
| 66 | environmentThreadDetails.statusAtom(threadRef), |
| 67 | (status) => status === "deleted", |
| 68 | ); |
| 69 | const threadLoadError = useAtomValue(environmentThreadDetails.errorAtom(threadRef)); |
| 70 | const canCreate = useAtomValue(serverEnvironment.createGuestShare.permissionAtom(environmentId)); |
| 71 | const createGuestShare = useAtomCommand(serverEnvironment.createGuestShare, { |
| 72 | reportFailure: false, |
| 73 | }); |
| 74 | const [state, setState] = useState<CreateState>({ status: "idle" }); |
| 75 | const [copiedAtMs, setCopiedAtMs] = useState<number | null>(null); |
| 76 | const copyFeedbackEnded = useDeadlinePassed( |
| 77 | copiedAtMs === null ? null : copiedAtMs + COPY_FEEDBACK_DURATION_MS, |
| 78 | ); |
| 79 | const copied = copiedAtMs !== null && !copyFeedbackEnded; |
| 80 | const creating = state.status === "creating"; |
| 81 | const close = () => navigation.goBack(); |
| 82 | // The link is shown only once, so any dismiss mid-request (swipe, header, Android back) would lose it. |
| 83 | usePreventRemove(creating, () => {}); |
| 84 | |
| 85 | const create = async () => { |
| 86 | if (hasPendingMessages) { |
| 87 | Dialog.alert(gt("Guest messages are waiting"), m(PENDING_MESSAGES_NOTICE)); |
| 88 | return; |
| 89 | } |
| 90 | setState({ status: "creating" }); |
| 91 | const result = await createGuestShare({ environmentId, input: { threadId } }); |
| 92 | if (result._tag === "Success") { |
| 93 | setState({ status: "created", url: result.value.url, share: result.value.share }); |
| 94 | return; |
| 95 | } |
| 96 | if (isAtomCommandInterrupted(result)) { |
| 97 | setState({ status: "idle" }); |
| 98 | return; |
| 99 | } |
| 100 | const error = squashAtomCommandFailure(result); |
| 101 | setState({ |
| 102 | status: "failed", |
| 103 | message: error instanceof Error ? error.message : gt("Could not create a guest link."), |
| 104 | }); |
| 105 | }; |
| 106 | |
| 107 | const copy = async (url: string) => { |
| 108 | if (!(await tryCopyTextWithHaptic(url, { target: "guest link" }))) { |
| 109 | Dialog.alert(gt("Could not copy"), gt("Try again.")); |
| 110 | return; |
| 111 | } |
| 112 | setCopiedAtMs(Date.now()); |
| 113 | }; |
| 114 | |
| 115 | const share = (url: string) => { |
| 116 | // Android ignores `url`; iOS turns a `message` URL into plain text instead of a link preview. |
| 117 | Share.share(Platform.OS === "ios" ? { url } : { message: url }).catch((cause: unknown) => { |
| 118 | console.error("Failed to share guest link.", cause); |
| 119 | Dialog.alert(gt("Could not share the link"), gt("Copy it instead.")); |
| 120 | }); |
| 121 | }; |
| 122 | |
| 123 | const createdShareExpired = useDeadlinePassed( |
| 124 | state.status === "created" ? Date.parse(state.share.expiresAt) : null, |
| 125 | ); |
| 126 | const createdLink = |
| 127 | state.status === "created" && |
| 128 | !createdShareExpired && |
| 129 | !threadDeleted && |
| 130 | (thread === null || |
| 131 | DateTime.toEpochMillis(thread.projection.thread.updatedAt) < |
| 132 | Date.parse(state.share.createdAt) || |
| 133 | (activeShare?.id === state.share.id && activeShare.guestName === null)) |
| 134 | ? state |
| 135 | : null; |
| 136 | |
| 137 | const content = ( |
| 138 | <ScrollView |
| 139 | nestedScrollEnabled |
| 140 | className="flex-1" |
| 141 | contentInsetAdjustmentBehavior={Platform.OS === "ios" ? "automatic" : "never"} |
| 142 | contentContainerClassName="gap-5 px-5 pt-4" |
| 143 | contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 16) + 8 }} |
| 144 | > |
| 145 | {createdLink !== null ? ( |
| 146 | <> |
| 147 | <View className="gap-2"> |
| 148 | <View className="rounded-2xl border border-input-border bg-input px-3.5 py-3"> |
| 149 | <Text selectable numberOfLines={3} className="text-sm text-foreground"> |
| 150 | {createdLink.url} |
| 151 | </Text> |
| 152 | </View> |
| 153 | <Text className="px-1 text-xs text-foreground-muted"> |
| 154 | <T>Send it to one person. It works once and expires in 24 hours.</T> |
| 155 | </Text> |
| 156 | </View> |
| 157 | <View className="flex-row gap-3"> |
| 158 | <View className="flex-1"> |
| 159 | <Button |
| 160 | label={copied ? gt("Copied") : gt("Copy")} |
| 161 | icon={copied ? "checkmark" : "doc.on.doc"} |
| 162 | fullWidth |
| 163 | onPress={() => void copy(createdLink.url)} |
| 164 | /> |
| 165 | </View> |
| 166 | <View className="flex-1"> |
| 167 | <Button |
| 168 | label={gt("Share")} |
| 169 | icon="square.and.arrow.up" |
| 170 | tone="primary" |
| 171 | fullWidth |
| 172 | onPress={() => share(createdLink.url)} |
| 173 | /> |
| 174 | </View> |
| 175 | </View> |
| 176 | </> |
| 177 | ) : ( |
| 178 | <> |
| 179 | <View className="flex-row gap-3"> |
| 180 | <View className="size-10 items-center justify-center rounded-full border border-border bg-card"> |
| 181 | <SymbolView |
| 182 | name="person.badge.plus" |
| 183 | size={18} |
| 184 | tintColorClassName="accent-foreground" |
| 185 | /> |
| 186 | </View> |
| 187 | <Text className="min-w-0 flex-1 text-sm leading-normal text-foreground-secondary"> |
| 188 | <T> |
| 189 | The guest sees the thread as you do, including command output and the agent's |
| 190 | reasoning. Each message they send waits for you. |
| 191 | </T> |
| 192 | </Text> |
| 193 | </View> |
| 194 | <View className="gap-1.5"> |
| 195 | {!threadLoaded ? ( |
| 196 | <Text className="text-sm text-foreground-muted"> |
| 197 | {threadDeleted |
| 198 | ? gt("This thread was deleted.") |
| 199 | : (threadLoadError ?? gt("Checking for an active link…"))} |
| 200 | </Text> |
| 201 | ) : activeShare !== null ? ( |
| 202 | <Text className="text-sm text-foreground"> |
| 203 | {activeShare.guestName !== null |
| 204 | ? gt("{name} is a guest. A new link ends their access.", { |
| 205 | name: activeShare.guestName, |
| 206 | }) |
| 207 | : gt("A guest link is active. A new link ends the current one.")} |
| 208 | </Text> |
| 209 | ) : ( |
| 210 | <Text className="text-sm text-foreground"> |
| 211 | <T>The link works once and expires in 24 hours.</T> |
| 212 | </Text> |
| 213 | )} |
| 214 | {hasPendingMessages ? ( |
| 215 | <Text className="text-sm text-foreground-muted">{m(PENDING_MESSAGES_NOTICE)}</Text> |
| 216 | ) : null} |
| 217 | {state.status === "failed" ? ( |
| 218 | <Text accessibilityRole="alert" className="text-sm text-danger-foreground"> |
| 219 | {state.message} |
| 220 | </Text> |
| 221 | ) : null} |
| 222 | </View> |
| 223 | <Button |
| 224 | label={ |
| 225 | creating |
| 226 | ? gt("Creating link…") |
| 227 | : state.status === "failed" |
| 228 | ? gt("Try again") |
| 229 | : activeShare !== null |
| 230 | ? gt("Create new link") |
| 231 | : gt("Create link") |
| 232 | } |
| 233 | tone="primary" |
| 234 | fullWidth |
| 235 | loading={creating} |
| 236 | disabled={!canCreate || !threadLoaded || hasPendingMessages} |
| 237 | onPress={() => void create()} |
| 238 | /> |
| 239 | </> |
| 240 | )} |
| 241 | </ScrollView> |
| 242 | ); |
| 243 | |
| 244 | if (Platform.OS === "ios") { |
| 245 | return ( |
| 246 | <View collapsable={false} className="flex-1 bg-sheet"> |
| 247 | <SheetHeader |
| 248 | presentation="native" |
| 249 | title={m(TITLE)} |
| 250 | onClose={close} |
| 251 | dismissDisabled={creating} |
| 252 | /> |
| 253 | {content} |
| 254 | </View> |
| 255 | ); |
| 256 | } |
| 257 | |
| 258 | return ( |
| 259 | <View collapsable={false} className="flex-1 bg-header"> |
| 260 | <NativeStackScreenOptions options={{ sheetCornerRadius: 28 }} /> |
| 261 | <AppBar sheet title={m(TITLE)} hideBottomBorder onBack={close} /> |
| 262 | <ScreenContent>{content}</ScreenContent> |
| 263 | </View> |
| 264 | ); |
| 265 | } |