const CELL_LOCAL_PATH_KEY = 'CELL_LOCAL_PATH__'; const CELL_LOCAL_SERVER_PATH_KEY = 'CELL_LOCAL_SERVER_PATH__'; export const CELL_LOCAL_PATH_CHANGED_EVENT = 'cell-local-path-changed'; const FILE_PROTOCOL = 'file://'; export function isValidCellLocalPath(value: string) { const text = String(value || '') .trim() .replace(/[\\/]+$/, ''); if (!text) return false; if (text === '[object Object]') return false; if (text === 'null' || text === 'undefined') return false; if (!/\.bin$/i.test(text)) return false; return /^[a-zA-Z]:[\\/]/.test(text) || /^\\\\/.test(text) || text.startsWith('/') || text.startsWith(FILE_PROTOCOL); } function normalizePath(value: string) { const text = String(value || '') .trim() .replace(/[\\/]+$/, ''); if (!isValidCellLocalPath(text)) return ''; return text; } export function getCellLocalPath() { try { const serverPath = normalizePath(window.localStorage.getItem(CELL_LOCAL_SERVER_PATH_KEY) || ''); if (serverPath) return serverPath; return normalizePath(window.localStorage.getItem(CELL_LOCAL_PATH_KEY) || ''); } catch { return ''; } } export function getManualCellLocalPath() { try { return normalizePath(window.localStorage.getItem(CELL_LOCAL_PATH_KEY) || ''); } catch { return ''; } } export function getServerCellLocalPath() { try { return normalizePath(window.localStorage.getItem(CELL_LOCAL_SERVER_PATH_KEY) || ''); } catch { return ''; } } export function setCellLocalPath(path: string) { const normalized = normalizePath(path); try { if (!normalized) { window.localStorage.removeItem(CELL_LOCAL_PATH_KEY); window.dispatchEvent(new CustomEvent(CELL_LOCAL_PATH_CHANGED_EVENT, { detail: '' })); return; } window.localStorage.setItem(CELL_LOCAL_PATH_KEY, normalized); window.dispatchEvent(new CustomEvent(CELL_LOCAL_PATH_CHANGED_EVENT, { detail: normalized })); } catch {} } export function clearCellLocalPath() { setCellLocalPath(''); } export function setServerCellLocalPath(path: string) { const normalized = normalizePath(path); try { if (!normalized) { window.localStorage.removeItem(CELL_LOCAL_SERVER_PATH_KEY); window.dispatchEvent(new CustomEvent(CELL_LOCAL_PATH_CHANGED_EVENT, { detail: getCellLocalPath() })); return; } window.localStorage.setItem(CELL_LOCAL_SERVER_PATH_KEY, normalized); window.dispatchEvent(new CustomEvent(CELL_LOCAL_PATH_CHANGED_EVENT, { detail: normalized })); } catch {} }