# Database Detail Pages Refactor Plan (SvelteKit-centric) This plan refactors the database detail pages to be modular, reusable, and easy to port across characters, weapons, and summons. It leans into SvelteKit features (server `load`, form `actions`, and `use:enhance`) and Svelte composition (slots, small components, and lightweight state helpers). ## Goals - Consistent scaffold for detail pages (header, edit toolbar, sections, feedback). - Encapsulated edit-mode state + mapping to/from API payloads. - Clean separation between server concerns (loading, saving, validation) and client UI. - Reusable section components that can be shared or adapted per resource. - Smooth path to port the architecture to weapons and summons. ## Architecture Overview - Client - `DetailScaffold.svelte`: Common header, edit/save/cancel controls, messages, and section slots. - `createEditForm.ts`: Small factory returning state + helpers for edit lifecycle. - Initializes `editData` from the resource model via a schema mapping. - Exposes `editMode`, `toggleEdit`, `reset`, `set`, `get`, and `submit` glue for forms. - `image.ts`: `getResourceImage(type, granblue_id)` centralizing image paths and fallbacks. - Section components per resource (e.g., `CharacterMetadataSection.svelte`, shared `StatsSection` when possible). - Server - Shared loader helpers (e.g., `lib/server/detail/load.ts`) to fetch a resource detail and normalize to a client-facing model. - Form actions in `+page.server.ts` for save (`actions.save`) with validation (optionally Zod) and proper error handling. - Progressive enhancement via `use:enhance` so the UI stays responsive without losing SSR form semantics. - Schema-driven mapping - Per resource (`characters/schema.ts`, `weapons/schema.ts`, `summons/schema.ts`) define: - `toEditData(model)`: API model -> UI edit state. - `toPayload(editData)`: UI edit state -> API payload. - Optional field metadata (labels, formatter hooks) for low-ceremony sections. ## File Structure (proposed) - `src/lib/features/database/detail/` - `DetailScaffold.svelte` (header + actions + slots) - `createEditForm.ts` (state helper) - `image.ts` (resource image helper) - `api.ts` (client-side action helpers if needed) - `src/lib/features/database/characters/` - `schema.ts` - `sections/` - `CharacterMetadataSection.svelte` - `CharacterUncapSection.svelte` - `CharacterTaxonomySection.svelte` - `CharacterStatsSection.svelte` - `HPStatsSubsection.svelte` - `ATKStatsSubsection.svelte` - Similar folders for `weapons` and `summons` as we port. ## Form Actions and Loaders - Loader strategy - Current route `+page.ts`/`+page.server.ts` fetches the detailed entity and returns a normalized `model`. - A shared helper `getResourceDetail(resource, id)` can live under `lib/server/detail/load.ts` to reduce duplication across resources. - Save strategy - Define `export const actions = { save: async (event) => { ... } }` in `+page.server.ts`. - Validate incoming form data (Zod/schema), map to API payload via the schema’s `toPayload`, then persist via your backend API. - Return `fail(status, { fieldErrors, message })` on validation errors. - On success, return the updated item; `load` can pick it up or the client can reconcile locally. - Client strategy - Wrap the editable fields in a `