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:
@@ -1,4 +1,3 @@
|
|||||||
import { fetch } from '@tauri-apps/plugin-http';
|
|
||||||
import { useStore } from './useStore.ts';
|
import { useStore } from './useStore.ts';
|
||||||
import { useCrypto } from './useCrypto.ts';
|
import { useCrypto } from './useCrypto.ts';
|
||||||
|
|
||||||
@@ -9,6 +8,9 @@ type Settings = {
|
|||||||
password: string;
|
password: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const isTauri = () =>
|
||||||
|
typeof window !== 'undefined' && Boolean((window as typeof window & { __TAURI__?: unknown }).__TAURI__);
|
||||||
|
|
||||||
async function buildAuthHeader(): Promise<string | undefined> {
|
async function buildAuthHeader(): Promise<string | undefined> {
|
||||||
const {decrypt} = useCrypto();
|
const {decrypt} = useCrypto();
|
||||||
const { getValue } = useStore();
|
const { getValue } = useStore();
|
||||||
@@ -36,10 +38,15 @@ export function useApi() {
|
|||||||
...options.headers,
|
...options.headers,
|
||||||
} as Record<string, string>;
|
} as Record<string, string>;
|
||||||
|
|
||||||
const response = await fetch(url, {
|
const response = isTauri()
|
||||||
...options,
|
? await (await import('@tauri-apps/plugin-http')).fetch(url, {
|
||||||
headers,
|
...options,
|
||||||
});
|
headers,
|
||||||
|
})
|
||||||
|
: await fetch(url, {
|
||||||
|
...options,
|
||||||
|
headers,
|
||||||
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`API call failed: ${response.statusText}`);
|
throw new Error(`API call failed: ${response.statusText}`);
|
||||||
|
|||||||
@@ -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;
|
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) {
|
if (!storePromise) {
|
||||||
|
const { load } = await import('@tauri-apps/plugin-store');
|
||||||
storePromise = load('store.json', {
|
storePromise = load('store.json', {
|
||||||
autoSave: false,
|
autoSave: false,
|
||||||
defaults: {},
|
defaults: {},
|
||||||
@@ -14,15 +19,28 @@ function getStore(): Promise<Store> {
|
|||||||
export function useStore() {
|
export function useStore() {
|
||||||
const setValue = async <T>(key: string, value: T) => {
|
const setValue = async <T>(key: string, value: T) => {
|
||||||
console.log('setValue',key,value);
|
console.log('setValue',key,value);
|
||||||
const store = await getStore();
|
if (isTauri()) {
|
||||||
await store.set(key, value);
|
const store = await getStore();
|
||||||
await store.save();
|
await store.set(key, value);
|
||||||
|
await store.save();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
localStorage.setItem(key, JSON.stringify(value));
|
||||||
};
|
};
|
||||||
|
|
||||||
const getValue = async <T>(key: string) => {
|
const getValue = async <T>(key: string) => {
|
||||||
console.log('getValue',key);
|
console.log('getValue',key);
|
||||||
const store = await getStore();
|
if (isTauri()) {
|
||||||
return store.get<T>(key);
|
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 {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user