From 6f8618d5230baa8f4c543cc483bd6796d2a4160e Mon Sep 17 00:00:00 2001 From: Triston Date: Wed, 3 Apr 2024 20:01:52 -0500 Subject: [PATCH 1/2] feat(stationstores.ts): refactor station stores to use the get function fix #19 --- src/hooks/stationStores.ts | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/hooks/stationStores.ts b/src/hooks/stationStores.ts index a2fe568..4139704 100644 --- a/src/hooks/stationStores.ts +++ b/src/hooks/stationStores.ts @@ -2,27 +2,20 @@ import { createStore } from "kaioken" export const useStationsStore = createStore( null as Station[] | null, - (set) => ({ + (set, get) => ({ add: (station: Station): Station[] => { - let newState: Station[] | null = null - set((state) => { - newState = [...(state ?? []), station] - return newState - }) - //@ts-ignore + let newState: Station[] = [...(get() ?? []), station] + set(newState) return newState }, rmStation: (stationId): Station[] => { - let newState: Station[] | null = null - set((state) => { - newState = state?.filter((station) => station.id !== stationId) ?? [] - return newState - }) - //@ts-ignore + let newState: Station[] | null = + get()?.filter((station) => station.id !== stationId) ?? [] + set(newState) return newState }, override: (stationsList: Station[]) => { - set((_state) => stationsList) + set(stationsList) }, }) ) From a083b589ebc9d68de43ba3f0f7c5263dcb008f13 Mon Sep 17 00:00:00 2001 From: Triston Date: Wed, 3 Apr 2024 21:34:14 -0500 Subject: [PATCH 2/2] fix: fix logic for writing storage file fix #19 --- src-tauri/Cargo.toml | 2 +- src-tauri/tauri.conf.json | 5 +++-- src/hooks/storageStores.ts | 23 ++++++++++++++++++++--- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 86f540c..8d59c64 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -11,7 +11,7 @@ edition = "2021" tauri-build = { version = "1", features = [] } [dependencies] -tauri = { version = "1", features = [ "fs-write-file", "fs-read-file", "path-all", "fs-exists", "shell-open"] } +tauri = { version = "1", features = [ "fs-read-file", "fs-create-dir", "fs-exists", "fs-write-file", "path-all", "shell-open"] } serde = { version = "1", features = ["derive"] } serde_json = "1" diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 8a71979..8d4e952 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -21,11 +21,12 @@ "all": false }, "fs": { - "all": false, + "all": false, + "createDir": true, "readFile": true, "writeFile": true, "exists": true, - "scope": ["$APPDATA/*"] + "scope": ["$APPDATA/*", "$HOME/Library/Application Support/com.KlectrRadio.dev"] }, "path": { "all": true diff --git a/src/hooks/storageStores.ts b/src/hooks/storageStores.ts index 9c6b163..0d80db0 100644 --- a/src/hooks/storageStores.ts +++ b/src/hooks/storageStores.ts @@ -1,4 +1,9 @@ -import { exists, readTextFile, writeTextFile } from "@tauri-apps/api/fs" +import { + createDir, + exists, + readTextFile, + writeTextFile, +} from "@tauri-apps/api/fs" import { appDataDir } from "@tauri-apps/api/path" import { createStore } from "kaioken" import { Station } from "../hooks/stationStores" @@ -10,7 +15,11 @@ export const useStorageStore = createStore( export function useStorage() { async function _createStationsFile(path: string): Promise { - await writeTextFile(path, "[]", { append: false }) + try { + await writeTextFile(path, "[]", { append: false }) + } catch (err) { + console.error("CUSTOM ERROR: ", err) + } return [] } @@ -22,9 +31,17 @@ export function useStorage() { console.error("getStationsFile: ", err) return undefined } + if (!dir) return undefined + + const dirExists = await exists(dir) + if (!dirExists) { + await createDir(dir) + } + const path = `${dir}stations.json` - if (!(await exists(path))) return await _createStationsFile(path) + const pathExists = await exists(path) + if (!pathExists) return await _createStationsFile(path) const jsonString = await readTextFile(path) const json = JSON.parse(jsonString) as Station[] useStorageStore.setState(() => path)