refactor(dashboard): extract settled<T>() helper; fix page.svelte.spec.ts types
Collapses 5x duplicated null-check pattern in the reader fetch branch into a single typed helper — addresses @Felix review blocker. Also adds isReader/incompleteDocs/incompleteTotal to page.svelte.spec.ts baseData so it satisfies the discriminated PageData union introduced by this PR. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -13,6 +13,12 @@ type PersonSummaryDTO = components['schemas']['PersonSummaryDTO'];
|
||||
type Document = components['schemas']['Document'];
|
||||
type Geschichte = components['schemas']['Geschichte'];
|
||||
|
||||
function settled<T>(res: PromiseSettledResult<unknown> | undefined): T | null {
|
||||
if (res?.status !== 'fulfilled') return null;
|
||||
const v = res.value as { response: Response; data: unknown };
|
||||
return v.response.ok ? ((v.data as T) ?? null) : null;
|
||||
}
|
||||
|
||||
export async function load({ fetch, parent }) {
|
||||
const { canWrite, canAnnotate, canBlogWrite } = await parent();
|
||||
const isReader = !canWrite && !canAnnotate;
|
||||
@@ -43,45 +49,12 @@ export async function load({ fetch, parent }) {
|
||||
const [statsRes, topPersonsRes, recentDocsRes, recentStoriesRes, draftsRes] =
|
||||
await Promise.allSettled(readerFetches);
|
||||
|
||||
let readerStats: StatsDTO | null = null;
|
||||
let topPersons: PersonSummaryDTO[] = [];
|
||||
let recentDocs: Document[] = [];
|
||||
let recentStories: Geschichte[] = [];
|
||||
let drafts: Geschichte[] = [];
|
||||
|
||||
if (
|
||||
statsRes?.status === 'fulfilled' &&
|
||||
(statsRes.value as { response: Response }).response.ok
|
||||
) {
|
||||
readerStats = ((statsRes.value as { data: unknown }).data as StatsDTO) ?? null;
|
||||
}
|
||||
if (
|
||||
topPersonsRes?.status === 'fulfilled' &&
|
||||
(topPersonsRes.value as { response: Response }).response.ok
|
||||
) {
|
||||
topPersons = ((topPersonsRes.value as { data: unknown }).data as PersonSummaryDTO[]) ?? [];
|
||||
}
|
||||
if (
|
||||
recentDocsRes?.status === 'fulfilled' &&
|
||||
(recentDocsRes.value as { response: Response }).response.ok
|
||||
) {
|
||||
const searchResult = (recentDocsRes.value as { data: unknown }).data as {
|
||||
items: { document: Document }[];
|
||||
} | null;
|
||||
recentDocs = searchResult?.items.map((i) => i.document) ?? [];
|
||||
}
|
||||
if (
|
||||
recentStoriesRes?.status === 'fulfilled' &&
|
||||
(recentStoriesRes.value as { response: Response }).response.ok
|
||||
) {
|
||||
recentStories = ((recentStoriesRes.value as { data: unknown }).data as Geschichte[]) ?? [];
|
||||
}
|
||||
if (
|
||||
draftsRes?.status === 'fulfilled' &&
|
||||
(draftsRes.value as { response: Response }).response.ok
|
||||
) {
|
||||
drafts = ((draftsRes.value as { data: unknown }).data as Geschichte[]) ?? [];
|
||||
}
|
||||
const readerStats = settled<StatsDTO>(statsRes);
|
||||
const topPersons = settled<PersonSummaryDTO[]>(topPersonsRes) ?? [];
|
||||
const searchData = settled<{ items: { document: Document }[] }>(recentDocsRes);
|
||||
const recentDocs = searchData?.items.map((i) => i.document) ?? [];
|
||||
const recentStories = settled<Geschichte[]>(recentStoriesRes) ?? [];
|
||||
const drafts = settled<Geschichte[]>(draftsRes) ?? [];
|
||||
|
||||
return {
|
||||
isReader: true as const,
|
||||
|
||||
@@ -20,6 +20,7 @@ const baseData = {
|
||||
enabled: true,
|
||||
createdAt: '2024-01-01T00:00:00Z'
|
||||
} as User,
|
||||
isReader: false as const,
|
||||
canWrite: true,
|
||||
canAnnotate: false,
|
||||
canBlogWrite: false,
|
||||
@@ -31,6 +32,8 @@ const baseData = {
|
||||
transcriptionDocs: [],
|
||||
readyDocs: [],
|
||||
weeklyStats: null,
|
||||
incompleteDocs: [],
|
||||
incompleteTotal: 0,
|
||||
error: null
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user