Spaces:
Running
Running
File size: 6,984 Bytes
23680f2 |
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 |
"use client";
import React from "react";
import { useStore } from "@/store/useStore";
import { Panel } from "./Panel";
import { PanelHeader } from "./PanelHeader";
import { Tag, Search, ChevronDown, ChevronRight } from "lucide-react";
import { cn } from "@/lib/utils";
import { FALLBACK_LABEL_COLOR, MISSING_LABEL_COLOR, normalizeLabel } from "@/lib/labelColors";
import { useLabelLegend } from "./useLabelLegend";
interface ExplorerPanelProps {
className?: string;
}
export function ExplorerPanel({ className }: ExplorerPanelProps) {
const {
datasetInfo,
embeddingsByLayoutKey,
activeLayoutKey,
labelFilter,
setLabelFilter,
} = useStore();
const [labelSearch, setLabelSearch] = React.useState("");
const [isSearchOpen, setIsSearchOpen] = React.useState(false);
const [isLabelsExpanded, setIsLabelsExpanded] = React.useState(true);
const searchInputRef = React.useRef<HTMLInputElement>(null);
const resolvedLayoutKey =
activeLayoutKey ?? datasetInfo?.layouts?.[0]?.layout_key ?? null;
const embeddings = resolvedLayoutKey
? embeddingsByLayoutKey[resolvedLayoutKey] ?? null
: null;
const {
labelCounts,
labelUniverse,
distinctLabelCount,
distinctColoringDisabled,
labelColorMap,
legendLabels,
} = useLabelLegend({ datasetInfo, embeddings, labelSearch, labelFilter });
const hasCounts = labelCounts.size > 0;
const baseLabelCount = labelUniverse.length > 0
? labelUniverse.filter((label) => label !== "undefined").length
: distinctLabelCount;
const displayCount = labelSearch.trim().length > 0
? legendLabels.length
: baseLabelCount;
const activeLabel = labelFilter ? normalizeLabel(labelFilter) : null;
// Focus search input when opened
React.useEffect(() => {
if (isSearchOpen && searchInputRef.current) {
searchInputRef.current.focus();
}
}, [isSearchOpen]);
const handleSearchToggle = () => {
setIsSearchOpen(!isSearchOpen);
if (isSearchOpen) {
setLabelSearch("");
}
};
return (
<Panel className={cn("h-full flex flex-col", className)}>
<PanelHeader title="Explorer" />
{/* Scrollable content area */}
<div className="flex-1 min-h-0 overflow-auto panel-scroll">
{/* Labels section */}
<div className="border-b border-border">
{/* Section header - collapsible with search icon */}
<div className="flex items-center h-6 px-2 bg-secondary/50">
<button
onClick={() => setIsLabelsExpanded(!isLabelsExpanded)}
className="flex items-center gap-1.5 flex-1 min-w-0 text-left hover:bg-muted/30 rounded px-1 -ml-1"
>
{isLabelsExpanded ? (
<ChevronDown className="h-3 w-3 flex-shrink-0 text-muted-foreground" />
) : (
<ChevronRight className="h-3 w-3 flex-shrink-0 text-muted-foreground" />
)}
<Tag className="h-3 w-3 flex-shrink-0 text-muted-foreground" />
<span className="text-xs text-muted-foreground truncate">
Labels
</span>
<span className="ml-auto text-xs text-muted-foreground/50 tabular-nums">
{displayCount}
</span>
</button>
{/* Search toggle button */}
<button
onClick={handleSearchToggle}
className={cn(
"h-5 w-5 flex items-center justify-center rounded text-muted-foreground/60 hover:text-muted-foreground hover:bg-muted/50 transition-colors ml-1",
isSearchOpen && "text-muted-foreground bg-muted/50"
)}
title="Search labels"
>
<Search className="h-3 w-3" />
</button>
</div>
{/* Search input - shown when search is toggled */}
{isSearchOpen && (
<div className="px-2 py-1.5 bg-secondary/30 border-b border-border/50">
<input
ref={searchInputRef}
value={labelSearch}
onChange={(e) => setLabelSearch(e.target.value)}
placeholder="Filter labels..."
className="w-full h-6 px-2 rounded bg-background border border-border text-[12px] leading-[16px] text-foreground placeholder:text-muted-foreground/50 outline-none focus:ring-1 focus:ring-ring focus:border-ring"
/>
</div>
)}
{/* Labels list - collapsible */}
{isLabelsExpanded && (
<div className="py-1">
{distinctColoringDisabled && (
<div className="px-3 py-1 text-[10px] text-muted-foreground/60">
Too many labels ({distinctLabelCount}) to color distinctly; using one color.
</div>
)}
{legendLabels.length === 0 ? (
<div className="px-3 py-2 text-[11px] text-muted-foreground/50">
No labels available
</div>
) : (
<div className="space-y-px">
{legendLabels.map((label) => {
const color =
label === "undefined"
? MISSING_LABEL_COLOR
: labelColorMap[label] ?? FALLBACK_LABEL_COLOR;
const normalized = normalizeLabel(label);
const isActive = activeLabel === normalized;
const isDimmed = activeLabel && !isActive;
return (
<button
key={label}
type="button"
onClick={() => setLabelFilter(isActive ? null : normalized)}
className={cn(
"flex items-center gap-2 w-full h-6 px-3 text-[12px] leading-[16px] text-left text-muted-foreground hover:text-foreground",
"hover:bg-muted/40 transition-colors",
isActive && "bg-muted/60 text-foreground",
isDimmed && "opacity-40"
)}
>
<span
className="w-2 h-2 rounded-full flex-shrink-0"
style={{ backgroundColor: color }}
/>
<span className="truncate flex-1" title={label}>
{label}
</span>
{hasCounts && (
<span className="text-[10px] text-muted-foreground/50 font-mono tabular-nums flex-shrink-0">
{labelCounts.get(label) ?? 0}
</span>
)}
</button>
);
})}
</div>
)}
</div>
)}
</div>
</div>
</Panel>
);
}
|