录制演示
源码
/* Data tests — free block published by UI Stacks (public page source).
Kept verbatim: the author publishes this block free, in full, at https://uistacks.dev/blocks/data-pipelines-data-tests
See replica/README.md for provenance. */
"use client";
import { useEffect, useId, useRef, useState, type ReactNode } from "react";
import { CircleAlert, CircleCheck, RefreshCw } from "lucide-react";
import { Button } from "../components/button";
import { Skeleton, SkeletonGroup } from "../components/skeleton";
/* ── Sample data: replace with your own ─────────────────────── */
export type DataTestsState = "default" | "loading" | "error";
export type FailedTest = {
/** "unique · order_id": the test and the column it checks. */
test: string;
/** The model the test runs on. */
model: string;
/** "14 rows": how many rows failed. */
failures: string;
/** Error stops the run's downstream models; Warn only reports. */
severity?: "error" | "warn";
};
export type DataTests = {
/** "Nightly run · 26 Oct, 02:00 UTC". */
period: string;
failed: number;
total: number;
tests: FailedTest[];
/** "397 passed, 5 warned, 8 skipped". */
footnote: string;
};
/** dbt nightly run, Mon 26 Oct 2026 02:00 UTC: 2 failed + 397 passed + 5 warned + 8 skipped = 412 tests. */
const SAMPLE: DataTests = {
period: "Nightly run · 26 Oct, 02:00 UTC",
failed: 2,
total: 412,
tests: [
{ test: "unique · order_id", model: "fct_orders", failures: "14 rows" },
{ test: "not_null · customer_id", model: "stg_stripe__charges", failures: "37 rows" },
],
footnote: "397 passed, 5 warned, 8 skipped",
};
/* ── Block ──────────────────────────────────────────────────── */
export interface DataTestsProps {
className?: string;
state?: DataTestsState;
data?: DataTests;
/** Where View tests goes. Without it the header action is a button that calls onViewTests. */
testsHref?: string;
onViewTests?: () => void;
/** Called by Retry in the error state. Without it, Retry reloads the sample locally. */
onRetry?: () => void;
}
// Secondary buttons get a raised fill in Dark (white 15% over the card), as in the Figma.
const RAISED = "dark:bg-[color-mix(in_srgb,var(--fg)_15%,transparent)]";
// Figma color/well: skeleton bars (#0a0a0a at 5% on white, 64% on the Dark card).
const WELL = "bg-black/[0.05]! dark:bg-black/[0.64]!";
const FOCUS =
"outline-none focus-visible:ring-2 focus-visible:ring-[var(--select)] focus-visible:ring-offset-2 focus-visible:ring-offset-[var(--surface)]";
// Figma color/danger-text; its Dark value is a soft pink (#ffb4b4) rather than our brighter token.
const DANGER = "text-danger-text dark:text-[#ffb4b4]";
// From 720 px of room the count sits on the left and the failed tests on the right.
const WIDE = "flex flex-col gap-4 @min-[720px]:grid @min-[720px]:grid-cols-2 @min-[720px]:gap-x-10";
export default function DataPipelinesDataTestsBlock({ className = "", state = "default", data = SAMPLE, testsHref, onViewTests, onRetry }: DataTestsProps) {
const id = useId();
const [retry, setRetry] = useState<{ from: DataTestsState; phase: "loading" | "done" } | null>(null);
const timer = useRef<ReturnType<typeof setTimeout>>(undefined);
useEffect(() => () => clearTimeout(timer.current), []);
const local = retry?.from === state ? retry : null;
const shown: DataTestsState = local ? (local.phase === "loading" ? "loading" : "default") : state;
const [announce, setAnnounce] = useState("");
const retryNow = () => {
if (onRetry) return onRetry();
setRetry({ from: state, phase: "loading" });
setAnnounce("Loading test results");
clearTimeout(timer.current);
timer.current = setTimeout(() => {
setRetry({ from: state, phase: "done" });
setAnnounce("Test results loaded");
}, 1200);
};
return (
<section
aria-labelledby={id}
aria-busy={shown === "loading" || undefined}
className={"@container flex h-full w-full min-w-0 flex-col gap-4 rounded-[24px] bg-surface p-5 text-fg shadow-[var(--shadow-card)] " + className}
>
<header className="flex flex-col gap-1">
<div className="flex h-5 items-center gap-2">
<h2 id={id} className="min-w-0 flex-1 truncate text-[13px] font-medium leading-5 tracking-[-0.003em]">
Data tests
</h2>
{shown === "default" && (
<HeaderLink icon={<CircleCheck aria-hidden className="size-5" strokeWidth={1.5} />} href={testsHref} onClick={onViewTests}>
View tests
</HeaderLink>
)}
</div>
<p className="truncate text-[12px] leading-4 text-fg-muted">{shown === "default" ? data.period : "dbt tests · nightly run"}</p>
</header>
{shown === "default" ? (
<Body d={data} />
) : shown === "loading" ? (
<LoadingBody />
) : (
// The message sits over an invisible copy of the Default body, so the card keeps its height.
<div className="grid flex-1">
<div aria-hidden inert className="invisible [grid-area:1/1]">
<Body d={data} />
</div>
<div className="flex flex-col items-center justify-center gap-2 text-center [grid-area:1/1]">
<CircleAlert aria-hidden className="size-5 text-fg-muted" strokeWidth={1.5} />
<div role="status" className="flex flex-col items-center gap-2">
<p className="text-[13px] font-medium leading-5">Couldn’t load test results</p>
<p className="max-w-[280px] text-[12px] leading-4 text-fg-muted">Timed out · last run 26 Oct, 02:00 UTC</p>
</div>
<div className="pt-2">
<Button variant="secondary" size="sm" shape="pill" className={RAISED} leadingIcon={<RefreshCw strokeWidth={1.5} className="size-5!" />} onClick={retryNow}>
Retry
</Button>
</div>
</div>
</div>
)}
<span className="sr-only" role="status" aria-live="polite">
{announce}
</span>
</section>
);
}
function Body({ d }: { d: DataTests }) {
const footnote = <p className="text-[12px] leading-4 text-fg-muted">{d.footnote}</p>;
return (
<div className={WIDE}>
<div className="flex flex-col gap-4 @min-[720px]:justify-between">
<p className="flex min-w-0 flex-wrap items-baseline gap-x-2">
<span className="text-[32px] font-medium leading-10 tracking-[-0.035em] tabular-nums">{d.failed} failed</span>
<span className="text-[12px] leading-4 text-fg-muted tabular-nums">of {d.total.toLocaleString("en-US")} tests</span>
</p>
<div className="hidden @min-[720px]:block">{footnote}</div>
</div>
<div className="flex flex-col gap-2">
<ul aria-label="Failed tests" className="flex flex-col gap-2">
{d.tests.map((t) => (
<li key={t.test + t.model} className="flex items-center gap-3 py-1">
<div className="flex min-w-0 flex-1 flex-col gap-1">
<p className="truncate text-[13px] leading-5 tracking-[-0.003em]" title={t.test}>
{t.test}
</p>
<p className="truncate text-[12px] leading-4 text-fg-muted" title={`${t.severity === "warn" ? "Warn" : "Error"} · ${t.model}`}>
{t.severity === "warn" ? "Warn" : "Error"} · {t.model}
</p>
</div>
<p className={`w-[72px] shrink-0 text-right text-[13px] font-medium leading-5 tracking-[-0.003em] tabular-nums ${t.severity === "warn" ? "text-warning-text" : DANGER}`}>
{t.failures}
<span className="sr-only"> failed</span>
</p>
</li>
))}
</ul>
<div className="@min-[720px]:hidden">{footnote}</div>
</div>
</div>
);
}
function HeaderLink({ icon, href, onClick, children }: { icon: ReactNode; href?: string; onClick?: () => void; children: ReactNode }) {
const cls = `-my-1 inline-flex h-7 shrink-0 items-center gap-1.5 rounded-full text-[13px] font-medium leading-5 tracking-[-0.003em] text-fg-muted ${FOCUS}`;
return href ? (
<a href={href} className={cls}>
{icon}
{children}
</a>
) : (
<button type="button" onClick={onClick} className={cls}>
{icon}
{children}
</button>
);
}
// Same slots as the Default body (value 40, two 48 px rows, the footnote), so the card keeps its height.
function LoadingBody() {
const foot = <Skeleton width={200} height={10} radius={6} className={WELL} />;
return (
<SkeletonGroup label="Loading Data tests" className={WIDE}>
<div className="flex flex-col gap-4 @min-[720px]:justify-between">
<div className="flex h-10 items-end gap-2 pb-1">
<Skeleton shape="block" width={108} height={32} radius={6} className={WELL} />
<Skeleton width={80} height={10} radius={6} className={WELL} />
</div>
<div className="hidden h-4 items-center @min-[720px]:flex">{foot}</div>
</div>
<div className="flex flex-col gap-2">
{[0, 1].map((i) => (
<div key={i} className="flex h-12 items-center gap-3 py-1">
<div className="flex flex-1 flex-col gap-2">
<Skeleton width={122} height={12} radius={6} className={WELL} delay={i * 0.1} />
<Skeleton width={118} height={10} radius={6} className={WELL} delay={i * 0.1} />
</div>
<Skeleton width={72} height={12} radius={6} className={WELL} delay={i * 0.1} />
</div>
))}
<div className="flex h-4 items-center @min-[720px]:hidden">{foot}</div>
</div>
</SkeletonGroup>
);
}
使用说明
把 TSX 加入 React 项目,并安装组件引用的依赖。页面样式依赖 Tailwind CSS 与 主题变量。
包依赖 · lucide-react、react
共享源码 · button.tsx、skeleton.tsx