|
1 | 1 | import React from "react"; |
2 | 2 |
|
3 | | -const formatDate = (date: Date) => { |
| 3 | +const formatDate = (date: Date): string | undefined => { |
4 | 4 | try { |
5 | 5 | return date.toISOString().split("T")[0]; |
6 | | - } catch {} |
| 6 | + } catch { |
| 7 | + return undefined; |
| 8 | + } |
7 | 9 | }; |
8 | 10 |
|
9 | | -const badgeColors = { |
10 | | - Proposed: "primary", |
11 | | - Accepted: "success", |
12 | | - Rejected: "danger", |
13 | | - Deprecated: "warning", |
14 | | - Superseded: "warning", |
| 11 | +const AdrStatus = { |
| 12 | + Proposed: "Proposed", |
| 13 | + Accepted: "Accepted", |
| 14 | + Rejected: "Rejected", |
| 15 | + Deprecated: "Deprecated", |
| 16 | + Superseded: "Superseded", |
| 17 | +} as const; |
| 18 | + |
| 19 | +type AdrStatus = (typeof AdrStatus)[keyof typeof AdrStatus]; |
| 20 | + |
| 21 | +const badgeColors: Record<AdrStatus, string> = { |
| 22 | + [AdrStatus.Proposed]: "primary", |
| 23 | + [AdrStatus.Accepted]: "success", |
| 24 | + [AdrStatus.Rejected]: "danger", |
| 25 | + [AdrStatus.Deprecated]: "warning", |
| 26 | + [AdrStatus.Superseded]: "warning", |
15 | 27 | }; |
16 | 28 |
|
17 | | -export default function AdrTable({ frontMatter }): JSX.Element { |
| 29 | +function isAdrStatus(value: unknown): value is AdrStatus { |
| 30 | + return typeof value === "string" && value in AdrStatus; |
| 31 | +} |
| 32 | + |
| 33 | +interface AdrFrontMatter { |
| 34 | + adr: string; |
| 35 | + status?: string; |
| 36 | + date: Date; |
| 37 | +} |
| 38 | + |
| 39 | +export default function AdrTable({ |
| 40 | + frontMatter, |
| 41 | +}: { |
| 42 | + frontMatter: AdrFrontMatter; |
| 43 | +}): React.JSX.Element { |
| 44 | + const { status } = frontMatter; |
| 45 | + const badgeColor = isAdrStatus(status) ? badgeColors[status] : "secondary"; |
| 46 | + |
18 | 47 | return ( |
19 | 48 | <table> |
20 | 49 | <tbody> |
21 | 50 | <tr> |
22 | 51 | <th>ID:</th> |
23 | 52 | <td>ADR-{frontMatter.adr}</td> |
24 | 53 | </tr> |
25 | | - {frontMatter.status && ( |
| 54 | + {status && ( |
26 | 55 | <tr> |
27 | 56 | <th>Status:</th> |
28 | 57 | <td> |
29 | | - <span className={`badge badge--${badgeColors[frontMatter.status] ?? "secondary"}`}> |
30 | | - {frontMatter.status?.toUpperCase()} |
31 | | - </span> |
| 58 | + <span className={`badge badge--${badgeColor}`}>{status.toUpperCase()}</span> |
32 | 59 | </td> |
33 | 60 | </tr> |
34 | 61 | )} |
|
0 commit comments