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

ComposerActivityStatus.tsx

106 lines3.1 KB

1import { LoaderCircleIcon } from "lucide-react";
2import { useEffect, useRef } from "react";
3import { threadSyncLabel, type ThreadSyncPhase } from "../../threadSync";
4import { ComposerBanner } from "./ComposerBanner";
5​
6export type ComposerActivityStatus =
7 | { readonly kind: "working"; readonly startedAt: string | null }
8 | { readonly kind: "sync"; readonly phase: ThreadSyncPhase };
9​
10export function ComposerActivityIcon({ status }: { readonly status: ComposerActivityStatus }) {
11 if (status.kind !== "sync") return null;
12 return (
13 <ComposerBanner.Icon>
14 <LoaderCircleIcon className="motion-safe:animate-spin" />
15 </ComposerBanner.Icon>
16 );
18​
19export function ComposerActivityRow({ status }: { readonly status: ComposerActivityStatus }) {
20 return (
21 <ComposerBanner.Row>
22 <ComposerActivityIcon status={status} />
23 <ComposerBanner.Content>
24 <ComposerActivityLabel status={status} />
25 </ComposerBanner.Content>
26 </ComposerBanner.Row>
27 );
29​
30export function ComposerActivityLabel({ status }: { readonly status: ComposerActivityStatus }) {
31 if (status.kind === "sync") {
32 return (
33 <span
34 className="shrink-0 whitespace-nowrap text-muted-foreground"
35 data-composer-sync-status={status.phase}
36 role="status"
37 >
38 {threadSyncLabel(status.phase)}
39 </span>
40 );
41 }
42 return (
43 <span
44 className="shrink-0 whitespace-nowrap text-muted-foreground"
45 data-composer-working-status="true"
46 >
47 {status.startedAt ? (
48 <>
49 Working for <WorkingTimer createdAt={status.startedAt} />
50 </>
51 ) : (
52 "Working…"
53 )}
54 </span>
55 );
57​
58/** Updates only the elapsed text, without committing the composer or timeline each second. */
59function WorkingTimer({ createdAt }: { createdAt: string }) {
60 const textRef = useRef<HTMLSpanElement>(null);
61 const initialText = formatWorkingTimerNow(createdAt);
62​
63 useEffect(() => {
64 const updateText = () => {
65 if (textRef.current) {
66 textRef.current.textContent = formatWorkingTimerNow(createdAt);
67 }
68 };
69 updateText();
70 const id = setInterval(updateText, 1000);
71 return () => clearInterval(id);
72 }, [createdAt]);
73​
74 return (
75 <span ref={textRef} className="tabular-nums">
76 {initialText}
77 </span>
78 );
80​
81function formatWorkingTimer(startIso: string, endIso: string): string | null {
82 const startedAtMs = Date.parse(startIso);
83 const endedAtMs = Date.parse(endIso);
84 if (!Number.isFinite(startedAtMs) || !Number.isFinite(endedAtMs)) {
85 return null;
86 }
87​
88 const elapsedSeconds = Math.max(0, Math.floor((endedAtMs - startedAtMs) / 1000));
89 if (elapsedSeconds < 60) {
90 return `${elapsedSeconds}s`;
91 }
92​
93 const hours = Math.floor(elapsedSeconds / 3600);
94 const minutes = Math.floor((elapsedSeconds % 3600) / 60);
95 const seconds = elapsedSeconds % 60;
96​
97 if (hours > 0) {
98 return minutes > 0 ? `${hours}h ${minutes}m` : `${hours}h`;
99 }
100​
101 return seconds > 0 ? `${minutes}m ${seconds}s` : `${minutes}m`;
103​
104function formatWorkingTimerNow(startIso: string): string {
105 return formatWorkingTimer(startIso, new Date().toISOString()) ?? "0s";