feat: save dsp to keep config
This commit is contained in:
@@ -13,5 +13,5 @@ This template should help get you started developing with Tauri, React and Types
|
|||||||
- Check end to end behavioour
|
- Check end to end behavioour
|
||||||
|
|
||||||
- Roll back on scroll when 2 scroll
|
- 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
|
32 packages are looking for funding
|
||||||
run `npm fund` for details
|
run `npm fund` for details
|
||||||
|
|||||||
+34
-2
@@ -8,15 +8,17 @@ import { useNotifications } from './hooks/useNotifications';
|
|||||||
import Notifications from './components/Notifications/Notifications';
|
import Notifications from './components/Notifications/Notifications';
|
||||||
import CreditsPage from './pages/CreditsPage/CreditsPage';
|
import CreditsPage from './pages/CreditsPage/CreditsPage';
|
||||||
import HomePage from './pages/HomePage/HomePage';
|
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 { removeDSP } from './dsp/dspShared';
|
||||||
import { stopAllPolling } from './dsp/pollRegistry';
|
import { stopAllPolling } from './dsp/pollRegistry';
|
||||||
|
import { loadSavedDSPs, saveDSPs, toSaved } from './dsp/dspStorage';
|
||||||
import { DSPMode } from './features/dsp408/types';
|
import { DSPMode } from './features/dsp408/types';
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [dsps, setDsps] = useState<DSP[]>([]);
|
const [dsps, setDsps] = useState<DSP[]>([]);
|
||||||
const [selected, setSelected] = useState<number | null>(null);
|
const [selected, setSelected] = useState<number | null>(null);
|
||||||
const [page, setPage] = useState<AppPage>('home');
|
const [page, setPage] = useState<AppPage>('home');
|
||||||
|
const [loaded, setLoaded] = useState(false);
|
||||||
const { notifications, notify, removeNotification, hoverNotification } = useNotifications();
|
const { notifications, notify, removeNotification, hoverNotification } = useNotifications();
|
||||||
const connectedDspCount = dsps.filter((dsp) => isConnected(dsp)).length;
|
const connectedDspCount = dsps.filter((dsp) => isConnected(dsp)).length;
|
||||||
|
|
||||||
@@ -29,6 +31,36 @@ function App() {
|
|||||||
return () => stopAllPolling(dspsRef.current);
|
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(() => {
|
useEffect(() => {
|
||||||
if (activeDsp == null) return;
|
if (activeDsp == null) return;
|
||||||
const instance = createDSPInstance(activeDsp, setDsps, notify);
|
const instance = createDSPInstance(activeDsp, setDsps, notify);
|
||||||
@@ -83,4 +115,4 @@ function App() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default App;
|
export default App;
|
||||||
@@ -3,6 +3,7 @@ import { SetDsps, NotifyFn } from './dspShared';
|
|||||||
import { forDSP408, create as createDSP408 } from '../features/dsp408/dsp408';
|
import { forDSP408, create as createDSP408 } from '../features/dsp408/dsp408';
|
||||||
import { DSP408Api } from '../features/dsp408/dsp408';
|
import { DSP408Api } from '../features/dsp408/dsp408';
|
||||||
import { DSPMode } from '../features/dsp408/types';
|
import { DSPMode } from '../features/dsp408/types';
|
||||||
|
import { SavedDSP } from './dspStorage';
|
||||||
// import { forDSP204 } from './dsp204';
|
// import { forDSP204 } from './dsp204';
|
||||||
|
|
||||||
export type DSPInstance = { dsp: Extract<DSP, { type: 'DSP408' }>; api: DSP408Api };
|
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(
|
export async function addDSP(
|
||||||
setDsps: SetDsps,
|
setDsps: SetDsps,
|
||||||
setSelected: React.Dispatch<React.SetStateAction<number | null>>,
|
setSelected: React.Dispatch<React.SetStateAction<number | null>>,
|
||||||
|
|||||||
+18
-16
@@ -1,10 +1,8 @@
|
|||||||
// src/dsp/dspStorage.ts
|
|
||||||
import { load, Store } from '@tauri-apps/plugin-store';
|
import { load, Store } from '@tauri-apps/plugin-store';
|
||||||
import { DSP, DSPTypes } from '../types';
|
import { DSP, DSPTypes } from '../types';
|
||||||
import { DSPMode } from '../features/dsp408/types';
|
import { DSPMode } from '../features/dsp408/types';
|
||||||
|
|
||||||
export type SavedDSP = {
|
export type SavedDSP = {
|
||||||
id: number;
|
|
||||||
type: DSPTypes;
|
type: DSPTypes;
|
||||||
name: string;
|
name: string;
|
||||||
ip: string;
|
ip: string;
|
||||||
@@ -13,26 +11,30 @@ export type SavedDSP = {
|
|||||||
mode: DSPMode;
|
mode: DSPMode;
|
||||||
};
|
};
|
||||||
|
|
||||||
let storePromise: Promise<Store> | null = null;
|
const STORE_FILE = 'dsps.json';
|
||||||
const getStore = () => (storePromise ??= load('dsps.json', { autoSave: false }));
|
const STORE_KEY = 'dsps';
|
||||||
|
|
||||||
export const toSaved = (d: DSP): SavedDSP => ({
|
let storePromise: Promise<Store> | null = null;
|
||||||
id: d.id,
|
const getStore = () => (storePromise ??= load(STORE_FILE, { autoSave: false }));
|
||||||
type: d.type,
|
|
||||||
name: d.name,
|
export function toSaved(d: DSP): SavedDSP {
|
||||||
ip: d.ip,
|
return {
|
||||||
port: d.port,
|
type: d.type,
|
||||||
deviceId: d.deviceId,
|
name: d.name,
|
||||||
mode: d.mode,
|
ip: d.ip,
|
||||||
});
|
port: d.port,
|
||||||
|
deviceId: d.deviceId,
|
||||||
|
mode: d.mode,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export async function loadSavedDSPs(): Promise<SavedDSP[]> {
|
export async function loadSavedDSPs(): Promise<SavedDSP[]> {
|
||||||
const store = await getStore();
|
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> {
|
export async function saveDSPs(dsps: DSP[]): Promise<void> {
|
||||||
const store = await getStore();
|
const store = await getStore();
|
||||||
await store.set('dsps', dsps.map(toSaved));
|
await store.set(STORE_KEY, dsps.map(toSaved));
|
||||||
await store.save();
|
await store.save();
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user