Files
sorsfonalak/services/frontend/src/components/AnalysisLoading.tsx
T

49 lines
1.4 KiB
TypeScript
Raw Normal View History

"use client";
import { useEffect, useState } from "react";
import { ZodiacWheel } from "./ZodiacWheel";
/**
* Slowly turning wheel with the elapsed time, while the LLM writes. The time
* counts from `since` (when the text was asked for), so it does not start
* again from zero after a reload; without it, from when the wheel appeared.
*/
export function AnalysisLoading({
title,
text,
compact = false,
since,
}: {
title?: string;
text: string;
compact?: boolean;
since?: string;
}) {
const [start] = useState(() => {
const t = since ? Date.parse(since) : NaN;
return Number.isNaN(t) ? Date.now() : Math.min(t, Date.now());
});
const [seconds, setSeconds] = useState(() => Math.floor((Date.now() - start) / 1000));
useEffect(() => {
const t = setInterval(() => setSeconds(Math.floor((Date.now() - start) / 1000)), 1000);
return () => clearInterval(t);
}, [start]);
const elapsed = `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`;
return (
<div className={compact ? "loading loading-compact" : "loading"}>
<ZodiacWheel className="loading-wheel" />
<div className="loading-text">
{title && <strong>{title}</strong>}
<p className="muted" role="status">
{text}
</p>
<p className="hint" aria-hidden="true" suppressHydrationWarning>
{elapsed}
</p>
</div>
</div>
);
}