From 364ebfc97fb82b5c8ae6acc59d343197dda7e8ca Mon Sep 17 00:00:00 2001 From: LucasDLTG Date: Sat, 24 Oct 2026 11:55:45 +0200 Subject: [PATCH] feat: save dsp to keep config --- README.md | 4 ++-- src-tauri/2 | 2 +- src/App.tsx | 36 ++++++++++++++++++++++++++++++++++-- src/dsp/dspApi.ts | 29 +++++++++++++++++++++++++++++ src/dsp/dspStorage.ts | 34 ++++++++++++++++++---------------- 5 files changed, 84 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 973d52e..a064a77 100644 --- a/README.md +++ b/README.md @@ -13,5 +13,5 @@ This template should help get you started developing with Tauri, React and Types - Check end to end behavioour - Roll back on scroll when 2 scroll - -- Save Configs to keep after reboot app. \ No newline at end of file + +- Make phone version \ No newline at end of file diff --git a/src-tauri/2 b/src-tauri/2 index 8fb4f0f..a435fdf 100644 --- a/src-tauri/2 +++ b/src-tauri/2 @@ -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 diff --git a/src/App.tsx b/src/App.tsx index 8c92bed..83f1c11 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -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([]); const [selected, setSelected] = useState(null); const [page, setPage] = useState('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); @@ -83,4 +115,4 @@ function App() { ); } -export default App; +export default App; \ No newline at end of file diff --git a/src/dsp/dspApi.ts b/src/dsp/dspApi.ts index 8e1a6cb..69dc6ef 100644 --- a/src/dsp/dspApi.ts +++ b/src/dsp/dspApi.ts @@ -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; api: DSP408Api }; @@ -17,6 +18,34 @@ export function createDSPInstance(dsp: DSP, setDsps: SetDsps, notify: NotifyFn): } } +export async function restoreDSPs( + saved: SavedDSP[], + notify: NotifyFn +): Promise { + 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>, diff --git a/src/dsp/dspStorage.ts b/src/dsp/dspStorage.ts index 0179e01..ca9c50d 100644 --- a/src/dsp/dspStorage.ts +++ b/src/dsp/dspStorage.ts @@ -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 | 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 | 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 { const store = await getStore(); - return (await store.get('dsps')) ?? []; + return (await store.get(STORE_KEY)) ?? []; } export async function saveDSPs(dsps: DSP[]): Promise { const store = await getStore(); - await store.set('dsps', dsps.map(toSaved)); + await store.set(STORE_KEY, dsps.map(toSaved)); await store.save(); -} +} \ No newline at end of file