File size: 4,037 Bytes
f555806
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 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);
};