| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- import { getCellLocalPath, setCellLocalPath } from './cell/cellLocal';
- import { getOfflineMapDir, setOfflineMapDir } from './map/offlineMap';
- type PersistedCaseGlobalConfig = {
- offlineMapDir?: string;
- cellLocalPath?: string;
- };
- const normalizeText = (value: unknown) => String(value ?? '').trim();
- const isPlainObject = (value: unknown): value is Record<string, unknown> =>
- Object.prototype.toString.call(value) === '[object Object]';
- export async function readPersistedCaseGlobalConfig(): Promise<PersistedCaseGlobalConfig> {
- const ipcRenderer = (window as any)?.electron?.ipcRenderer;
- if (!ipcRenderer?.invoke) return {};
- try {
- const payload = await ipcRenderer.invoke('controller/os/readCaseGlobalConfig');
- if (!isPlainObject(payload)) return {};
- return {
- offlineMapDir: normalizeText(payload.offlineMapDir),
- cellLocalPath: normalizeText(payload.cellLocalPath),
- };
- } catch {
- return {};
- }
- }
- export async function writePersistedCaseGlobalConfig(patch: Partial<PersistedCaseGlobalConfig>): Promise<boolean> {
- const ipcRenderer = (window as any)?.electron?.ipcRenderer;
- if (!ipcRenderer?.invoke) return false;
- try {
- const payload: Partial<PersistedCaseGlobalConfig> = {};
- if (Object.prototype.hasOwnProperty.call(patch, 'offlineMapDir')) {
- payload.offlineMapDir = normalizeText(patch.offlineMapDir);
- }
- if (Object.prototype.hasOwnProperty.call(patch, 'cellLocalPath')) {
- payload.cellLocalPath = normalizeText(patch.cellLocalPath);
- }
- return !!(await ipcRenderer.invoke('controller/os/writeCaseGlobalConfig', payload));
- } catch {
- return false;
- }
- }
- export async function hydratePersistedCaseGlobalConfig() {
- const config = await readPersistedCaseGlobalConfig();
- const offlineMapDir = normalizeText(config.offlineMapDir);
- const cellLocalPath = normalizeText(config.cellLocalPath);
- if (offlineMapDir && !getOfflineMapDir()) {
- setOfflineMapDir(offlineMapDir);
- }
- if (cellLocalPath && !getCellLocalPath()) {
- setCellLocalPath(cellLocalPath);
- }
- return {
- offlineMapDir: getOfflineMapDir(),
- cellLocalPath: getCellLocalPath(),
- };
- }
|