2026-10-20 15:32:21 +08:00
|
|
|
import { forwardRef } from "react";
|
2026-09-27 21:06:59 +08:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
|
2026-10-20 15:32:21 +08:00
|
|
|
import { HoverHint } from "@/components/ui/hover-hint";
|
|
|
|
|
import { cn } from "@/lib/utils";
|
2026-09-27 21:06:59 +08:00
|
|
|
|
|
|
|
|
interface ThreadHistoryStatusProps {
|
|
|
|
|
loading: boolean;
|
|
|
|
|
error: string | null;
|
2026-10-20 15:32:21 +08:00
|
|
|
exhausted?: boolean;
|
2026-09-27 21:06:59 +08:00
|
|
|
onRetry: () => void;
|
2026-10-20 15:32:21 +08:00
|
|
|
className?: string;
|
2026-09-27 21:06:59 +08:00
|
|
|
}
|
|
|
|
|
|
2026-10-20 15:32:21 +08:00
|
|
|
export const ThreadHistoryStatus = forwardRef<HTMLDivElement, ThreadHistoryStatusProps>(function ThreadHistoryStatus({ loading, error, exhausted = false, onRetry, className }, ref) {
|
2026-09-27 21:06:59 +08:00
|
|
|
const { t } = useTranslation();
|
2026-10-20 15:32:21 +08:00
|
|
|
const state = !loading && error ? "error" : !loading && exhausted ? "exhausted" : "loading";
|
|
|
|
|
const label = t(state === "error" ? "thread.history.retryLabel" : `thread.history.${state}`);
|
|
|
|
|
// Blocks adapted from loading.dev (MIT); see THIRD_PARTY_NOTICES.md.
|
|
|
|
|
const blocks = <span aria-hidden="true" className="thread-history-blocks">
|
|
|
|
|
{Array.from({ length: 9 }, (_, index) => <span key={index}
|
|
|
|
|
style={{ animationDelay: `${(index % 3 + Math.floor(index / 3) - 5) * 260}ms` }} />)}
|
|
|
|
|
</span>;
|
2026-09-27 21:06:59 +08:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
2026-10-20 15:32:21 +08:00
|
|
|
ref={ref}
|
|
|
|
|
className={cn("thread-history-status thread-message-row pointer-events-none absolute inset-x-0 top-0 z-10 flex items-center justify-center", className)}
|
|
|
|
|
data-state={state}
|
2026-09-27 21:06:59 +08:00
|
|
|
>
|
|
|
|
|
<div role="status" aria-live="polite" aria-atomic="true">
|
2026-10-20 15:32:21 +08:00
|
|
|
{state === "exhausted" ? (
|
|
|
|
|
<span className="text-xs">{label}</span>
|
|
|
|
|
) : (
|
|
|
|
|
<HoverHint content={state === "error" ? error : label} side="bottom">
|
|
|
|
|
{loading ? (
|
|
|
|
|
<div tabIndex={0} aria-label={label} className="thread-history-control pointer-events-auto rounded-sm outline-none focus-visible:ring-2 focus-visible:ring-ring">
|
|
|
|
|
{blocks}<span className="sr-only">{label}</span>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<button type="button" aria-label={state === "error" ? label : t("thread.loadEarlier")} onClick={onRetry}
|
|
|
|
|
className="thread-history-control pointer-events-auto rounded-sm outline-none focus-visible:ring-2 focus-visible:ring-ring">
|
|
|
|
|
{blocks}<span className="sr-only">{label}</span>
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
|
|
|
|
</HoverHint>
|
|
|
|
|
)}
|
2026-09-27 21:06:59 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2026-10-20 15:32:21 +08:00
|
|
|
});
|