From 515ea481ed3b67e1de3946c9990a4ba1855d09ca Mon Sep 17 00:00:00 2001 From: Vomitblood Date: Tue, 6 Aug 2024 12:39:19 +0800 Subject: [PATCH] paths global state storage --- bun.lockb | Bin 170480 -> 170480 bytes package.json | 18 ++--- src-tauri/tauri.conf.json | 12 ++- src/components/Generic/Initialization.tsx | 12 +++ src/components/Generic/Layout.tsx | 10 +++ src/lib/path.ts | 40 ---------- src/lib/store/zustand/path.ts | 91 ++++++++++++++++++++-- src/pages/_app.tsx | 4 +- src/pages/_document.tsx | 11 +-- src/pages/index.tsx | 9 +-- src/pages/testing.tsx | 23 ++---- 11 files changed, 140 insertions(+), 90 deletions(-) create mode 100644 src/components/Generic/Initialization.tsx create mode 100644 src/components/Generic/Layout.tsx delete mode 100644 src/lib/path.ts diff --git a/bun.lockb b/bun.lockb index 4a446f30c57f48500a45a65b363f70b7414d25d7..861244558967d3a43c81036245c671a5690b40fc 100755 GIT binary patch delta 935 zcmeycnd`%5t_gYyvx?;UAI+Na_%2RFxTyN_-Yo29pe*gQe zWX~K0n=`Y_#1@9UynAyh{lz z-Fqfh<9NL2+@0YsH*Y(o{#E3YZsXZ|r=u&gs->^Lkgnb`Yg={Jt|NNOHp{NP!BqcT z!br`)!{CpNOxw(*-;OaZjId`?bE-RgN`AwEr3?3Yaa368IEVR!ZM$3)KEHe8o|c=d z+O$d@-kx!=E1D(pU^62F10%yf{(nH)lmP)))^OUn$umvkJ5;_~F->Oejl#)3egz4O zgVbm1Ed9GjY~_C2d3%NSXGp$S`t2F7`JcdTrN0uMBy32N|7g|m$oJ>=_Q|~)BpkM8 zOjs3B$aMFVz~v-Qp?^G`f92=+hOF!C{rBZ~@$*{d&sPo~ER*|mqMhMnK=5~o4ex}% zdv#81`BL3(;KQQvN?`NT4GN47XO}#jskb{V+ubweg`eV^she#2xt=MS^L{V7kSV)V zgX{KQy_@%xo*a}?>T(X-b*I)q+Qxa6iq^NAt3v`3WZ%Er?7B&V$)WJETVLzLiu!__ z#;7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!(c5y?WWrk4l{N!O}9D#6n+JgTXcX?bi45}#`lpxNo55l1*YkTB$*VZ z+leu;ZO>F<3IZ9tM}?^nB&;~yPnAg=!fI1xIx$@+3#e2 { + const setPaths = usePathStore((state) => state.setPaths); + + useEffect(() => { + setPaths(); + }); + + return null; +}; diff --git a/src/components/Generic/Layout.tsx b/src/components/Generic/Layout.tsx new file mode 100644 index 0000000..d1c197d --- /dev/null +++ b/src/components/Generic/Layout.tsx @@ -0,0 +1,10 @@ +import { Box } from "@mui/material"; +import { HeaderBar } from "../HeaderBar/HeaderBar"; + +export const Layout = () => { + return ( + + + + ); +}; diff --git a/src/lib/path.ts b/src/lib/path.ts deleted file mode 100644 index 4eac8c6..0000000 --- a/src/lib/path.ts +++ /dev/null @@ -1,40 +0,0 @@ -export const getConfigDirectory = async () => { - const { configDir } = await import("@tauri-apps/api/path"); - return (await configDir()) + "stort/"; -}; - -const pathBase = "stort/"; - -export const getPaths = async () => { - const { - cacheDir, - configDir, - dataDir, - desktopDir, - documentDir, - downloadDir, - executableDir, - fontDir, - homeDir, - logDir, - pictureDir, - templateDir, - videoDir, - } = await import("@tauri-apps/api/path"); - - return { - cacheDirectory: (await cacheDir()) + pathBase, - configDirectory: (await configDir()) + pathBase, - dataDirectory: (await dataDir()) + pathBase, - desktopDirectory: (await desktopDir()) + pathBase, - documentDirectory: (await documentDir()) + pathBase, - downloadDirectory: (await downloadDir()) + pathBase, - executableDirectory: (await executableDir()) + pathBase, - fontDirectory: (await fontDir()) + pathBase, - homeDirectory: (await homeDir()) + pathBase, - logDirectory: (await logDir()) + pathBase, - pictureDirectory: (await pictureDir()) + pathBase, - templateDirectory: (await templateDir()) + pathBase, - videoDirectory: (await videoDir()) + pathBase, - }; -}; diff --git a/src/lib/store/zustand/path.ts b/src/lib/store/zustand/path.ts index 1467f48..718e4e5 100644 --- a/src/lib/store/zustand/path.ts +++ b/src/lib/store/zustand/path.ts @@ -1,14 +1,93 @@ import { create } from "zustand"; -type PathType = { +type Paths = { + audioDirectory: string; + cacheDirectory: string; configDirectory: string; + dataDirectory: string; + desktopDirectory: string; + documentDirectory: string; + downloadDirectory: string; + executableDirectory: string; + fontDirectory: string; + homeDirectory: string; + logDirectory: string; + pictureDirectory: string; + templateDirectory: string; + videoDirectory: string; }; -export const getPaths = async () => { - const { configDir } = await import("@tauri-apps/api/path"); - return (await configDir()) + "stort/"; +type PathStore = { + paths: Paths; + setPaths: () => Promise; }; -export const usePathStore = create((set) => ({ - configDirectory: getPaths(), +const programTrailingPath = "stort/"; + +const initializePaths = async (): Promise => { + try { + const { + audioDir, + cacheDir, + configDir, + dataDir, + desktopDir, + documentDir, + downloadDir, + executableDir, + fontDir, + homeDir, + logDir, + pictureDir, + templateDir, + videoDir, + } = await import("@tauri-apps/api/path"); + + return { + audioDirectory: await audioDir(), + cacheDirectory: (await cacheDir()) + programTrailingPath, + configDirectory: (await configDir()) + programTrailingPath, + dataDirectory: (await dataDir()) + programTrailingPath, + desktopDirectory: await desktopDir(), + documentDirectory: await documentDir(), + downloadDirectory: await downloadDir(), + executableDirectory: await executableDir(), + fontDirectory: await fontDir(), + homeDirectory: await homeDir(), + logDirectory: await logDir(), + pictureDirectory: await pictureDir(), + templateDirectory: await templateDir(), + videoDirectory: await videoDir(), + }; + } catch (error) { + console.error("Error initializing paths:", error); + throw error; + } +}; + +export const usePathStore = create((set) => ({ + paths: { + audioDirectory: "", + cacheDirectory: "", + configDirectory: "", + dataDirectory: "", + desktopDirectory: "", + documentDirectory: "", + downloadDirectory: "", + executableDirectory: "", + fontDirectory: "", + homeDirectory: "", + logDirectory: "", + pictureDirectory: "", + templateDirectory: "", + videoDirectory: "", + }, + setPaths: async () => { + try { + const paths = await initializePaths(); + set({ paths }); + } catch (error) { + console.error("Failed to set paths:", error); + } + }, })); diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx index b91cc5f..3f360e2 100644 --- a/src/pages/_app.tsx +++ b/src/pages/_app.tsx @@ -1,7 +1,8 @@ import { CacheProvider, EmotionCache } from "@emotion/react"; -import CssBaseline from "@mui/material/CssBaseline"; +import { CssBaseline } from "@mui/material"; import { AppProps } from "next/app"; import Head from "next/head"; +import { Initialization } from "../components/Generic/Initialization"; import { UserThemeProvider } from "../contexts/ThemeContext"; import createEmotionCache from "../lib/createEmotionCache"; import "../styles/global.css"; @@ -23,6 +24,7 @@ export default function MyApp(props: MyAppProps) { {/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */} + diff --git a/src/pages/_document.tsx b/src/pages/_document.tsx index 6ab529d..e96bf08 100644 --- a/src/pages/_document.tsx +++ b/src/pages/_document.tsx @@ -20,15 +20,8 @@ export default function MyDocument({ emotionStyleTags }: MyDocumentProps) { const theme = useTheme(); return ( - - - {/* PWA primary color */} - - - - - {emotionStyleTags} - + + {emotionStyleTags}
diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 4f785f5..2e81fcf 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -1,7 +1,6 @@ -import { Box } from "@mui/material"; import { useAtom } from "jotai"; +import { Layout } from "../components/Generic/Layout"; import { LoadingScreen } from "../components/Generic/LoadingScreen"; -import { HeaderBar } from "../components/HeaderBar/HeaderBar"; import { loadingAtom } from "../lib/store/jotai/loading"; export default function Home() { @@ -10,10 +9,6 @@ export default function Home() { if (loading) { return ; } else { - return ( - - - - ); + return ; } } diff --git a/src/pages/testing.tsx b/src/pages/testing.tsx index 534deda..09b77b6 100644 --- a/src/pages/testing.tsx +++ b/src/pages/testing.tsx @@ -1,27 +1,18 @@ "use client"; import { BugReport } from "@mui/icons-material"; -import { Box, Button, IconButton, Typography } from "@mui/material"; +import { Box, Button, IconButton, TextField, Typography } from "@mui/material"; import { useRouter } from "next/router"; -import { useEffect, useState } from "react"; -import { getConfigDirectory } from "../lib/path"; +import { useState } from "react"; import { usePathStore } from "../lib/store/zustand/path"; export default function Testing() { const router = useRouter(); - const configPath = usePathStore((state) => state.configDirectory); - const dataPath = usePathStore((state) => state.dataDirectory); - const cachePath = usePathStore((state) => state.cacheDirectory); + const paths = usePathStore((state) => state.paths); const [text, setText] = useState(""); - useEffect(() => { - getConfigDirectory().then((configDirectory) => { - setText(configDirectory); - }); - }); - return ( {text} + ); }