artifacts.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 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. <ul className="min-h-0 flex-1 overflow-hidden">
  103. {rows.map((r, i) => (
  104. <li
  105. key={r.id}
  106. className="grid grid-cols-[1fr_1.2fr_1fr] items-center border-b border-line/70 py-[5px] [animation:rise_.4s_ease_both]"
  107. style={{ animationDelay: `${i * 40}ms` }}
  108. >
  109. <span className="font-mono text-[10.5px] text-glacier-ink">
  110. {r.id}
  111. </span>
  112. <span className="truncate">{r.type}</span>
  113. <span className="flex items-center gap-1.5">
  114. <span className="h-1 flex-1 overflow-hidden rounded-full bg-line">
  115. <span
  116. className={cn(
  117. "block h-full rounded-full",
  118. r.cpu > 80 ? "bg-ember" : "bg-glacier",
  119. )}
  120. style={{ width: `${r.cpu}%` }}
  121. />
  122. </span>
  123. <span className="w-7 text-right tabular-nums text-muted">
  124. {r.cpu}%
  125. </span>
  126. </span>
  127. </li>
  128. ))}
  129. </ul>
  130. </div>
  131. );
  132. }
  133. function ForecastChart() {
  134. const all = [...ACTUAL, ...FORECAST];
  135. const px = (i: number) => (i / (all.length - 1)) * 300;
  136. const py = (v: number) => 92 - ((v - 7) / 8.5) * 84;
  137. const line = (pts: number[], offset = 0) =>
  138. pts
  139. .map(
  140. (v, i) =>
  141. `${i ? "L" : "M"}${px(i + offset).toFixed(1)} ${py(v).toFixed(1)}`,
  142. )
  143. .join(" ");
  144. const band = [
  145. ...FORECAST.map(
  146. (v, i) =>
  147. `${i ? "L" : "M"}${px(i + 6).toFixed(1)} ${py(v * (1 + 0.022 * (i + 1))).toFixed(1)}`,
  148. ),
  149. ...[...FORECAST]
  150. .reverse()
  151. .map(
  152. (v, j) =>
  153. `L${px(11 - j).toFixed(1)} ${py(v * (1 - 0.022 * (6 - j))).toFixed(1)}`,
  154. ),
  155. "Z",
  156. ].join(" ");
  157. return (
  158. <div className="flex h-full flex-col">
  159. <p className="mb-1 flex justify-between text-[11.5px] text-muted">
  160. <span className="font-medium text-ink">Monthly spend</span>
  161. <span>+29% by March</span>
  162. </p>
  163. <svg
  164. aria-label="Spend forecast"
  165. className="min-h-0 w-full flex-1"
  166. preserveAspectRatio="none"
  167. role="img"
  168. viewBox="0 0 300 96"
  169. >
  170. <path
  171. className="[animation:fade-in_.8s_.5s_ease_both]"
  172. d={band}
  173. fill="var(--ember)"
  174. fillOpacity="0.16"
  175. />
  176. <path
  177. className="[animation:draw_.9s_ease_both]"
  178. d={line(ACTUAL)}
  179. fill="none"
  180. pathLength={1}
  181. stroke="var(--glacier)"
  182. strokeDasharray="1 1"
  183. strokeLinecap="round"
  184. strokeWidth="2.4"
  185. />
  186. <path
  187. className="[animation:fade-in_.6s_.8s_ease_both]"
  188. d={line([ACTUAL[5], ...FORECAST], 5)}
  189. fill="none"
  190. stroke="var(--ember)"
  191. strokeDasharray="5 5"
  192. strokeWidth="2"
  193. vectorEffect="non-scaling-stroke"
  194. />
  195. </svg>
  196. </div>
  197. );
  198. }
  199. /** Amazon Q artifacts: ask, and the answer arrives as interactive UI. */
  200. export function ArtifactsCard() {
  201. const [active, setActive] = useState<TabId>("chart");
  202. const [busy, setBusy] = useState(false);
  203. const index = TABS.findIndex((t) => t.id === active);
  204. useEffect(() => {
  205. if (!busy) return;
  206. const t = window.setTimeout(() => setBusy(false), 520);
  207. return () => window.clearTimeout(t);
  208. }, [busy, active]);
  209. return (
  210. <div className="flex h-full flex-col gap-4 p-5 sm:flex-row sm:gap-5 sm:p-6 lg:p-7">
  211. <div className="flex flex-col sm:w-[40%] sm:shrink-0">
  212. <Eyebrow className="text-glacier-ink">AWS · Amazon Q</Eyebrow>
  213. <div className="mt-1 flex items-baseline justify-between gap-3">
  214. <h2 className="title text-[26px] lg:text-[28px]">Q artifacts</h2>
  215. <TextLink
  216. className="sm:hidden"
  217. href={projects.artifacts.href}
  218. label="Docs for Amazon Q chat artifacts"
  219. >
  220. Docs
  221. </TextLink>
  222. </div>
  223. <p className="mt-2 text-[15px] leading-snug text-muted">
  224. Answers that arrive as live charts and tables you can sort and
  225. explore.
  226. </p>
  227. <div className="mt-auto pt-3 max-sm:hidden">
  228. <TextLink
  229. href={projects.artifacts.href}
  230. label="Read the docs for Amazon Q chat artifacts"
  231. >
  232. Read the docs
  233. </TextLink>
  234. </div>
  235. </div>
  236. <div
  237. className="flex min-h-0 flex-1 flex-col gap-2 rounded-[20px] bg-inset p-2.5 ring-1 ring-line ring-inset"
  238. data-nodrag
  239. >
  240. <div
  241. className="relative grid grid-cols-3 rounded-full bg-ink/[0.06] p-0.5 dark:bg-white/[0.07]"
  242. role="tablist"
  243. >
  244. <span
  245. aria-hidden
  246. 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"
  247. style={{ transform: `translateX(${index * 100}%)` }}
  248. />
  249. {TABS.map((t) => (
  250. <button
  251. key={t.id}
  252. aria-selected={t.id === active}
  253. className={cn(
  254. "relative z-10 h-7 rounded-full text-[12px] font-medium transition-colors",
  255. t.id === active ? "text-ink" : "text-muted hover:text-ink",
  256. )}
  257. data-cursor="Ask Q"
  258. role="tab"
  259. type="button"
  260. onClick={() => {
  261. if (t.id === active) return;
  262. setActive(t.id);
  263. setBusy(true);
  264. }}
  265. >
  266. {t.label}
  267. </button>
  268. ))}
  269. </div>
  270. <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 dark:text-[#06223a]">
  271. {TABS[index].prompt}
  272. </p>
  273. <div className="relative min-h-0 flex-1 rounded-[14px] bg-card/75 p-2.5 dark:bg-black/25">
  274. {busy ? (
  275. <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%]" />
  276. ) : (
  277. <div
  278. key={active}
  279. className="h-full [animation:rise_.45s_cubic-bezier(.2,.8,.2,1)_both]"
  280. >
  281. {active === "chart" && <RegionChart />}
  282. {active === "table" && <InstanceTable />}
  283. {active === "forecast" && <ForecastChart />}
  284. </div>
  285. )}
  286. </div>
  287. <SampleNote className="text-right" />
  288. </div>
  289. </div>
  290. );
  291. }