Add Tauri compatibility fallback and switch to localStorage for web environments

- Updated `useApi` to detect Tauri runtime and dynamically import Tauri-specific modules.
- Refactored `useStore` to use `localStorage` fallback when not in Tauri.
- Improved runtime checks with `isTauri` utility to handle platform-specific logic.
This commit is contained in:
2026-02-22 13:40:43 +01:00
parent f1e098d3c7
commit 90793571e2
2 changed files with 37 additions and 12 deletions

View File

@@ -1,8 +1,13 @@
import { load, type Store } from '@tauri-apps/plugin-store';
import type { Store } from '@tauri-apps/plugin-store';
let storePromise: Promise<Store> | null = null;
function getStore(): Promise<Store> {
const isTauri = () =>
typeof window !== 'undefined' && Boolean((window as typeof window & { __TAURI__?: unknown }).__TAURI__);
async function getStore(): Promise<Store> {
if (!storePromise) {
const { load } = await import('@tauri-apps/plugin-store');
storePromise = load('store.json', {
autoSave: false,
defaults: {},
@@ -14,15 +19,28 @@ function getStore(): Promise<Store> {
export function useStore() {
const setValue = async <T>(key: string, value: T) => {
console.log('setValue',key,value);
const store = await getStore();
await store.set(key, value);
await store.save();
if (isTauri()) {
const store = await getStore();
await store.set(key, value);
await store.save();
return;
}
localStorage.setItem(key, JSON.stringify(value));
};
const getValue = async <T>(key: string) => {
console.log('getValue',key);
const store = await getStore();
return store.get<T>(key);
if (isTauri()) {
const store = await getStore();
return store.get<T>(key);
}
const rawValue = localStorage.getItem(key);
if (rawValue === null) return null;
try {
return JSON.parse(rawValue) as T;
} catch {
return null;
}
};
return {