mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
feat: directory files upload system, deferred restart model, cache invalidation consolidation, and misc improvements (#281)
* feat: show variant label on model picker and add folder upload for agents/commands
- Replace three-dot variant menu trigger with selected variant label
- Add POST /api/settings/opencode-directory-files/install endpoint
- Upload agents/commands markdown folders into .config/opencode/{agents,commands}/
- Reload OpenCode server after upload so files are picked up
* fix: revalidate branch/git caches when mobile sheet opens and on vcs.branch.updated SSE
- add shared invalidateRepoGitCaches() helper for consistent invalidation
- revalidate repo list when mobile repo quick-switch sheet opens
- revalidate current repo when mobile More drawer opens
- revalidate repo/git caches when Source Control dialog opens
- handle vcs.branch.updated SSE event with optimistic cache update
- use helper in branch create/switch mutations in useGit and BranchesTab
* style: make variant selector look more like a button with chevron and compact sizing
* refactor: consolidate settings editors, add folder upload support, and clean up unused code
* refactor: consolidate settings editors, add folder upload support, and clean up unused code
* feat: add opencode-directory-files listing service and consolidate settings editors
* fix: distinguish scroll from tap in suggestion popups on touch devices
* refactor: consolidate settings editors and add DirectoryFilesList component
* refactor: target repo status cache updates only to affected batch entries
* refactor: streamline settings, git hooks, and query invalidation logic
This commit is contained in:
1 parent
e53b4fd32c
commit
617962f2dd
48 files changed
+1522
-293
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { renderHook, waitFor } from '@testing-library/react'
|
||||
import { describe, expect, it, vi, beforeEach } from 'vitest'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { useCommands } from './useCommands'
|
||||
import { createOpenCodeClient } from '../api/opencode'
|
||||
|
||||
@@ -7,13 +8,24 @@ vi.mock('../api/opencode', () => ({
|
||||
createOpenCodeClient: vi.fn(),
|
||||
}))
|
||||
|
||||
const createWrapper = () => {
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
},
|
||||
})
|
||||
return ({ children }: { children: React.ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
)
|
||||
}
|
||||
|
||||
describe('useCommands', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('returns commands in alphabetical order when there is no query', () => {
|
||||
const { result } = renderHook(() => useCommands(null))
|
||||
const { result } = renderHook(() => useCommands(null), { wrapper: createWrapper() })
|
||||
|
||||
expect(result.current.filterCommands('').map(command => command.name).slice(0, 5)).toEqual([
|
||||
'clear',
|
||||
@@ -25,7 +37,7 @@ describe('useCommands', () => {
|
||||
})
|
||||
|
||||
it('prioritizes exact and prefix matches before other matches', () => {
|
||||
const { result } = renderHook(() => useCommands(null))
|
||||
const { result } = renderHook(() => useCommands(null), { wrapper: createWrapper() })
|
||||
|
||||
expect(result.current.filterCommands('co').map(command => command.name)).toEqual([
|
||||
'compact',
|
||||
@@ -45,7 +57,7 @@ describe('useCommands', () => {
|
||||
]),
|
||||
} as unknown as ReturnType<typeof createOpenCodeClient>)
|
||||
|
||||
const { result } = renderHook(() => useCommands('http://localhost:5551'))
|
||||
const { result } = renderHook(() => useCommands('http://localhost:5551'), { wrapper: createWrapper() })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.filterCommands('').map(command => command.name).slice(0, 3)).toEqual([
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { createOpenCodeClient } from '@/api/opencode'
|
||||
import type { components } from '@/api/opencode-types'
|
||||
|
||||
@@ -163,36 +163,23 @@ const BUILTIN_COMMANDS: CommandType[] = [
|
||||
}
|
||||
]
|
||||
|
||||
const SORTED_BUILTIN_COMMANDS = sortCommandsByName(BUILTIN_COMMANDS)
|
||||
|
||||
export function useCommands(opcodeUrl: string | null) {
|
||||
const [commands, setCommands] = useState<CommandType[]>(sortCommandsByName(BUILTIN_COMMANDS))
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (!opcodeUrl) return
|
||||
|
||||
const fetchCommands = async () => {
|
||||
setLoading(true)
|
||||
setError(null)
|
||||
|
||||
try {
|
||||
const client = createOpenCodeClient(opcodeUrl)
|
||||
const commandList = await client.listCommands()
|
||||
const allCommands = [...BUILTIN_COMMANDS, ...commandList]
|
||||
const uniqueCommands = allCommands.filter((command, index, self) =>
|
||||
index === self.findIndex((c) => c.name === command.name)
|
||||
)
|
||||
setCommands(sortCommandsByName(uniqueCommands))
|
||||
} catch {
|
||||
setError('Failed to load commands')
|
||||
setCommands(sortCommandsByName(BUILTIN_COMMANDS))
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
fetchCommands()
|
||||
}, [opcodeUrl])
|
||||
const { data: commands, isLoading: loading, error } = useQuery({
|
||||
queryKey: ['opencode', 'commands', opcodeUrl],
|
||||
queryFn: async () => {
|
||||
const client = createOpenCodeClient(opcodeUrl!)
|
||||
const commandList = await client.listCommands()
|
||||
const allCommands = [...BUILTIN_COMMANDS, ...commandList]
|
||||
const uniqueCommands = allCommands.filter((command, index, self) =>
|
||||
index === self.findIndex((c) => c.name === command.name)
|
||||
)
|
||||
return sortCommandsByName(uniqueCommands)
|
||||
},
|
||||
enabled: !!opcodeUrl,
|
||||
initialData: SORTED_BUILTIN_COMMANDS,
|
||||
})
|
||||
|
||||
const filterCommands = (query: string) => {
|
||||
if (!query.trim()) return commands
|
||||
@@ -210,7 +197,7 @@ export function useCommands(opcodeUrl: string | null) {
|
||||
return {
|
||||
commands,
|
||||
loading,
|
||||
error,
|
||||
error: error ? 'Failed to load commands' : null,
|
||||
filterCommands
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import { useGit } from './useGit'
|
||||
import * as gitApi from '../api/git'
|
||||
import * as toast from '../lib/toast'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import type { GitStatusResponse } from '../types/git'
|
||||
|
||||
vi.mock('../api/git', () => ({
|
||||
gitFetch: vi.fn(),
|
||||
@@ -14,6 +15,7 @@ vi.mock('../api/git', () => ({
|
||||
gitUnstageFiles: vi.fn(),
|
||||
gitDiscardFiles: vi.fn(),
|
||||
gitReset: vi.fn(),
|
||||
fetchGitStatus: vi.fn(),
|
||||
fetchGitLog: vi.fn(),
|
||||
fetchGitDiff: vi.fn(),
|
||||
createBranch: vi.fn(),
|
||||
@@ -34,6 +36,15 @@ vi.mock('../lib/toast', () => ({
|
||||
|
||||
const mockInvalidateQueries = vi.fn()
|
||||
const mockSetQueryData = vi.fn()
|
||||
const mockSetQueriesData = vi.fn()
|
||||
|
||||
const mockGitStatus: GitStatusResponse = {
|
||||
branch: 'main',
|
||||
ahead: 0,
|
||||
behind: 0,
|
||||
files: [],
|
||||
hasChanges: false,
|
||||
}
|
||||
|
||||
vi.mock('@tanstack/react-query', async () => {
|
||||
const actual = await vi.importActual('@tanstack/react-query')
|
||||
@@ -42,6 +53,7 @@ vi.mock('@tanstack/react-query', async () => {
|
||||
useQueryClient: vi.fn(() => ({
|
||||
invalidateQueries: mockInvalidateQueries,
|
||||
setQueryData: mockSetQueryData,
|
||||
setQueriesData: mockSetQueriesData,
|
||||
}))
|
||||
}
|
||||
})
|
||||
@@ -62,8 +74,40 @@ describe('useGit', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mockInvalidateQueries.mockClear()
|
||||
vi.mocked(gitApi.gitFetch).mockResolvedValue(mockGitStatus)
|
||||
vi.mocked(gitApi.gitPull).mockResolvedValue(mockGitStatus)
|
||||
vi.mocked(gitApi.gitPush).mockResolvedValue(mockGitStatus)
|
||||
vi.mocked(gitApi.gitCommit).mockResolvedValue(mockGitStatus)
|
||||
vi.mocked(gitApi.gitStageFiles).mockResolvedValue(mockGitStatus)
|
||||
vi.mocked(gitApi.gitUnstageFiles).mockResolvedValue(mockGitStatus)
|
||||
vi.mocked(gitApi.gitDiscardFiles).mockResolvedValue(mockGitStatus)
|
||||
vi.mocked(gitApi.gitReset).mockResolvedValue(mockGitStatus)
|
||||
vi.mocked(gitApi.fetchGitStatus).mockResolvedValue(mockGitStatus)
|
||||
})
|
||||
|
||||
const expectTargetedStatusCacheUpdate = () => {
|
||||
expect(mockSetQueryData).toHaveBeenCalledWith(['gitStatus', 1], mockGitStatus)
|
||||
expect(mockSetQueriesData).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
queryKey: ['reposGitStatus'],
|
||||
predicate: expect.any(Function),
|
||||
}),
|
||||
expect.any(Function),
|
||||
)
|
||||
|
||||
const [filters, updater] = mockSetQueriesData.mock.calls[0]
|
||||
expect(filters.predicate({ queryKey: ['reposGitStatus', [1, 2]] })).toBe(true)
|
||||
expect(filters.predicate({ queryKey: ['reposGitStatus', [2, 3]] })).toBe(false)
|
||||
expect(filters.predicate({ queryKey: ['other', [1]] })).toBe(false)
|
||||
|
||||
const otherStatus = { ...mockGitStatus, branch: 'dev' }
|
||||
const oldData = new Map<number, GitStatusResponse>([[1, otherStatus], [2, otherStatus]])
|
||||
const updated = updater(oldData)
|
||||
expect(updated).not.toBe(oldData)
|
||||
expect(updated.get(1)).toBe(mockGitStatus)
|
||||
expect(updated.get(2)).toBe(otherStatus)
|
||||
}
|
||||
|
||||
it('returns all mutations', () => {
|
||||
const { result } = renderHook(() => useGit(1), { wrapper: createWrapper() })
|
||||
|
||||
@@ -86,7 +130,9 @@ describe('useGit', () => {
|
||||
})
|
||||
|
||||
expect(gitApi.gitFetch).toHaveBeenCalledWith(1)
|
||||
expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['gitStatus', 1] })
|
||||
expectTargetedStatusCacheUpdate()
|
||||
expect(mockInvalidateQueries).not.toHaveBeenCalledWith({ queryKey: ['reposGitStatus'] })
|
||||
expect(mockInvalidateQueries).not.toHaveBeenCalledWith({ queryKey: ['gitStatus', 1] })
|
||||
expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['fileDiff', 1] })
|
||||
expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['gitLog', 1] })
|
||||
})
|
||||
@@ -113,7 +159,9 @@ describe('useGit', () => {
|
||||
})
|
||||
|
||||
expect(gitApi.gitPull).toHaveBeenCalledWith(1)
|
||||
expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['gitStatus', 1] })
|
||||
expectTargetedStatusCacheUpdate()
|
||||
expect(mockInvalidateQueries).not.toHaveBeenCalledWith({ queryKey: ['reposGitStatus'] })
|
||||
expect(mockInvalidateQueries).not.toHaveBeenCalledWith({ queryKey: ['gitStatus', 1] })
|
||||
expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['fileDiff', 1] })
|
||||
expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['gitLog', 1] })
|
||||
})
|
||||
@@ -136,10 +184,12 @@ describe('useGit', () => {
|
||||
const { result } = renderHook(() => useGit(1), { wrapper: createWrapper() })
|
||||
|
||||
await waitFor(() => {
|
||||
result.current.push.mutateAsync()
|
||||
result.current.push.mutateAsync(undefined)
|
||||
})
|
||||
|
||||
expect(gitApi.gitPush).toHaveBeenCalledWith(1, false)
|
||||
expectTargetedStatusCacheUpdate()
|
||||
expect(mockInvalidateQueries).not.toHaveBeenCalledWith({ queryKey: ['reposGitStatus'] })
|
||||
expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['fileDiff', 1] })
|
||||
expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['gitLog', 1] })
|
||||
})
|
||||
@@ -150,7 +200,7 @@ describe('useGit', () => {
|
||||
const { result } = renderHook(() => useGit(1), { wrapper: createWrapper() })
|
||||
|
||||
await waitFor(() => {
|
||||
result.current.push.mutateAsync().catch(() => {})
|
||||
result.current.push.mutateAsync(undefined).catch(() => {})
|
||||
})
|
||||
|
||||
expect(toast.showToast.error).toHaveBeenCalledWith('Push failed')
|
||||
@@ -166,7 +216,9 @@ describe('useGit', () => {
|
||||
})
|
||||
|
||||
expect(gitApi.gitCommit).toHaveBeenCalledWith(1, 'test commit', undefined)
|
||||
expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['gitStatus', 1] })
|
||||
expectTargetedStatusCacheUpdate()
|
||||
expect(mockInvalidateQueries).not.toHaveBeenCalledWith({ queryKey: ['reposGitStatus'] })
|
||||
expect(mockInvalidateQueries).not.toHaveBeenCalledWith({ queryKey: ['gitStatus', 1] })
|
||||
expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['fileDiff', 1] })
|
||||
expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['gitLog', 1] })
|
||||
})
|
||||
@@ -193,7 +245,9 @@ describe('useGit', () => {
|
||||
})
|
||||
|
||||
expect(gitApi.gitStageFiles).toHaveBeenCalledWith(1, ['file.txt'])
|
||||
expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['gitStatus', 1] })
|
||||
expectTargetedStatusCacheUpdate()
|
||||
expect(mockInvalidateQueries).not.toHaveBeenCalledWith({ queryKey: ['reposGitStatus'] })
|
||||
expect(mockInvalidateQueries).not.toHaveBeenCalledWith({ queryKey: ['gitStatus', 1] })
|
||||
expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['fileDiff', 1] })
|
||||
expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['gitLog', 1] })
|
||||
})
|
||||
@@ -220,7 +274,9 @@ describe('useGit', () => {
|
||||
})
|
||||
|
||||
expect(gitApi.gitUnstageFiles).toHaveBeenCalledWith(1, ['file.txt'])
|
||||
expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['gitStatus', 1] })
|
||||
expectTargetedStatusCacheUpdate()
|
||||
expect(mockInvalidateQueries).not.toHaveBeenCalledWith({ queryKey: ['reposGitStatus'] })
|
||||
expect(mockInvalidateQueries).not.toHaveBeenCalledWith({ queryKey: ['gitStatus', 1] })
|
||||
expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['fileDiff', 1] })
|
||||
expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['gitLog', 1] })
|
||||
})
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { gitFetch, gitPull, gitPush, gitCommit, gitStageFiles, gitUnstageFiles, gitDiscardFiles, fetchGitLog, fetchGitDiff, gitReset, getApiErrorMessage } from '@/api/git'
|
||||
import { gitFetch, gitPull, gitPush, gitCommit, gitStageFiles, gitUnstageFiles, gitDiscardFiles, fetchGitLog, fetchGitDiff, gitReset, getApiErrorMessage, fetchGitStatus } from '@/api/git'
|
||||
import { createBranch, switchBranch } from '@/api/repos'
|
||||
import { showToast } from '@/lib/toast'
|
||||
import { invalidateRepoGitCaches, setRepoGitStatusCaches } from '@/lib/queryInvalidation'
|
||||
|
||||
export function useGit(repoId: number | undefined, onError?: (error: unknown) => void) {
|
||||
const queryClient = useQueryClient()
|
||||
@@ -14,19 +15,14 @@ export function useGit(repoId: number | undefined, onError?: (error: unknown) =>
|
||||
}
|
||||
}
|
||||
|
||||
const invalidateCache = (additionalKeys: string[] = []) => {
|
||||
if (!repoId) return
|
||||
const keys = ['gitStatus', 'fileDiff', 'gitLog', ...additionalKeys]
|
||||
keys.forEach(key => queryClient.invalidateQueries({ queryKey: [key, repoId] }))
|
||||
}
|
||||
|
||||
const fetch = useMutation({
|
||||
mutationFn: () => {
|
||||
if (!repoId) throw new Error('No repo ID')
|
||||
return gitFetch(repoId)
|
||||
},
|
||||
onSuccess: () => {
|
||||
invalidateCache()
|
||||
onSuccess: (data) => {
|
||||
if (repoId) setRepoGitStatusCaches(queryClient, repoId, data)
|
||||
invalidateRepoGitCaches(queryClient, repoId, { invalidateStatus: false, invalidateRepoMeta: false })
|
||||
showToast.success('Fetch completed')
|
||||
},
|
||||
onError: handleError,
|
||||
@@ -37,8 +33,9 @@ export function useGit(repoId: number | undefined, onError?: (error: unknown) =>
|
||||
if (!repoId) throw new Error('No repo ID')
|
||||
return gitPull(repoId)
|
||||
},
|
||||
onSuccess: () => {
|
||||
invalidateCache()
|
||||
onSuccess: (data) => {
|
||||
if (repoId) setRepoGitStatusCaches(queryClient, repoId, data)
|
||||
invalidateRepoGitCaches(queryClient, repoId, { invalidateStatus: false, invalidateRepoMeta: false })
|
||||
showToast.success('Pull completed')
|
||||
},
|
||||
onError: handleError,
|
||||
@@ -50,9 +47,8 @@ export function useGit(repoId: number | undefined, onError?: (error: unknown) =>
|
||||
return gitPush(repoId, options?.setUpstream ?? false)
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
queryClient.setQueryData(['gitStatus', repoId], data)
|
||||
const keysToInvalidate = ['fileDiff', 'gitLog', 'repo', 'branches']
|
||||
keysToInvalidate.forEach(key => queryClient.invalidateQueries({ queryKey: [key, repoId] }))
|
||||
if (repoId) setRepoGitStatusCaches(queryClient, repoId, data)
|
||||
invalidateRepoGitCaches(queryClient, repoId, { invalidateStatus: false, invalidateRepoMeta: false })
|
||||
showToast.success('Push completed')
|
||||
},
|
||||
onError: handleError,
|
||||
@@ -63,8 +59,9 @@ export function useGit(repoId: number | undefined, onError?: (error: unknown) =>
|
||||
if (!repoId) throw new Error('No repo ID')
|
||||
return gitCommit(repoId, message, stagedPaths)
|
||||
},
|
||||
onSuccess: () => {
|
||||
invalidateCache()
|
||||
onSuccess: (data) => {
|
||||
if (repoId) setRepoGitStatusCaches(queryClient, repoId, data)
|
||||
invalidateRepoGitCaches(queryClient, repoId, { invalidateStatus: false, invalidateRepoMeta: false })
|
||||
showToast.success('Commit created')
|
||||
},
|
||||
onError: handleError,
|
||||
@@ -75,8 +72,9 @@ export function useGit(repoId: number | undefined, onError?: (error: unknown) =>
|
||||
if (!repoId) throw new Error('No repo ID')
|
||||
return gitStageFiles(repoId, paths)
|
||||
},
|
||||
onSuccess: () => {
|
||||
invalidateCache()
|
||||
onSuccess: (data) => {
|
||||
if (repoId) setRepoGitStatusCaches(queryClient, repoId, data)
|
||||
invalidateRepoGitCaches(queryClient, repoId, { invalidateStatus: false, invalidateRepoMeta: false })
|
||||
showToast.success('Files staged')
|
||||
},
|
||||
onError: handleError,
|
||||
@@ -87,8 +85,9 @@ export function useGit(repoId: number | undefined, onError?: (error: unknown) =>
|
||||
if (!repoId) throw new Error('No repo ID')
|
||||
return gitUnstageFiles(repoId, paths)
|
||||
},
|
||||
onSuccess: () => {
|
||||
invalidateCache()
|
||||
onSuccess: (data) => {
|
||||
if (repoId) setRepoGitStatusCaches(queryClient, repoId, data)
|
||||
invalidateRepoGitCaches(queryClient, repoId, { invalidateStatus: false, invalidateRepoMeta: false })
|
||||
showToast.success('Files unstaged')
|
||||
},
|
||||
onError: handleError,
|
||||
@@ -99,8 +98,9 @@ export function useGit(repoId: number | undefined, onError?: (error: unknown) =>
|
||||
if (!repoId) throw new Error('No repo ID')
|
||||
return gitDiscardFiles(repoId, paths, staged)
|
||||
},
|
||||
onSuccess: () => {
|
||||
invalidateCache()
|
||||
onSuccess: (data) => {
|
||||
if (repoId) setRepoGitStatusCaches(queryClient, repoId, data)
|
||||
invalidateRepoGitCaches(queryClient, repoId, { invalidateStatus: false, invalidateRepoMeta: false })
|
||||
},
|
||||
onError: handleError,
|
||||
})
|
||||
@@ -122,24 +122,28 @@ export function useGit(repoId: number | undefined, onError?: (error: unknown) =>
|
||||
})
|
||||
|
||||
const createBranchMutation = useMutation({
|
||||
mutationFn: (branchName: string) => {
|
||||
mutationFn: async (branchName: string) => {
|
||||
if (!repoId) throw new Error('No repo ID')
|
||||
return createBranch(repoId, branchName)
|
||||
await createBranch(repoId, branchName)
|
||||
return fetchGitStatus(repoId)
|
||||
},
|
||||
onSuccess: () => {
|
||||
invalidateCache(['branches'])
|
||||
onSuccess: (data) => {
|
||||
if (repoId) setRepoGitStatusCaches(queryClient, repoId, data)
|
||||
invalidateRepoGitCaches(queryClient, repoId, { invalidateStatus: false })
|
||||
showToast.success('Branch created')
|
||||
},
|
||||
onError: handleError,
|
||||
})
|
||||
|
||||
const switchBranchMutation = useMutation({
|
||||
mutationFn: (branchName: string) => {
|
||||
mutationFn: async (branchName: string) => {
|
||||
if (!repoId) throw new Error('No repo ID')
|
||||
return switchBranch(repoId, branchName)
|
||||
await switchBranch(repoId, branchName)
|
||||
return fetchGitStatus(repoId)
|
||||
},
|
||||
onSuccess: () => {
|
||||
invalidateCache(['branches'])
|
||||
onSuccess: (data) => {
|
||||
if (repoId) setRepoGitStatusCaches(queryClient, repoId, data)
|
||||
invalidateRepoGitCaches(queryClient, repoId, { invalidateStatus: false })
|
||||
showToast.success('Switched to branch')
|
||||
},
|
||||
onError: handleError,
|
||||
@@ -150,8 +154,9 @@ export function useGit(repoId: number | undefined, onError?: (error: unknown) =>
|
||||
if (!repoId) throw new Error('No repo ID')
|
||||
return gitReset(repoId, commitHash)
|
||||
},
|
||||
onSuccess: () => {
|
||||
invalidateCache()
|
||||
onSuccess: (data) => {
|
||||
if (repoId) setRepoGitStatusCaches(queryClient, repoId, data)
|
||||
invalidateRepoGitCaches(queryClient, repoId, { invalidateStatus: false, invalidateRepoMeta: false })
|
||||
showToast.success('Reset to commit')
|
||||
},
|
||||
onError: handleError,
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import { useEffect, useRef } from 'react'
|
||||
|
||||
export function useRefreshOnOpen(isOpen: boolean, refresh: () => void) {
|
||||
const refreshRef = useRef(refresh)
|
||||
refreshRef.current = refresh
|
||||
|
||||
useEffect(() => {
|
||||
if (!isOpen) return
|
||||
refreshRef.current()
|
||||
}, [isOpen])
|
||||
}
|
||||
@@ -22,6 +22,7 @@ interface HealthResponse {
|
||||
opencodeMinVersion: string
|
||||
opencodeVersionSupported: boolean
|
||||
opencodeManagerVersion: string | null
|
||||
opencodeRestartPending?: boolean
|
||||
error?: string
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import { useCallback, useRef, type TouchEvent } from 'react'
|
||||
|
||||
const TOUCH_TAP_MOVE_THRESHOLD = 8
|
||||
const TOUCH_CLICK_SUPPRESS_MS = 400
|
||||
|
||||
export function useTouchTapSelect<T>(onSelect: (item: T) => void) {
|
||||
const touchStartRef = useRef<{ x: number, y: number } | null>(null)
|
||||
const hasTouchMovedRef = useRef(false)
|
||||
const suppressClickRef = useRef(false)
|
||||
|
||||
const suppressNextClick = useCallback(() => {
|
||||
suppressClickRef.current = true
|
||||
setTimeout(() => {
|
||||
suppressClickRef.current = false
|
||||
}, TOUCH_CLICK_SUPPRESS_MS)
|
||||
}, [])
|
||||
|
||||
const onTouchStart = useCallback((event: TouchEvent<HTMLElement>) => {
|
||||
const touch = event.touches[0]
|
||||
if (!touch) return
|
||||
|
||||
touchStartRef.current = { x: touch.clientX, y: touch.clientY }
|
||||
hasTouchMovedRef.current = false
|
||||
}, [])
|
||||
|
||||
const onTouchMove = useCallback((event: TouchEvent<HTMLElement>) => {
|
||||
const start = touchStartRef.current
|
||||
const touch = event.touches[0]
|
||||
if (!start || !touch) return
|
||||
|
||||
const deltaX = Math.abs(touch.clientX - start.x)
|
||||
const deltaY = Math.abs(touch.clientY - start.y)
|
||||
if (deltaX > TOUCH_TAP_MOVE_THRESHOLD || deltaY > TOUCH_TAP_MOVE_THRESHOLD) {
|
||||
hasTouchMovedRef.current = true
|
||||
}
|
||||
}, [])
|
||||
|
||||
const onTouchEnd = useCallback((event: TouchEvent<HTMLElement>, item: T) => {
|
||||
const hasTouchMoved = hasTouchMovedRef.current
|
||||
|
||||
touchStartRef.current = null
|
||||
hasTouchMovedRef.current = false
|
||||
|
||||
if (hasTouchMoved) {
|
||||
suppressNextClick()
|
||||
return
|
||||
}
|
||||
|
||||
event.preventDefault()
|
||||
suppressNextClick()
|
||||
onSelect(item)
|
||||
}, [onSelect, suppressNextClick])
|
||||
|
||||
const onClick = useCallback((item: T) => {
|
||||
if (suppressClickRef.current) {
|
||||
suppressClickRef.current = false
|
||||
return
|
||||
}
|
||||
|
||||
onSelect(item)
|
||||
}, [onSelect])
|
||||
|
||||
return {
|
||||
onTouchStart,
|
||||
onTouchMove,
|
||||
onTouchEnd,
|
||||
onClick,
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,12 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
|
||||
const isTextInputFocused = () => {
|
||||
const el = document.activeElement
|
||||
if (!el) return false
|
||||
const tag = el.tagName
|
||||
return tag === 'TEXTAREA' || tag === 'INPUT' || (el as HTMLElement).isContentEditable
|
||||
}
|
||||
|
||||
export function useVisualViewport() {
|
||||
const [keyboardHeight, setKeyboardHeight] = useState(0)
|
||||
|
||||
@@ -8,6 +15,10 @@ export function useVisualViewport() {
|
||||
if (!viewport) return
|
||||
|
||||
const update = () => {
|
||||
if (!isTextInputFocused()) {
|
||||
setKeyboardHeight(0)
|
||||
return
|
||||
}
|
||||
const layoutHeight = window.innerHeight
|
||||
const visualHeight = viewport.height + viewport.offsetTop
|
||||
const offset = Math.max(0, layoutHeight - visualHeight)
|
||||
@@ -16,11 +27,17 @@ export function useVisualViewport() {
|
||||
|
||||
viewport.addEventListener('resize', update)
|
||||
viewport.addEventListener('scroll', update)
|
||||
window.addEventListener('focusout', update)
|
||||
window.addEventListener('pageshow', update)
|
||||
document.addEventListener('visibilitychange', update)
|
||||
update()
|
||||
|
||||
return () => {
|
||||
viewport.removeEventListener('resize', update)
|
||||
viewport.removeEventListener('scroll', update)
|
||||
window.removeEventListener('focusout', update)
|
||||
window.removeEventListener('pageshow', update)
|
||||
document.removeEventListener('visibilitychange', update)
|
||||
}
|
||||
}, [])
|
||||
|
||||
|
||||
Reference in new issue
Block a user