import { useState } from 'react';
import { ConfiguredIcon } from '@/components/ConfiguredIcon';
import { Check, ChevronRight } from '@/components/icons';
import { Button } from '@/components/ui/button';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import type { ResolvedContextualOption, ThemeLabels } from '@/lib/types';
export function ContextualMenu({
options,
labels,
}: {
options: ResolvedContextualOption[];
labels: ThemeLabels;
}) {
const [copied, setCopied] = useState(false);
const primary = options[0];
if (!primary) {
return null;
}
const runOption = async (option: ResolvedContextualOption) => {
if (option.action === 'copy') {
try {
const response = await fetch(option.href);
const text = response.ok ? await response.text() : window.location.href;
await navigator.clipboard.writeText(text);
setCopied(true);
setTimeout(() => setCopied(false), 1500);
} catch {
// Clipboard unavailable; leave the control unchanged.
}
return;
}
window.open(option.href, option.target, option.target === '_blank' ? 'noreferrer' : undefined);
};
const optionIcon = (option: ResolvedContextualOption, showCopied = false) =>
showCopied && copied ?