Admin polish: stability fixes, schema security, consistent UI + sidebar #1

Merged
reudy merged 21 commits from session/polish into main 2026-10-07 16:30:40 +02:00
4 changed files with 181 additions and 113 deletions
Showing only changes of commit 52d7a5e3e2 - Show all commits

View file

@ -4,6 +4,17 @@
themes: light --default; themes: light --default;
} }
/* Admin UI theme: DaisyUI light with a fixed sage primary. Deliberately NOT
tied to the site's Globals → Colors (those only apply inside /preview), so
a light brand colour can't make admin buttons unreadable. #647556 on white
text is ~5:1 contrast (WCAG AA). */
@plugin 'daisyui/theme' {
name: 'light';
default: true;
--color-primary: #647556;
--color-primary-content: #ffffff;
}
@theme { @theme {
--font-sans: 'DM Sans', ui-sans-serif, system-ui, sans-serif; --font-sans: 'DM Sans', ui-sans-serif, system-ui, sans-serif;
--font-serif: 'Playfair Display', Georgia, serif; --font-serif: 'Playfair Display', Georgia, serif;

View file

@ -1,13 +1,29 @@
<script lang="ts"> <script lang="ts">
import '../app.css'; import '../app.css';
import favicon from '$lib/assets/favicon.svg'; import favicon from '$lib/assets/favicon.svg';
import { page } from '$app/state';
import { createBasketStore, setBasketContext } from '$lib/basket.svelte'; import { createBasketStore, setBasketContext } from '$lib/basket.svelte';
import { onMount } from 'svelte';
let { children, data } = $props(); let { children } = $props();
let importStatus = $state<{ ok: boolean; message: string } | null>(null); let importStatus = $state<{ ok: boolean; message: string; errors?: string[] } | null>(null);
let importing = $state(false); let importing = $state(false);
// Routes rendered without the admin shell: the login screen, and the
// preview iframe (which must look like the public site, not the admin).
const BARE_ROUTES = ['/login', '/preview'];
let bare = $derived(BARE_ROUTES.includes(page.url.pathname));
const navLinks = [
{ href: '/pages', label: 'Pages' },
{ href: '/calendar', label: 'Calendar' },
{ href: '/items', label: 'Items' },
{ href: '/slots', label: 'Slots' },
{ href: '/orders', label: 'Orders' },
{ href: '/emails', label: 'Emails' },
{ href: '/globals', label: 'Globals' }
];
const isActive = (href: string) => page.url.pathname === href || page.url.pathname.startsWith(`${href}/`);
// One basket store per layout instance — provided to all children // One basket store per layout instance — provided to all children
// (preview iframe block renderers). Never module-level state. // (preview iframe block renderers). Never module-level state.
// Admin doesn't have a real basket; pass null so the store starts empty. // Admin doesn't have a real basket; pass null so the store starts empty.
@ -34,12 +50,21 @@
const input = event.target as HTMLInputElement; const input = event.target as HTMLInputElement;
const file = input.files?.[0]; const file = input.files?.[0];
if (!file) return; if (!file) return;
if (!confirm(`Import "${file.name}"? Pages, globals and events with the same slug/key/name will be overwritten.`)) {
input.value = '';
return;
}
importing = true; importing = true;
importStatus = null; importStatus = null;
try { try {
const text = await file.text(); const text = await file.text();
const json = JSON.parse(text); let json: unknown;
try {
json = JSON.parse(text);
} catch {
throw new Error('The file is not valid JSON.');
}
const res = await fetch('/api/import', { const res = await fetch('/api/import', {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
@ -51,8 +76,13 @@
} }
const result = await res.json(); const result = await res.json();
const msg = `Imported ${result.pagesImported} page(s), ${result.globalsImported} global(s), ${result.eventsImported || 0} event(s), ${result.imagesUploaded} image(s).`; const msg = `Imported ${result.pagesImported} page(s), ${result.globalsImported} global(s), ${result.eventsImported || 0} event(s), ${result.imagesUploaded} image(s).`;
importStatus = { ok: true, message: result.errors?.length ? msg + ` ${result.errors.length} error(s).` : msg }; if (result.errors?.length) {
setTimeout(() => location.reload(), 1500); // Keep the message up so the errors can be read; no auto-reload.
importStatus = { ok: false, message: `${msg} ${result.errors.length} error(s) — reload to see the imported content.`, errors: result.errors };
} else {
importStatus = { ok: true, message: msg };
setTimeout(() => location.reload(), 1500);
}
} catch (e) { } catch (e) {
importStatus = { ok: false, message: `Import error: ${e instanceof Error ? e.message : String(e)}` }; importStatus = { ok: false, message: `Import error: ${e instanceof Error ? e.message : String(e)}` };
} finally { } finally {
@ -60,19 +90,6 @@
input.value = ''; input.value = '';
} }
} }
onMount(() => {
// Apply saved colors so the preview iframe matches the public site.
if (data.colors) {
const root = document.documentElement;
root.style.setProperty('--color-bg', data.colors.background);
root.style.setProperty('--color-heading', data.colors.heading);
root.style.setProperty('--color-subheading', data.colors.subheading);
root.style.setProperty('--color-text', data.colors.text);
root.style.setProperty('--color-primary', data.colors.primary);
root.style.setProperty('--color-accent', data.colors.accent);
}
});
</script> </script>
<svelte:head> <svelte:head>
@ -80,48 +97,55 @@
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
</svelte:head> </svelte:head>
<div class="min-h-screen bg-base-200"> {#if bare}
<div class="navbar bg-base-100 shadow-sm"> {@render children()}
<div class="flex-1"> {:else}
<a class="btn btn-ghost text-xl" href="/pages">Massage CMS</a> <div class="admin-shell min-h-screen bg-base-200">
</div> <header class="navbar sticky top-0 z-30 gap-2 bg-base-100 px-4 shadow-sm">
<div class="flex-none gap-2"> <div class="flex-1">
<a class="btn btn-ghost" href="/pages">Pages</a> <a class="text-lg font-bold tracking-tight" href="/pages">Massage CMS</a>
<a class="btn btn-ghost" href="/calendar">Calendar</a>
<a class="btn btn-ghost" href="/items">Items</a>
<a class="btn btn-ghost" href="/slots">Slots</a>
<a class="btn btn-ghost" href="/orders">Orders</a>
<a class="btn btn-ghost" href="/emails">Emails</a>
<a class="btn btn-ghost" href="/globals">Globals</a>
<div class="dropdown dropdown-end">
<button class="btn btn-ghost btn-sm" type="button" tabindex="0">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v2a2 2 0 002 2h12a2 2 0 002-2v-2M7 11l5 5 5-5M12 4v12" /></svg>
Export/Import
</button>
<ul class="menu dropdown-content z-20 mt-2 w-48 rounded-box bg-base-100 p-2 shadow">
<li><button type="button" onclick={handleExport}>Export all</button></li>
<li>
<button type="button" onclick={() => (document.getElementById('admin-import-file') as HTMLInputElement)?.click()} disabled={importing}>
{importing ? 'Importing…' : 'Import'}
</button>
</li>
</ul>
</div> </div>
<input id="admin-import-file" type="file" accept=".json" class="hidden" onchange={handleImport} /> <nav class="flex flex-none flex-wrap items-center gap-1">
<form class="inline" method="POST" action="/login?/logout"> {#each navLinks as link (link.href)}
<button class="btn btn-outline btn-sm" type="submit">Logout</button> <a class="btn btn-ghost btn-sm" class:btn-active={isActive(link.href)} aria-current={isActive(link.href) ? 'page' : undefined} href={link.href}>{link.label}</a>
</form> {/each}
</div> <div class="dropdown dropdown-end">
<button class="btn btn-ghost btn-sm" type="button" tabindex="0">
<svg xmlns="http://www.w3.org/2000/svg" class="size-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v2a2 2 0 002 2h12a2 2 0 002-2v-2M7 11l5 5 5-5M12 4v12" /></svg>
Export/Import
</button>
<ul class="menu dropdown-content z-40 mt-2 w-48 rounded-box bg-base-100 p-2 shadow">
<li><button type="button" onclick={handleExport}>Export all</button></li>
<li>
<button type="button" onclick={() => (document.getElementById('admin-import-file') as HTMLInputElement)?.click()} disabled={importing}>
{importing ? 'Importing…' : 'Import'}
</button>
</li>
</ul>
</div>
<input id="admin-import-file" type="file" accept=".json" class="hidden" onchange={handleImport} />
<form method="POST" action="/login?/logout">
<button class="btn btn-outline btn-sm" type="submit">Logout</button>
</form>
</nav>
</header>
{#if importStatus}
<div class="mx-auto max-w-7xl px-4 pt-4 md:px-8">
<div class="alert items-start" class:alert-success={importStatus.ok} class:alert-error={!importStatus.ok}>
<div class="flex-1">
<p>{importStatus.message}</p>
{#if importStatus.errors?.length}
<ul class="mt-2 list-disc pl-5 text-sm">
{#each importStatus.errors as err}<li>{err}</li>{/each}
</ul>
{/if}
</div>
<button class="btn btn-ghost btn-xs" type="button" aria-label="Dismiss" onclick={() => (importStatus = null)}>✕</button>
</div>
</div>
{/if}
<main class="mx-auto max-w-7xl p-4 md:p-8">
{@render children()}
</main>
</div> </div>
{#if importStatus} {/if}
<div class="mx-auto max-w-7xl px-4 pt-2">
<div class="alert" class:alert-success={importStatus.ok} class:alert-error={!importStatus.ok}>
{importStatus.message}
<button class="btn btn-ghost btn-xs" onclick={() => (importStatus = null)}>×</button>
</div>
</div>
{/if}
<main class="mx-auto max-w-7xl p-4 md:p-8">
{@render children()}
</main>
</div>

View file

@ -1,26 +1,38 @@
<script lang="ts"> <script lang="ts">
let { form } = $props(); let { form } = $props();
let submitting = $state(false);
</script> </script>
<svelte:head><title>Admin login</title></svelte:head> <svelte:head><title>Sign in | CMS</title></svelte:head>
<div class="grid min-h-screen place-items-center bg-base-200 p-6"> <div class="grid min-h-screen place-items-center bg-base-200 p-4">
<form class="card w-full max-w-md bg-base-100 shadow-xl" method="POST" action="?/login"> <div class="w-full max-w-sm">
<div class="card-body"> <p class="mb-6 text-center text-lg font-bold tracking-tight">Massage CMS</p>
<h1 class="card-title text-3xl">Sign in</h1> <form class="card bg-base-100 shadow-xl" method="POST" action="?/login" onsubmit={() => (submitting = true)}>
<p class="text-base-content/60">Use your PocketBase admin or CMS user credentials.</p> <div class="card-body gap-4">
{#if form?.message} <div>
<div class="alert alert-error mt-4">{form.message}</div> <h1 class="text-2xl font-bold">Sign in</h1>
{/if} <p class="text-sm text-base-content/60">Use your CMS account to manage the website.</p>
<label class="form-control mt-4"> </div>
<span class="label-text">Email</span> {#if form?.message}
<input class="input input-bordered" name="email" type="email" required /> <div class="alert alert-error text-sm" role="alert">{form.message}</div>
</label> {/if}
<label class="form-control"> <fieldset class="fieldset gap-3 p-0">
<span class="label-text">Password</span> <label class="flex flex-col gap-1">
<input class="input input-bordered" name="password" type="password" required /> <span class="fieldset-legend p-0">Email</span>
</label> <!-- svelte-ignore a11y_autofocus -->
<button class="btn btn-primary mt-4" type="submit">Login</button> <input class="input w-full" name="email" type="email" autocomplete="username" defaultValue={form?.email ?? ''} required autofocus />
</div> </label>
</form> <label class="flex flex-col gap-1">
<span class="fieldset-legend p-0">Password</span>
<input class="input w-full" name="password" type="password" autocomplete="current-password" required />
</label>
</fieldset>
<button class="btn btn-primary w-full" type="submit" disabled={submitting}>
{#if submitting}<span class="loading loading-spinner loading-sm"></span>{/if}
Sign in
</button>
</div>
</form>
</div>
</div> </div>

View file

@ -3,11 +3,32 @@
import BlockRenderer from '$shared/blocks/BlockRenderer.svelte'; import BlockRenderer from '$shared/blocks/BlockRenderer.svelte';
import type { ContentBlock } from '$lib/types'; import type { ContentBlock } from '$lib/types';
let { data } = $props();
let blocks = $state<ContentBlock[]>([]); let blocks = $state<ContentBlock[]>([]);
let error = $state('');
onMount(() => { onMount(() => {
// Apply the site's colors so the preview matches the public site. Only
// here: these variable names overlap DaisyUI's theme (--color-primary,
// --color-accent), so setting them in the admin shell restyled the admin.
if (data.colors) {
const root = document.documentElement;
root.style.setProperty('--color-bg', data.colors.background);
root.style.setProperty('--color-heading', data.colors.heading);
root.style.setProperty('--color-subheading', data.colors.subheading);
root.style.setProperty('--color-text', data.colors.text);
root.style.setProperty('--color-primary', data.colors.primary);
root.style.setProperty('--color-accent', data.colors.accent);
}
const raw = sessionStorage.getItem('cms-preview-blocks'); const raw = sessionStorage.getItem('cms-preview-blocks');
if (raw) blocks = JSON.parse(raw); if (!raw) return;
try {
const parsed = JSON.parse(raw);
if (Array.isArray(parsed)) blocks = parsed;
} catch {
error = 'The preview data could not be read. Close the preview and open it again.';
}
}); });
</script> </script>
@ -20,6 +41,6 @@
{:else} {:else}
<section class="mx-auto max-w-3xl px-6 py-20 text-center"> <section class="mx-auto max-w-3xl px-6 py-20 text-center">
<h1 class="text-4xl">No preview content</h1> <h1 class="text-4xl">No preview content</h1>
<p class="mt-4 text-charcoal/65">Open preview from the page editor to view the current draft.</p> <p class="mt-4 text-charcoal/65">{error || 'Open preview from the page editor to view the current draft.'}</p>
</section> </section>
{/if} {/if}