Extract reverse-lookup scan to core/relationships (one shared impl)

This commit is contained in:
2026-06-07 23:04:20 +03:00
parent 39555322e4
commit 1cad9ba140
6 changed files with 167 additions and 76 deletions
+3 -15
View File
@@ -16,6 +16,7 @@ import { useState } from 'react';
import { useShallow } from 'zustand/react/shallow';
import { datasetReference, type DataSource, type Dataset } from '@core/dataset';
import { detectFormat, detectFormatFromUrl, type DataFormat } from '@core/format-detection';
import { datasetUsageCounts, snippetsReferencingDataset } from '@core/relationships';
import { closeModal, openModal, resnapshot } from '../modals/ModalCoordinator';
import { confirm } from '../stores/ConfirmStore';
import { notify } from '../stores/NotificationStore';
@@ -44,18 +45,6 @@ function humanBytes(bytes: number): string {
return `${mb < 10 ? mb.toFixed(1) : Math.round(mb)} MB`;
}
/** Map of dataset name (lower-cased) → how many snippets reference it. */
function usageByName(snippets: ReadonlyArray<{ datasetRefs: string[] }>): Map<string, number> {
const counts = new Map<string, number>();
for (const s of snippets) {
for (const ref of s.datasetRefs) {
const key = ref.toLowerCase();
counts.set(key, (counts.get(key) ?? 0) + 1);
}
}
return counts;
}
const SOURCE_OPTIONS: ReadonlyArray<SegmentedOption<DataSource>> = [
{ value: 'inline', label: 'Inline' },
{ value: 'url', label: 'URL' },
@@ -70,7 +59,7 @@ export function DatasetsModal() {
const select = useDatasetStore((s) => s.select);
const startCreate = useDatasetStore((s) => s.startCreate);
const usage = usageByName(snippets);
const usage = datasetUsageCounts(snippets);
const ordered = [...datasets].sort(byModifiedDesc);
const handleNew = () => {
@@ -165,8 +154,7 @@ function DatasetDetail({
const selectSnippet = useSnippetStore((s) => s.selectSnippet);
const [copied, setCopied] = useState(false);
const lower = dataset.name.toLowerCase();
const linked = snippets.filter((s) => s.datasetRefs.some((r) => r.toLowerCase() === lower));
const linked = snippetsReferencingDataset(snippets, dataset.name);
const handleEdit = () => {
startEdit();