Files
ARTEX/web/src/lib/utils.ts
T
BaaRai e233c02314 Translate all Chinese text to English
Comments, doc comments, UI labels, log and error messages, LLM prompt
strings, markdown docs, test data and assertions. Identifiers, enum
keys, routes, placeholders and code structure are unchanged.

Notes:
- docs/漏洞流量证据.md renamed to docs/vulnerability-traffic-evidence.md
- tests that exercised CJK specific handling (GBK zip names, Chinese
  full text search) now use ASCII equivalents; go build passes and the
  test suite matches the pre translation baseline (remaining failures
  need a PostgreSQL DSN and fail identically on the original)
- frontend mention syntax now uses English kinds (finding, asset, ...)
  matching the backend, chat mentions tests pass
2026-10-08 23:14:34 +02:00

112 lines
3.8 KiB
TypeScript

import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
// Helper for onInteractOutside close decisions in sheets/dialogs (Sheet/Dialog).
//
// Background: Radix popovers inside a sheet (Select dropdowns, DropdownMenu, Popover, etc.)
// portal outside the sheet. With a popover open, clicking the overlay or outside the sheet
// to dismiss it makes this one pointerdown get handled by both the Select and the Sheet
// DismissableLayers; Select closes first as a discrete event React flushes synchronously, so
// by the time Sheet's handler runs the popover's data-state has long flipped to closed — detecting "right now" whether a popover is open is inherently unreliable (verified empirically).
//
// Correct approach: Radix listens for pointerdown in the bubble phase; we record in the
// capture phase (earlier) whether any popover is open right now, and onInteractOutside reads that recorded value to decide whether to allow closing.
function isRadixOverlayOpenNow(): boolean {
if (typeof document === "undefined") return false;
return !!document.querySelector(
[
"[data-slot='select-trigger'][data-state='open']",
"[data-slot='select-content'][data-state='open']",
"[role='listbox'][data-state='open']",
"[data-radix-popper-content-wrapper]",
"[aria-expanded='true'][data-state='open']",
].join(","),
);
}
let overlayOpenAtLastPointerDown = false;
if (typeof document !== "undefined") {
document.addEventListener(
"pointerdown",
() => {
overlayOpenAtLastPointerDown = isRadixOverlayOpenNow();
},
true, // capture: record before Radix's bubble-phase pointerdown handlers
);
}
// radixOverlayWasOpenAtPointerDown returns whether a Radix popover was open at the most
// recent pointerdown. Sheets/dialogs use it: overlay click with a popover open → dismiss only the popover, keep themselves open.
export function radixOverlayWasOpenAtPointerDown(): boolean {
return overlayOpenAtLastPointerDown;
}
// copyText writes text to the clipboard and returns whether it succeeded.
// Background: navigator.clipboard is only available in secure contexts (HTTPS /
// localhost); it is undefined when accessed via IP + HTTP, so we fall back to execCommand("copy").
export async function copyText(text: string): Promise<boolean> {
if (navigator.clipboard && window.isSecureContext) {
try {
await navigator.clipboard.writeText(text);
return true;
} catch {
// continue with the fallback
}
}
try {
const textarea = document.createElement("textarea");
textarea.value = text;
textarea.style.position = "fixed";
textarea.style.left = "-9999px";
textarea.style.top = "0";
document.body.appendChild(textarea);
textarea.focus();
textarea.select();
const ok = document.execCommand("copy");
document.body.removeChild(textarea);
return ok;
} catch {
return false;
}
}
export const getInitials = (str: string): string => {
if (typeof str !== "string" || !str.trim()) return "?";
return (
str
.trim()
.split(/\s+/)
.filter(Boolean)
.map((word) => word[0])
.join("")
.toUpperCase() || "?"
);
};
export function formatCurrency(
amount: number,
opts?: {
currency?: string;
locale?: string;
minimumFractionDigits?: number;
maximumFractionDigits?: number;
noDecimals?: boolean;
},
) {
const { currency = "USD", locale = "en-US", minimumFractionDigits, maximumFractionDigits, noDecimals } = opts ?? {};
const formatOptions: Intl.NumberFormatOptions = {
style: "currency",
currency,
minimumFractionDigits: noDecimals ? 0 : minimumFractionDigits,
maximumFractionDigits: noDecimals ? 0 : maximumFractionDigits,
};
return new Intl.NumberFormat(locale, formatOptions).format(amount);
}