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 { |
| 3 | isAtomCommandInterrupted, |
| 4 | squashAtomCommandFailure, |
| 5 | } from "@t3tools/client-runtime/state/runtime"; |
| 6 | import { |
| 7 | AuthOrchestrationOperateScope, |
| 8 | type ChatAttachment, |
| 9 | type GuestMessageId, |
| 10 | type ScopedThreadRef, |
| 11 | type ThreadGuestShare, |
| 12 | } from "@t3tools/contracts"; |
| 13 | import { msg, T, useGT } from "gt-react"; |
| 14 | import { CheckIcon, CopyIcon, UserIcon } from "lucide-react"; |
| 15 | import { useState } from "react"; |
| 16 | |
| 17 | import { useCopyToClipboard } from "~/hooks/useCopyToClipboard"; |
| 18 | import { useNowMinute } from "~/hooks/useNowMinute"; |
| 19 | import { appAtomRegistry } from "~/rpc/atomRegistry"; |
| 20 | import { useQuietThreadProjection } from "~/state/entities"; |
| 21 | import { serverEnvironment } from "~/state/server"; |
| 22 | import { useEnvironmentScope } from "~/state/session"; |
| 23 | import { threadEnvironment } from "~/state/threads"; |
| 24 | import { useAtomCommand } from "~/state/use-atom-command"; |
| 25 | import { cn } from "~/lib/utils"; |
| 26 | import { useOrchestrationCommand } from "~/state/use-orchestration-command"; |
| 27 | import { Button } from "../ui/button"; |
| 28 | import { |
| 29 | Dialog, |
| 30 | DialogDescription, |
| 31 | DialogFooter, |
| 32 | DialogHeader, |
| 33 | DialogPanel, |
| 34 | DialogPopup, |
| 35 | DialogTitle, |
| 36 | } from "../ui/dialog"; |
| 37 | import { Input } from "../ui/input"; |
| 38 | import { useAssetUrls } from "~/assets/assetUrls"; |
| 39 | import { type ChatFileAttachment, isImageAttachment } from "../../types"; |
| 40 | import { buildExpandedImagePreview, type ExpandedImagePreview } from "./ExpandedImagePreview"; |
| 41 | import { GuestMessageAttachments } from "./GuestMessageAttachments"; |
| 42 | import { GuestMessageName, guestBubbleClassName } from "./guestMessageStyle"; |
| 43 | import { guestShareDialogThreadAtom } from "./guestShareDialogState"; |
| 44 | |
| 45 | function useThreadGuestShare(threadRef: ScopedThreadRef | null): ThreadGuestShare | null { |
| 46 | return useQuietThreadProjection(threadRef)?.projection.thread.guestShare ?? null; |
| 47 | } |
| 48 | |
| 49 | export function GuestShareDialogHost() { |
| 50 | const threadRef = useAtomValue(guestShareDialogThreadAtom); |
| 51 | if (threadRef === null) return null; |
| 52 | return ( |
| 53 | <GuestShareDialog |
| 54 | key={`${threadRef.environmentId}:${threadRef.threadId}`} |
| 55 | threadRef={threadRef} |
| 56 | onClose={() => appAtomRegistry.set(guestShareDialogThreadAtom, null)} |
| 57 | /> |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | type CreateState = |
| 62 | | { readonly status: "idle" } |
| 63 | | { readonly status: "creating" } |
| 64 | | { readonly status: "created"; readonly url: string } |
| 65 | | { readonly status: "failed"; readonly message: string }; |
| 66 | |
| 67 | function GuestShareDialog({ |
| 68 | threadRef, |
| 69 | onClose, |
| 70 | }: { |
| 71 | readonly threadRef: ScopedThreadRef; |
| 72 | readonly onClose: () => void; |
| 73 | }) { |
| 74 | const gt = useGT(); |
| 75 | const activeShare = useThreadGuestShare(threadRef); |
| 76 | const canCreate = useAtomValue( |
| 77 | serverEnvironment.createGuestShare.permissionAtom(threadRef.environmentId), |
| 78 | ); |
| 79 | const createGuestShare = useAtomCommand(serverEnvironment.createGuestShare, { |
| 80 | reportFailure: false, |
| 81 | }); |
| 82 | const { copyToClipboard, isCopied } = useCopyToClipboard({ |
| 83 | target: { |
| 84 | target: "guest link", |
| 85 | unavailableMessage: msg("Clipboard API is unavailable while copying guest link."), |
| 86 | failureMessage: msg("Failed to copy guest link to the clipboard."), |
| 87 | }, |
| 88 | }); |
| 89 | const [state, setState] = useState<CreateState>({ status: "idle" }); |
| 90 | |
| 91 | const create = async () => { |
| 92 | setState({ status: "creating" }); |
| 93 | const result = await createGuestShare({ |
| 94 | environmentId: threadRef.environmentId, |
| 95 | input: { threadId: threadRef.threadId }, |
| 96 | }); |
| 97 | if (result._tag === "Success") { |
| 98 | setState({ status: "created", url: result.value.url }); |
| 99 | return; |
| 100 | } |
| 101 | if (isAtomCommandInterrupted(result)) { |
| 102 | setState({ status: "idle" }); |
| 103 | return; |
| 104 | } |
| 105 | const error = squashAtomCommandFailure(result); |
| 106 | setState({ |
| 107 | status: "failed", |
| 108 | message: error instanceof Error ? error.message : gt("Could not create a guest link."), |
| 109 | }); |
| 110 | }; |
| 111 | |
| 112 | return ( |
| 113 | <Dialog |
| 114 | open |
| 115 | onOpenChange={(open) => { |
| 116 | if (!open && state.status !== "creating") onClose(); |
| 117 | }} |
| 118 | > |
| 119 | <DialogPopup className="max-w-lg"> |
| 120 | <DialogHeader> |
| 121 | <DialogTitle> |
| 122 | <T>Share with guest</T> |
| 123 | </DialogTitle> |
| 124 | <DialogDescription> |
| 125 | <T> |
| 126 | The guest sees the thread as you do, including command output and the agent's |
| 127 | reasoning. Each message they send waits for you. |
| 128 | </T> |
| 129 | </DialogDescription> |
| 130 | </DialogHeader> |
| 131 | <DialogPanel> |
| 132 | {state.status === "created" ? ( |
| 133 | <div className="flex items-center gap-2"> |
| 134 | <Input |
| 135 | readOnly |
| 136 | aria-label={gt("Guest link")} |
| 137 | value={state.url} |
| 138 | onFocus={(event) => event.currentTarget.select()} |
| 139 | /> |
| 140 | <Button size="sm" variant="outline" onClick={() => copyToClipboard(state.url)}> |
| 141 | {isCopied ? <CheckIcon /> : <CopyIcon />} |
| 142 | {isCopied ? gt("Copied") : gt("Copy", { $context: "verb, copy the link" })} |
| 143 | </Button> |
| 144 | </div> |
| 145 | ) : ( |
| 146 | <p className="text-muted-foreground text-sm"> |
| 147 | {activeShare !== null |
| 148 | ? gt("A new link ends the current one.") |
| 149 | : gt("The link works once and expires in 24 hours.")} |
| 150 | </p> |
| 151 | )} |
| 152 | {state.status === "failed" ? ( |
| 153 | <p role="alert" className="text-destructive text-xs"> |
| 154 | {state.message} |
| 155 | </p> |
| 156 | ) : null} |
| 157 | </DialogPanel> |
| 158 | <DialogFooter> |
| 159 | {state.status === "created" ? ( |
| 160 | <Button size="sm" onClick={onClose}> |
| 161 | <T context="button that closes the dialog">Done</T> |
| 162 | </Button> |
| 163 | ) : ( |
| 164 | <> |
| 165 | <Button |
| 166 | variant="outline" |
| 167 | size="sm" |
| 168 | onClick={onClose} |
| 169 | disabled={state.status === "creating"} |
| 170 | > |
| 171 | <T>Cancel</T> |
| 172 | </Button> |
| 173 | <Button |
| 174 | size="sm" |
| 175 | onClick={() => void create()} |
| 176 | disabled={!canCreate || state.status === "creating"} |
| 177 | > |
| 178 | {state.status === "creating" ? gt("Creating...") : gt("Create link")} |
| 179 | </Button> |
| 180 | </> |
| 181 | )} |
| 182 | </DialogFooter> |
| 183 | </DialogPopup> |
| 184 | </Dialog> |
| 185 | ); |
| 186 | } |
| 187 | |
| 188 | /** Header status for a shared thread, with the way to end the share. */ |
| 189 | export function GuestShareIndicator({ threadRef }: { readonly threadRef: ScopedThreadRef }) { |
| 190 | const share = useThreadGuestShare(threadRef); |
| 191 | const closeGuestShare = useOrchestrationCommand(threadEnvironment.closeGuestShare, { |
| 192 | label: "stop guest share", |
| 193 | }); |
| 194 | const [closing, setClosing] = useState(false); |
| 195 | if (share === null) return null; |
| 196 | return ( |
| 197 | <div className="flex shrink-0 items-center gap-1 text-muted-foreground text-xs"> |
| 198 | <UserIcon aria-hidden className="size-3.5" /> |
| 199 | <GuestShareStatus share={share} /> |
| 200 | <Button |
| 201 | size="xs" |
| 202 | variant="ghost-muted" |
| 203 | disabled={closing} |
| 204 | onClick={async () => { |
| 205 | setClosing(true); |
| 206 | await closeGuestShare({ |
| 207 | environmentId: threadRef.environmentId, |
| 208 | input: { threadId: threadRef.threadId }, |
| 209 | }); |
| 210 | setClosing(false); |
| 211 | }} |
| 212 | > |
| 213 | <T>Stop sharing</T> |
| 214 | </Button> |
| 215 | </div> |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | function GuestShareStatus({ share }: { readonly share: ThreadGuestShare }) { |
| 220 | const gt = useGT(); |
| 221 | const nowMinute = useNowMinute(); |
| 222 | // The share stays on the thread after it expires, until the owner clears it. |
| 223 | if (share.expiresAt.slice(0, 16) <= nowMinute) { |
| 224 | return ( |
| 225 | <span> |
| 226 | <T>Guest link expired</T> |
| 227 | </span> |
| 228 | ); |
| 229 | } |
| 230 | return ( |
| 231 | <span> |
| 232 | {share.guestName !== null |
| 233 | ? gt("{guestName} is a guest", { guestName: share.guestName }) |
| 234 | : gt("Guest link active")} |
| 235 | </span> |
| 236 | ); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * A guest's messages wait at the end of the timeline until the owner sends or dismisses each one. |
| 241 | * Sent ones then show in the timeline as normal user messages with the guest's name. |
| 242 | */ |
| 243 | export function GuestPendingMessages({ |
| 244 | threadRef, |
| 245 | onImageExpand, |
| 246 | onFileOpen, |
| 247 | }: { |
| 248 | readonly threadRef: ScopedThreadRef; |
| 249 | readonly onImageExpand: (preview: ExpandedImagePreview) => void; |
| 250 | readonly onFileOpen: (attachment: ChatFileAttachment) => void; |
| 251 | }) { |
| 252 | const gt = useGT(); |
| 253 | const share = useThreadGuestShare(threadRef); |
| 254 | const canOperate = useEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope); |
| 255 | const resolveGuestMessage = useOrchestrationCommand(threadEnvironment.resolveGuestMessage, { |
| 256 | label: "resolve guest message", |
| 257 | }); |
| 258 | const [busyIds, setBusyIds] = useState<ReadonlySet<GuestMessageId>>(new Set()); |
| 259 | const pending = share?.messages.filter((message) => message.status === "pending") ?? []; |
| 260 | // The files are the thread's attachments, which the owner's environment serves like any other. |
| 261 | const images = pending.flatMap((message) => message.attachments ?? []).filter(isImageAttachment); |
| 262 | const imageUrls = useAssetUrls( |
| 263 | threadRef.environmentId, |
| 264 | images.map((image) => ({ _tag: "attachment" as const, attachmentId: image.id })), |
| 265 | ); |
| 266 | const imageUrlById = new Map( |
| 267 | images.flatMap((image, index) => { |
| 268 | const url = imageUrls[index]; |
| 269 | return url ? [[image.id, url] as const] : []; |
| 270 | }), |
| 271 | ); |
| 272 | if (share === null || pending.length === 0) return null; |
| 273 | const guestName = share.guestName ?? gt("Guest"); |
| 274 | const openImage = (attachment: ChatAttachment) => { |
| 275 | const preview = buildExpandedImagePreview( |
| 276 | images.map((image) => ({ ...image, previewUrl: imageUrlById.get(image.id) ?? "" })), |
| 277 | attachment.id, |
| 278 | ); |
| 279 | if (preview) onImageExpand(preview); |
| 280 | }; |
| 281 | |
| 282 | const resolve = async (guestMessageId: GuestMessageId, decision: "send" | "dismiss") => { |
| 283 | setBusyIds((ids) => new Set(ids).add(guestMessageId)); |
| 284 | await resolveGuestMessage({ |
| 285 | environmentId: threadRef.environmentId, |
| 286 | input: { threadId: threadRef.threadId, guestMessageId, decision }, |
| 287 | }); |
| 288 | setBusyIds((ids) => { |
| 289 | const next = new Set(ids); |
| 290 | next.delete(guestMessageId); |
| 291 | return next; |
| 292 | }); |
| 293 | }; |
| 294 | |
| 295 | return ( |
| 296 | <ol |
| 297 | className="chat-content-lane flex flex-col gap-4 pb-4" |
| 298 | aria-label={gt( |
| 299 | "{count, plural, one {{count} guest message waiting} other {{count} guest messages waiting}}", |
| 300 | { count: pending.length }, |
| 301 | )} |
| 302 | aria-live="polite" |
| 303 | > |
| 304 | {pending.map((message) => { |
| 305 | const busy = busyIds.has(message.id); |
| 306 | return ( |
| 307 | <li key={message.id} className="flex flex-col items-end gap-1"> |
| 308 | <GuestMessageName name={guestName} /> |
| 309 | <div |
| 310 | className={cn( |
| 311 | "flex min-w-0 max-w-[80%] flex-col gap-2 rounded-2xl p-3 text-foreground", |
| 312 | guestBubbleClassName(true), |
| 313 | )} |
| 314 | > |
| 315 | <GuestMessageAttachments |
| 316 | attachments={message.attachments ?? []} |
| 317 | imageUrl={(attachment) => imageUrlById.get(attachment.id)} |
| 318 | onOpenImage={openImage} |
| 319 | onOpenFile={onFileOpen} |
| 320 | /> |
| 321 | {message.text ? ( |
| 322 | <p className="text-sm leading-relaxed whitespace-pre-wrap [overflow-wrap:anywhere]"> |
| 323 | {message.text} |
| 324 | </p> |
| 325 | ) : null} |
| 326 | </div> |
| 327 | {canOperate ? ( |
| 328 | <div className="flex items-center gap-1 pe-1"> |
| 329 | <Button |
| 330 | size="xs" |
| 331 | variant="ghost-muted" |
| 332 | disabled={busy} |
| 333 | aria-label={gt("Dismiss message from {guestName}", { guestName })} |
| 334 | onClick={() => void resolve(message.id, "dismiss")} |
| 335 | > |
| 336 | <T>Dismiss</T> |
| 337 | </Button> |
| 338 | <Button |
| 339 | size="xs" |
| 340 | disabled={busy} |
| 341 | aria-label={gt("Send message from {guestName}", { guestName })} |
| 342 | onClick={() => void resolve(message.id, "send")} |
| 343 | > |
| 344 | <T>Send</T> |
| 345 | </Button> |
| 346 | </div> |
| 347 | ) : null} |
| 348 | </li> |
| 349 | ); |
| 350 | })} |
| 351 | </ol> |
| 352 | ); |
| 353 | } |