// AVA — Script ⇢ Hash share dialogs (v7.8). // // Two dialogs used by the Script tab toolbar: // • ScriptToHashDialog — encodes the current editor script into ONE // shareable Unicode string (see lib/ava/share-codec.ts for the format), // shows it with a Copy button, live stats and the symbol legend. // Compiling is NOT required — sharing a broken script to get it // debugged is a feature. // • HashToScriptDialog — pastes a share string back; on success it shows // what came out (name, size, compile status) and loads the script into // the editor exactly as it was, ready to Apply / Save As…. 'use client' import { useMemo, useState } from 'react' import { ArrowRightLeft, Check, ClipboardPaste, CircleHelp, FileCode2, Share2, X } from 'lucide-react' import { Button } from '@/components/ui/button' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog' import { Textarea } from '@/components/ui/textarea' import { decodeScriptHash, encodeScriptHash, SHARE_LEGEND } from '@/lib/ava/share-codec' import { validateScript } from '@/lib/ava/script' import { useToast } from '@/hooks/use-toast' import { cn } from '@/lib/utils' /** Copy text to the clipboard with a legacy fallback. */ async function copyText(text: string): Promise { try { await navigator.clipboard.writeText(text) return true } catch { try { const ta = document.createElement('textarea') ta.value = text ta.style.position = 'fixed' ta.style.opacity = '0' document.body.appendChild(ta) ta.select() const ok = document.execCommand('copy') ta.remove() return ok } catch { return false } } } // ── Script ⇢ Hash ───────────────────────────────────────────────────────── export function ScriptToHashDialog({ open, onOpenChange, scriptText, }: { open: boolean onOpenChange: (open: boolean) => void scriptText: string }) { const { toast } = useToast() const [copied, setCopied] = useState(false) const encoded = useMemo(() => encodeScriptHash(scriptText), [scriptText]) const savedPct = Math.round((1 - encoded.hashLength / Math.max(1, encoded.scriptLength)) * 100) const empty = scriptText.trim() === '' async function copy() { const ok = await copyText(encoded.hash) if (ok) { setCopied(true) setTimeout(() => setCopied(false), 2000) } else { toast({ title: 'Copy failed', description: 'Select the string in the box and copy it manually (⌘/Ctrl+C).', variant: 'destructive', }) } } return ( Script ⇢ Hash Your script as ONE shareable string -- post it anywhere (chat, social, forums). Anyone pastes it back with Hash⇢Script and gets your script exactly as it is now.