The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { discardComposerDraft } from "../../lib/discardComposerDraft"; |
| 2 | import type { ContextMenuSource } from "@t3tools/contracts"; |
| 3 | import { contextMenuSourceOf } from "../../contextMenuSource"; |
| 4 | import { assistantCitationsToPlainText } from "@t3tools/shared/assistantCitations"; |
| 5 | import { replaceComposerContextReferences } from "@t3tools/shared/composerContextReferences"; |
| 6 | import { T, useGT } from "gt-react"; |
| 7 | import { SquarePenIcon, XIcon } from "lucide-react"; |
| 8 | import { |
| 9 | memo, |
| 10 | useState, |
| 11 | type KeyboardEvent as ReactKeyboardEvent, |
| 12 | type MouseEvent as ReactMouseEvent, |
| 13 | } from "react"; |
| 14 | import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; |
| 15 | import { useShallow } from "zustand/react/shallow"; |
| 16 | import { cn } from "~/lib/utils"; |
| 17 | import { resolveSidebarRowAccessibility } from "../Sidebar.logic"; |
| 18 | import { ProjectFavicon, type ProjectFaviconProject } from "../ProjectFavicon"; |
| 19 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 20 | import { |
| 21 | composerDraftHasUserContent, |
| 22 | DraftId, |
| 23 | useComposerDraftStore, |
| 24 | type ComposerThreadDraftState, |
| 25 | type DraftSessionState, |
| 26 | } from "../../composerDraftStore"; |
| 27 | import { draftPenClassName, draftSurfaceClassName } from "./SidebarThreadRow"; |
| 28 | |
| 29 | const SidebarDraftRow = memo(function SidebarDraftRow(props: { |
| 30 | draftId: DraftId; |
| 31 | composer: ComposerThreadDraftState; |
| 32 | project: ProjectFaviconProject | null; |
| 33 | projectDisplayName: string | null; |
| 34 | isActive: boolean; |
| 35 | onNavigate: (draftId: DraftId) => void; |
| 36 | onDiscard: (draftId: DraftId) => void; |
| 37 | onContextMenu: ( |
| 38 | draftId: DraftId, |
| 39 | position: { x: number; y: number }, |
| 40 | source: ContextMenuSource, |
| 41 | ) => void; |
| 42 | }) { |
| 43 | const { composer, draftId, onContextMenu, onDiscard, onNavigate } = props; |
| 44 | const gt = useGT(); |
| 45 | const promptPreview = |
| 46 | assistantCitationsToPlainText( |
| 47 | replaceComposerContextReferences(composer.prompt, (occurrence) => occurrence.label), |
| 48 | ) |
| 49 | .trim() |
| 50 | .split("\n", 1)[0] ?? ""; |
| 51 | // images mirrors persistedAttachments once rehydration finishes; before |
| 52 | // that only the persisted list is populated, hence max not sum. |
| 53 | const attachmentCount = |
| 54 | Math.max(composer.images.length, composer.persistedAttachments.length) + |
| 55 | composer.files.length + |
| 56 | composer.terminalContexts.length + |
| 57 | composer.previewAnnotations.length + |
| 58 | composer.reviewComments.length; |
| 59 | const preview = |
| 60 | promptPreview.length > 0 |
| 61 | ? promptPreview |
| 62 | : gt("{count, plural, one {{count} attachment} other {{count} attachments}}", { |
| 63 | count: attachmentCount, |
| 64 | }); |
| 65 | const accessibility = resolveSidebarRowAccessibility({ |
| 66 | title: preview, |
| 67 | statusLabel: gt("Unsent draft"), |
| 68 | projectDisplayName: props.projectDisplayName, |
| 69 | isActive: props.isActive, |
| 70 | }); |
| 71 | const handleActivate = () => onNavigate(draftId); |
| 72 | const handleKeyDown = (event: ReactKeyboardEvent) => { |
| 73 | // Keys targeting the nested discard button belong to the button: |
| 74 | // preventDefault here would swallow Space's synthesized click and |
| 75 | // navigate instead of discarding. |
| 76 | if ((event.target as HTMLElement).closest("button")) return; |
| 77 | if (event.key === "ContextMenu" || (event.shiftKey && event.key === "F10")) { |
| 78 | event.preventDefault(); |
| 79 | const rect = event.currentTarget.getBoundingClientRect(); |
| 80 | onContextMenu(draftId, { x: rect.left, y: rect.bottom }, "keyboard"); |
| 81 | } else if (event.key === "Enter" || event.key === " ") { |
| 82 | event.preventDefault(); |
| 83 | onNavigate(draftId); |
| 84 | } |
| 85 | }; |
| 86 | const handleContextMenu = (event: ReactMouseEvent) => { |
| 87 | event.preventDefault(); |
| 88 | onContextMenu(draftId, { x: event.clientX, y: event.clientY }, contextMenuSourceOf(event)); |
| 89 | }; |
| 90 | const handleDiscard = (event: ReactMouseEvent) => { |
| 91 | event.preventDefault(); |
| 92 | event.stopPropagation(); |
| 93 | onDiscard(draftId); |
| 94 | }; |
| 95 | return ( |
| 96 | <li className="list-none py-0.5"> |
| 97 | <div |
| 98 | role="button" |
| 99 | tabIndex={0} |
| 100 | data-sidebar-row={`draft:${draftId}`} |
| 101 | aria-label={accessibility.label} |
| 102 | aria-current={accessibility.current} |
| 103 | data-testid="sidebar-draft-row" |
| 104 | className={cn( |
| 105 | "group/sidebar-row relative w-full cursor-pointer overflow-hidden rounded-md text-left text-sidebar-foreground outline-none select-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring", |
| 106 | props.isActive ? "bg-sidebar-row-active" : draftSurfaceClassName, |
| 107 | )} |
| 108 | onClick={handleActivate} |
| 109 | onContextMenu={handleContextMenu} |
| 110 | onKeyDown={handleKeyDown} |
| 111 | > |
| 112 | <div className="relative z-10 h-[4.875rem] px-(--sidebar-row-content-inset) py-(--sidebar-content-inset)"> |
| 113 | <div className="flex h-5 min-w-0 items-center gap-1.5"> |
| 114 | <SquarePenIcon aria-hidden className={draftPenClassName} /> |
| 115 | {props.project ? ( |
| 116 | <ProjectFavicon project={props.project} className="size-4 shrink-0" /> |
| 117 | ) : null} |
| 118 | <span className="min-w-0 flex-1 truncate text-xs font-medium text-secondary-label"> |
| 119 | {props.projectDisplayName} |
| 120 | </span> |
| 121 | <span className="ml-auto flex h-5 min-w-5 shrink-0 items-center justify-end"> |
| 122 | <Tooltip> |
| 123 | <TooltipTrigger |
| 124 | render={ |
| 125 | <button |
| 126 | type="button" |
| 127 | aria-label={gt("Discard draft")} |
| 128 | onClick={handleDiscard} |
| 129 | className="pointer-events-none inline-flex cursor-pointer items-center rounded-md bg-transparent px-1 text-muted-foreground opacity-0 transition-opacity hover:text-foreground focus-visible:pointer-events-auto focus-visible:opacity-100 group-hover/sidebar-row:pointer-events-auto group-hover/sidebar-row:opacity-100" |
| 130 | > |
| 131 | <XIcon className="size-3" /> |
| 132 | </button> |
| 133 | } |
| 134 | /> |
| 135 | <TooltipPopup side="top"> |
| 136 | <T>Discard draft</T> |
| 137 | </TooltipPopup> |
| 138 | </Tooltip> |
| 139 | </span> |
| 140 | </div> |
| 141 | <div className="mt-0.5 truncate text-sm font-medium text-foreground/90">{preview}</div> |
| 142 | </div> |
| 143 | </div> |
| 144 | </li> |
| 145 | ); |
| 146 | }); |
| 147 | |
| 148 | interface SidebarDraftRowData { |
| 149 | draftId: DraftId; |
| 150 | session: DraftSessionState; |
| 151 | composer: ComposerThreadDraftState; |
| 152 | } |
| 153 | |
| 154 | export const SidebarDraftBlock = memo(function SidebarDraftBlock(props: { |
| 155 | projectByKey: ReadonlyMap<string, EnvironmentProject>; |
| 156 | projectDisplayNameByKey: ReadonlyMap<string, string>; |
| 157 | scopedProjectKeys: ReadonlySet<string> | null; |
| 158 | routeDraftId: string | null; |
| 159 | onNavigateToDraft: (draftId: DraftId) => void; |
| 160 | onDraftContextMenu: ( |
| 161 | draftId: DraftId, |
| 162 | position: { x: number; y: number }, |
| 163 | source: ContextMenuSource, |
| 164 | ) => void; |
| 165 | }) { |
| 166 | const draftThreadsByThreadKey = useComposerDraftStore((store) => store.draftThreadsByThreadKey); |
| 167 | const draftsByThreadKey = useComposerDraftStore( |
| 168 | useShallow((store) => |
| 169 | Object.fromEntries( |
| 170 | Object.keys(store.draftThreadsByThreadKey).map( |
| 171 | (key) => [key, store.draftsByThreadKey[key]] as const, |
| 172 | ), |
| 173 | ), |
| 174 | ), |
| 175 | ); |
| 176 | const [frozenActive, setFrozenActive] = useState<{ |
| 177 | routeDraftId: string | null; |
| 178 | row: SidebarDraftRowData | null; |
| 179 | }>({ routeDraftId: null, row: null }); |
| 180 | if (frozenActive.routeDraftId !== props.routeDraftId) { |
| 181 | let row: SidebarDraftRowData | null = null; |
| 182 | if (props.routeDraftId !== null) { |
| 183 | const draftId = DraftId.make(props.routeDraftId); |
| 184 | const session = draftThreadsByThreadKey[draftId]; |
| 185 | const composer = draftsByThreadKey[draftId]; |
| 186 | row = |
| 187 | session && session.promotedTo == null && composer && composerDraftHasUserContent(composer) |
| 188 | ? { draftId, session, composer } |
| 189 | : null; |
| 190 | } |
| 191 | setFrozenActive({ routeDraftId: props.routeDraftId, row }); |
| 192 | } |
| 193 | const drafts = (() => { |
| 194 | const rows: SidebarDraftRowData[] = []; |
| 195 | for (const [draftKey, session] of Object.entries(draftThreadsByThreadKey)) { |
| 196 | if (session.promotedTo != null) { |
| 197 | continue; |
| 198 | } |
| 199 | if ( |
| 200 | props.scopedProjectKeys !== null && |
| 201 | !props.scopedProjectKeys.has(`${session.environmentId}:${session.projectId}`) |
| 202 | ) { |
| 203 | continue; |
| 204 | } |
| 205 | if (draftKey === props.routeDraftId) { |
| 206 | if (frozenActive.routeDraftId === draftKey && frozenActive.row !== null) { |
| 207 | rows.push(frozenActive.row); |
| 208 | } |
| 209 | continue; |
| 210 | } |
| 211 | const composer = draftsByThreadKey[draftKey]; |
| 212 | if (!composer || !composerDraftHasUserContent(composer)) { |
| 213 | continue; |
| 214 | } |
| 215 | rows.push({ draftId: DraftId.make(draftKey), session, composer }); |
| 216 | } |
| 217 | rows.sort((left, right) => right.session.createdAt.localeCompare(left.session.createdAt)); |
| 218 | return rows; |
| 219 | })(); |
| 220 | if (drafts.length === 0) { |
| 221 | return null; |
| 222 | } |
| 223 | return ( |
| 224 | <> |
| 225 | {drafts.map(({ composer, draftId, session }) => { |
| 226 | const projectKey = `${session.environmentId}:${session.projectId}`; |
| 227 | return ( |
| 228 | <SidebarDraftRow |
| 229 | key={draftId} |
| 230 | draftId={draftId} |
| 231 | composer={composer} |
| 232 | project={props.projectByKey.get(projectKey) ?? null} |
| 233 | projectDisplayName={props.projectDisplayNameByKey.get(projectKey) ?? null} |
| 234 | isActive={draftId === props.routeDraftId} |
| 235 | onNavigate={props.onNavigateToDraft} |
| 236 | onDiscard={discardComposerDraft} |
| 237 | onContextMenu={props.onDraftContextMenu} |
| 238 | /> |
| 239 | ); |
| 240 | })} |
| 241 | <li |
| 242 | aria-hidden |
| 243 | data-testid="sidebar-draft-divider" |
| 244 | className="mx-2.5 my-1.5 h-px list-none bg-sidebar-border/60" |
| 245 | /> |
| 246 | </> |
| 247 | ); |
| 248 | }); |