Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ComposerPendingElementContexts.tsx

95 lines3.0 KB

1import { MousePointerClick, X } from "lucide-react";
2​
3import {
4 COMPOSER_INLINE_CHIP_CLASS_NAME,
5 COMPOSER_INLINE_CHIP_DISMISS_BUTTON_CLASS_NAME,
6 COMPOSER_INLINE_CHIP_ICON_CLASS_NAME,
7 COMPOSER_INLINE_CHIP_LABEL_CLASS_NAME,
8} from "../composerInlineChip";
9import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
10import { cn } from "~/lib/utils";
11import {
12 type ElementContextDraft,
13 formatElementContextLabel,
14 formatElementContextSourceLabel,
15} from "~/lib/elementContext";
16​
17interface ComposerPendingElementContextsProps {
18 contexts: ReadonlyArray<ElementContextDraft>;
19 onRemove: (contextId: string) => void;
20 className?: string;
22​
23interface ComposerPendingElementContextChipProps {
24 context: ElementContextDraft;
25 onRemove: (contextId: string) => void;
27​
28function buildTooltipContent(context: ElementContextDraft): string {
29 const lines: string[] = [];
30 lines.push(formatElementContextLabel(context));
31 const source = formatElementContextSourceLabel(context);
32 if (source) lines.push(source);
33 if (context.pageUrl) lines.push(context.pageUrl);
34 if (context.selector) lines.push(context.selector);
35 if (context.htmlPreview.trim().length > 0) {
36 lines.push("");
37 lines.push(context.htmlPreview.trim().slice(0, 600));
38 }
39 return lines.join("\n");
41​
42function ComposerPendingElementContextChip({
43 context,
44 onRemove,
45}: ComposerPendingElementContextChipProps) {
46 const label = formatElementContextLabel(context);
47 const sourceLabel = formatElementContextSourceLabel(context);
48 return (
49 <Tooltip>
50 <TooltipTrigger
51 render={
52 <span className={cn(COMPOSER_INLINE_CHIP_CLASS_NAME, "pr-1")}>
53 <MousePointerClick className={cn(COMPOSER_INLINE_CHIP_ICON_CLASS_NAME, "size-3.5")} />
54 <span className={COMPOSER_INLINE_CHIP_LABEL_CLASS_NAME}>{label}</span>
55 {sourceLabel ? (
56 <span className="select-none text-[10px] font-normal leading-tight text-muted-foreground/85">
57 {sourceLabel}
58 </span>
59 ) : null}
60 <button
61 type="button"
62 aria-label={`Remove ${label}`}
63 className={COMPOSER_INLINE_CHIP_DISMISS_BUTTON_CLASS_NAME}
64 onClick={(event) => {
65 event.preventDefault();
66 event.stopPropagation();
67 onRemove(context.id);
68 }}
69 >
70 <X className="size-3" aria-hidden />
71 </button>
72 </span>
73 }
74 />
75 <TooltipPopup side="top" className="max-w-96 whitespace-pre-wrap leading-tight">
76 {buildTooltipContent(context)}
77 </TooltipPopup>
78 </Tooltip>
79 );
81​
82export function ComposerPendingElementContexts({
83 contexts,
84 onRemove,
85 className,
86}: ComposerPendingElementContextsProps) {
87 if (contexts.length === 0) return null;
88 return (
89 <div className={cn("flex flex-wrap gap-1.5", className)}>
90 {contexts.map((context) => (
91 <ComposerPendingElementContextChip key={context.id} context={context} onRemove={onRemove} />
92 ))}
93 </div>
94 );