Copy a garden (deep duplicate) from the gardens list (#46)
Build image / build-and-push (push) Successful in 16s
Build image / build-and-push (push) Successful in 16s
Co-authored-by: Steve Dudenhoeffer <[email protected]>
This commit was merged in pull request #46.
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { defaultCopyName } from './gardens'
|
||||
|
||||
// The server caps a garden name at 200 BYTES and derives a copy's name the same
|
||||
// way (copyName in internal/service/gardens.go). These cases pin the mirror: an
|
||||
// over-long prefill would come back as a 400 rather than a copy.
|
||||
describe('defaultCopyName', () => {
|
||||
it('appends the suffix to a short name', () => {
|
||||
expect(defaultCopyName('Home')).toBe('Home (copy)')
|
||||
})
|
||||
|
||||
it('keeps a derived name within the 200-byte cap', () => {
|
||||
const name = 'a'.repeat(200)
|
||||
const derived = defaultCopyName(name)
|
||||
expect(new TextEncoder().encode(derived).length).toBeLessThanOrEqual(200)
|
||||
expect(derived.endsWith(' (copy)')).toBe(true)
|
||||
})
|
||||
|
||||
it('truncates multi-byte names on a code-point boundary', () => {
|
||||
const name = 'é'.repeat(100) // 200 bytes, exactly at the cap
|
||||
const derived = defaultCopyName(name)
|
||||
expect(new TextEncoder().encode(derived).length).toBeLessThanOrEqual(200)
|
||||
// No replacement character: nothing was cut mid-code-point.
|
||||
expect(derived).not.toContain('�')
|
||||
})
|
||||
|
||||
it('does not leave a dangling space before the suffix', () => {
|
||||
const name = 'x'.repeat(192) + ' y' // truncation lands on the space
|
||||
expect(defaultCopyName(name)).toBe('x'.repeat(192) + ' (copy)')
|
||||
})
|
||||
})
|
||||
@@ -86,6 +86,50 @@ export function useUpdateGarden() {
|
||||
})
|
||||
}
|
||||
|
||||
// Mirrors the server's garden-name cap (maxGardenNameLen in
|
||||
// internal/service/gardens.go). It is a BYTE cap, not a character count.
|
||||
const MAX_GARDEN_NAME_BYTES = 200
|
||||
const COPY_SUFFIX = ' (copy)'
|
||||
|
||||
/** Trim s to at most maxBytes of UTF-8, never splitting a code point. */
|
||||
function truncateUtf8(s: string, maxBytes: number): string {
|
||||
const enc = new TextEncoder()
|
||||
if (enc.encode(s).length <= maxBytes) return s
|
||||
let out = ''
|
||||
let used = 0
|
||||
for (const ch of s) {
|
||||
const size = enc.encode(ch).length
|
||||
if (used + size > maxBytes) break
|
||||
out += ch
|
||||
used += size
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/**
|
||||
* The name a copy gets by default. Mirrors the server's copyName (same file as
|
||||
* the cap above) so the prefilled value is one the server will accept — without
|
||||
* the byte-capped truncation, copying a garden whose name is already at the cap
|
||||
* would prefill an over-long name and fail with a 400.
|
||||
*/
|
||||
export function defaultCopyName(name: string): string {
|
||||
return truncateUtf8(name, MAX_GARDEN_NAME_BYTES - COPY_SUFFIX.length).trim() + COPY_SUFFIX
|
||||
}
|
||||
|
||||
/**
|
||||
* Duplicate a garden the actor owns, including its objects and current plantings.
|
||||
* An omitted/blank name lets the server derive "<source> (copy)". The copy does
|
||||
* not inherit the source's public link or shares.
|
||||
*/
|
||||
export function useCopyGarden() {
|
||||
const qc = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: async ({ id, name }: { id: number; name?: string }): Promise<Garden> =>
|
||||
gardenSchema.parse(await api.post(`/gardens/${id}/copy`, { name: name ?? '' })),
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: gardensKey }),
|
||||
})
|
||||
}
|
||||
|
||||
export function useDeleteGarden() {
|
||||
const qc = useQueryClient()
|
||||
return useMutation({
|
||||
|
||||
Reference in New Issue
Block a user