repo details page mostly working

This commit is contained in:
2026-05-07 13:04:13 +02:00
parent 00aede9c91
commit 12bcf59bc9
12 changed files with 407 additions and 214 deletions
+29 -3
View File
@@ -1,6 +1,6 @@
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { z } from 'zod'
import { api } from '../client'
import { api, getCSRFToken } from '../client'
import type { Repository, TreeEntry } from '../../types/api'
const fileDiffSchema = z.object({
@@ -19,6 +19,7 @@ const repositorySchema = z.object({
ownerName: z.string(),
isEmpty: z.boolean(),
size: z.number().default(0),
avatarUrl: z.string().default(''),
name: z.string(),
description: z.string(),
isPrivate: z.boolean(),
@@ -91,7 +92,7 @@ export function useRepoDiff(owner: string, name: string, base: string, head: str
export function useUpdateRepo(owner: string, name: string) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (data: { description?: string; isPrivate?: boolean; defaultBranch?: string }) =>
mutationFn: (data: { name?: string; description?: string; isPrivate?: boolean; defaultBranch?: string }) =>
api.patch<Repository>(
`/api/v1/repos/${owner}/${name}`,
repositorySchema,
@@ -99,7 +100,32 @@ export function useUpdateRepo(owner: string, name: string) {
),
onSuccess: (updated) => {
queryClient.invalidateQueries({ queryKey: ['repos'] })
queryClient.setQueryData(['repos', owner, name], updated)
queryClient.setQueryData(['repos', updated.ownerName, updated.name], updated)
},
})
}
export function useUploadRepoAvatar(owner: string, name: string) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async (file: File) => {
const token = await getCSRFToken()
const formData = new FormData()
formData.append('avatar', file)
const res = await fetch(`/api/v1/repos/${owner}/${name}/avatar`, {
method: 'POST',
credentials: 'include',
headers: { 'X-CSRF-Token': token },
body: formData,
})
if (!res.ok) {
const body = await res.json().catch(() => ({ error: 'Upload failed' }))
throw new Error(body.error ?? 'Upload failed')
}
return res.json() as Promise<{ avatarUrl: string }>
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['repos', owner, name] })
},
})
}