File size: 9,625 Bytes
df790cc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | "use client";
import { api } from "@/lib/api";
import { useChatStore } from "@/lib/chatStore";
import { useQuery } from "@tanstack/react-query";
import clsx from "clsx";
import { useEffect } from "react";
import ReactMarkdown from "react-markdown";
/**
* View-source drawer. Shown when the user clicks a source chip or an inline
* citation. Renders the full markdown of a doc, plus the option to "Pin this
* source" — which scopes follow-up questions to that doc via /ask.
*/
export function SourceDrawer({
open,
docId,
docName,
onClose,
}: {
open: boolean;
docId: string | null;
docName?: string | null;
onClose: () => void;
}) {
const activeId = useChatStore((s) => s.activeId);
const conv = useChatStore((s) => (s.activeId ? s.conversations[s.activeId] : null));
const pinDoc = useChatStore((s) => s.pinDoc);
const newConversationPinned = useChatStore((s) => s.newConversationPinned);
const { data, isLoading, error } = useQuery({
queryKey: ["doc-content", docId],
queryFn: () => api.getDocumentContent(docId as string),
enabled: open && !!docId,
staleTime: 60_000,
});
// ESC to close
useEffect(() => {
if (!open) return;
const onKey = (e: KeyboardEvent) => {
if (e.key === "Escape") onClose();
};
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
}, [open, onClose]);
const isPinned = conv?.pinnedDocId === docId;
const cleanName = (n: string | null | undefined) =>
(n ?? "").replace(/_\d{6,}$/, "");
const displayName = data?.name ? cleanName(data.name) : cleanName(docName);
const handlePin = () => {
if (!docId || !activeId) return;
if (isPinned) pinDoc(activeId, null, null);
else pinDoc(activeId, docId, data?.name ?? docName ?? null);
};
const handleNewScoped = () => {
if (!docId) return;
newConversationPinned(docId, data?.name ?? docName ?? "");
onClose();
};
return (
<>
{/* Scrim */}
<div
className={clsx(
"fixed inset-0 z-40 bg-canvas-deep/60 backdrop-blur-sm transition-opacity duration-300",
open ? "opacity-100" : "opacity-0 pointer-events-none"
)}
onClick={onClose}
aria-hidden
/>
<aside
className={clsx(
"fixed top-0 right-0 z-50 h-screen w-full sm:w-[560px] glass-strong",
"flex flex-col transition-transform duration-300 ease-atelier",
open ? "translate-x-0" : "translate-x-full"
)}
role="dialog"
aria-modal="true"
aria-label="Source document"
>
{/* Header */}
<header className="px-6 py-5 border-b border-glass-border">
<div className="flex items-start justify-between gap-3">
<div className="min-w-0 flex-1">
<div className="text-micro uppercase tracking-[0.16em] text-ink-50 font-mono mb-1">
Source · view
</div>
<h2
className="text-display-sm text-ink truncate"
title={displayName ?? ""}
>
{displayName || "Loading…"}
</h2>
{docId && (
<div className="text-micro font-mono text-ink-50 mt-0.5 truncate">
{docId}
</div>
)}
</div>
<button
onClick={onClose}
className="shrink-0 p-2 rounded-md text-ink-50 hover:text-ink hover:bg-glass transition-colors"
aria-label="Close"
>
<svg width="14" height="14" viewBox="0 0 14 14" fill="none">
<path
d="m3 3 8 8M11 3l-8 8"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
/>
</svg>
</button>
</div>
{/* Pin / scope actions */}
<div className="mt-4 flex flex-wrap items-center gap-2">
<button
onClick={handlePin}
disabled={!activeId || !docId}
className={clsx(
"text-caption px-3 py-1.5 rounded-pill transition-colors disabled:opacity-40",
isPinned
? "bg-amber text-canvas hover:bg-amber-soft"
: "bg-glass text-ink hover:bg-glass-stronger"
)}
style={
isPinned
? undefined
: { boxShadow: "0 0 0 1px rgba(255,255,255,0.10)" }
}
title={
isPinned
? "Currently pinned — click to unpin"
: "Pin: future questions in this conversation will use only this doc"
}
>
{isPinned ? "✓ Pinned to this conversation" : "Pin to this conversation"}
</button>
<button
onClick={handleNewScoped}
disabled={!docId}
className="text-caption px-3 py-1.5 rounded-pill bg-glass text-ink hover:bg-glass-stronger transition-colors disabled:opacity-40"
style={{ boxShadow: "0 0 0 1px rgba(255,255,255,0.10)" }}
title="Open a new conversation scoped to this doc"
>
New scoped conversation →
</button>
</div>
</header>
{/* Body */}
<div className="flex-1 overflow-y-auto px-6 py-5">
{isLoading && (
<div className="text-ink-50 text-shimmer">Loading source…</div>
)}
{error && (
<div
className="p-4 rounded-md"
style={{
background: "rgba(255, 122, 122, 0.06)",
boxShadow: "0 0 0 1px rgba(255, 122, 122, 0.28)",
}}
>
<div className="text-caption-strong text-status-err">
Couldn't load source
</div>
<div className="text-caption text-ink-70 mt-1">
{error instanceof Error ? error.message : String(error)}
</div>
</div>
)}
{data && (
<article className="markdown-body text-ink leading-[1.7]">
<ReactMarkdown
components={{
p: ({ children }) => <p className="mb-3 last:mb-0">{children}</p>,
h1: ({ children }) => (
<h2 className="text-display-md text-ink mt-6 mb-3">{children}</h2>
),
h2: ({ children }) => (
<h3 className="text-display-sm text-ink mt-5 mb-2.5">{children}</h3>
),
h3: ({ children }) => (
<h4 className="text-body-strong text-ink mt-4 mb-2">{children}</h4>
),
ul: ({ children }) => (
<ul className="list-disc pl-5 mb-3 space-y-1 marker:text-amber/70">
{children}
</ul>
),
ol: ({ children }) => (
<ol className="list-decimal pl-5 mb-3 space-y-1 marker:text-amber/70 marker:font-mono">
{children}
</ol>
),
strong: ({ children }) => (
<strong className="font-semibold text-ink">{children}</strong>
),
em: ({ children }) => (
<em className="serif-italic text-amber-soft">{children}</em>
),
code: ({ children }) => (
<code
className="font-mono text-amber px-1.5 py-0.5 rounded text-[0.92em]"
style={{ background: "rgba(255,181,69,0.10)" }}
>
{children}
</code>
),
pre: ({ children }) => (
<pre
className="font-mono text-caption p-3 rounded-md overflow-x-auto my-3"
style={{
background: "rgba(0,0,0,0.28)",
boxShadow: "0 0 0 1px rgba(255,255,255,0.06)",
}}
>
{children}
</pre>
),
a: ({ href, children }) => (
<a
href={href}
className="text-amber underline decoration-amber/40 hover:decoration-amber"
target="_blank"
rel="noopener noreferrer"
>
{children}
</a>
),
hr: () => <hr className="my-5 border-0 h-px bg-glass-border" />,
blockquote: ({ children }) => (
<blockquote className="border-l-2 border-amber/50 pl-4 my-3 text-ink-70 serif-italic">
{children}
</blockquote>
),
}}
>
{data.text}
</ReactMarkdown>
</article>
)}
</div>
{data && (
<footer className="px-6 py-3 border-t border-glass-border text-micro font-mono uppercase tracking-[0.12em] text-ink-50">
{data.length_chars.toLocaleString()} chars
{data.created_at
? ` · ${new Date(data.created_at * 1000).toLocaleDateString()}`
: ""}
</footer>
)}
</aside>
</>
);
}
|