From c300e8cbd512dd5914701bfc0c01e306067471d0 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Wed, 19 Mar 2025 11:35:20 -0700 Subject: [PATCH] Fix dropdown refresh after Git init --- src-tauri/yaak-git/index.ts | 14 ++++++++++--- src-web/components/CreateWorkspaceDialog.tsx | 5 +++-- src-web/components/GitDropdown.tsx | 12 +++++++---- src-web/lib/queryClient.ts | 18 ++++++++++++++++ src-web/routes/__root.tsx | 22 +++----------------- 5 files changed, 43 insertions(+), 28 deletions(-) create mode 100644 src-web/lib/queryClient.ts diff --git a/src-tauri/yaak-git/index.ts b/src-tauri/yaak-git/index.ts index 9b92429e..3e61add7 100644 --- a/src-tauri/yaak-git/index.ts +++ b/src-tauri/yaak-git/index.ts @@ -52,7 +52,7 @@ export function useGit(dir: string) { onSuccess, }), commitAndPush: useMutation({ - mutationKey: ['git', 'commitpush', dir], + mutationKey: ['git', 'commit_push', dir], mutationFn: async (args) => { await invoke('plugin:yaak-git|commit', { dir, ...args }); return invoke('plugin:yaak-git|push', { dir }); @@ -79,10 +79,18 @@ export function useGit(dir: string) { mutationFn: (args) => invoke('plugin:yaak-git|unstage', { dir, ...args }), onSuccess, }), + init: useGitInit(), }, ] as const; } -export async function gitInit(dir: string) { - await invoke('plugin:yaak-git|initialize', { dir }); +export function useGitInit() { + const queryClient = useQueryClient(); + const onSuccess = () => queryClient.invalidateQueries({ queryKey: ['git'] }); + + return useMutation({ + mutationKey: ['git', 'init'], + mutationFn: (args) => invoke('plugin:yaak-git|initialize', { ...args }), + onSuccess, + }); } diff --git a/src-web/components/CreateWorkspaceDialog.tsx b/src-web/components/CreateWorkspaceDialog.tsx index c1501560..887f617a 100644 --- a/src-web/components/CreateWorkspaceDialog.tsx +++ b/src-web/components/CreateWorkspaceDialog.tsx @@ -1,4 +1,4 @@ -import { gitInit } from '@yaakapp-internal/git'; +import { useGitInit } from '@yaakapp-internal/git'; import type { WorkspaceMeta } from '@yaakapp-internal/models'; import { useState } from 'react'; import { upsertWorkspace } from '../commands/upsertWorkspace'; @@ -17,6 +17,7 @@ interface Props { export function CreateWorkspaceDialog({ hide }: Props) { const [name, setName] = useState(''); + const gitInit = useGitInit(); const [syncConfig, setSyncConfig] = useState<{ filePath: string | null; initGit?: boolean; @@ -44,7 +45,7 @@ export function CreateWorkspaceDialog({ hide }: Props) { }); if (syncConfig.initGit && syncConfig.filePath) { - gitInit(syncConfig.filePath).catch((err) => { + gitInit.mutateAsync({ dir: syncConfig.filePath }).catch((err) => { showErrorToast('git-init-error', String(err)); }); } diff --git a/src-web/components/GitDropdown.tsx b/src-web/components/GitDropdown.tsx index 83d6f198..c7e53074 100644 --- a/src-web/components/GitDropdown.tsx +++ b/src-web/components/GitDropdown.tsx @@ -1,4 +1,4 @@ -import { gitInit, useGit } from '@yaakapp-internal/git'; +import { useGit } from '@yaakapp-internal/git'; import type { WorkspaceMeta } from '@yaakapp-internal/models'; import classNames from 'classnames'; import type { HTMLAttributes } from 'react'; @@ -34,8 +34,10 @@ export function GitDropdown() { function SyncDropdownWithSyncDir({ syncDir }: { syncDir: string }) { const workspace = useActiveWorkspace(); - const [{ status, log }, { branch, deleteBranch, fetchAll, mergeBranch, push, pull, checkout }] = - useGit(syncDir); + const [ + { status, log }, + { branch, deleteBranch, fetchAll, mergeBranch, push, pull, checkout, init }, + ] = useGit(syncDir); const localBranches = status.data?.localBranches ?? []; const remoteBranches = status.data?.remoteBranches ?? []; @@ -50,7 +52,9 @@ function SyncDropdownWithSyncDir({ syncDir }: { syncDir: string }) { const noRepo = status.error?.includes('not found'); if (noRepo) { - return gitInit(syncDir)} />; + return ( + init.mutate({ dir: syncDir })} /> + ); } const tryCheckout = (branch: string, force: boolean) => { diff --git a/src-web/lib/queryClient.ts b/src-web/lib/queryClient.ts new file mode 100644 index 00000000..5c4830c6 --- /dev/null +++ b/src-web/lib/queryClient.ts @@ -0,0 +1,18 @@ +import {QueryCache, QueryClient} from "@tanstack/react-query"; + +export const queryClient = new QueryClient({ + queryCache: new QueryCache({ + onError: (err, query) => { + console.log('Query client error', { err, query }); + }, + }), + defaultOptions: { + queries: { + retry: false, + networkMode: 'always', + refetchOnWindowFocus: true, + refetchOnReconnect: false, + refetchOnMount: false, // Don't refetch when a hook mounts + }, + }, +}); diff --git a/src-web/routes/__root.tsx b/src-web/routes/__root.tsx index d0b4150b..7bfc978f 100644 --- a/src-web/routes/__root.tsx +++ b/src-web/routes/__root.tsx @@ -1,8 +1,8 @@ -import { QueryCache, QueryClient, QueryClientProvider } from '@tanstack/react-query'; +import { QueryClientProvider } from '@tanstack/react-query'; import { createRootRoute, Outlet } from '@tanstack/react-router'; import classNames from 'classnames'; -import { domAnimation, LazyMotion, MotionConfig } from 'motion/react'; import { Provider as JotaiProvider } from 'jotai'; +import { domAnimation, LazyMotion, MotionConfig } from 'motion/react'; import React, { Suspense } from 'react'; import { DndProvider } from 'react-dnd'; import { HTML5Backend } from 'react-dnd-html5-backend'; @@ -13,23 +13,7 @@ import RouteError from '../components/RouteError'; import { Toasts } from '../components/Toasts'; import { useOsInfo } from '../hooks/useOsInfo'; import { jotaiStore } from '../lib/jotai'; - -const queryClient = new QueryClient({ - queryCache: new QueryCache({ - onError: (err, query) => { - console.log('Query client error', { err, query }); - }, - }), - defaultOptions: { - queries: { - retry: false, - networkMode: 'always', - refetchOnWindowFocus: true, - refetchOnReconnect: false, - refetchOnMount: false, // Don't refetch when a hook mounts - }, - }, -}); +import { queryClient } from '../lib/queryClient'; // eslint-disable-next-line @typescript-eslint/no-unused-vars const TanStackRouterDevtools =