|
| 1 | +import { useState, useRef, useEffect } from "react" |
| 2 | +import type { BaseSolver } from "../BaseSolver" |
| 3 | + |
| 4 | +interface DownloadDropdownProps { |
| 5 | + solver: BaseSolver |
| 6 | + className?: string |
| 7 | +} |
| 8 | + |
| 9 | +const deepRemoveUnderscoreProperties = (obj: any): any => { |
| 10 | + if (obj === null || typeof obj !== "object") { |
| 11 | + return obj |
| 12 | + } |
| 13 | + |
| 14 | + if (Array.isArray(obj)) { |
| 15 | + return obj.map(deepRemoveUnderscoreProperties) |
| 16 | + } |
| 17 | + |
| 18 | + const result: any = {} |
| 19 | + for (const [key, value] of Object.entries(obj)) { |
| 20 | + if (!key.startsWith("_")) { |
| 21 | + result[key] = deepRemoveUnderscoreProperties(value) |
| 22 | + } |
| 23 | + } |
| 24 | + return result |
| 25 | +} |
| 26 | + |
| 27 | +export const DownloadDropdown = ({ |
| 28 | + solver, |
| 29 | + className = "", |
| 30 | +}: DownloadDropdownProps) => { |
| 31 | + const [isOpen, setIsOpen] = useState(false) |
| 32 | + const dropdownRef = useRef<HTMLDivElement>(null) |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + const handleClickOutside = (event: MouseEvent) => { |
| 36 | + if ( |
| 37 | + dropdownRef.current && |
| 38 | + !dropdownRef.current.contains(event.target as Node) |
| 39 | + ) { |
| 40 | + setIsOpen(false) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + document.addEventListener("mousedown", handleClickOutside) |
| 45 | + return () => document.removeEventListener("mousedown", handleClickOutside) |
| 46 | + }, []) |
| 47 | + |
| 48 | + const downloadJSON = () => { |
| 49 | + try { |
| 50 | + if (typeof solver.getConstructorParams !== "function") { |
| 51 | + alert( |
| 52 | + `getConstructorParams() is not implemented for ${solver.constructor.name}`, |
| 53 | + ) |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + const params = deepRemoveUnderscoreProperties( |
| 58 | + solver.getConstructorParams(), |
| 59 | + ) |
| 60 | + const blob = new Blob([JSON.stringify(params, null, 2)], { |
| 61 | + type: "application/json", |
| 62 | + }) |
| 63 | + const url = URL.createObjectURL(blob) |
| 64 | + const a = document.createElement("a") |
| 65 | + a.href = url |
| 66 | + a.download = `${solver.constructor.name}_params.json` |
| 67 | + a.click() |
| 68 | + URL.revokeObjectURL(url) |
| 69 | + } catch (error) { |
| 70 | + alert( |
| 71 | + `Error downloading params for ${solver.constructor.name}: ${error instanceof Error ? error.message : String(error)}`, |
| 72 | + ) |
| 73 | + } |
| 74 | + setIsOpen(false) |
| 75 | + } |
| 76 | + |
| 77 | + const downloadPageTsx = () => { |
| 78 | + try { |
| 79 | + const params = deepRemoveUnderscoreProperties( |
| 80 | + solver.getConstructorParams(), |
| 81 | + ) |
| 82 | + const solverName = solver.constructor.name |
| 83 | + const isSchematicTracePipelineSolver = |
| 84 | + solverName === "SchematicTracePipelineSolver" |
| 85 | + |
| 86 | + let content: string |
| 87 | + |
| 88 | + if (isSchematicTracePipelineSolver) { |
| 89 | + content = `import { PipelineDebugger } from "site/components/PipelineDebugger" |
| 90 | +import type { InputProblem } from "lib/types/InputProblem" |
| 91 | +
|
| 92 | +const inputProblem: InputProblem = ${JSON.stringify(params, null, 2)} |
| 93 | +
|
| 94 | +export default () => <PipelineDebugger inputProblem={inputProblem} /> |
| 95 | +` |
| 96 | + } else { |
| 97 | + content = `import { useMemo } from "react" |
| 98 | +import { GenericSolverDebugger } from "../components/GenericSolverDebugger" |
| 99 | +import { ${solverName} } from "lib/solvers/${solverName}/${solverName}" |
| 100 | +
|
| 101 | +export const inputProblem = ${JSON.stringify(params, null, 2)} |
| 102 | +
|
| 103 | +export default () => { |
| 104 | + const solver = useMemo(() => { |
| 105 | + return new ${solverName}(inputProblem as any) |
| 106 | + }, []) |
| 107 | + return <GenericSolverDebugger solver={solver} /> |
| 108 | +} |
| 109 | +` |
| 110 | + } |
| 111 | + |
| 112 | + const blob = new Blob([content], { type: "text/plain" }) |
| 113 | + const url = URL.createObjectURL(blob) |
| 114 | + const a = document.createElement("a") |
| 115 | + a.href = url |
| 116 | + a.download = `${solverName}.page.tsx` |
| 117 | + a.click() |
| 118 | + URL.revokeObjectURL(url) |
| 119 | + } catch (error) { |
| 120 | + alert( |
| 121 | + `Error generating page.tsx for ${solver.constructor.name}: ${error instanceof Error ? error.message : String(error)}`, |
| 122 | + ) |
| 123 | + } |
| 124 | + setIsOpen(false) |
| 125 | + } |
| 126 | + |
| 127 | + const downloadTestTs = () => { |
| 128 | + try { |
| 129 | + const params = deepRemoveUnderscoreProperties( |
| 130 | + solver.getConstructorParams(), |
| 131 | + ) |
| 132 | + const solverName = solver.constructor.name |
| 133 | + |
| 134 | + const content = `import { ${solverName} } from "lib/solvers/${solverName}/${solverName}" |
| 135 | +import { test, expect } from "bun:test" |
| 136 | +
|
| 137 | +test("${solverName} should solve problem correctly", () => { |
| 138 | + const input = ${JSON.stringify(params, null, 2)} |
| 139 | + |
| 140 | + const solver = new ${solverName}(input as any) |
| 141 | + solver.solve() |
| 142 | +
|
| 143 | + expect(solver).toMatchSolverSnapshot(import.meta.path) |
| 144 | + |
| 145 | + // Add more specific assertions based on expected output |
| 146 | + // expect(solver.netLabelPlacementSolver!.netLabelPlacements).toMatchInlineSnapshot() |
| 147 | +}) |
| 148 | +` |
| 149 | + |
| 150 | + const blob = new Blob([content], { type: "text/plain" }) |
| 151 | + const url = URL.createObjectURL(blob) |
| 152 | + const a = document.createElement("a") |
| 153 | + a.href = url |
| 154 | + a.download = `${solverName}.test.ts` |
| 155 | + a.click() |
| 156 | + URL.revokeObjectURL(url) |
| 157 | + } catch (error) { |
| 158 | + alert( |
| 159 | + `Error generating test.ts for ${solver.constructor.name}: ${error instanceof Error ? error.message : String(error)}`, |
| 160 | + ) |
| 161 | + } |
| 162 | + setIsOpen(false) |
| 163 | + } |
| 164 | + |
| 165 | + return ( |
| 166 | + <div className={`relative ${className}`} ref={dropdownRef}> |
| 167 | + <button |
| 168 | + className="px-2 py-1 rounded text-xs cursor-pointer" |
| 169 | + onClick={() => setIsOpen(!isOpen)} |
| 170 | + title={`Download options for ${solver.constructor.name}`} |
| 171 | + > |
| 172 | + {solver.constructor.name} |
| 173 | + </button> |
| 174 | + |
| 175 | + {isOpen && ( |
| 176 | + <div className="absolute top-full left-0 mt-1 bg-white border border-gray-300 rounded shadow-lg z-10 min-w-[150px]"> |
| 177 | + <button |
| 178 | + className="w-full text-left px-3 py-2 hover:bg-gray-100 text-xs" |
| 179 | + onClick={downloadJSON} |
| 180 | + > |
| 181 | + Download JSON |
| 182 | + </button> |
| 183 | + <button |
| 184 | + className="w-full text-left px-3 py-2 hover:bg-gray-100 text-xs" |
| 185 | + onClick={downloadPageTsx} |
| 186 | + > |
| 187 | + Download page.tsx |
| 188 | + </button> |
| 189 | + <button |
| 190 | + className="w-full text-left px-3 py-2 hover:bg-gray-100 text-xs" |
| 191 | + onClick={downloadTestTs} |
| 192 | + > |
| 193 | + Download test.ts |
| 194 | + </button> |
| 195 | + </div> |
| 196 | + )} |
| 197 | + </div> |
| 198 | + ) |
| 199 | +} |
0 commit comments