mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-18 23:44:12 +01:00
Environment dropdown and actions
This commit is contained in:
19
src-web/hooks/useActiveEnvironment.ts
Normal file
19
src-web/hooks/useActiveEnvironment.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { useCallback, useMemo } from 'react';
|
||||
import type { Environment } from '../lib/models';
|
||||
import { useActiveEnvironmentId } from './useActiveEnvironmentId';
|
||||
import { useEnvironments } from './useEnvironments';
|
||||
|
||||
export function useActiveEnvironment(): [Environment | null, (environment: Environment) => void] {
|
||||
const [id, setId] = useActiveEnvironmentId();
|
||||
const environments = useEnvironments();
|
||||
const environment = useMemo(
|
||||
() => environments.find((w) => w.id === id) ?? null,
|
||||
[environments, id],
|
||||
);
|
||||
|
||||
const setActiveEnvironment = useCallback((e: Environment) => {
|
||||
setId(e.id)
|
||||
}, [setId]);
|
||||
|
||||
return [environment, setActiveEnvironment];
|
||||
}
|
||||
14
src-web/hooks/useActiveEnvironmentId.ts
Normal file
14
src-web/hooks/useActiveEnvironmentId.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { useCallback } from 'react';
|
||||
import { useSearchParams } from 'react-router-dom';
|
||||
|
||||
export function useActiveEnvironmentId(): [string | null, (id: string) => void] {
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
const id = searchParams.get('environmentId') ?? null;
|
||||
|
||||
const setId = useCallback((id: string) => {
|
||||
searchParams.set('environmentId', id)
|
||||
setSearchParams(searchParams);
|
||||
}, [searchParams, setSearchParams])
|
||||
|
||||
return [id, setId];
|
||||
}
|
||||
@@ -3,20 +3,23 @@ import { invoke } from '@tauri-apps/api';
|
||||
import type { Environment } from '../lib/models';
|
||||
import { environmentsQueryKey } from './useEnvironments';
|
||||
import { useActiveWorkspaceId } from './useActiveWorkspaceId';
|
||||
import { useActiveEnvironmentId } from './useActiveEnvironmentId';
|
||||
|
||||
export function useCreateEnvironment() {
|
||||
const workspaceId = useActiveWorkspaceId();
|
||||
const queryClient = useQueryClient();
|
||||
const [, setActiveEnvironmentId ] = useActiveEnvironmentId();
|
||||
return useMutation<Environment, unknown, Pick<Environment, 'name'>>({
|
||||
mutationFn: (patch) => {
|
||||
return invoke('create_environment', { ...patch, workspaceId });
|
||||
},
|
||||
onSuccess: async (environment) => {
|
||||
if (workspaceId == null) return;
|
||||
queryClient.setQueryData<Environment[]>(environmentsQueryKey({ workspaceId }), (environments) => [
|
||||
...(environments ?? []),
|
||||
environment,
|
||||
]);
|
||||
setActiveEnvironmentId(environment.id);
|
||||
queryClient.setQueryData<Environment[]>(
|
||||
environmentsQueryKey({ workspaceId }),
|
||||
(environments) => [...(environments ?? []), environment],
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user