Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
| import { usingBrowserDb } from '@/utils/env'; | |
| const DATASETS_KEY = 'aitk.browser.datasets'; | |
| export interface DatasetEntry { | |
| name: string; | |
| path: string; | |
| } | |
| const normalizeEntry = (value: any): DatasetEntry | null => { | |
| if (!value) { | |
| return null; | |
| } | |
| if (typeof value === 'string') { | |
| return { name: value, path: '' }; | |
| } | |
| if (typeof value === 'object') { | |
| const name = typeof value.name === 'string' ? value.name : ''; | |
| if (!name) { | |
| return null; | |
| } | |
| const path = typeof value.path === 'string' ? value.path : ''; | |
| return { name, path }; | |
| } | |
| return null; | |
| }; | |
| const readDatasets = (): DatasetEntry[] => { | |
| if (typeof window === 'undefined') { | |
| return []; | |
| } | |
| try { | |
| const raw = window.localStorage.getItem(DATASETS_KEY); | |
| if (!raw) { | |
| return []; | |
| } | |
| const parsed = JSON.parse(raw); | |
| if (!Array.isArray(parsed)) { | |
| return []; | |
| } | |
| const items = parsed | |
| .map(normalizeEntry) | |
| .filter((entry): entry is DatasetEntry => Boolean(entry)); | |
| return items; | |
| } catch (error) { | |
| console.error('Failed to read datasets from localStorage:', error); | |
| return []; | |
| } | |
| }; | |
| const writeDatasets = (datasets: DatasetEntry[]) => { | |
| if (typeof window === 'undefined') { | |
| return; | |
| } | |
| try { | |
| window.localStorage.setItem(DATASETS_KEY, JSON.stringify(datasets)); | |
| } catch (error) { | |
| console.error('Failed to write datasets to localStorage:', error); | |
| } | |
| }; | |
| const sanitizeName = (name: string) => name.trim().toLowerCase(); | |
| export const listUserDatasetEntries = (): DatasetEntry[] => { | |
| if (!usingBrowserDb) { | |
| return []; | |
| } | |
| const datasets = readDatasets(); | |
| const unique = new Map<string, DatasetEntry>(); | |
| datasets.forEach(entry => { | |
| if (!unique.has(entry.name)) { | |
| unique.set(entry.name, entry); | |
| } | |
| }); | |
| return Array.from(unique.values()); | |
| }; | |
| export const listUserDatasets = (): string[] => { | |
| return listUserDatasetEntries().map(entry => entry.name); | |
| }; | |
| export const getUserDatasetPath = (datasetName: string): string | null => { | |
| if (!usingBrowserDb) { | |
| return null; | |
| } | |
| const normalized = sanitizeName(datasetName); | |
| const entry = listUserDatasetEntries().find(item => sanitizeName(item.name) === normalized); | |
| return entry?.path || null; | |
| }; | |
| export const addUserDataset = (datasetName: string, datasetPath: string) => { | |
| if (!usingBrowserDb) { | |
| return; | |
| } | |
| if (!datasetName) { | |
| return; | |
| } | |
| const trimmed = sanitizeName(datasetName); | |
| if (trimmed === '') { | |
| return; | |
| } | |
| const existing = listUserDatasetEntries(); | |
| const index = existing.findIndex(entry => sanitizeName(entry.name) === trimmed); | |
| if (index !== -1) { | |
| existing[index] = { | |
| ...existing[index], | |
| name: datasetName, | |
| path: datasetPath || existing[index].path, | |
| }; | |
| writeDatasets(existing); | |
| return; | |
| } | |
| existing.push({ name: datasetName, path: datasetPath || '' }); | |
| writeDatasets(existing); | |
| }; | |
| export const updateUserDatasetPath = (datasetName: string, datasetPath: string) => { | |
| if (!usingBrowserDb) { | |
| return; | |
| } | |
| const normalized = sanitizeName(datasetName); | |
| const existing = listUserDatasetEntries(); | |
| const updated = existing.map(entry => | |
| sanitizeName(entry.name) === normalized ? { ...entry, path: datasetPath || entry.path } : entry, | |
| ); | |
| writeDatasets(updated); | |
| }; | |
| export const renameUserDataset = (currentName: string, newName: string, datasetPath: string) => { | |
| if (!usingBrowserDb) { | |
| return; | |
| } | |
| const normalizedCurrent = sanitizeName(currentName); | |
| const normalizedNew = sanitizeName(newName); | |
| if (!normalizedCurrent || !normalizedNew) { | |
| return; | |
| } | |
| const entries = listUserDatasetEntries(); | |
| const updated = entries | |
| .map(entry => { | |
| if (sanitizeName(entry.name) === normalizedCurrent) { | |
| return { | |
| name: newName, | |
| path: datasetPath || entry.path, | |
| }; | |
| } | |
| return entry; | |
| }) | |
| .filter(entry => Boolean(entry?.name)); | |
| const hasNew = updated.some(entry => sanitizeName(entry.name) === normalizedNew); | |
| if (!hasNew) { | |
| updated.push({ name: newName, path: datasetPath || '' }); | |
| } | |
| writeDatasets(updated); | |
| }; | |
| export const removeUserDataset = (datasetName: string) => { | |
| if (!usingBrowserDb) { | |
| return; | |
| } | |
| const normalized = sanitizeName(datasetName); | |
| const existing = listUserDatasetEntries(); | |
| const updated = existing.filter(entry => sanitizeName(entry.name) !== normalized); | |
| writeDatasets(updated); | |
| }; | |
| export const hasUserDataset = (datasetName: string): boolean => { | |
| if (!usingBrowserDb) { | |
| return true; | |
| } | |
| const normalized = sanitizeName(datasetName); | |
| return listUserDatasetEntries().some(entry => sanitizeName(entry.name) === normalized); | |
| }; | |
| export const clearUserDatasets = () => { | |
| if (typeof window === 'undefined') { | |
| return; | |
| } | |
| window.localStorage.removeItem(DATASETS_KEY); | |
| }; | |