"use client"; import { useCallback, useEffect, useState } from "react"; import { IconAlertTriangle, IconCopy, IconExternalLink } from "@tabler/icons-react"; import { Alert, AlertDescription } from "@kandev/ui/alert"; import { Button } from "@kandev/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@kandev/ui/dialog"; import { createShare, previewShare, type Share, type SnapshotPreview, } from "@/lib/api/domains/share-api"; import { useShares } from "@/hooks/domains/session/use-shares"; import { ShareList } from "./share-list"; import { ShareSnapshotPreview } from "./share-snapshot-preview"; type DialogState = | { kind: "loading" } | { kind: "preview"; snapshot: SnapshotPreview } | { kind: "publishing"; snapshot: SnapshotPreview } | { kind: "published"; share: Share } | { kind: "error"; message: string }; type Props = { open: boolean; onOpenChange: (open: boolean) => void; taskId: string; sessionId: string; }; export function ShareDialog({ open, onOpenChange, taskId, sessionId }: Props) { const [state, setState] = useState({ kind: "loading" }); const { shares, refresh } = useShares(open ? taskId : null, open ? sessionId : null); const loadPreview = useCallback(async () => { setState({ kind: "loading" }); try { const snapshot = await previewShare(taskId, sessionId); setState({ kind: "preview", snapshot }); } catch (e) { setState({ kind: "error", message: e instanceof Error ? e.message : "Failed to load preview.", }); } }, [taskId, sessionId]); useEffect(() => { if (!open) return; // Defer to a microtask so setState runs outside the synchronous effect // body (react-hooks/set-state-in-effect). void Promise.resolve().then(() => loadPreview()); }, [open, loadPreview]); const handlePublish = useCallback( async (snapshot: SnapshotPreview) => { setState({ kind: "publishing", snapshot }); try { const share = await createShare(taskId, sessionId); setState({ kind: "published", share }); void refresh(); } catch (e) { setState({ kind: "error", message: e instanceof Error ? e.message : "Failed to publish share.", }); } }, [taskId, sessionId, refresh], ); return ( Share this task Publish a redacted snapshot of this completed task as a secret GitHub Gist on your account. onOpenChange(false)} /> ); } type BodyProps = { state: DialogState; shares: Share[]; onPublish: (snapshot: SnapshotPreview) => void; onRevoked: () => void; onRetry: () => void; onClose: () => void; }; function DialogBody({ state, shares, onPublish, onRevoked, onRetry, onClose }: BodyProps) { if (state.kind === "loading") { return

Building preview…

; } if (state.kind === "error") { return (
{state.message}
); } if (state.kind === "published") { return ( ); } const snapshot = state.snapshot; const isPublishing = state.kind === "publishing"; return (
Anyone with this link can view this conversation. Review the preview carefully before publishing — once published, the snapshot is uploaded to GitHub Gist on your account.
); } function PublishedState({ share, shares, onRevoked, onClose, }: { share: Share; shares: Share[]; onRevoked: () => void; onClose: () => void; }) { const [copied, setCopied] = useState(false); const handleCopy = async () => { await navigator.clipboard.writeText(share.url); setCopied(true); setTimeout(() => setCopied(false), 1500); }; return (
Published. Anyone with this link can view the conversation.
{share.url}
); }