repo details page mostly working
This commit is contained in:
@@ -12,7 +12,7 @@ export async function bootstrapCSRF(): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
async function getCSRFToken(): Promise<string> {
|
||||
export async function getCSRFToken(): Promise<string> {
|
||||
if (csrfToken) return csrfToken
|
||||
await bootstrapCSRF()
|
||||
return csrfToken ?? ''
|
||||
|
||||
@@ -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] })
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user