Restore recent environment on workspace change

Fixes #6
This commit is contained in:
Gregory Schier
2023-10-29 11:31:13 -07:00
parent 90d2743267
commit 88eea09428
4 changed files with 40 additions and 19 deletions

View File

@@ -8,7 +8,7 @@
},
"package": {
"productName": "Yaak",
"version": "2023.1.2"
"version": "2023.1.3"
},
"tauri": {
"windows": [],

View File

@@ -18,9 +18,12 @@ import { useEffect } from 'react';
import { setPathname } from '../lib/persistPathname';
export function GlobalHooks() {
// Include here so they always update, even
// if no component references them
useRecentWorkspaces();
useRecentEnvironments();
useRecentRequests();
const queryClient = useQueryClient();
const { wasUpdatedExternally } = useRequestUpdateKey(null);

View File

@@ -16,6 +16,7 @@ import { Icon } from './core/Icon';
import { InlineCode } from './core/InlineCode';
import { HStack } from './core/Stacks';
import { useDialog } from './DialogContext';
import { getRecentEnvironments } from '../hooks/useRecentEnvironments';
type Props = Pick<ButtonProps, 'className' | 'justify' | 'forDropdown'>;
@@ -54,26 +55,28 @@ export const WorkspaceActionsDropdown = memo(function WorkspaceActionsDropdown({
<Button
className="focus"
color="gray"
onClick={() => {
rightSlot={<Icon icon="openNewWindow" />}
onClick={async () => {
hide();
routes.navigate('workspace', { workspaceId: w.id });
const environmentId = (await getRecentEnvironments(w.id))[0];
await invoke('new_window', {
url: routes.paths.workspace({ workspaceId: w.id, environmentId }),
});
}}
>
This Window
New Window
</Button>
<Button
autoFocus
className="focus"
color="gray"
rightSlot={<Icon icon="openNewWindow" />}
onClick={async () => {
hide();
await invoke('new_window', {
url: routes.paths.workspace({ workspaceId: w.id }),
});
const environmentId = (await getRecentEnvironments(w.id))[0];
routes.navigate('workspace', { workspaceId: w.id, environmentId });
}}
>
New Window
This Window
</Button>
</HStack>
);

View File

@@ -1,31 +1,38 @@
import { useEffect } from 'react';
import { createGlobalState, useEffectOnce, useLocalStorage } from 'react-use';
import { useActiveRequestId } from './useActiveRequestId';
import { createGlobalState, useEffectOnce } from 'react-use';
import { useActiveWorkspaceId } from './useActiveWorkspaceId';
import { useActiveEnvironmentId } from './useActiveEnvironmentId';
import { useKeyValue } from './useKeyValue';
import { NAMESPACE_GLOBAL, getKeyValue } from '../lib/keyValueStore';
const useHistoryState = createGlobalState<string[]>([]);
const kvKey = (workspaceId: string) => 'recent_environments::' + workspaceId;
const namespace = NAMESPACE_GLOBAL;
const defaultValue: string[] = [];
export function useRecentEnvironments() {
const activeWorkspaceId = useActiveWorkspaceId();
const activeEnvironmentId = useActiveEnvironmentId();
const [history, setHistory] = useHistoryState();
const [lsState, setLSState] = useLocalStorage<string[]>(
'recent_environments::' + activeWorkspaceId,
[],
);
const kv = useKeyValue<string[]>({
key: kvKey(activeWorkspaceId ?? 'n/a'),
namespace,
defaultValue,
});
// Load local storage state on initial render
useEffectOnce(() => {
if (lsState) {
setHistory(lsState);
if (kv.value) {
setHistory(kv.value);
}
});
// Update local storage state when history changes
useEffect(() => {
setLSState(history);
}, [history, setLSState]);
kv.set(history);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [history]);
// Set history when active request changes
useEffect(() => {
@@ -38,3 +45,11 @@ export function useRecentEnvironments() {
return history;
}
export async function getRecentEnvironments(workspaceId: string) {
return getKeyValue<string[]>({
namespace,
key: kvKey(workspaceId),
fallback: defaultValue,
});
}