Restore recent environment on workspace change

Fixes #6
This commit is contained in:
Gregory Schier
2023-10-29 11:31:13 -07:00
parent 7dac299edd
commit 9dd4489049
4 changed files with 40 additions and 19 deletions

View File

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

View File

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

View File

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

View File

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