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 { errorDisplayMessage } from "@t3tools/client-runtime/errors"; |
| 3 | import { |
| 4 | type AtomCommandResult, |
| 5 | isAtomCommandInterrupted, |
| 6 | squashAtomCommandFailure, |
| 7 | } from "@t3tools/client-runtime/state/runtime"; |
| 8 | import { |
| 9 | AuthOrchestrationOperateScope, |
| 10 | type ChatAttachment, |
| 11 | type ScopedThreadRef, |
| 12 | type ThreadGuestMessage, |
| 13 | type ThreadGuestShare, |
| 14 | } from "@t3tools/contracts"; |
| 15 | import { Atom } from "effect/reactivity"; |
| 16 | import { msg, useGT } from "gt-react-native"; |
| 17 | import { type ReactNode, useEffect, useMemo, useState } from "react"; |
| 18 | import { View } from "react-native"; |
| 19 | |
| 20 | import { Button } from "../../components/ui/Button"; |
| 21 | import { AppText as Text } from "../../components/ui/AppText"; |
| 22 | import { SymbolView } from "../../components/ui/AppSymbol"; |
| 23 | import * as Dialog from "../../components/ui/Dialog"; |
| 24 | import { m } from "../../i18n/messages"; |
| 25 | import { useEnvironmentScope } from "../../state/session"; |
| 26 | import { environmentThreadDetails, threadEnvironment } from "../../state/threads"; |
| 27 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 28 | import { GUEST_PENDING_BUBBLE_CLASS_NAME, GuestMessageName } from "./guestMessageStyle"; |
| 29 | |
| 30 | const NO_GUEST_SHARE_ATOM = Atom.make<ThreadGuestShare | null>(null); |
| 31 | |
| 32 | function guestShareAtom(ref: ScopedThreadRef) { |
| 33 | let previous: ThreadGuestShare | null = null; |
| 34 | return Atom.make((get) => { |
| 35 | const next = |
| 36 | get(environmentThreadDetails.threadAtom(ref))?.projection.thread.guestShare ?? null; |
| 37 | // The thread record is replaced on unrelated updates; only share changes matter here. |
| 38 | if (JSON.stringify(next) !== JSON.stringify(previous)) previous = next; |
| 39 | return previous; |
| 40 | }); |
| 41 | } |
| 42 | |
| 43 | export function useThreadGuestShare(threadRef: ScopedThreadRef | null): ThreadGuestShare | null { |
| 44 | const shareAtom = useMemo( |
| 45 | () => (threadRef === null ? NO_GUEST_SHARE_ATOM : guestShareAtom(threadRef)), |
| 46 | [threadRef], |
| 47 | ); |
| 48 | return useAtomValue(shareAtom); |
| 49 | } |
| 50 | |
| 51 | function alertOnFailure(failureTitle: string, result: AtomCommandResult<unknown, unknown>) { |
| 52 | if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { |
| 53 | const error = squashAtomCommandFailure(result); |
| 54 | Dialog.alert(failureTitle, errorDisplayMessage(error, m) ?? m(msg("Try again."))); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Owner controls for a guest link, above the composer: who the link is for and a way to stop |
| 60 | * sharing. Links are created from the thread's menu, see `ThreadGuestShareSheet`. Waiting |
| 61 | * messages show in the feed, see `ThreadGuestPendingMessage`. |
| 62 | */ |
| 63 | export function ThreadGuestShareBar(props: { |
| 64 | readonly threadRef: ScopedThreadRef | null; |
| 65 | readonly canOperateThread: boolean; |
| 66 | }) { |
| 67 | const { threadRef } = props; |
| 68 | const gt = useGT(); |
| 69 | const share = useThreadGuestShare(threadRef); |
| 70 | const closeGuestShare = useAtomCommand(threadEnvironment.closeGuestShare, "stop guest sharing"); |
| 71 | const [busy, setBusy] = useState(false); |
| 72 | // The server keeps an expired link so waiting messages stay resolvable; the label flips when |
| 73 | // it expires. |
| 74 | // Keyed by the expiry, so a replaced link (even one already expired) gets its own timer. |
| 75 | const [expiredAt, setExpiredAt] = useState<string | null>(null); |
| 76 | const expiresAt = share?.expiresAt; |
| 77 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- arms a timer that flips the share label when the guest link expires |
| 78 | useEffect(() => { |
| 79 | if (expiresAt === undefined) return; |
| 80 | const timer = setTimeout( |
| 81 | () => setExpiredAt(expiresAt), |
| 82 | Math.max(0, Date.parse(expiresAt) - Date.now()), |
| 83 | ); |
| 84 | return () => clearTimeout(timer); |
| 85 | }, [expiresAt]); |
| 86 | if (threadRef === null || !share) return null; |
| 87 | |
| 88 | const disabled = busy || !props.canOperateThread; |
| 89 | const stopSharing = () => |
| 90 | Dialog.confirm({ |
| 91 | title: gt("Stop sharing?"), |
| 92 | message: gt("The guest loses access and the link stops working."), |
| 93 | confirmText: gt("Stop sharing"), |
| 94 | destructive: true, |
| 95 | onConfirm: () => { |
| 96 | if (disabled) return; |
| 97 | setBusy(true); |
| 98 | void closeGuestShare({ |
| 99 | environmentId: threadRef.environmentId, |
| 100 | input: { threadId: threadRef.threadId }, |
| 101 | }).then((result) => { |
| 102 | setBusy(false); |
| 103 | alertOnFailure(gt("Could not stop sharing"), result); |
| 104 | }); |
| 105 | }, |
| 106 | }); |
| 107 | |
| 108 | return ( |
| 109 | <View className="flex-row items-center gap-2 px-4 pb-2"> |
| 110 | <SymbolView name="person.2" size={12} tintColorClassName="accent-foreground-muted" /> |
| 111 | <Text className="min-w-0 flex-1 text-xs text-foreground-muted" numberOfLines={1}> |
| 112 | {expiredAt === share.expiresAt |
| 113 | ? gt("Guest link expired") |
| 114 | : share.guestName === null |
| 115 | ? gt("Guest link active") |
| 116 | : gt("{guestName} is a guest", { guestName: share.guestName })} |
| 117 | </Text> |
| 118 | <Button label={gt("Stop sharing")} tone="text" disabled={disabled} onPress={stopSharing} /> |
| 119 | </View> |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * One guest message waiting at the end of the feed until the owner sends or dismisses it. Sent |
| 125 | * ones then show in the feed as user messages with the guest's name. |
| 126 | */ |
| 127 | export function ThreadGuestPendingMessage(props: { |
| 128 | readonly threadRef: ScopedThreadRef; |
| 129 | readonly guestName: string; |
| 130 | readonly message: ThreadGuestMessage; |
| 131 | readonly bubbleMaxWidth: number | `${number}%`; |
| 132 | /** The feed's own attachment rendering, so the owner sees the files before Send. */ |
| 133 | readonly renderAttachments: (attachments: ReadonlyArray<ChatAttachment>) => ReactNode; |
| 134 | }) { |
| 135 | const { threadRef, guestName, message } = props; |
| 136 | const gt = useGT(); |
| 137 | const canOperate = useEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope); |
| 138 | const resolveGuestMessage = useAtomCommand( |
| 139 | threadEnvironment.resolveGuestMessage, |
| 140 | "resolve guest message", |
| 141 | ); |
| 142 | // One request at a time: a second tap while the first is in flight would double-send. |
| 143 | const [busy, setBusy] = useState(false); |
| 144 | |
| 145 | const resolve = async (decision: "send" | "dismiss") => { |
| 146 | if (busy) return; |
| 147 | setBusy(true); |
| 148 | const result = await resolveGuestMessage({ |
| 149 | environmentId: threadRef.environmentId, |
| 150 | input: { |
| 151 | threadId: threadRef.threadId, |
| 152 | guestMessageId: message.id, |
| 153 | decision, |
| 154 | creationSource: "mobile", |
| 155 | }, |
| 156 | }); |
| 157 | setBusy(false); |
| 158 | alertOnFailure( |
| 159 | decision === "send" |
| 160 | ? gt("Could not send guest message") |
| 161 | : gt("Could not dismiss guest message"), |
| 162 | result, |
| 163 | ); |
| 164 | }; |
| 165 | |
| 166 | return ( |
| 167 | <View className="items-end pb-5"> |
| 168 | <GuestMessageName name={guestName} /> |
| 169 | <View className={GUEST_PENDING_BUBBLE_CLASS_NAME} style={{ maxWidth: props.bubbleMaxWidth }}> |
| 170 | {message.text ? ( |
| 171 | <Text selectable className="text-base text-foreground"> |
| 172 | {message.text} |
| 173 | </Text> |
| 174 | ) : null} |
| 175 | {props.renderAttachments(message.attachments ?? [])} |
| 176 | </View> |
| 177 | {canOperate ? ( |
| 178 | <View className="mt-1.5 flex-row gap-2"> |
| 179 | <Button |
| 180 | label={gt("Dismiss")} |
| 181 | tone="secondary" |
| 182 | accessibilityLabel={gt("Dismiss message from {guestName}", { guestName })} |
| 183 | disabled={busy} |
| 184 | onPress={() => void resolve("dismiss")} |
| 185 | /> |
| 186 | <Button |
| 187 | label={gt("Send", { $context: "verb, button" })} |
| 188 | tone="primary" |
| 189 | accessibilityLabel={gt("Send message from {guestName}", { guestName })} |
| 190 | disabled={busy} |
| 191 | onPress={() => void resolve("send")} |
| 192 | /> |
| 193 | </View> |
| 194 | ) : null} |
| 195 | </View> |
| 196 | ); |
| 197 | } |