@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 78 lines 2.77 KB
Raw
1 import { useState } from "react";
2 import { flushSync } from "react-dom";
3
4 import { cn } from "../../lib/utils";
5 import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
6
7 const REDACTED_TEXT_ALPHABET = "abcdefghjkmnpqrstuvwxyz23456789";
8
9 export function redactedPlaceholder(value: string): string {
10 let state = 0x811c9dc5;
11 for (let index = 0; index < value.length; index += 1) {
12 state ^= value.charCodeAt(index);
13 state = Math.imul(state, 0x01000193);
14 }
15
16 const nextChar = () => {
17 state = Math.imul(state ^ (state >>> 13), 0x85ebca6b);
18 state = Math.imul(state ^ (state >>> 16), 0xc2b2ae35);
19 return REDACTED_TEXT_ALPHABET[Math.abs(state) % REDACTED_TEXT_ALPHABET.length] ?? "x";
20 };
21
22 return Array.from(value, (char) => {
23 if (char === "@" || char === "." || char === "-" || char === "_") return char;
24 return nextChar();
25 }).join("");
26 }
27
28 export function RedactedSensitiveText(props: {
29 readonly value: string | null | undefined;
30 readonly ariaLabel: string;
31 readonly revealTooltip: string;
32 readonly hideTooltip: string;
33 readonly className?: string;
34 }) {
35 const [visibility, setVisibility] = useState<"hidden" | "hiding" | "revealed">("hidden");
36 const revealed = visibility === "revealed";
37 const value = props.value?.trim();
38 const redacted = value ? redactedPlaceholder(value) : "";
39
40 if (!value) return null;
41
42 return (
43 <Tooltip>
44 <TooltipTrigger
45 render={
46 <button
47 type="button"
48 className={cn(
49 "min-w-0 cursor-pointer rounded-sm font-mono text-2xs leading-normal transition hover:text-foreground",
50 revealed ? "text-muted-foreground" : "select-none text-muted-foreground blur-xs",
51 props.className,
52 )}
53 onClick={(event) => {
54 if (!revealed) return setVisibility("revealed");
55 const button = event.currentTarget;
56 flushSync(() => setVisibility("hiding"));
57 const finishHiding = () =>
58 setVisibility((current) => (current === "hiding" ? "hidden" : current));
59 const blur = button
60 .getAnimations()
61 .find(
62 (animation) =>
63 animation instanceof CSSTransition && animation.transitionProperty === "filter",
64 );
65 // Theme changes suppress the transition; a reversed one settles once revealed.
66 if (blur) void blur.finished.then(finishHiding, finishHiding);
67 else finishHiding();
68 }}
69 aria-label={props.ariaLabel}
70 >
71 {visibility === "hidden" ? redacted : value}
72 </button>
73 }
74 />
75 <TooltipPopup side="top">{revealed ? props.hideTooltip : props.revealTooltip}</TooltipPopup>
76 </Tooltip>
77 );
78 }