"use client"; import { useMemo, useState } from "react"; import Link from "next/link"; import { MessagesSquare, Trash2 } from "lucide-react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { QueryErrorRow } from "@/components/ui/query-error-row"; import { SettingsCard } from "@/components/settings/settings-card"; import { SettingsCardHeader } from "@/components/settings/_card-header"; import { SettingsCardActions } from "@/components/settings/_card-actions"; import { ConversationTranscript } from "@/components/insights/coach-panel/conversation-transcript"; import { useCoachConversationHistory, useDeleteCoachConversationWithUndo, } from "@/components/insights/coach-panel/use-coach"; import { useTranslations } from "@/lib/i18n/context"; import { formatRelativeTime } from "@/lib/i18n/relative-time"; /** * The Coach's stored conversations, readable and deletable from Settings. * * A conversation is the person's own record, not a cache, so it stays * reachable whatever the Coach's state: switched off by the operator, hidden * by the person, without a provider or without consent. Each row opens its * transcript in place and can be deleted (with the same undo window the * conversation list uses). Reading and deleting never ask the Coach; the * routes behind them are data routes. * * The card links to `/coach/conversations`, which stays reachable and * read-only with the Coach off, so the full list and its search are one step * away even when no navigation entry leads there. * * `hideWhenEmpty`: the card renders nothing when there is nothing stored. * Settings → AI mounts it that way while the Coach is off, so it appears only * when there is something to see. */ export function CoachConversationsMemoryCard({ isAuthenticated, hideWhenEmpty = false, }: { isAuthenticated: boolean; hideWhenEmpty?: boolean; }) { const { t, locale } = useTranslations(); const [openId, setOpenId] = useState(null); const history = useCoachConversationHistory({ enabled: isAuthenticated }); const { pendingDeleteIds, requestDelete, undoDelete } = useDeleteCoachConversationWithUndo(); const visible = useMemo( () => history.conversations.filter((c) => !pendingDeleteIds.has(c.id)), [history.conversations, pendingDeleteIds], ); if (hideWhenEmpty && (history.isLoading || history.isError)) return null; if (hideWhenEmpty && visible.length === 0) return null; function remove(id: string) { requestDelete(id); if (openId === id) setOpenId(null); toast.success(t("insights.coach.historyDeleted"), { action: { label: t("common.undo"), onClick: () => undoDelete(id) }, }); } return ( {history.isError ? ( void history.refetch()} /> ) : visible.length === 0 ? (

{history.isLoading ? t("common.loading") : t("insights.coach.historyEmpty")}

) : (
    {visible.map((c) => (
  • {openId === c.id ? : null}
  • ))}
)} {history.hasNextPage ? ( ) : null} {visible.length > 0 ? ( ) : null}
); }