Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | 2x 19x 19x 19x 19x 19x 19x 6x 2x 2x 4x 4x 4x 4x 4x 4x 1x 1x 3x 1x 1x 19x 19x 19x 20x 3x 17x 3x 14x 13x | import { useEffect, useRef } from "react";
import { Check, X, Sparkles, AlertCircle } from "lucide-react";
import { Button } from "../ui/button";
import type { CopilotAction } from "./EditorToolbar";
interface AIResultPreviewProps {
/** The AI-generated text result */
result: string;
/** Which action produced this result */
action: CopilotAction | string;
/** Whether the AI is still loading */
isLoading: boolean;
/** Error message if the AI call failed */
error: string | null;
/** Called when user accepts the result */
onInsert: () => void;
/** Called when user discards the result */
onDiscard: () => void;
/** Called to retry a failed action */
onRetry: () => void;
}
const ACTION_LABELS: Record<string, string> = {
expand: "Expanded text",
shorten: "Shortened text",
rewrite: "Rewritten text",
indianize: "With Indian context",
"suggest-next": "Suggested next section",
cta: "Call to action",
};
function ModalBackdrop({
children,
onClose,
label,
}: {
children: React.ReactNode;
onClose: () => void;
label: string;
}) {
const dialogRef = useRef<HTMLDivElement>(null);
// Focus trap + Escape key
useEffect(() => {
const dialog = dialogRef.current;
Iif (!dialog) return;
// Focus the dialog on mount
dialog.focus();
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === "Escape") {
onClose();
return;
}
// Focus trap
Eif (e.key === "Tab") {
const focusable = dialog.querySelectorAll<HTMLElement>(
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])',
);
Iif (focusable.length === 0) return;
const first = focusable[0];
const last = focusable[focusable.length - 1];
if (e.shiftKey && document.activeElement === first) {
e.preventDefault();
last.focus();
} else if (!e.shiftKey && document.activeElement === last) {
e.preventDefault();
first.focus();
}
}
};
document.addEventListener("keydown", handleKeyDown);
return () => document.removeEventListener("keydown", handleKeyDown);
}, [onClose]);
return (
<div className="fixed inset-0 z-[70] flex items-center justify-center bg-black/40">
<div
ref={dialogRef}
role="dialog"
aria-modal="true"
aria-label={label}
tabIndex={-1}
className="outline-none"
>
{children}
</div>
</div>
);
}
export function AIResultPreview({
result,
action,
isLoading,
error,
onInsert,
onDiscard,
onRetry,
}: AIResultPreviewProps) {
// Loading state
if (isLoading) {
return (
<ModalBackdrop onClose={onDiscard} label="AI processing">
<div className="bg-background-elevated rounded-xl shadow-xl p-6 mx-4 max-w-sm w-full text-center space-y-3">
<Sparkles className="h-8 w-8 text-primary-500 mx-auto animate-pulse" />
<p className="text-sm font-medium text-foreground-default">
AI is working on it...
</p>
<p className="text-xs text-foreground-muted">
{ACTION_LABELS[action] || "Processing"}
</p>
</div>
</ModalBackdrop>
);
}
// Error state
if (error) {
return (
<ModalBackdrop onClose={onDiscard} label="AI action failed">
<div className="bg-background-elevated rounded-xl shadow-xl p-6 mx-4 max-w-sm w-full space-y-4">
<div className="flex items-center gap-3">
<AlertCircle className="h-6 w-6 text-error flex-shrink-0" />
<p className="text-sm font-medium text-foreground-default">AI action failed</p>
</div>
<p className="text-sm text-foreground-muted">{error}</p>
<div className="flex items-center gap-2 justify-end">
<Button size="sm" variant="outline" onClick={onDiscard}>
Dismiss
</Button>
<Button size="sm" onClick={onRetry}>
Retry
</Button>
</div>
</div>
</ModalBackdrop>
);
}
if (!result) return null;
// Result state
return (
<ModalBackdrop onClose={onDiscard} label={ACTION_LABELS[action] || "AI Result"}>
<div className="bg-background-elevated rounded-xl shadow-xl mx-4 max-w-lg w-full flex flex-col max-h-[80vh]">
{/* Header */}
<div className="flex items-center justify-between px-5 py-4 border-b border-border-default flex-shrink-0">
<div className="flex items-center gap-2">
<Sparkles className="h-4 w-4 text-primary-500" />
<span className="text-sm font-semibold text-foreground-default">
{ACTION_LABELS[action] || "AI Result"}
</span>
</div>
<button
type="button"
onClick={onDiscard}
aria-label="Close"
className="p-1 text-foreground-subtle hover:text-foreground-default rounded"
>
<X className="h-4 w-4" />
</button>
</div>
{/* Content */}
<div className="flex-1 overflow-y-auto px-5 py-4">
<div className="text-sm text-foreground-default whitespace-pre-wrap leading-relaxed">
{result}
</div>
</div>
{/* Footer */}
<div className="flex items-center gap-3 px-5 py-4 border-t border-border-default flex-shrink-0">
<Button onClick={onInsert} className="flex-1 gap-1.5">
<Check className="h-4 w-4" />
Accept
</Button>
<Button variant="outline" onClick={onDiscard} className="flex-1 gap-1.5">
<X className="h-4 w-4" />
Reject
</Button>
</div>
</div>
</ModalBackdrop>
);
}
|