feat: save dsp to keep config
This commit is contained in:
@@ -14,4 +14,4 @@ This template should help get you started developing with Tauri, React and Types
|
||||
|
||||
- Roll back on scroll when 2 scroll
|
||||
|
||||
- Save Configs to keep after reboot app.
|
||||
- Make phone version
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
|
||||
changed 1 package, and audited 148 packages in 1s
|
||||
up to date, audited 148 packages in 1s
|
||||
|
||||
32 packages are looking for funding
|
||||
run `npm fund` for details
|
||||
|
||||
+33
-1
@@ -8,15 +8,17 @@ import { useNotifications } from './hooks/useNotifications';
|
||||
import Notifications from './components/Notifications/Notifications';
|
||||
import CreditsPage from './pages/CreditsPage/CreditsPage';
|
||||
import HomePage from './pages/HomePage/HomePage';
|
||||
import { addDSP, createDSPInstance } from './dsp/dspApi';
|
||||
import { addDSP, createDSPInstance, restoreDSPs } from './dsp/dspApi';
|
||||
import { removeDSP } from './dsp/dspShared';
|
||||
import { stopAllPolling } from './dsp/pollRegistry';
|
||||
import { loadSavedDSPs, saveDSPs, toSaved } from './dsp/dspStorage';
|
||||
import { DSPMode } from './features/dsp408/types';
|
||||
|
||||
function App() {
|
||||
const [dsps, setDsps] = useState<DSP[]>([]);
|
||||
const [selected, setSelected] = useState<number | null>(null);
|
||||
const [page, setPage] = useState<AppPage>('home');
|
||||
const [loaded, setLoaded] = useState(false);
|
||||
const { notifications, notify, removeNotification, hoverNotification } = useNotifications();
|
||||
const connectedDspCount = dsps.filter((dsp) => isConnected(dsp)).length;
|
||||
|
||||
@@ -29,6 +31,36 @@ function App() {
|
||||
return () => stopAllPolling(dspsRef.current);
|
||||
}, []);
|
||||
|
||||
// Load saved DSPs on startup
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
(async () => {
|
||||
try {
|
||||
const saved = await loadSavedDSPs();
|
||||
const restored = await restoreDSPs(saved, notify);
|
||||
if (!cancelled) setDsps(restored);
|
||||
} catch (e) {
|
||||
notify(`Failed to load saved DSPs: ${String(e)}`, 'error');
|
||||
} finally {
|
||||
if (!cancelled) setLoaded(true);
|
||||
}
|
||||
})();
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
// Save only the config part, only after loading finished
|
||||
const savedKey = JSON.stringify(dsps.map(toSaved));
|
||||
useEffect(() => {
|
||||
if (!loaded) return;
|
||||
saveDSPs(dspsRef.current).catch((e) =>
|
||||
notify(`Failed to save DSPs: ${String(e)}`, 'error')
|
||||
);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [savedKey, loaded]);
|
||||
|
||||
useEffect(() => {
|
||||
if (activeDsp == null) return;
|
||||
const instance = createDSPInstance(activeDsp, setDsps, notify);
|
||||
|
||||
@@ -3,6 +3,7 @@ import { SetDsps, NotifyFn } from './dspShared';
|
||||
import { forDSP408, create as createDSP408 } from '../features/dsp408/dsp408';
|
||||
import { DSP408Api } from '../features/dsp408/dsp408';
|
||||
import { DSPMode } from '../features/dsp408/types';
|
||||
import { SavedDSP } from './dspStorage';
|
||||
// import { forDSP204 } from './dsp204';
|
||||
|
||||
export type DSPInstance = { dsp: Extract<DSP, { type: 'DSP408' }>; api: DSP408Api };
|
||||
@@ -17,6 +18,34 @@ export function createDSPInstance(dsp: DSP, setDsps: SetDsps, notify: NotifyFn):
|
||||
}
|
||||
}
|
||||
|
||||
export async function restoreDSPs(
|
||||
saved: SavedDSP[],
|
||||
notify: NotifyFn
|
||||
): Promise<DSP[]> {
|
||||
const results = await Promise.allSettled(
|
||||
saved.map((s) =>
|
||||
s.type === 'DSP408'
|
||||
? createDSP408({
|
||||
name: s.name,
|
||||
ip: s.ip,
|
||||
port: s.port,
|
||||
deviceId: s.deviceId,
|
||||
demo: s.mode,
|
||||
})
|
||||
: Promise.reject(new Error('DSP204 not implemented'))
|
||||
)
|
||||
);
|
||||
|
||||
const restored: DSP[] = [];
|
||||
results.forEach((r, i) => {
|
||||
if (r.status === 'fulfilled') {
|
||||
restored.push(r.value);
|
||||
} else {
|
||||
notify(`Could not restore DSP: ${String(r.reason)}`, 'error', saved[i].name);
|
||||
}
|
||||
});
|
||||
return restored;
|
||||
}
|
||||
export async function addDSP(
|
||||
setDsps: SetDsps,
|
||||
setSelected: React.Dispatch<React.SetStateAction<number | null>>,
|
||||
|
||||
+17
-15
@@ -1,10 +1,8 @@
|
||||
// src/dsp/dspStorage.ts
|
||||
import { load, Store } from '@tauri-apps/plugin-store';
|
||||
import { DSP, DSPTypes } from '../types';
|
||||
import { DSPMode } from '../features/dsp408/types';
|
||||
|
||||
export type SavedDSP = {
|
||||
id: number;
|
||||
type: DSPTypes;
|
||||
name: string;
|
||||
ip: string;
|
||||
@@ -13,26 +11,30 @@ export type SavedDSP = {
|
||||
mode: DSPMode;
|
||||
};
|
||||
|
||||
let storePromise: Promise<Store> | null = null;
|
||||
const getStore = () => (storePromise ??= load('dsps.json', { autoSave: false }));
|
||||
const STORE_FILE = 'dsps.json';
|
||||
const STORE_KEY = 'dsps';
|
||||
|
||||
export const toSaved = (d: DSP): SavedDSP => ({
|
||||
id: d.id,
|
||||
type: d.type,
|
||||
name: d.name,
|
||||
ip: d.ip,
|
||||
port: d.port,
|
||||
deviceId: d.deviceId,
|
||||
mode: d.mode,
|
||||
});
|
||||
let storePromise: Promise<Store> | null = null;
|
||||
const getStore = () => (storePromise ??= load(STORE_FILE, { autoSave: false }));
|
||||
|
||||
export function toSaved(d: DSP): SavedDSP {
|
||||
return {
|
||||
type: d.type,
|
||||
name: d.name,
|
||||
ip: d.ip,
|
||||
port: d.port,
|
||||
deviceId: d.deviceId,
|
||||
mode: d.mode,
|
||||
};
|
||||
}
|
||||
|
||||
export async function loadSavedDSPs(): Promise<SavedDSP[]> {
|
||||
const store = await getStore();
|
||||
return (await store.get<SavedDSP[]>('dsps')) ?? [];
|
||||
return (await store.get<SavedDSP[]>(STORE_KEY)) ?? [];
|
||||
}
|
||||
|
||||
export async function saveDSPs(dsps: DSP[]): Promise<void> {
|
||||
const store = await getStore();
|
||||
await store.set('dsps', dsps.map(toSaved));
|
||||
await store.set(STORE_KEY, dsps.map(toSaved));
|
||||
await store.save();
|
||||
}
|
||||
Reference in New Issue
Block a user