Skip to content

Commit

Permalink
fix: use new store api
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludea committed Oct 18, 2024
1 parent 12e4aab commit 074ca69
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 63 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
"@tauri-apps/plugin-os": "^2.0.0",
"@tauri-apps/plugin-process": "^2.0.0",
"@tauri-apps/plugin-shell": "^2.0.0",
"@tauri-apps/plugin-store": "^2.0.0",
"@tauri-apps/plugin-store": "github:tauri-apps/tauri-plugin-store#v2",
"@tauri-apps/plugin-stronghold": "^2.0.0",
"internal-ip": "^8.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-i18next": "^15.0.1",
"react-router-dom": "^6.22.3",
"tauri-plugin-autostart-api": "https://github.com/tauri-apps/tauri-plugin-autostart",
"tauri-plugin-store-api": "https://github.com/tauri-apps/tauri-plugin-store",
"tauri-plugin-stronghold-api": "https://github.com/tauri-apps/tauri-plugin-stronghold"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ tauri-plugin-process = "2.0.0"
tauri-plugin-single-instance = "2.0.0"
tauri-plugin-autostart = "2.0.0"
tauri-plugin-shell = "2.0.0"
tauri-plugin-store = "2.0.0"
tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" }
tauri-plugin-os = "2.0.0"

[features]
Expand Down
2 changes: 2 additions & 0 deletions src-tauri/capabilities/desktop.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"core:window:allow-start-dragging",
"allow-check-if-installed",
"allow-update-workspace",
"store:default",
"notification:default",
{
"identifier": "shell:allow-spawn",
"allow": [
Expand Down
4 changes: 3 additions & 1 deletion src-tauri/capabilities/mobile.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"core:window:default",
"core:app:default",
"core:resources:default",
"core:menu:default"
"core:menu:default",
"store:default",
"notification:default"
]
}

2 changes: 1 addition & 1 deletion src-tauri/gen/schemas/acl-manifests.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src-tauri/gen/schemas/capabilities.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"desktop-capability":{"identifier":"desktop-capability","description":"Capability for desktop","local":true,"windows":["main"],"permissions":["core:path:default","core:event:default","core:window:default","core:app:default","core:resources:default","core:menu:default","core:tray:default","core:window:allow-minimize","core:window:allow-hide","core:window:allow-start-dragging","allow-check-if-installed","allow-update-workspace",{"identifier":"shell:allow-spawn","allow":[{"args":["-c",{"validator":".+"}],"cmd":"sh","name":"sh"},{"args":["/C",{"validator":".+"}],"cmd":"cmd","name":"cmd"}]}],"platforms":["linux","macOS","windows"]},"mobile-capability":{"identifier":"mobile-capability","description":"Capability for mobile","local":true,"windows":["main"],"permissions":["core:path:default","core:event:default","core:window:default","core:app:default","core:resources:default","core:menu:default"],"platforms":["iOS","android"]}}
{"desktop-capability":{"identifier":"desktop-capability","description":"Capability for desktop","local":true,"windows":["main"],"permissions":["core:path:default","core:event:default","core:window:default","core:app:default","core:resources:default","core:menu:default","core:tray:default","core:window:allow-minimize","core:window:allow-hide","core:window:allow-start-dragging","allow-check-if-installed","allow-update-workspace","store:default","notification:default",{"identifier":"shell:allow-spawn","allow":[{"args":["-c",{"validator":".+"}],"cmd":"sh","name":"sh"},{"args":["/C",{"validator":".+"}],"cmd":"cmd","name":"cmd"}]}],"platforms":["linux","macOS","windows"]},"mobile-capability":{"identifier":"mobile-capability","description":"Capability for mobile","local":true,"windows":["main"],"permissions":["core:path:default","core:event:default","core:window:default","core:app:default","core:resources:default","core:menu:default","store:default","notification:default"],"platforms":["iOS","android"]}}
30 changes: 20 additions & 10 deletions src-tauri/gen/schemas/desktop-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2260,11 +2260,6 @@
"type": "string",
"const": "store:allow-clear"
},
{
"description": "Enables the create_store command without any pre-configured scope.",
"type": "string",
"const": "store:allow-create-store"
},
{
"description": "Enables the delete command without any pre-configured scope.",
"type": "string",
Expand All @@ -2280,6 +2275,11 @@
"type": "string",
"const": "store:allow-get"
},
{
"description": "Enables the get_store command without any pre-configured scope.",
"type": "string",
"const": "store:allow-get-store"
},
{
"description": "Enables the has command without any pre-configured scope.",
"type": "string",
Expand All @@ -2300,6 +2300,11 @@
"type": "string",
"const": "store:allow-load"
},
{
"description": "Enables the reload command without any pre-configured scope.",
"type": "string",
"const": "store:allow-reload"
},
{
"description": "Enables the reset command without any pre-configured scope.",
"type": "string",
Expand All @@ -2325,11 +2330,6 @@
"type": "string",
"const": "store:deny-clear"
},
{
"description": "Denies the create_store command without any pre-configured scope.",
"type": "string",
"const": "store:deny-create-store"
},
{
"description": "Denies the delete command without any pre-configured scope.",
"type": "string",
Expand All @@ -2345,6 +2345,11 @@
"type": "string",
"const": "store:deny-get"
},
{
"description": "Denies the get_store command without any pre-configured scope.",
"type": "string",
"const": "store:deny-get-store"
},
{
"description": "Denies the has command without any pre-configured scope.",
"type": "string",
Expand All @@ -2365,6 +2370,11 @@
"type": "string",
"const": "store:deny-load"
},
{
"description": "Denies the reload command without any pre-configured scope.",
"type": "string",
"const": "store:deny-reload"
},
{
"description": "Denies the reset command without any pre-configured scope.",
"type": "string",
Expand Down
30 changes: 20 additions & 10 deletions src-tauri/gen/schemas/linux-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2260,11 +2260,6 @@
"type": "string",
"const": "store:allow-clear"
},
{
"description": "Enables the create_store command without any pre-configured scope.",
"type": "string",
"const": "store:allow-create-store"
},
{
"description": "Enables the delete command without any pre-configured scope.",
"type": "string",
Expand All @@ -2280,6 +2275,11 @@
"type": "string",
"const": "store:allow-get"
},
{
"description": "Enables the get_store command without any pre-configured scope.",
"type": "string",
"const": "store:allow-get-store"
},
{
"description": "Enables the has command without any pre-configured scope.",
"type": "string",
Expand All @@ -2300,6 +2300,11 @@
"type": "string",
"const": "store:allow-load"
},
{
"description": "Enables the reload command without any pre-configured scope.",
"type": "string",
"const": "store:allow-reload"
},
{
"description": "Enables the reset command without any pre-configured scope.",
"type": "string",
Expand All @@ -2325,11 +2330,6 @@
"type": "string",
"const": "store:deny-clear"
},
{
"description": "Denies the create_store command without any pre-configured scope.",
"type": "string",
"const": "store:deny-create-store"
},
{
"description": "Denies the delete command without any pre-configured scope.",
"type": "string",
Expand All @@ -2345,6 +2345,11 @@
"type": "string",
"const": "store:deny-get"
},
{
"description": "Denies the get_store command without any pre-configured scope.",
"type": "string",
"const": "store:deny-get-store"
},
{
"description": "Denies the has command without any pre-configured scope.",
"type": "string",
Expand All @@ -2365,6 +2370,11 @@
"type": "string",
"const": "store:deny-load"
},
{
"description": "Denies the reload command without any pre-configured scope.",
"type": "string",
"const": "store:deny-reload"
},
{
"description": "Denies the reset command without any pre-configured scope.",
"type": "string",
Expand Down
75 changes: 40 additions & 35 deletions src/components/Options.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useState, useEffect, useContext } from "react";
import Button from "@mui/material/Button";
import TextField from "@mui/material/TextField";
import Slide from "@mui/material/Slide";
import Grid from "@mui/material/Grid";
import Grid from "@mui/material/Grid2";
import Box from "@mui/material/Box";
import FormControlLabel from "@mui/material/FormControlLabel";
import Checkbox from "@mui/material/Checkbox";
Expand All @@ -19,33 +18,40 @@ import { enable, disable } from "tauri-plugin-autostart-api";
import DeleteIcon from "@mui/icons-material/Delete";

function Options() {
const [gameURL, setGameURL] = useState<string | null>("");
const [gameURL, setGameURL] = useState<string>("");
const [autostart, setAutostart] = useState<boolean>();
const [launcherURL, setLauncherURL] = useState<string | null>("");
const [workspacePath, setWorkspacePath] = useState<string | null>("");
const [launcherURL, setLauncherURL] = useState<string>("");
const [workspacePath, setWorkspacePath] = useState<string>("");

const { setGlobalError } = useContext(SparusErrorContext);
const store = useContext(SparusStoreContext);

useEffect(() => {
store.load().catch((err: string) => setGlobalError(err));
store
.get<string>("game_url")
.then((value: string | null) => setGameURL(value))
.then((value) => {
if (value) setGameURL(value);
})
.catch((err: string) => setGlobalError(err));
store
.get<string>("launcher_url")
.then((value: string | null) => {
setLauncherURL(value);
.then((value) => {
if (value) setLauncherURL(value);
})
.catch((err: string) => setGlobalError(err));
store
.get<string>("workspace_path")
.then((value: string | null) => {
setWorkspacePath(value);
.then((value) => {
if (value) setWorkspacePath(value);
})
.catch((err: string) => setGlobalError(err));
store
.get<boolean>("autostart")
.then((value) => {
if (value) setAutostart(value);
})
.catch((err: string) => setGlobalError(err));
});
}, [setGlobalError, store]);

return (
<Slide direction="right" in mountOnEnter unmountOnExit>
Expand Down Expand Up @@ -77,7 +83,12 @@ function Options() {
type="text"
variant="standard"
value={gameURL}
onChange={(event) => setGameURL(event.target.value)}
onChange={(event) => {
setGameURL(event.target.value);
store
.set("game_url", event.target.value)
.catch((err: string) => setGlobalError(err));
}}
/>
<TextField
autoFocus
Expand All @@ -87,7 +98,12 @@ function Options() {
type="text"
variant="standard"
value={launcherURL}
onChange={(event) => setLauncherURL(event.target.value)}
onChange={(event) => {
setLauncherURL(event.target.value);
store
.set("launcher_url", event.target.value)
.catch((err: string) => setGlobalError(err));
}}
/>
<TextField
autoFocus
Expand All @@ -97,43 +113,32 @@ function Options() {
type="text"
variant="standard"
value={workspacePath}
onChange={(event) => setWorkspacePath(event.target.value)}
onChange={(event) => {
setWorkspacePath(event.target.value);
store
.set("workspace_path", event.target.value)
.catch((err: string) => setGlobalError(err));
}}
/>
<FormControlLabel
control={
<Checkbox
checked={autostart}
onChange={(event) => {
setAutostart(event.target.checked);
if (event.target.checked) {
if (autostart) {
enable().catch((err: string) => setGlobalError(err));
} else disable().catch((err: string) => setGlobalError(err));
store
.set("autostart", event.target.value)
.catch((err: string) => setGlobalError(err));
}}
inputProps={{ "aria-label": "controlled" }}
/>
}
label="Start Sparus on system boot"
/>
</Box>
<Button
variant="contained"
color="primary"
type="submit"
onClick={() => {
store
.set("game_url", gameURL)
.catch((err: string) => setGlobalError(err));
store
.set("launcher_url", launcherURL)
.catch((err: string) => setGlobalError(err));
store
.set("workspace_path", workspacePath)
.catch((err: string) => setGlobalError(err));
store.save().catch((err: string) => setGlobalError(err));
}}
>
Save
</Button>
<IconButton
aria-label="delete"
onClick={() => {
Expand Down
6 changes: 4 additions & 2 deletions src/utils/Context.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ReactNode, createContext } from "react";
import { Store } from "tauri-plugin-store-api";
import { LazyStore } from "@tauri-apps/plugin-store";

type SparusError = {
globalError: string;
Expand All @@ -11,7 +11,9 @@ const SparusErrorContext = createContext<SparusError>({
setGlobalError: () => {},
});

const store = new Store("Sparus.json");
const store = new LazyStore("Sparus.json", {
autoSave: true,
});

const SparusStoreContext = createContext(store);

Expand Down

0 comments on commit 074ca69

Please sign in to comment.