import { useEditor } from '@/admin/features/activity-editor/core'; import { Button } from '@/components/ui/button'; import { PopoverContent } from '@/components/ui/popover'; import { Popover, PopoverTrigger } from '@radix-ui/react-popover'; import { Check, Link2, Trash } from 'lucide-react'; import { useRef } from 'react'; export function isValidUrl(url: string) { try { new URL(url); return true; } catch (_e) { return false; } } export function getUrlFromString(str: string) { if (isValidUrl(str)) return str; try { if (str.includes('.') && !str.includes(' ')) { return new URL(`https://${str}`).toString(); } } catch (_e) { return null; } } interface LinkSelectorProps { open: boolean; onOpenChange: (open: boolean) => void; } export const LinkSelector = ({ open, onOpenChange }: LinkSelectorProps) => { const inputRef = useRef(null); const { editor } = useEditor(); if (!editor) return null; return (
{ const target = e.currentTarget as HTMLFormElement; e.preventDefault(); const input = target[0] as HTMLInputElement; const url = getUrlFromString(input.value); if (url) { editor.chain().focus().setLink({ href: url }).run(); onOpenChange(false); } }} className="flex items-center" > {editor.getAttributes('link').href ? ( ) : ( )}
); };