mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-15 21:53:36 +01:00
30 lines
1.1 KiB
TypeScript
30 lines
1.1 KiB
TypeScript
import type { Environment } from '@yaakapp-internal/models';
|
|
import { useSetAtom } from 'jotai/index';
|
|
import { invokeCmd } from '../lib/tauri';
|
|
import { environmentsAtom, getEnvironment } from './useEnvironments';
|
|
import { useFastMutation } from './useFastMutation';
|
|
import { updateModelList } from './useSyncModelStores';
|
|
|
|
export function useUpdateEnvironment(id: string | null) {
|
|
const setEnvironments = useSetAtom(environmentsAtom);
|
|
return useFastMutation<
|
|
Environment,
|
|
unknown,
|
|
Partial<Environment> | ((r: Environment) => Environment)
|
|
>({
|
|
mutationKey: ['update_environment', id],
|
|
mutationFn: async (v) => {
|
|
const environment = getEnvironment(id);
|
|
if (environment == null) {
|
|
throw new Error("Can't update a null environment");
|
|
}
|
|
|
|
const newEnvironment = typeof v === 'function' ? v(environment) : { ...environment, ...v };
|
|
return invokeCmd<Environment>('cmd_update_environment', { environment: newEnvironment });
|
|
},
|
|
onSuccess: async (environment) => {
|
|
setEnvironments(updateModelList(environment));
|
|
},
|
|
});
|
|
}
|