import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { useState } from "react"; import { useNavigate } from "react-router"; import { useApi } from "../api/context"; import { errorMessage } from "../state/actions"; import { PLAYER_STATE_KEY } from "../state/player"; import { Button, Sheet } from "../ui"; /** The file a save is shared as: plain text, dated. */ export function saveFile(code: string, now = new Date()): File { const day = now.toISOString().slice(0, 10); return new File([code], `deadhead-save-${day}.txt`, { type: "text/plain" }); } /** Shares the file where the device can (the share sheet), otherwise downloads it. */ async function shareFile(file: File): Promise { if (navigator.canShare?.({ files: [file] })) { await navigator.share({ files: [file], title: "Deadhead save" }); return; } const url = URL.createObjectURL(file); const link = document.createElement("a"); link.href = url; link.download = file.name; link.click(); URL.revokeObjectURL(url); } const codeBox = "mt-1 h-28 w-full resize-none rounded-lg border border-space-600 bg-space-950/70 px-3 py-2 font-mono text-xs break-all text-ink-100 focus:border-amber-400 focus:outline-none"; /** "Export save" (decision 0.5.92): the whole company as one code, to copy or share as a file. */ export function ExportSave() { const api = useApi(); const [open, setOpen] = useState(false); const [copied, setCopied] = useState(false); const exported = useQuery({ queryKey: ["export-save"], queryFn: () => api.call("exportSave", {}), enabled: open, gcTime: 0, staleTime: 0, }); const code = exported.data?.code; const close = () => { setOpen(false); setCopied(false); }; return ( <> {open && (

This code is your whole company. Keep it somewhere safe, or import it on another device.

{code ? ( <>