Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PlanSidebar.tsx

269 lines9.6 KB

1import { memo, useState, useCallback } from "react";
2import type { EnvironmentId } from "@t3tools/contracts";
3import { type TimestampFormat } from "@t3tools/contracts/settings";
4import { Badge } from "./ui/badge";
5import { Button } from "./ui/button";
6import { ScrollArea } from "./ui/scroll-area";
7import ChatMarkdown from "./ChatMarkdown";
8import {
9 CheckIcon,
10 ChevronDownIcon,
11 ChevronRightIcon,
12 EllipsisIcon,
13 LoaderIcon,
14 PanelRightCloseIcon,
15} from "lucide-react";
16import { cn } from "~/lib/utils";
17import type { ActivePlanState } from "../session-logic";
18import type { LatestProposedPlanState } from "../session-logic";
19import { formatTimestamp } from "../timestampFormat";
20import {
21 proposedPlanTitle,
22 buildProposedPlanMarkdownFilename,
23 normalizePlanMarkdownForExport,
24 downloadPlanAsTextFile,
25 stripDisplayedPlanMarkdown,
26} from "../proposedPlan";
27import { Menu, MenuItem, MenuPopup, MenuTrigger } from "./ui/menu";
28import { readEnvironmentApi } from "~/environmentApi";
29import { toastManager } from "./ui/toast";
30import { useCopyToClipboard } from "~/hooks/useCopyToClipboard";
31​
32function stepStatusIcon(status: string): React.ReactNode {
33 if (status === "completed") {
34 return (
35 <span className="flex size-5 shrink-0 items-center justify-center rounded-full bg-emerald-500/15 text-emerald-500">
36 <CheckIcon className="size-3" />
37 </span>
38 );
39 }
40 if (status === "inProgress") {
41 return (
42 <span className="flex size-5 shrink-0 items-center justify-center rounded-full bg-blue-500/15 text-blue-400">
43 <LoaderIcon className="size-3 animate-spin" />
44 </span>
45 );
46 }
47 return (
48 <span className="flex size-5 shrink-0 items-center justify-center rounded-full border border-border/60 bg-muted/30">
49 <span className="size-1.5 rounded-full bg-muted-foreground/30" />
50 </span>
51 );
53​
54interface PlanSidebarProps {
55 activePlan: ActivePlanState | null;
56 activeProposedPlan: LatestProposedPlanState | null;
57 environmentId: EnvironmentId;
58 markdownCwd: string | undefined;
59 workspaceRoot: string | undefined;
60 timestampFormat: TimestampFormat;
61 onClose: () => void;
63​
64const PlanSidebar = memo(function PlanSidebar({
65 activePlan,
66 activeProposedPlan,
67 environmentId,
68 markdownCwd,
69 workspaceRoot,
70 timestampFormat,
71 onClose,
72}: PlanSidebarProps) {
73 const [proposedPlanExpanded, setProposedPlanExpanded] = useState(false);
74 const [isSavingToWorkspace, setIsSavingToWorkspace] = useState(false);
75 const { copyToClipboard, isCopied } = useCopyToClipboard();
76​
77 const planMarkdown = activeProposedPlan?.planMarkdown ?? null;
78 const displayedPlanMarkdown = planMarkdown ? stripDisplayedPlanMarkdown(planMarkdown) : null;
79 const planTitle = planMarkdown ? proposedPlanTitle(planMarkdown) : null;
80​
81 const handleCopyPlan = useCallback(() => {
82 if (!planMarkdown) return;
83 copyToClipboard(planMarkdown);
84 }, [planMarkdown, copyToClipboard]);
85​
86 const handleDownload = useCallback(() => {
87 if (!planMarkdown) return;
88 const filename = buildProposedPlanMarkdownFilename(planMarkdown);
89 downloadPlanAsTextFile(filename, normalizePlanMarkdownForExport(planMarkdown));
90 }, [planMarkdown]);
91​
92 const handleSaveToWorkspace = useCallback(() => {
93 const api = readEnvironmentApi(environmentId);
94 if (!api || !workspaceRoot || !planMarkdown) return;
95 const filename = buildProposedPlanMarkdownFilename(planMarkdown);
96 setIsSavingToWorkspace(true);
97 void api.projects
98 .writeFile({
99 cwd: workspaceRoot,
100 relativePath: filename,
101 contents: normalizePlanMarkdownForExport(planMarkdown),
102 })
103 .then((result) => {
104 toastManager.add({
105 type: "success",
106 title: "Plan saved",
107 description: result.relativePath,
108 });
109 })
110 .catch((error) => {
111 toastManager.add({
112 type: "error",
113 title: "Could not save plan",
114 description: error instanceof Error ? error.message : "An error occurred.",
115 });
116 })
117 .then(
118 () => setIsSavingToWorkspace(false),
119 () => setIsSavingToWorkspace(false),
120 );
121 }, [environmentId, planMarkdown, workspaceRoot]);
122​
123 return (
124 <div className="flex h-full w-[340px] shrink-0 flex-col border-l border-border/70 bg-card/50">
125 {/* Header */}
126 <div className="flex h-12 shrink-0 items-center justify-between border-b border-border/60 px-3">
127 <div className="flex items-center gap-2">
128 <Badge
129 variant="secondary"
130 className="rounded-md bg-blue-500/10 px-1.5 py-0 text-[10px] font-semibold tracking-wide text-blue-400 uppercase"
131 >
132 Plan
133 </Badge>
134 {activePlan ? (
135 <span className="text-[11px] text-muted-foreground/60">
136 {formatTimestamp(activePlan.createdAt, timestampFormat)}
137 </span>
138 ) : null}
139 </div>
140 <div className="flex items-center gap-1">
141 {planMarkdown ? (
142 <Menu>
143 <MenuTrigger
144 render={
145 <Button
146 size="icon-xs"
147 variant="ghost"
148 className="text-muted-foreground/50 hover:text-foreground/70"
149 aria-label="Plan actions"
150 />
151 }
152 >
153 <EllipsisIcon className="size-3.5" />
154 </MenuTrigger>
155 <MenuPopup align="end">
156 <MenuItem onClick={handleCopyPlan}>
157 {isCopied ? "Copied!" : "Copy to clipboard"}
158 </MenuItem>
159 <MenuItem onClick={handleDownload}>Download as markdown</MenuItem>
160 <MenuItem
161 onClick={handleSaveToWorkspace}
162 disabled={!workspaceRoot || isSavingToWorkspace}
163 >
164 Save to workspace
165 </MenuItem>
166 </MenuPopup>
167 </Menu>
168 ) : null}
169 <Button
170 size="icon-xs"
171 variant="ghost"
172 onClick={onClose}
173 aria-label="Close plan sidebar"
174 className="text-muted-foreground/50 hover:text-foreground/70"
175 >
176 <PanelRightCloseIcon className="size-3.5" />
177 </Button>
178 </div>
179 </div>
180​
181 {/* Content */}
182 <ScrollArea className="min-h-0 flex-1">
183 <div className="p-3 space-y-4">
184 {/* Explanation */}
185 {activePlan?.explanation ? (
186 <p className="text-[13px] leading-relaxed text-muted-foreground/80">
187 {activePlan.explanation}
188 </p>
189 ) : null}
190​
191 {/* Plan Steps */}
192 {activePlan && activePlan.steps.length > 0 ? (
193 <div className="space-y-1">
194 <p className="mb-2 text-[10px] font-semibold tracking-widest text-muted-foreground/40 uppercase">
195 Steps
196 </p>
197 {activePlan.steps.map((step) => (
198 <div
199 key={`${step.status}:${step.step}`}
200 className={cn(
201 "flex items-start gap-2.5 rounded-lg px-2.5 py-2 transition-colors duration-200",
202 step.status === "inProgress" && "bg-blue-500/5",
203 step.status === "completed" && "bg-emerald-500/5",
204 )}
205 >
206 <div className="mt-0.5">{stepStatusIcon(step.status)}</div>
207 <p
208 className={cn(
209 "text-[13px] leading-snug",
210 step.status === "completed"
211 ? "text-muted-foreground/50 line-through decoration-muted-foreground/20"
212 : step.status === "inProgress"
213 ? "text-foreground/90"
214 : "text-muted-foreground/70",
215 )}
216 >
217 {step.step}
218 </p>
219 </div>
220 ))}
221 </div>
222 ) : null}
223​
224 {/* Proposed Plan Markdown */}
225 {planMarkdown ? (
226 <div className="space-y-2">
227 <button
228 type="button"
229 className="group flex w-full items-center gap-1.5 text-left"
230 onClick={() => setProposedPlanExpanded((v) => !v)}
231 >
232 {proposedPlanExpanded ? (
233 <ChevronDownIcon className="size-3 shrink-0 text-muted-foreground/40 transition-transform" />
234 ) : (
235 <ChevronRightIcon className="size-3 shrink-0 text-muted-foreground/40 transition-transform" />
236 )}
237 <span className="text-[10px] font-semibold tracking-widest text-muted-foreground/40 uppercase group-hover:text-muted-foreground/60">
238 {planTitle ?? "Full Plan"}
239 </span>
240 </button>
241 {proposedPlanExpanded ? (
242 <div className="rounded-lg border border-border/50 bg-background/50 p-3">
243 <ChatMarkdown
244 text={displayedPlanMarkdown ?? ""}
245 cwd={markdownCwd}
246 isStreaming={false}
247 />
248 </div>
249 ) : null}
250 </div>
251 ) : null}
252​
253 {/* Empty state */}
254 {!activePlan && !planMarkdown ? (
255 <div className="flex flex-col items-center justify-center py-12 text-center">
256 <p className="text-[13px] text-muted-foreground/40">No active plan yet.</p>
257 <p className="mt-1 text-[11px] text-muted-foreground/30">
258 Plans will appear here when generated.
259 </p>
260 </div>
261 ) : null}
262 </div>
263 </ScrollArea>
264 </div>
265 );
266});
267​
268export default PlanSidebar;
269export type { PlanSidebarProps };