2026-08-15 17:53:48 +07:00
|
|
|
"use client";
|
|
|
|
|
|
2026-08-24 15:29:21 +07:00
|
|
|
/**
|
|
|
|
|
* Analysis scene — semantic search lives in a floating console (left);
|
|
|
|
|
* results stream into a translucent dossier (right). The stage shows an
|
|
|
|
|
* analysis hub wired to the guild's top channels.
|
|
|
|
|
*/
|
|
|
|
|
import { Hash, Search, Sparkles } from "lucide-react";
|
|
|
|
|
import { useEffect, useMemo, useState } from "react";
|
2026-08-15 17:53:48 +07:00
|
|
|
import { useAmbient } from "@/components/ambient/ambient-context";
|
2026-08-24 15:29:21 +07:00
|
|
|
import { Avatar, Badge } from "@/components/primitives";
|
|
|
|
|
import { EmptyState, SkeletonRows } from "@/components/shared";
|
|
|
|
|
import { useScenePublish } from "@/components/shell/scene-graph-context";
|
|
|
|
|
import { useChannels, useMessageSearch } from "@/hooks";
|
2026-08-18 09:05:12 +07:00
|
|
|
import { aiTone } from "@/lib/ai-status";
|
2026-08-24 15:29:21 +07:00
|
|
|
import type { ConstellationGraph } from "@/lib/constellation/graph";
|
2026-08-18 09:05:12 +07:00
|
|
|
import {
|
|
|
|
|
formatDuration,
|
|
|
|
|
getMessageChannelLabel,
|
|
|
|
|
renderMessageContent,
|
|
|
|
|
} from "@/lib/format";
|
2026-08-16 00:11:00 +07:00
|
|
|
|
2026-08-24 15:29:21 +07:00
|
|
|
function analysisGraph(
|
|
|
|
|
channels:
|
|
|
|
|
| {
|
|
|
|
|
channel_id: string;
|
|
|
|
|
channel_name?: string | null;
|
|
|
|
|
total_messages: number;
|
|
|
|
|
}[]
|
|
|
|
|
| undefined,
|
|
|
|
|
): ConstellationGraph {
|
|
|
|
|
const rows = (channels ?? []).slice(0, 10);
|
|
|
|
|
const maxMsg = rows.reduce((m, c) => Math.max(m, c.total_messages), 0);
|
|
|
|
|
const clamp = (n: number) => (maxMsg <= 0 ? 0.3 : Math.max(0.15, n / maxMsg));
|
|
|
|
|
return {
|
|
|
|
|
nodes: [
|
|
|
|
|
{ id: "hub", label: "analysis", kind: "guild", value: 1 },
|
|
|
|
|
...rows.map((c) => ({
|
|
|
|
|
id: `channel:${c.channel_id}`,
|
|
|
|
|
label: c.channel_name ?? c.channel_id.slice(0, 8),
|
|
|
|
|
kind: "channel" as const,
|
|
|
|
|
href: "/channels/",
|
|
|
|
|
value: clamp(c.total_messages),
|
|
|
|
|
})),
|
|
|
|
|
],
|
|
|
|
|
edges: rows.map((c) => ({
|
|
|
|
|
source: "hub",
|
|
|
|
|
target: `channel:${c.channel_id}`,
|
|
|
|
|
})),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-15 17:53:48 +07:00
|
|
|
export function AnalysisView() {
|
|
|
|
|
const [query, setQuery] = useState("");
|
|
|
|
|
const search = useMessageSearch(query, query.trim().length >= 2);
|
|
|
|
|
const { data: channels } = useChannels();
|
|
|
|
|
const ambient = useAmbient();
|
2026-08-24 15:29:21 +07:00
|
|
|
const publish = useScenePublish();
|
|
|
|
|
|
|
|
|
|
const graph = useMemo(() => analysisGraph(channels), [channels]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
publish({ graph, focus: null });
|
|
|
|
|
}, [graph, publish]);
|
|
|
|
|
|
|
|
|
|
useEffect(
|
|
|
|
|
() => () => publish({ graph: { nodes: [], edges: [] }, focus: null }),
|
|
|
|
|
[publish],
|
|
|
|
|
);
|
2026-08-15 17:53:48 +07:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-08-15 20:03:55 +07:00
|
|
|
ambient.set(
|
|
|
|
|
query ? "amber" : "signal",
|
|
|
|
|
0.3,
|
|
|
|
|
query ? "analyzing" : "search",
|
|
|
|
|
);
|
2026-08-15 17:53:48 +07:00
|
|
|
}, [query, ambient]);
|
|
|
|
|
|
|
|
|
|
return (
|
2026-08-24 15:29:21 +07:00
|
|
|
<div className="min-h-full">
|
|
|
|
|
{/* Search console — left */}
|
|
|
|
|
<section
|
|
|
|
|
className="pointer-events-auto absolute left-5 top-16 w-[min(26rem,88vw)]"
|
|
|
|
|
aria-label="Semantic search"
|
|
|
|
|
>
|
|
|
|
|
<p className="eyebrow mb-2 flex items-center gap-1.5">
|
|
|
|
|
<Sparkles className="size-3.5 text-signal" /> Search the archive
|
|
|
|
|
</p>
|
|
|
|
|
<div className="relative">
|
|
|
|
|
<Search className="absolute left-4 top-1/2 size-4 -translate-y-1/2 text-[var(--color-ink-faint)]" />
|
|
|
|
|
<input
|
|
|
|
|
type="search"
|
2026-08-15 17:53:48 +07:00
|
|
|
value={query}
|
|
|
|
|
onChange={(e) => setQuery(e.target.value)}
|
2026-08-24 15:29:21 +07:00
|
|
|
placeholder="Find messages, patterns, flags…"
|
2026-08-15 17:53:48 +07:00
|
|
|
autoFocus
|
2026-08-24 15:29:21 +07:00
|
|
|
className="h-11 w-full rounded-full border border-[var(--color-hairline)] bg-[var(--color-canvas)]/70 pl-11 pr-4 text-sm text-[var(--color-ink)] backdrop-blur-md outline-none placeholder:text-[var(--color-ink-faint)] focus:border-[var(--color-signal)]"
|
2026-08-15 17:53:48 +07:00
|
|
|
/>
|
|
|
|
|
</div>
|
2026-08-24 15:29:21 +07:00
|
|
|
{query.trim().length > 0 && query.trim().length < 2 ? (
|
|
|
|
|
<p className="mt-2 font-mono text-xs text-[var(--color-ink-faint)]">
|
|
|
|
|
minimal 2 karakter…
|
|
|
|
|
</p>
|
|
|
|
|
) : null}
|
|
|
|
|
</section>
|
2026-08-15 17:53:48 +07:00
|
|
|
|
2026-08-24 15:29:21 +07:00
|
|
|
{/* Results dossier — right */}
|
|
|
|
|
<section
|
|
|
|
|
className="pointer-events-auto absolute bottom-20 right-5 hidden max-h-[62vh] w-[min(30rem,92vw)] flex-col overflow-hidden rounded-2xl border border-[var(--color-hairline)] bg-[var(--color-canvas-2)]/75 backdrop-blur-xl md:flex"
|
|
|
|
|
aria-label="Search results"
|
|
|
|
|
>
|
|
|
|
|
<div className="flex items-center justify-between border-b border-[var(--color-hairline)] px-4 py-2.5">
|
|
|
|
|
<span className="eyebrow">matches</span>
|
|
|
|
|
<span className="font-mono text-xs text-[var(--color-ink-faint)]">
|
|
|
|
|
{(search.data ?? []).length}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="overflow-y-auto p-3">
|
|
|
|
|
{query.trim().length >= 2 && search.isLoading ? (
|
|
|
|
|
<SkeletonRows rows={5} />
|
|
|
|
|
) : (search.data ?? []).length === 0 ? (
|
2026-08-15 20:03:55 +07:00
|
|
|
<EmptyState
|
2026-08-24 15:29:21 +07:00
|
|
|
icon={<Search className="size-6" />}
|
2026-08-15 20:03:55 +07:00
|
|
|
title="No matches yet"
|
|
|
|
|
description="Run a search to surface messages across the guild."
|
|
|
|
|
/>
|
2026-08-15 17:53:48 +07:00
|
|
|
) : (
|
|
|
|
|
<div className="space-y-1.5">
|
|
|
|
|
{(search.data ?? []).map((m) => (
|
2026-08-15 20:03:55 +07:00
|
|
|
<div
|
|
|
|
|
key={m.id}
|
2026-08-24 15:29:21 +07:00
|
|
|
className="flex items-start gap-3 rounded-xl border border-[var(--color-hairline)] p-2.5"
|
2026-08-15 20:03:55 +07:00
|
|
|
>
|
2026-08-24 15:29:21 +07:00
|
|
|
<Avatar src={m.avatar_url} name={m.username} size={28} />
|
2026-08-15 17:53:48 +07:00
|
|
|
<div className="min-w-0 flex-1">
|
|
|
|
|
<div className="flex items-center gap-2">
|
2026-08-24 15:29:21 +07:00
|
|
|
<span className="text-sm font-semibold text-[var(--color-ink)]">
|
2026-08-15 20:03:55 +07:00
|
|
|
{m.username}
|
|
|
|
|
</span>
|
2026-08-24 15:29:21 +07:00
|
|
|
<span className="font-mono text-[0.65rem] text-[var(--color-ink-faint)]">
|
2026-08-15 20:03:55 +07:00
|
|
|
{getMessageChannelLabel(m)}
|
|
|
|
|
</span>
|
2026-08-24 15:29:21 +07:00
|
|
|
{m.ai_status ? (
|
2026-08-15 20:03:55 +07:00
|
|
|
<Badge tone={aiTone(m.ai_status)} className="ml-auto">
|
2026-08-16 00:11:00 +07:00
|
|
|
{m.ai_analysis_duration_ms &&
|
|
|
|
|
m.ai_analysis_duration_ms > 0
|
2026-08-18 09:05:12 +07:00
|
|
|
? `${m.ai_status} · ${formatDuration(m.ai_analysis_duration_ms)}`
|
2026-08-16 00:11:00 +07:00
|
|
|
: m.ai_status}
|
2026-08-15 20:03:55 +07:00
|
|
|
</Badge>
|
2026-08-24 15:29:21 +07:00
|
|
|
) : null}
|
2026-08-15 20:03:55 +07:00
|
|
|
</div>
|
2026-08-24 15:29:21 +07:00
|
|
|
<div className="mt-0.5 line-clamp-2 text-sm text-[var(--color-ink-soft)]">
|
2026-08-15 20:03:55 +07:00
|
|
|
{renderMessageContent(m.content, m.metadata) || "(embed)"}
|
2026-08-15 17:53:48 +07:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2026-08-24 15:29:21 +07:00
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
{/* Channels hint — bottom-left */}
|
|
|
|
|
<p className="pointer-events-none absolute bottom-24 left-5 hidden items-center gap-1.5 font-mono text-xs text-[var(--color-ink-faint)] lg:flex">
|
|
|
|
|
<Hash className="size-3.5" /> {(channels ?? []).length} channels
|
|
|
|
|
terhubung ke hub analitik — klik untuk membuka konstelasi channel
|
|
|
|
|
</p>
|
2026-08-15 17:53:48 +07:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|