Skip to content

Commit

Permalink
feat: preserve webview state
Browse files Browse the repository at this point in the history
  • Loading branch information
hverlin committed Dec 28, 2024
1 parent 2c284de commit 1a69efb
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 68 deletions.
8 changes: 0 additions & 8 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,3 @@ mise generate git-pre-commit --write --task=check
```shell
mise tasks # list all available tasks
```

## Roadmap

- [ ] Automatically setup SDKs based on mise tools
- [ ] Improve UI to install tools
- [ ] Manage mise settings
- [ ] Upgrade mise tools
- ...
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@
"react": "18.3.1",
"react-dom": "18.3.1",
"react-error-boundary": "^4.1.2",
"toml-v1": "^1.0.0"
"toml-v1": "^1.0.0",
"zustand": "^5.0.2"
}
}
26 changes: 26 additions & 0 deletions pnpm-lock.yaml

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

3 changes: 1 addition & 2 deletions src/webviewPanel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { readFileSync, writeFileSync } from "node:fs";
import { homedir } from "node:os";
import * as os from "node:os";
import path from "node:path";
import * as cheerio from "cheerio";
Expand All @@ -19,7 +18,7 @@ export default class WebViewPanel {
private readonly _extContext: vscode.ExtensionContext;
private _disposables: vscode.Disposable[] = [];
private readonly miseService: MiseService;
private view: PanelView = "TOOLS";
private readonly view: PanelView = "TOOLS";

public static createOrShow(
extContext: vscode.ExtensionContext,
Expand Down
1 change: 0 additions & 1 deletion src/webviews/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Settings } from "./Settings";
import { Tools } from "./Tools";
import { TrackedConfigs } from "./TrackedConfigs";
import { Tabs } from "./components/Tabs";

export function App() {
const view = document
Expand Down
9 changes: 7 additions & 2 deletions src/webviews/Settings.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { VscodeCheckbox } from "@vscode-elements/react-elements";
import React, { useState } from "react";
import { type FlattenedProperty, getDefaultForType } from "../utils/miseUtilts";
import CustomTable from "./components/CustomTable";
import { IconButton } from "./components/IconButton";
import { useWebviewStore } from "./store";
import {
toDisplayPath,
useEditSettingMutation,
Expand All @@ -12,7 +12,12 @@ import {
} from "./webviewVsCodeApi";

export const Settings = () => {
const [showModifiedOnly, setShowModifiedOnly] = useState(true);
const showModifiedOnly = useWebviewStore(
(state) => state.showModifiedSettingsOnly,
);
const setShowModifiedOnly = useWebviewStore(
(state) => state.setShowModifiedSettingsOnly,
);
const openFileMutation = useOpenFileMutation();
const queryClient = useQueryClient();

Expand Down
16 changes: 12 additions & 4 deletions src/webviews/Tools.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { VscodeButton, VscodeCheckbox } from "@vscode-elements/react-elements";
import { useState } from "react";
import CustomTable from "./components/CustomTable";
import { IconButton } from "./components/IconButton";
import { useWebviewStore } from "./store";
import {
toDisplayPath,
trackedConfigsQueryOptions,
Expand Down Expand Up @@ -223,9 +223,17 @@ const ActionCell = ({

export const Tools = () => {
const queryClient = useQueryClient();
const [selectedTool, setSelectedTool] = useState<MiseTool | null>(null);
const [showOutdatedOnly, setShowOutdatedOnly] = useState(false);
const [activeOnly, setActiveOnly] = useState(true);
const selectedTool = useWebviewStore((state) => state.selectedTool);
const setSelectedTool = useWebviewStore((state) => state.setSelectedTool);

const showOutdatedOnly = useWebviewStore((state) => state.showOutdatedOnly);
const setShowOutdatedOnly = useWebviewStore(
(state) => state.setShowOutdatedOnly,
);
const activeOnly = useWebviewStore((state) => state.showActiveToolsOnly);
const setActiveOnly = useWebviewStore(
(state) => state.setShowActiveToolsOnly,
);

const toolsQuery = useQuery({
queryKey: ["tools"],
Expand Down
45 changes: 0 additions & 45 deletions src/webviews/components/Tabs.tsx

This file was deleted.

54 changes: 54 additions & 0 deletions src/webviews/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { create } from "zustand/index";
import { type StorageValue, persist } from "zustand/middleware";
import { vscode } from "./webviewVsCodeApi";

interface WebviewStore {
showModifiedSettingsOnly: boolean;
setShowModifiedSettingsOnly: (value: boolean) => void;

selectedTool: MiseTool | null;
setSelectedTool: (value: MiseTool | null) => void;

showOutdatedOnly: boolean;
setShowOutdatedOnly: (value: boolean) => void;

showActiveToolsOnly: boolean;
setShowActiveToolsOnly: (value: boolean) => void;
}

export const useWebviewStore = create<WebviewStore>()(
persist(
(set) => ({
showModifiedSettingsOnly: true,
setShowModifiedSettingsOnly: (value: boolean) =>
set({ showModifiedSettingsOnly: value }),

selectedTool: null,
setSelectedTool: (value: MiseTool | null) => set({ selectedTool: value }),

showOutdatedOnly: false,
setShowOutdatedOnly: (value: boolean) => set({ showOutdatedOnly: value }),

showActiveToolsOnly: true,
setShowActiveToolsOnly: (value: boolean) =>
set({ showActiveToolsOnly: value }),
}),
{
name: "webview-store",
storage: {
getItem: async (name) => {
const state = await vscode.getState();
return state[name] as StorageValue<WebviewStore>;
},
setItem: async (name, value) => {
return vscode.setState({ [name]: value });
},
removeItem: async (name) => {
const state = await vscode.getState();
delete state[name];
vscode.setState(state);
},
},
},
),
);
8 changes: 3 additions & 5 deletions src/webviews/webviewVsCodeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ import { queryOptions, useMutation } from "@tanstack/react-query";
declare global {
interface Window {
acquireVsCodeApi(): {
getState(): Promise<Record<string, unknown>>;
setState(state: Record<string, unknown>): void;
postMessage(message: unknown): void;
};
}
}

export function getVsCode() {
return window.acquireVsCodeApi();
}

export const vscode = getVsCode();
export const vscode = window.acquireVsCodeApi();

export const vscodeClient = {
async request({
Expand Down

0 comments on commit 1a69efb

Please sign in to comment.