Update local model stores in all mutations (#129)

This commit is contained in:
Gregory Schier
2024-10-23 09:54:43 -07:00
committed by GitHub
parent c5e6d6f2cb
commit 7759649963
29 changed files with 270 additions and 79 deletions

View File

@@ -1,17 +1,21 @@
import { useMutation } from '@tanstack/react-query';
import type { Environment } from '@yaakapp-internal/models';
import {useSetAtom} from "jotai";
import { trackEvent } from '../lib/analytics';
import { invokeCmd } from '../lib/tauri';
import { useActiveEnvironment } from './useActiveEnvironment';
import { useActiveWorkspace } from './useActiveWorkspace';
import {environmentsAtom} from "./useEnvironments";
import { usePrompt } from './usePrompt';
import {updateModelList} from "./useSyncModelStores";
export function useCreateEnvironment() {
const [, setActiveEnvironmentId] = useActiveEnvironment();
const prompt = usePrompt();
const workspace = useActiveWorkspace();
const setEnvironments = useSetAtom(environmentsAtom);
return useMutation<Environment, unknown, void>({
return useMutation<Environment | null, unknown, void>({
mutationKey: ['create_environment'],
mutationFn: async () => {
const name = await prompt({
@@ -23,6 +27,8 @@ export function useCreateEnvironment() {
defaultValue: 'My Environment',
confirmText: 'Create',
});
if (name == null) return null;
return invokeCmd('cmd_create_environment', {
name,
variables: [],
@@ -31,7 +37,11 @@ export function useCreateEnvironment() {
},
onSettled: () => trackEvent('environment', 'create'),
onSuccess: async (environment) => {
if (workspace == null) return;
if (environment == null) return;
// Optimistic update
setEnvironments(updateModelList(environment));
setActiveEnvironmentId(environment.id);
},
});