返回 CodeWhale
product-route.tsx
根目录 / web / components / route / product-route.tsx
1 import Link from "next/link";
2 import { useId, type CSSProperties, type ReactNode } from "react";
3 import type { ProductMapId, ProductMapState } from "@/lib/content/product-map";
4 import { InlineCode } from "./inline-code";
5 import { RouteWhale } from "./route-whale";
6
7 export interface RoutePart {
8 id: ProductMapId;
9 state: ProductMapState;
10 name: string;
11 stateWord: string;
12 sentence: string | null;
13 href: string | null;
14 }
15
16 type MarkState = ProductMapState | "engine";
17
18 interface Geometry {
19 stops: { x: number; state: MarkState }[];
20 today: number;
21 solidEnd: number;
22 }
23
24 const pct = (value: number) => `${(value * 100).toFixed(3)}%`;
25 const vars = (values: Record<string, string | number>) => values as CSSProperties;
26
27 function releasedCount(parts: readonly RoutePart[]): number {
28 const first = parts.findIndex((part) => part.state !== "released");
29 return first < 0 ? parts.length : first;
30 }
31
32 function geometryFrom(xs: readonly number[], parts: readonly RoutePart[], split: number): Geometry {
33 const today = releasedCount(parts);
34 const next = today + 1 < xs.length ? xs[today + 1] : 1;
35 const solidEnd = xs[today] + (next - xs[today]) * split;
36 return {
37 stops: xs.map((x, index) => ({ x, state: index === 0 ? "engine" : parts[index - 1].state })),
38 today: solidEnd,
39 solidEnd: today === parts.length ? 1 : solidEnd,
40 };
41 }
42
43 function mapGeometry(parts: readonly RoutePart[]): Geometry {
44 const count = parts.length + 1;
45 return geometryFrom(Array.from({ length: count }, (_, index) => index / count), parts, 0.5);
46 }
47
48 function StateMark({ state }: { state: MarkState }) {
49 return (
50 <svg className="road-mark" data-state={state} viewBox="-6 -6 12 12" aria-hidden="true" focusable="false">
51 <MarkShape state={state} />
52 </svg>
53 );
54 }
55
56 function MarkShape({ state }: { state: MarkState }) {
57 if (state === "engine" || state === "released") return <circle r="5" className="road-mark-fill" />;
58 if (state === "developing") {
59 return (
60 <>
61 <circle r="4.25" className="road-mark-ring" />
62 <path d="M0 -4.25a4.25 4.25 0 0 0 0 8.5z" className="road-mark-fill" />
63 </>
64 );
65 }
66 if (state === "inviteOnly") {
67 return (
68 <>
69 <circle r="4.25" className="road-mark-ring" />
70 <circle r="1.75" className="road-mark-fill" />
71 </>
72 );
73 }
74 return <circle r="4.25" className="road-mark-ring road-mark-dashed" />;
75 }
76
77 function Rail({ geometry }: { geometry: Geometry }) {
78 const id = useId().replace(/[^a-z0-9]/gi, "");
79 const mask = `road-mask-${id}`;
80 const fade = `road-fade-${id}`;
81 const { stops, solidEnd } = geometry;
82 const tail = stops[stops.length - 1];
83 const lines: ReactNode[] = [];
84 stops.forEach((stop, index) => {
85 const end = index + 1 < stops.length ? stops[index + 1].x : 1;
86 const into: MarkState = index + 1 < stops.length ? stops[index + 1].state : stop.state;
87 const line = (from: number, to: number, state: MarkState, key: string) => (
88 <line
89 key={key}
90 className="road-line"
91 data-line={state === "engine" ? "released" : state}
92 x1={pct(from)}
93 x2={pct(to)}
94 y1="50%"
95 y2="50%"
96 pathLength={state === "released" || state === "engine" ? 1 : undefined}
97 style={vars(to === 1 ? { "--i": index, stroke: `url(#${fade})` } : { "--i": index })}
98 />
99 );
100 if (end <= solidEnd) lines.push(line(stop.x, end, "released", `s${index}`));
101 else if (stop.x < solidEnd) {
102 lines.push(line(stop.x, solidEnd, "released", `s${index}`));
103 lines.push(line(solidEnd, end, into, `l${index}`));
104 } else lines.push(line(stop.x, end, into, `l${index}`));
105 });
106 return (
107 <div className="road-rail" aria-hidden="true" style={vars({ "--today": pct(geometry.today) })}>
108 <svg className="road-drawing" focusable="false">
109 <linearGradient id={fade} className="road-fade" data-state={tail.state} gradientUnits="userSpaceOnUse" x1={pct(Math.max(tail.x, 0.92))} x2="100%">
110 <stop className="road-fade-near" />
111 <stop className="road-fade-far" offset="1" />
112 </linearGradient>
113 <mask id={mask} maskUnits="userSpaceOnUse" x="0" y="0" width="100%" height="100%">
114 <rect width="100%" height="100%" fill="#fff" />
115 {stops.map((stop, index) =>
116 stop.state === "engine" || stop.state === "released" ? null : <circle key={index} cx={pct(stop.x)} cy="50%" r="7.5" fill="#000" />,
117 )}
118 </mask>
119 <g mask={`url(#${mask})`}>{lines}</g>
120 {stops.map((stop, index) => (
121 <svg key={index} className="road-station" data-state={stop.state} x={pct(stop.x)} y="50%" width="1" height="1" overflow="visible" style={vars({ "--i": index })}>
122 <g transform="scale(1.2)">
123 <MarkShape state={stop.state} />
124 </g>
125 </svg>
126 ))}
127 </svg>
128 <span className="road-today">
129 <RouteWhale />
130 </span>
131 </div>
132 );
133 }
134
135 function StateWord({ part }: { part: RoutePart }) {
136 return (
137 <span className="road-state" data-state={part.state}>
138 <StateMark state={part.state} />
139 {part.stateWord}
140 </span>
141 );
142 }
143
144 function PartName({ part, locale }: { part: RoutePart; locale: string }) {
145 return part.href ? (
146 <Link className="road-name" href={`/${locale}${part.href}`}>
147 {part.name}
148 </Link>
149 ) : (
150 <span className="road-name">{part.name}</span>
151 );
152 }
153
154 export function RouteMap({ parts, engine, label, locale }: { parts: RoutePart[]; engine: string; label: string; locale: string }) {
155 const treeRoot = useId();
156 return (
157 <>
158 <div className="road road-map">
159 <Rail geometry={mapGeometry(parts)} />
160 <ol className="road-stops" aria-label={label} style={{ gridTemplateColumns: `repeat(${parts.length + 1}, minmax(0, 1fr))` }}>
161 <li className="road-stop" data-state="engine">
162 <span className="road-name">{engine}</span>
163 </li>
164 {parts.map((part) => (
165 <li key={part.id} className="road-stop" data-state={part.state}>
166 <PartName part={part} locale={locale} />{" "}
167 <StateWord part={part} />{" "}
168 {part.sentence ? (
169 <span className="road-sentence">
170 <InlineCode text={part.sentence} />
171 </span>
172 ) : null}
173 </li>
174 ))}
175 </ol>
176 </div>
177 <div className="road-tree">
178 <p className="road-tree-root" id={treeRoot}>
179 {engine}
180 </p>
181 <ul aria-labelledby={treeRoot}>
182 {parts.map((part) => (
183 <li key={part.id} data-state={part.state}>
184 <span className="road-tree-line">
185 <PartName part={part} locale={locale} />
186 <span className="road-tree-leader" aria-hidden="true" />
187 <StateWord part={part} />
188 </span>
189 {part.sentence ? (
190 <span className="road-sentence">
191 <InlineCode text={part.sentence} />
192 </span>
193 ) : null}
194 </li>
195 ))}
196 </ul>
197 </div>
198 </>
199 );
200 }
201
201 lines Plain Text