artifacts.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. "use client";
  2. import { useEffect, useMemo, useState } from "react";
  3. import { Eyebrow, SampleNote, TextLink } from "./ui";
  4. import { projects } from "@/config/site";
  5. import { cn } from "@/lib/utils";
  6. const TABS = [
  7. {
  8. id: "chart",
  9. label: "Chart",
  10. prompt: "Chart my costs by region last month",
  11. },
  12. { id: "table", label: "Table", prompt: "List my running EC2 instances" },
  13. {
  14. id: "forecast",
  15. label: "Forecast",
  16. prompt: "Forecast my spend for 6 months",
  17. },
  18. ] as const;
  19. type TabId = (typeof TABS)[number]["id"];
  20. const REGIONS = [
  21. { name: "us-east-1", value: 4820 },
  22. { name: "us-west-2", value: 2310 },
  23. { name: "eu-west-1", value: 1540 },
  24. { name: "ap-ne-1", value: 960 },
  25. { name: "eu-c-1", value: 610 },
  26. ];
  27. const INSTANCES = [
  28. { id: "i-0d3f42", type: "g5.xlarge", cpu: 88 },
  29. { id: "i-04be19", type: "c7i.xlarge", cpu: 71 },
  30. { id: "i-09c6a0", type: "r7g.2xlarge", cpu: 54 },
  31. { id: "i-0a71c3", type: "m7g.large", cpu: 38 },
  32. { id: "i-0f2d88", type: "t4g.medium", cpu: 12 },
  33. ];
  34. const ACTUAL = [8.1, 8.6, 9.4, 9.1, 10.2, 10.9];
  35. const FORECAST = [11.3, 11.9, 12.2, 12.9, 13.4, 14.1];
  36. function RegionChart() {
  37. const [hover, setHover] = useState<number | null>(null);
  38. const max = REGIONS[0].value;
  39. return (
  40. <div className="flex h-full flex-col">
  41. <p className="mb-1.5 flex justify-between text-[11.5px] text-muted">
  42. <span className="font-medium text-ink">Cost by region</span>
  43. <span className="tabular-nums">
  44. {hover === null
  45. ? "$10,240"
  46. : `$${REGIONS[hover].value.toLocaleString()}`}
  47. </span>
  48. </p>
  49. <div
  50. className="flex min-h-0 flex-1 items-end gap-1.5"
  51. onPointerLeave={() => setHover(null)}
  52. >
  53. {REGIONS.map((r, i) => (
  54. <button
  55. key={r.name}
  56. aria-label={`${r.name}: $${r.value.toLocaleString()}`}
  57. className="flex h-full min-w-0 flex-1 flex-col items-center justify-end gap-1"
  58. type="button"
  59. onFocus={() => setHover(i)}
  60. onPointerEnter={() => setHover(i)}
  61. >
  62. <span
  63. className={cn(
  64. "w-full origin-bottom rounded-[6px] transition-opacity duration-300 [animation:grow_.8s_cubic-bezier(.2,.9,.25,1.05)_both]",
  65. hover === null || hover === i ? "opacity-100" : "opacity-35",
  66. )}
  67. style={{
  68. height: `${(r.value / max) * 100}%`,
  69. animationDelay: `${i * 60}ms`,
  70. background:
  71. "linear-gradient(180deg, var(--glacier), color-mix(in oklab, var(--glacier) 55%, var(--sky)))",
  72. }}
  73. />
  74. <span className="w-full truncate text-center text-[9.5px] text-muted">
  75. {r.name}
  76. </span>
  77. </button>
  78. ))}
  79. </div>
  80. </div>
  81. );
  82. }
  83. function InstanceTable() {
  84. const [dir, setDir] = useState<1 | -1>(-1);
  85. const rows = useMemo(
  86. () => [...INSTANCES].sort((a, b) => (a.cpu - b.cpu) * dir),
  87. [dir],
  88. );
  89. return (
  90. <div className="flex h-full flex-col text-[11.5px]">
  91. <div className="grid grid-cols-[1fr_1.2fr_1fr] border-b border-line pb-1 text-muted">
  92. <span>Instance</span>
  93. <span>Type</span>
  94. <button
  95. className="text-left hover:text-ink"
  96. type="button"
  97. onClick={() => setDir((d) => (d === 1 ? -1 : 1))}
  98. >
  99. CPU {dir === -1 ? "↓" : "↑"}
  100. </button>
  101. </div>
  102. {/* Rows that don't fit wrap into a hidden second column: only whole rows show. */}
  103. <ul className="flex min-h-0 flex-1 flex-col flex-wrap overflow-hidden">
  104. {rows.map((r, i) => (
  105. <li
  106. key={r.id}
  107. className="grid w-full grid-cols-[1fr_1.2fr_1fr] items-center border-b border-line/70 py-[5px] [animation:rise_.4s_ease_both]"
  108. style={{ animationDelay: `${i * 40}ms` }}
  109. >
  110. <span className="font-mono text-[10.5px] text-glacier-ink">
  111. {r.id}
  112. </span>
  113. <span className="truncate">{r.type}</span>
  114. <span className="flex items-center gap-1.5">
  115. <span className="h-1 flex-1 overflow-hidden rounded-full bg-line">
  116. <span
  117. className={cn(
  118. "block h-full rounded-full",
  119. r.cpu > 80 ? "bg-ember" : "bg-glacier",
  120. )}
  121. style={{ width: `${r.cpu}%` }}
  122. />
  123. </span>
  124. <span className="w-7 text-right tabular-nums text-muted">
  125. {r.cpu}%
  126. </span>
  127. </span>
  128. </li>
  129. ))}
  130. </ul>
  131. </div>
  132. );
  133. }
  134. function ForecastChart() {
  135. const all = [...ACTUAL, ...FORECAST];
  136. const px = (i: number) => (i / (all.length - 1)) * 300;
  137. const py = (v: number) => 92 - ((v - 7) / 8.5) * 84;
  138. const line = (pts: number[], offset = 0) =>
  139. pts
  140. .map(
  141. (v, i) =>
  142. `${i ? "L" : "M"}${px(i + offset).toFixed(1)} ${py(v).toFixed(1)}`,
  143. )
  144. .join(" ");
  145. const band = [
  146. ...FORECAST.map(
  147. (v, i) =>
  148. `${i ? "L" : "M"}${px(i + 6).toFixed(1)} ${py(v * (1 + 0.022 * (i + 1))).toFixed(1)}`,
  149. ),
  150. ...[...FORECAST]
  151. .reverse()
  152. .map(
  153. (v, j) =>
  154. `L${px(11 - j).toFixed(1)} ${py(v * (1 - 0.022 * (6 - j))).toFixed(1)}`,
  155. ),
  156. "Z",
  157. ].join(" ");
  158. return (
  159. <div className="flex h-full flex-col">
  160. <p className="mb-1 flex justify-between text-[11.5px] text-muted">
  161. <span className="font-medium text-ink">Monthly spend</span>
  162. <span>+29% by March</span>
  163. </p>
  164. <svg
  165. aria-label="Spend forecast"
  166. className="min-h-0 w-full flex-1"
  167. preserveAspectRatio="none"
  168. role="img"
  169. viewBox="0 0 300 96"
  170. >
  171. <path
  172. className="[animation:fade-in_.8s_.5s_ease_both]"
  173. d={band}
  174. fill="var(--ember)"
  175. fillOpacity="0.16"
  176. />
  177. <path
  178. className="[animation:draw_.9s_ease_both]"
  179. d={line(ACTUAL)}
  180. fill="none"
  181. pathLength={1}
  182. stroke="var(--glacier)"
  183. strokeDasharray="1 1"
  184. strokeLinecap="round"
  185. strokeWidth="2.4"
  186. />
  187. <path
  188. className="[animation:fade-in_.6s_.8s_ease_both]"
  189. d={line([ACTUAL[5], ...FORECAST], 5)}
  190. fill="none"
  191. stroke="var(--ember)"
  192. strokeDasharray="5 5"
  193. strokeWidth="2"
  194. vectorEffect="non-scaling-stroke"
  195. />
  196. </svg>
  197. </div>
  198. );
  199. }
  200. /** Amazon Q artifacts: ask, and the answer arrives as interactive UI. */
  201. export function ArtifactsCard() {
  202. const [active, setActive] = useState<TabId>("chart");
  203. const [busy, setBusy] = useState(false);
  204. const index = TABS.findIndex((t) => t.id === active);
  205. useEffect(() => {
  206. if (!busy) return;
  207. const t = window.setTimeout(() => setBusy(false), 520);
  208. return () => window.clearTimeout(t);
  209. }, [busy, active]);
  210. return (
  211. <div className="flex h-full flex-col gap-4 p-5 sm:flex-row sm:gap-5 sm:p-6 lg:p-7">
  212. <div className="flex flex-col sm:w-[40%] sm:shrink-0">
  213. <Eyebrow className="text-glacier-ink">AWS · Amazon Q</Eyebrow>
  214. <div className="mt-1 flex items-baseline justify-between gap-3">
  215. <h2 className="title text-[26px] lg:text-[28px]">Q artifacts</h2>
  216. <TextLink
  217. className="sm:hidden"
  218. href={projects.artifacts.href}
  219. label="Docs for Amazon Q chat artifacts"
  220. >
  221. Docs
  222. </TextLink>
  223. </div>
  224. <p className="mt-2 text-[15px] leading-snug text-muted">
  225. Answers that arrive as live charts and tables you can sort and
  226. explore.
  227. </p>
  228. <div className="mt-auto pt-3 max-sm:hidden">
  229. <TextLink
  230. href={projects.artifacts.href}
  231. label="Read the docs for Amazon Q chat artifacts"
  232. >
  233. Read the docs
  234. </TextLink>
  235. </div>
  236. </div>
  237. <div
  238. className="flex min-h-0 flex-1 flex-col gap-2 rounded-[20px] bg-inset p-2.5 ring-1 ring-line ring-inset [container-type:size]"
  239. data-nodrag
  240. >
  241. <div
  242. className="relative grid grid-cols-3 rounded-full bg-ink/[0.06] p-0.5 dark:bg-white/[0.07]"
  243. role="tablist"
  244. >
  245. <span
  246. aria-hidden
  247. className="lg absolute inset-y-0.5 left-0.5 w-[calc((100%-4px)/3)] rounded-full bg-card/80 shadow-[0_2px_8px_-2px_rgb(0_0_0/0.2)] transition-transform duration-500 ease-[cubic-bezier(.3,1.35,.5,1)] [--lg-blur:0px] [backdrop-filter:none] dark:bg-white/15"
  248. style={{ transform: `translateX(${index * 100}%)` }}
  249. />
  250. {TABS.map((t) => (
  251. <button
  252. key={t.id}
  253. aria-selected={t.id === active}
  254. className={cn(
  255. "relative z-10 h-7 rounded-full text-[12px] font-medium transition-colors",
  256. t.id === active ? "text-ink" : "text-muted hover:text-ink",
  257. )}
  258. data-cursor="Ask Q"
  259. role="tab"
  260. type="button"
  261. onClick={() => {
  262. if (t.id === active) return;
  263. setActive(t.id);
  264. setBusy(true);
  265. }}
  266. >
  267. {t.label}
  268. </button>
  269. ))}
  270. </div>
  271. <p className="ml-auto max-w-[90%] truncate rounded-[14px] rounded-br-[5px] bg-glacier-ink px-2.5 py-1 text-[11.5px] text-white [@container(height<185px)]:hidden dark:text-[#06223a]">
  272. {TABS[index].prompt}
  273. </p>
  274. <div className="relative min-h-0 flex-1 rounded-[14px] bg-card/75 p-2.5 dark:bg-black/25">
  275. {busy ? (
  276. <div className="h-full animate-shimmer rounded-[10px] bg-[linear-gradient(90deg,transparent,color-mix(in_oklab,var(--glacier)_14%,transparent),transparent)] bg-[length:200%_100%]" />
  277. ) : (
  278. <div
  279. key={active}
  280. className="h-full [animation:rise_.45s_cubic-bezier(.2,.8,.2,1)_both]"
  281. >
  282. {active === "chart" && <RegionChart />}
  283. {active === "table" && <InstanceTable />}
  284. {active === "forecast" && <ForecastChart />}
  285. </div>
  286. )}
  287. </div>
  288. <SampleNote className="text-right" />
  289. </div>
  290. </div>
  291. );
  292. }