2026-08-31 22:06:29 +02:00
|
|
|
<script lang="ts">
|
|
|
|
|
import { goto } from '$app/navigation'
|
|
|
|
|
import { page } from '$app/state'
|
|
|
|
|
import { onMount } from 'svelte'
|
|
|
|
|
import AuthPage from '$lib/components/AuthPage.svelte'
|
|
|
|
|
import ProjectIcon from '$lib/components/ProjectIcon.svelte'
|
|
|
|
|
import AboutWorkspace from '$lib/features/AboutWorkspace.svelte'
|
2026-09-04 09:29:23 +02:00
|
|
|
import { AboutDialog, Header, NotificationStack, PageHeader, ProfileDialog } from '@gabriel20xx/web-ui'
|
|
|
|
|
import type { AuthLoginInput, AuthSetupInput, ProfileUpdateInput } from '@gabriel20xx/web-ui'
|
2026-08-31 22:06:29 +02:00
|
|
|
import SettingsDialog from '$lib/components/SettingsDialog.svelte'
|
|
|
|
|
import { getPanelForPath } from '$lib/app-config'
|
|
|
|
|
import { pageActions } from '$lib/stores/page-actions.svelte'
|
2026-09-04 09:29:23 +02:00
|
|
|
import { get, post, put } from '$lib/api'
|
2026-08-31 22:06:29 +02:00
|
|
|
import '$lib/language.svelte'
|
|
|
|
|
import type { AppConfig, AuthState, AuthUser } from '$lib/types'
|
|
|
|
|
import type { Snippet } from 'svelte'
|
|
|
|
|
|
|
|
|
|
let { app, children }: { app: AppConfig; children: Snippet } = $props()
|
|
|
|
|
|
|
|
|
|
let auth = $state<AuthState>({
|
|
|
|
|
user: null,
|
|
|
|
|
authRequired: true,
|
|
|
|
|
setupRequired: true,
|
|
|
|
|
loading: true,
|
|
|
|
|
error: null,
|
|
|
|
|
})
|
|
|
|
|
let settingsOpen = $state(false)
|
|
|
|
|
let aboutOpen = $state(false)
|
|
|
|
|
let profileOpen = $state(false)
|
|
|
|
|
|
|
|
|
|
// Header only renders its profile/settings menu when username is truthy - with login not
|
|
|
|
|
// required and nobody signed in, auth.user is legitimately null (there's no session to read
|
|
|
|
|
// it from), which silently hid Settings (and with it, the only UI path back to re-enabling
|
|
|
|
|
// login) behind a button that never appeared. Falls back to a placeholder identity in that
|
|
|
|
|
// one case so the menu - and Settings - stays reachable.
|
|
|
|
|
const displayName = $derived(auth.user?.username || auth.user?.email || (auth.authRequired ? '' : 'Admin'))
|
|
|
|
|
const homePath = $derived(app.nav[0]?.path || '/')
|
|
|
|
|
const navItems = $derived(
|
|
|
|
|
app.nav.map((item) => ({
|
|
|
|
|
href: item.path,
|
|
|
|
|
label: item.label,
|
|
|
|
|
icon: item.icon,
|
|
|
|
|
description: item.description,
|
|
|
|
|
})),
|
|
|
|
|
)
|
|
|
|
|
const activeNavItem = $derived(
|
|
|
|
|
app.nav.find((item) => item.panel === getPanelForPath(app, page.url.pathname)) || app.nav[0],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
function openSettings() {
|
|
|
|
|
settingsOpen = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function openAbout() {
|
|
|
|
|
aboutOpen = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function openProfile() {
|
|
|
|
|
profileOpen = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function closeSettings() {
|
|
|
|
|
settingsOpen = false
|
|
|
|
|
if (page.url.pathname === '/settings') void goto(homePath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function closeAbout() {
|
|
|
|
|
aboutOpen = false
|
|
|
|
|
if (page.url.pathname === '/about') void goto(homePath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function currentPageToken() {
|
|
|
|
|
if (typeof window === 'undefined') return '/'
|
|
|
|
|
return `${window.location.pathname}${window.location.search}` || '/'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function loadAuth() {
|
|
|
|
|
try {
|
|
|
|
|
const data = await get<{ user: AuthUser | null; authRequired?: boolean }>(
|
|
|
|
|
`/auth/me?page=${encodeURIComponent(currentPageToken())}`,
|
|
|
|
|
)
|
|
|
|
|
let setupRequired = false
|
|
|
|
|
try {
|
|
|
|
|
const bootstrap = await get<{ adminNeeded?: boolean }>('/auth/bootstrap/admin-needed')
|
|
|
|
|
setupRequired = bootstrap.adminNeeded === true
|
|
|
|
|
} catch {
|
|
|
|
|
// The auth status remains usable if the optional bootstrap check is unavailable.
|
|
|
|
|
}
|
|
|
|
|
auth = {
|
|
|
|
|
user: data.user || null,
|
|
|
|
|
authRequired: data.authRequired !== false,
|
|
|
|
|
setupRequired,
|
|
|
|
|
loading: false,
|
|
|
|
|
error: null,
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
auth = {
|
|
|
|
|
...auth,
|
|
|
|
|
loading: false,
|
|
|
|
|
error: error instanceof Error ? error.message : 'Unable to load authentication state.',
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function login(input: AuthLoginInput) {
|
|
|
|
|
const data = await post<{ user: AuthUser }>('/auth/login', {
|
|
|
|
|
identifier: input.identifier,
|
|
|
|
|
password: input.password,
|
|
|
|
|
totpToken: input.totpToken,
|
|
|
|
|
})
|
|
|
|
|
auth = { ...auth, user: data.user, setupRequired: false, loading: false, error: null }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function setup(input: AuthSetupInput) {
|
|
|
|
|
const data = await post<{ user: AuthUser }>('/auth/signup', {
|
|
|
|
|
email: input.email,
|
|
|
|
|
username: input.username,
|
|
|
|
|
password: input.password,
|
|
|
|
|
})
|
|
|
|
|
auth = { ...auth, user: data.user, setupRequired: false, loading: false, error: null }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function logout() {
|
|
|
|
|
try {
|
|
|
|
|
await post('/auth/logout')
|
|
|
|
|
auth.user = null
|
|
|
|
|
} catch (error) {
|
|
|
|
|
auth.error = error instanceof Error ? error.message : 'Logout failed.'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function updateProfile(input: ProfileUpdateInput) {
|
|
|
|
|
const data = await put<{ user: AuthUser }>('/auth/me', input)
|
|
|
|
|
auth = { ...auth, user: data.user, error: null }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
|
void loadAuth()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
$effect(() => {
|
|
|
|
|
if (page.url.pathname === '/settings') settingsOpen = true
|
|
|
|
|
if (page.url.pathname === '/about') aboutOpen = true
|
|
|
|
|
})
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
{#if auth.loading || auth.error || auth.setupRequired || (auth.authRequired && !auth.user)}
|
|
|
|
|
<AuthPage
|
|
|
|
|
{app}
|
|
|
|
|
setupRequired={auth.setupRequired}
|
|
|
|
|
loading={auth.loading}
|
|
|
|
|
error={auth.error || ''}
|
|
|
|
|
onLogin={login}
|
|
|
|
|
onSetup={setup}
|
|
|
|
|
/>
|
|
|
|
|
{:else}
|
2026-09-04 15:02:16 +02:00
|
|
|
<div class="sc-root sc-app-shell min-h-screen bg-bg text-fg theme-shell">
|
2026-08-31 22:06:29 +02:00
|
|
|
<NotificationStack />
|
|
|
|
|
|
|
|
|
|
<Header
|
|
|
|
|
brand={app.name}
|
|
|
|
|
brandHref={homePath}
|
|
|
|
|
currentPath={page.url.pathname}
|
|
|
|
|
faviconHref="/favicon.svg"
|
|
|
|
|
{navItems}
|
|
|
|
|
onAbout={openAbout}
|
|
|
|
|
onLogout={logout}
|
|
|
|
|
onProfile={openProfile}
|
|
|
|
|
onSettings={openSettings}
|
|
|
|
|
username={displayName}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<main class="mx-auto w-full max-w-7xl px-4 pt-4 pb-8">
|
|
|
|
|
<PageHeader
|
|
|
|
|
title={activeNavItem?.label || app.name}
|
|
|
|
|
description={activeNavItem?.description || app.description}
|
|
|
|
|
actions={pageActions.snippet ?? undefined}
|
|
|
|
|
/>
|
|
|
|
|
{@render children?.()}
|
|
|
|
|
</main>
|
|
|
|
|
</div>
|
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
|
|
<SettingsDialog bind:open={settingsOpen} onClose={closeSettings} />
|
|
|
|
|
<AboutDialog bind:open={aboutOpen} brand={app.name} description={app.description} onClose={closeAbout}>
|
|
|
|
|
{#snippet icon()}
|
|
|
|
|
<ProjectIcon icon={app.icon} class="size-8 text-primary" />
|
|
|
|
|
{/snippet}
|
|
|
|
|
<AboutWorkspace />
|
|
|
|
|
</AboutDialog>
|
|
|
|
|
<ProfileDialog
|
|
|
|
|
bind:open={profileOpen}
|
|
|
|
|
currentEmail={auth.user?.email || ''}
|
|
|
|
|
currentUsername={auth.user?.username || ''}
|
|
|
|
|
onSave={updateProfile}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
/* The shared component library (header, page title box, dialogs, buttons, ...) otherwise
|
|
|
|
|
renders in its own generic default palette instead of this app's own branding - remap its
|
|
|
|
|
--sc-* tokens onto this app's own --color-* palette so every shared component follows suit.
|
|
|
|
|
--sc-secondary is a subtle tinted-surface color for low-emphasis buttons, a different role
|
|
|
|
|
than this app's own saturated --color-secondary, so it's derived instead of mapped directly. */
|
2026-09-04 15:02:16 +02:00
|
|
|
:global(.theme-shell) {
|
2026-08-31 22:06:29 +02:00
|
|
|
--sc-bg: var(--color-bg);
|
|
|
|
|
--sc-surface: var(--color-surface);
|
|
|
|
|
--sc-surface-raised: var(--color-surface);
|
|
|
|
|
--sc-surface-hover: var(--color-surface-hover);
|
|
|
|
|
--sc-border: var(--color-border);
|
|
|
|
|
--sc-border-strong: var(--color-border);
|
|
|
|
|
--sc-fg: var(--color-fg);
|
|
|
|
|
--sc-muted-fg: var(--color-muted-fg);
|
|
|
|
|
--sc-subtle-fg: var(--color-muted-fg);
|
|
|
|
|
--sc-primary: var(--color-primary);
|
|
|
|
|
--sc-primary-hover: var(--color-secondary);
|
|
|
|
|
--sc-primary-fg: var(--color-primary-fg);
|
|
|
|
|
--sc-secondary: color-mix(in srgb, var(--color-primary) 12%, var(--color-surface));
|
|
|
|
|
--sc-secondary-hover: color-mix(in srgb, var(--color-primary) 20%, var(--color-surface));
|
|
|
|
|
--sc-secondary-fg: var(--color-fg);
|
|
|
|
|
--sc-accent: var(--color-accent);
|
|
|
|
|
--sc-accent-fg: var(--color-accent-fg);
|
|
|
|
|
--sc-danger: var(--color-danger);
|
|
|
|
|
--sc-danger-hover: color-mix(in srgb, var(--color-danger) 90%, black);
|
|
|
|
|
--sc-danger-fg: var(--color-danger-fg);
|
|
|
|
|
--sc-success: var(--color-success);
|
|
|
|
|
--sc-success-fg: var(--color-success-fg);
|
|
|
|
|
--sc-warning: var(--color-warning);
|
|
|
|
|
--sc-warning-fg: var(--color-warning-fg);
|
|
|
|
|
--sc-ring: var(--color-ring);
|
|
|
|
|
}
|
|
|
|
|
</style>
|