aboutsummaryrefslogtreecommitdiff
path: root/packages/ui/memory-graph/legend.tsx
blob: 81c634f332f9434d4887d553142ea498ada978c6 (plain) (blame)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
"use client";

import { useIsMobile } from "@hooks/use-mobile";
import { cn } from "@repo/lib/utils";
import {
	Collapsible,
	CollapsibleContent,
	CollapsibleTrigger,
} from "@repo/ui/components/collapsible";
import { GlassMenuEffect } from "@repo/ui/other/glass-effect";
import { Brain, ChevronDown, ChevronUp, FileText } from "lucide-react";
import { memo, useEffect, useState } from "react";
import { colors } from "./constants";
import type { GraphEdge, GraphNode, LegendProps } from "./types";

// Cookie utility functions for legend state
const setCookie = (name: string, value: string, days = 365) => {
	if (typeof document === "undefined") return;
	const expires = new Date();
	expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000);
	document.cookie = `${name}=${value};expires=${expires.toUTCString()};path=/`;
};

const getCookie = (name: string): string | null => {
	if (typeof document === "undefined") return null;
	const nameEQ = `${name}=`;
	const ca = document.cookie.split(";");
	for (let i = 0; i < ca.length; i++) {
		let c = ca[i];
		if (!c) continue;
		while (c.charAt(0) === " ") c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
};

interface ExtendedLegendProps extends LegendProps {
	id?: string;
	nodes?: GraphNode[];
	edges?: GraphEdge[];
	isLoading?: boolean;
	isExperimental?: boolean;
}

export const Legend = memo(function Legend({
	variant = "console",
	id,
	nodes = [],
	edges = [],
	isLoading = false,
	isExperimental = false,
}: ExtendedLegendProps) {
	const isMobile = useIsMobile();
	const [isExpanded, setIsExpanded] = useState(true);
	const [isInitialized, setIsInitialized] = useState(false);

	const relationData = isExperimental
		? [
				["updates", colors.relations.updates],
				["extends", colors.relations.extends],
				["derives", colors.relations.derives],
			]
		: [["updates", colors.relations.updates]];

	// Load saved preference on client side
	useEffect(() => {
		if (!isInitialized) {
			const savedState = getCookie("legendCollapsed");
			if (savedState === "true") {
				setIsExpanded(false);
			} else if (savedState === "false") {
				setIsExpanded(true);
			} else {
				// Default: collapsed on mobile, expanded on desktop
				setIsExpanded(!isMobile);
			}
			setIsInitialized(true);
		}
	}, [isInitialized, isMobile]);

	// Save to cookie when state changes
	const handleToggleExpanded = (expanded: boolean) => {
		setIsExpanded(expanded);
		setCookie("legendCollapsed", expanded ? "false" : "true");
	};

	// Use explicit classes that Tailwind can detect
	const getPositioningClasses = () => {
		if (variant === "console") {
			// Both desktop and mobile use same positioning for console
			return "bottom-4 right-4";
		}
		if (variant === "consumer") {
			return isMobile ? "bottom-48 left-4" : "top-18 right-4";
		}
		return "";
	};

	const getMobileSize = () => {
		if (!isMobile) return "";
		return isExpanded ? "max-w-xs" : "w-16 h-12";
	};

	const hexagonClipPath =
		"polygon(50% 0%, 93% 25%, 93% 75%, 50% 100%, 7% 75%, 7% 25%)";

	// Calculate stats
	const memoryCount = nodes.filter((n) => n.type === "memory").length;
	const documentCount = nodes.filter((n) => n.type === "document").length;

	return (
		<div
			className={cn(
				"absolute z-10 rounded-xl overflow-hidden w-fit h-fit",
				getPositioningClasses(),
				getMobileSize(),
				isMobile && "hidden md:block",
			)}
			id={id}
		>
			<Collapsible onOpenChange={handleToggleExpanded} open={isExpanded}>
				{/* Glass effect background */}
				<GlassMenuEffect rounded="rounded-xl" />

				<div className="relative z-10">
					{/* Mobile and Desktop collapsed state */}
					{!isExpanded && (
						<CollapsibleTrigger className="w-full h-full p-2 flex items-center justify-center hover:bg-white/5 transition-colors">
							<div className="flex flex-col items-center gap-1">
								<div className="text-xs text-slate-300 font-medium">?</div>
								<ChevronUp className="w-3 h-3 text-slate-400" />
							</div>
						</CollapsibleTrigger>
					)}

					{/* Expanded state */}
					{isExpanded && (
						<>
							{/* Header with toggle */}
							<div className="flex items-center justify-between px-4 py-3 border-b border-slate-600/50">
								<div className="text-sm font-medium text-slate-100">Legend</div>
								<CollapsibleTrigger className="p-1 hover:bg-white/10 rounded">
									<ChevronDown className="w-4 h-4 text-slate-400" />
								</CollapsibleTrigger>
							</div>

							<CollapsibleContent>
								<div className="text-xs text-slate-200 px-4 py-3 space-y-3">
									{/* Stats Section */}
									{!isLoading && (
										<div className="space-y-2">
											<div className="text-xs font-medium text-slate-300 uppercase tracking-wide">
												Statistics
											</div>
											<div className="space-y-1.5">
												<div className="flex items-center gap-2">
													<Brain className="w-3 h-3 text-blue-400" />
													<span className="text-xs">
														{memoryCount} memories
													</span>
												</div>
												<div className="flex items-center gap-2">
													<FileText className="w-3 h-3 text-slate-300" />
													<span className="text-xs">
														{documentCount} documents
													</span>
												</div>
												<div className="flex items-center gap-2">
													<div className="w-3 h-3 bg-gradient-to-r from-slate-400 to-blue-400 rounded-full" />
													<span className="text-xs">
														{edges.length} connections
													</span>
												</div>
											</div>
										</div>
									)}

									{/* Node Types */}
									<div className="space-y-2">
										<div className="text-xs font-medium text-slate-300 uppercase tracking-wide">
											Nodes
										</div>
										<div className="space-y-1.5">
											<div className="flex items-center gap-2">
												<div className="w-4 h-3 bg-white/8 border border-white/25 rounded-sm flex-shrink-0" />
												<span className="text-xs">Document</span>
											</div>
											<div className="flex items-center gap-2">
												<div
													className="w-3 h-3 bg-blue-400/10 border border-blue-400/35 flex-shrink-0"
													style={{
														clipPath: hexagonClipPath,
													}}
												/>
												<span className="text-xs">Memory (latest)</span>
											</div>
											<div className="flex items-center gap-2">
												<div
													className="w-3 h-3 bg-blue-400/10 border border-blue-400/35 opacity-40 flex-shrink-0"
													style={{
														clipPath: hexagonClipPath,
													}}
												/>
												<span className="text-xs">Memory (older)</span>
											</div>
										</div>
									</div>

									{/* Status Indicators */}
									<div className="space-y-2">
										<div className="text-xs font-medium text-slate-300 uppercase tracking-wide">
											Status
										</div>
										<div className="space-y-1.5">
											<div className="flex items-center gap-2">
												<div
													className="w-3 h-3 bg-red-500/30 border border-red-500/80 relative flex-shrink-0"
													style={{
														clipPath: hexagonClipPath,
													}}
												>
													<div className="absolute inset-0 flex items-center justify-center text-red-400 text-xs leading-none">
														
													</div>
												</div>
												<span className="text-xs">Forgotten</span>
											</div>
											<div className="flex items-center gap-2">
												<div
													className="w-3 h-3 bg-blue-400/10 border-2 border-amber-500 flex-shrink-0"
													style={{
														clipPath: hexagonClipPath,
													}}
												/>
												<span className="text-xs">Expiring soon</span>
											</div>
											<div className="flex items-center gap-2">
												<div
													className="w-3 h-3 bg-blue-400/10 border-2 border-emerald-500 relative flex-shrink-0"
													style={{
														clipPath: hexagonClipPath,
													}}
												>
													<div className="absolute -top-1 -right-1 w-2 h-2 bg-emerald-500 rounded-full" />
												</div>
												<span className="text-xs">New memory</span>
											</div>
										</div>
									</div>

									{/* Connection Types */}
									<div className="space-y-2">
										<div className="text-xs font-medium text-slate-300 uppercase tracking-wide">
											Connections
										</div>
										<div className="space-y-1.5">
											<div className="flex items-center gap-2">
												<div className="w-4 h-0 border-t border-slate-400 flex-shrink-0" />
												<span className="text-xs">Doc  Memory</span>
											</div>
											<div className="flex items-center gap-2">
												<div className="w-4 h-0 border-t-2 border-dashed border-slate-400 flex-shrink-0" />
												<span className="text-xs">Doc similarity</span>
											</div>
										</div>
									</div>

									{/* Relation Types */}
									<div className="space-y-2">
										<div className="text-xs font-medium text-slate-300 uppercase tracking-wide">
											Relations
										</div>
										<div className="space-y-1.5">
											{(isExperimental
												? [
														["updates", colors.relations.updates],
														["extends", colors.relations.extends],
														["derives", colors.relations.derives],
													]
												: [["updates", colors.relations.updates]]
											).map(([label, color]) => (
												<div className="flex items-center gap-2" key={label}>
													<div
														className="w-4 h-0 border-t-2 flex-shrink-0"
														style={{ borderColor: color }}
													/>
													<span
														className="text-xs capitalize"
														style={{ color: color }}
													>
														{label}
													</span>
												</div>
											))}
										</div>
									</div>

									{/* Similarity Strength */}
									<div className="space-y-2">
										<div className="text-xs font-medium text-slate-300 uppercase tracking-wide">
											Similarity
										</div>
										<div className="space-y-1.5">
											<div className="flex items-center gap-2">
												<div className="w-3 h-3 rounded-full bg-slate-400/20 flex-shrink-0" />
												<span className="text-xs">Weak</span>
											</div>
											<div className="flex items-center gap-2">
												<div className="w-3 h-3 rounded-full bg-slate-400/60 flex-shrink-0" />
												<span className="text-xs">Strong</span>
											</div>
										</div>
									</div>
								</div>
							</CollapsibleContent>
						</>
					)}
				</div>
			</Collapsible>
		</div>
	);
});

Legend.displayName = "Legend";