forked from Dshield-xyz/Dshield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatusMessage.tsx
More file actions
44 lines (40 loc) · 1.25 KB
/
Copy pathStatusMessage.tsx
File metadata and controls
44 lines (40 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { cva } from "class-variance-authority";
import { cn } from "@/lib/cn";
const status = cva("rounded-xl p-3 text-sm", {
variants: {
tone: {
error: "bg-red-900/30 text-red-400",
success: "bg-green-900/30 text-green-400",
info: "bg-zinc-800 text-zinc-300",
},
},
defaultVariants: { tone: "info" },
});
/**
* Infer tone from a status string the same way the flows always have: anything
* starting with "Error" is an error, anything containing one of `successHints`
* is a success, everything else is neutral progress text.
*/
function inferTone(
message: string,
successHints: string[],
): "error" | "success" | "info" {
if (message.startsWith("Error")) return "error";
if (successHints.some((hint) => message.includes(hint))) return "success";
return "info";
}
export interface StatusMessageProps {
message: string;
/** Substrings that mark the message as a success. Defaults to ["successful"]. */
successHints?: string[];
className?: string;
}
export function StatusMessage({
message,
successHints = ["successful"],
className,
}: StatusMessageProps) {
if (!message) return null;
const tone = inferTone(message, successHints);
return <div className={cn(status({ tone }), className)}>{message}</div>;
}