import { For, Show, createSignal, onCleanup } from "solid-js"; import type { RagOutput } from "@/api/client"; import { Alert } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { SidePanel } from "@/components/ui/side-panel"; import { IconCopy, IconDownload, IconFileText } from "@/components/ui/icons"; import { buildRagOutputMarkdown, fieldValueText, ragOutputFileName, readRagOutputDocument, type RagOutputDocument, type RagOutputPassage, } from "@/components/notes/rag-output-document"; import { ragOutputMessage, ragOutputPassagePages, type RagOutputMessageKey, } from "@/components/notes/rag-output-messages"; import { formatDateTime } from "@/lib/format"; import { usePreferences } from "@/stores/preferences-context"; /** * Print isolation: the SidePanel portals into the app's document, so a bare * `window.print()` would capture the whole page. These rules hide every * element except the document body, flatten the panel out of its fixed, * scroll-clipped layout, and pull the document to the page origin. The action * row and the technical disclosure opt out with `print:hidden`. */ const PRINT_STYLES = ` @media print { body * { visibility: hidden; } .rag-output-print, .rag-output-print * { overflow: visible !important; } .rag-output-print { position: static !important; max-width: none !important; height: auto !important; box-shadow: none !important; } .rag-output-print [data-rag-print-doc], .rag-output-print [data-rag-print-doc] * { visibility: visible; } .rag-output-print [data-rag-print-doc] { position: absolute; left: 0; top: 0; width: 100%; color: #000; } } `; /** Payload keys with a Turkish label; anything else is labelled by its key. */ const FIELD_LABELS: Partial> = { chunks: "chunks", }; /** * One stored AI output, readable and exportable: the note it belongs to, the * attachments by name, the produced prose, the lesser payload fields, and the * raw record behind a disclosure. Copy / download / print all build their * output from this same document. */ export function RagOutputDrawer(props: { output: RagOutput | null; /** The note the output belongs to — shown by title, never by id. */ noteTitle?: string; onOpenChange: (open: boolean) => void; }) { const { locale } = usePreferences(); const rt = (key: RagOutputMessageKey, vars?: Record) => ragOutputMessage(locale(), key, vars); const [copied, setCopied] = createSignal(false); const [copyError, setCopyError] = createSignal(""); let copiedTimer: ReturnType | undefined; onCleanup(() => copiedTimer && clearTimeout(copiedTimer)); const doc = () => (props.output ? readRagOutputDocument(props.output) : null); const noteTitle = () => props.noteTitle?.trim() ?? ""; /** One passage's heading: the source file by name, its page range if any. */ const passageHeader = (passage: RagOutputPassage) => { const source = passage.sourceName ?? (passage.fromNote ? noteTitle() || rt("noteText") : rt("unnamedSource")); const pages = ragOutputPassagePages(locale(), passage.pageStart, passage.pageEnd); return pages ? `${source} · ${pages}` : source; }; /** The honest note when the service's cap means more passages exist. */ const passagesNote = (read: RagOutputDocument) => { if (!read.passagesTruncated) return ""; return read.chunksTotal != null ? rt("passagesTruncated", { shown: read.passages.length, total: read.chunksTotal }) : rt("passagesTruncatedNoTotal", { shown: read.passages.length }); }; const markdown = () => { const output = props.output; const read = doc(); if (!output || !read) return ""; return buildRagOutputMarkdown({ title: noteTitle() || rt("drawerTitle"), date: formatDateTime(output.generated_at, locale()), noteTitle: noteTitle(), document: read, labels: { date: rt("date"), note: rt("note"), sources: rt("sources"), failed: rt("failedTitle"), otherFields: rt("otherFields"), passages: rt("passages"), passageHeader, passagesTruncated: passagesNote(read), }, }); }; const copy = async () => { setCopyError(""); if (!navigator.clipboard?.writeText) { setCopyError(rt("copyFailed")); return; } try { await navigator.clipboard.writeText(markdown()); setCopied(true); copiedTimer = setTimeout(() => setCopied(false), 2500); } catch { setCopied(false); setCopyError(rt("copyFailed")); } }; const download = () => { const output = props.output; if (!output) return; const blob = new Blob([markdown()], { type: "text/markdown;charset=utf-8" }); const url = URL.createObjectURL(blob); const link = document.createElement("a"); link.href = url; link.download = ragOutputFileName(output.generated_at); document.body.appendChild(link); link.click(); link.remove(); URL.revokeObjectURL(url); }; return ( {(output) => ( {(read) => (

{rt("drawerTitle")}

{noteTitle() || rt("drawerTitle")}

{formatDateTime(output().generated_at, locale())}

{rt("copied")} {copyError()}
{rt("date")}
{formatDateTime(output().generated_at, locale())}
{rt("note")}
{noteTitle()}

{rt("sources")}

0 || read().unnamedSources > 0} fallback={

{rt("noSources")}

} >
    {(name) =>
  • {name}
  • }
    0}>
  • {rt("unnamedSource")} {read().unnamedSources > 1 ? ` × ${read().unnamedSources}` : ""}
0}> {/* One flex item: Alert lays its children out in a row. */}

{rt("failedTitle")}

{rt("failedLine", { names: read().failed.join(", ") })}

0} fallback={{rt("emptyText")}} >

{read().summary}

0}>
    {(point) =>
  • {point}
  • }
0}>

{rt("passages")}

    {(passage) => (
  • {passageHeader(passage)}

    {/* Scrolls past its cap on screen; print flows it out in full. */}

    {passage.text}

  • )}

{passagesNote(read())}

0}>

{rt("otherFields")}

{(field) => (
{FIELD_LABELS[field.key] ? rt(FIELD_LABELS[field.key]!) : field.key}
{fieldValueText(field.value)}
)}
{rt("technical")}
                    {JSON.stringify(read().raw, null, 2)}
                  
)}
)}
); }