Skip to content

Commit

Permalink
open pgn file with cli argument
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Nov 5, 2023
1 parent 47f6073 commit 4b622bd
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 7 deletions.
66 changes: 66 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tauri-build = { version = "1.5", features = [] }
tauri-plugin-log = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.5", features = ["api-all", "updater"] }
tauri = { version = "1.5", features = [ "cli", "api-all", "updater"] }
zip = "0.6.2"
tokio = { version = "1.33", features = ["full"] }
futures-util = "0.3.24"
Expand Down
11 changes: 10 additions & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@
"maximized": true,
"visible": false
}
]
],
"cli": {
"args": [
{
"name": "file",
"index": 1,
"takesValue": true
}
]
}
}
}
44 changes: 41 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import DatabaseView from "@/components/databases/DatabaseView";
import HomePage from "@/components/home/HomePage";
import { getVersion } from "@tauri-apps/api/app";
import { attachConsole } from "tauri-plugin-log-api";
import { getMatches } from '@tauri-apps/api/cli'

import {
Outlet,
Expand All @@ -27,12 +28,15 @@ import {
} from "react-router-dom";
import { useEffect } from "react";
import DatabasesPage from "./components/databases/DatabasesPage";
import { useAtomValue } from "jotai";
import { pieceSetAtom, primaryColorAtom } from "./atoms/atoms";
import { useAtom, useAtomValue } from "jotai";
import { currentTabAtom, pieceSetAtom, primaryColorAtom } from "./atoms/atoms";

import "@/styles/chessgroundBaseOverride.css";
import "@/styles/chessgroundColorsOverride.css";
import { commands } from "./bindings";
import { count_pgn_games, read_games } from "./utils/db";
import { parsePGN } from "./utils/chess";
import { getGameName } from "./utils/treeReducer";

const router = createBrowserRouter(
createRoutesFromElements(
Expand Down Expand Up @@ -82,15 +86,49 @@ export default function App() {
const toggleColorScheme = (value?: ColorScheme) =>
setColorScheme(value || (colorScheme === "dark" ? "light" : "dark"));
const pieceSet = useAtomValue(pieceSetAtom);
const [, setCurrentTab] = useAtom(currentTabAtom);

useEffect(() => {
(async () => {
await commands.closeSplashscreen();
const detach = await attachConsole();

const matches = await getMatches();
if (matches.args["file"].occurrences > 0) {
if (typeof matches.args["file"].value !== "string") return;
const file = matches.args["file"].value;

router.navigate("/boards", { replace: true });
const count = await count_pgn_games(file);
const input = (await read_games(file, 0, 0))[0];

const fileInfo = {
metadata: {
tags: [],
type: "game" as const,
},
name: file,
path: file,
numGames: count,
} ;
const tree = await parsePGN(input);
setCurrentTab((prev) => {
sessionStorage.setItem(prev.value, JSON.stringify(tree));
return {
...prev,
name: `${getGameName(tree.headers)} (Imported)`,
file: fileInfo,
gameNumber: 0,
type: "analysis",
};
});
}

return () => {
detach();
};
})();
});
}, []);

Check warning on line 131 in src/App.tsx

View workflow job for this annotation

GitHub Actions / test

React Hook useEffect has a missing dependency: 'setCurrentTab'. Either include it or remove the dependency array

Check warning on line 131 in src/App.tsx

View workflow job for this annotation

GitHub Actions / test

React Hook useEffect has a missing dependency: 'setCurrentTab'. Either include it or remove the dependency array

return (
<ColorSchemeProvider
Expand Down
3 changes: 1 addition & 2 deletions src/components/boards/BoardAnalysis.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Paper, Stack, Tabs } from "@mantine/core";
import { Stack, Tabs } from "@mantine/core";
import { useHotkeys, useToggle } from "@mantine/hooks";
import {
IconDatabase,
Expand Down Expand Up @@ -33,7 +33,6 @@ import {
currentTabSelectedAtom,
} from "@/atoms/atoms";
import { saveToFile } from "@/utils/tabs";
import EvalChart from "../common/EvalChart";

function BoardAnalysis() {
const [editingMode, toggleEditingMode] = useToggle();
Expand Down

0 comments on commit 4b622bd

Please sign in to comment.