refactor(document): extract DocumentTopBarTitle from DocumentTopBar
First step of the Phase 5 split plan from issue #496. The 14-line title + date block becomes its own component named after the visual region. TDD red/green: DocumentTopBarTitle.svelte.test.ts written first (7 tests covering title, originalFilename fallback, empty-string fallback, short-date rendering, no-date branch, title attribute sourcing). After the test was red the component was created. DocumentTopBar.svelte updated to use it; the existing 18-test suite still passes. Refs #496. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { m } from '$lib/paraglide/messages.js';
|
import { m } from '$lib/paraglide/messages.js';
|
||||||
import { slide } from 'svelte/transition';
|
import { slide } from 'svelte/transition';
|
||||||
import { formatDate } from '$lib/shared/utils/date';
|
|
||||||
import { clickOutside } from '$lib/shared/actions/clickOutside';
|
import { clickOutside } from '$lib/shared/actions/clickOutside';
|
||||||
import PersonChipRow from '$lib/person/PersonChipRow.svelte';
|
import PersonChipRow from '$lib/person/PersonChipRow.svelte';
|
||||||
import OverflowPillButton from '$lib/shared/primitives/OverflowPillButton.svelte';
|
import OverflowPillButton from '$lib/shared/primitives/OverflowPillButton.svelte';
|
||||||
import DocumentMetadataDrawer from './DocumentMetadataDrawer.svelte';
|
import DocumentMetadataDrawer from './DocumentMetadataDrawer.svelte';
|
||||||
|
import DocumentTopBarTitle from './DocumentTopBarTitle.svelte';
|
||||||
import BackButton from '$lib/shared/primitives/BackButton.svelte';
|
import BackButton from '$lib/shared/primitives/BackButton.svelte';
|
||||||
|
|
||||||
type Person = { id: string; firstName?: string | null; lastName: string; displayName: string };
|
type Person = { id: string; firstName?: string | null; lastName: string; displayName: string };
|
||||||
@@ -59,9 +59,6 @@ const receivers = $derived(doc.receivers ?? []);
|
|||||||
const extraCount = $derived(Math.max(0, receivers.length - 2));
|
const extraCount = $derived(Math.max(0, receivers.length - 2));
|
||||||
const overflowPersons = $derived(receivers.slice(2));
|
const overflowPersons = $derived(receivers.slice(2));
|
||||||
|
|
||||||
const shortDate = $derived(doc.documentDate ? formatDate(doc.documentDate, 'short') : null);
|
|
||||||
const longDate = $derived(doc.documentDate ? formatDate(doc.documentDate, 'long') : null);
|
|
||||||
|
|
||||||
let mobileMenuOpen = $state(false);
|
let mobileMenuOpen = $state(false);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -161,20 +158,11 @@ let mobileMenuOpen = $state(false);
|
|||||||
<div class="mx-2 h-6 w-px shrink-0 bg-line"></div>
|
<div class="mx-2 h-6 w-px shrink-0 bg-line"></div>
|
||||||
|
|
||||||
<!-- Title + meta -->
|
<!-- Title + meta -->
|
||||||
<div class="min-w-0 flex-1 overflow-hidden">
|
<DocumentTopBarTitle
|
||||||
<h1
|
title={doc.title}
|
||||||
class="truncate font-serif text-[18px] leading-tight text-ink lg:text-[20px]"
|
originalFilename={doc.originalFilename}
|
||||||
title={doc.title ?? doc.originalFilename ?? ''}
|
documentDate={doc.documentDate}
|
||||||
>
|
/>
|
||||||
{doc.title || doc.originalFilename}
|
|
||||||
</h1>
|
|
||||||
{#if shortDate}
|
|
||||||
<p class="font-sans text-[16px] text-ink-2">
|
|
||||||
<span class="lg:hidden">{shortDate}</span>
|
|
||||||
<span class="hidden lg:inline">{longDate}</span>
|
|
||||||
</p>
|
|
||||||
{/if}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Chip row — desktop only, hidden on small screens to make room for buttons -->
|
<!-- Chip row — desktop only, hidden on small screens to make room for buttons -->
|
||||||
<div class="mx-3 hidden min-w-0 shrink-0 md:block">
|
<div class="mx-3 hidden min-w-0 shrink-0 md:block">
|
||||||
|
|||||||
30
frontend/src/lib/document/DocumentTopBarTitle.svelte
Normal file
30
frontend/src/lib/document/DocumentTopBarTitle.svelte
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { formatDate } from '$lib/shared/utils/date';
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
title?: string | null;
|
||||||
|
originalFilename?: string | null;
|
||||||
|
documentDate?: string | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
let { title, originalFilename, documentDate }: Props = $props();
|
||||||
|
|
||||||
|
const displayTitle = $derived(title || originalFilename || '');
|
||||||
|
const shortDate = $derived(documentDate ? formatDate(documentDate, 'short') : null);
|
||||||
|
const longDate = $derived(documentDate ? formatDate(documentDate, 'long') : null);
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="min-w-0 flex-1 overflow-hidden">
|
||||||
|
<h1
|
||||||
|
class="truncate font-serif text-[18px] leading-tight text-ink lg:text-[20px]"
|
||||||
|
title={displayTitle}
|
||||||
|
>
|
||||||
|
{displayTitle}
|
||||||
|
</h1>
|
||||||
|
{#if shortDate}
|
||||||
|
<p class="font-sans text-[16px] text-ink-2">
|
||||||
|
<span class="lg:hidden">{shortDate}</span>
|
||||||
|
<span class="hidden lg:inline">{longDate}</span>
|
||||||
|
</p>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
64
frontend/src/lib/document/DocumentTopBarTitle.svelte.test.ts
Normal file
64
frontend/src/lib/document/DocumentTopBarTitle.svelte.test.ts
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
import { describe, it, expect, afterEach } from 'vitest';
|
||||||
|
import { cleanup, render } from 'vitest-browser-svelte';
|
||||||
|
import { page } from 'vitest/browser';
|
||||||
|
import DocumentTopBarTitle from './DocumentTopBarTitle.svelte';
|
||||||
|
|
||||||
|
afterEach(cleanup);
|
||||||
|
|
||||||
|
const baseProps = {
|
||||||
|
title: 'Brief an Helene' as string | null,
|
||||||
|
originalFilename: 'brief.pdf' as string | null,
|
||||||
|
documentDate: '1923-04-15' as string | null
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('DocumentTopBarTitle', () => {
|
||||||
|
it('renders the title as a level-1 heading', async () => {
|
||||||
|
render(DocumentTopBarTitle, { props: baseProps });
|
||||||
|
|
||||||
|
await expect
|
||||||
|
.element(page.getByRole('heading', { level: 1, name: 'Brief an Helene' }))
|
||||||
|
.toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('falls back to originalFilename when title is null', async () => {
|
||||||
|
render(DocumentTopBarTitle, { props: { ...baseProps, title: null } });
|
||||||
|
|
||||||
|
await expect.element(page.getByRole('heading', { name: 'brief.pdf' })).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('falls back to originalFilename when title is an empty string', async () => {
|
||||||
|
render(DocumentTopBarTitle, { props: { ...baseProps, title: '' } });
|
||||||
|
|
||||||
|
await expect.element(page.getByRole('heading', { name: 'brief.pdf' })).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('renders the short date format when a documentDate is supplied', async () => {
|
||||||
|
render(DocumentTopBarTitle, { props: baseProps });
|
||||||
|
|
||||||
|
await expect.element(page.getByText('15.04.1923')).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('omits the date paragraph entirely when documentDate is null', async () => {
|
||||||
|
render(DocumentTopBarTitle, { props: { ...baseProps, documentDate: null } });
|
||||||
|
|
||||||
|
expect(document.querySelector('p')).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('uses the title (not the originalFilename) for the title attribute when title is set', async () => {
|
||||||
|
render(DocumentTopBarTitle, { props: baseProps });
|
||||||
|
|
||||||
|
const heading = (await page
|
||||||
|
.getByRole('heading', { name: 'Brief an Helene' })
|
||||||
|
.element()) as HTMLElement;
|
||||||
|
expect(heading.getAttribute('title')).toBe('Brief an Helene');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('uses the originalFilename for the title attribute when title is null', async () => {
|
||||||
|
render(DocumentTopBarTitle, { props: { ...baseProps, title: null } });
|
||||||
|
|
||||||
|
const heading = (await page
|
||||||
|
.getByRole('heading', { name: 'brief.pdf' })
|
||||||
|
.element()) as HTMLElement;
|
||||||
|
expect(heading.getAttribute('title')).toBe('brief.pdf');
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user