import { useState } from "react"; import { resolveResource } from "@tauri-apps/api/path"; import { readTextFile } from "@tauri-apps/api/fs"; import { invoke } from '@tauri-apps/api/tauri' import Modal from "react-modal"; import { platform } from '@tauri-apps/api/os'; import "./App.css"; import { Command } from '@tauri-apps/api/shell' import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faGitAlt } from "@fortawesome/free-brands-svg-icons/faGitAlt"; import { PhysicalPosition, appWindow } from "@tauri-apps/api/window"; // TODO: Keyboard usage // const codes = JSON.parse(await readTextFile(await resolveResource("resources/codes.json"))); type Code = { code: number; type: string; text: string; }; // Set window to above the mouse cursor // TODO: Fix for MacOS invoke('get_mouse_pos', {}).then((posJson: any) => { let pos = JSON.parse(posJson); appWindow.innerSize().then((size: any) => { pos.y -= size.height + 32 /*Standard windows titlebar height*/; console.log(JSON.stringify(pos)); appWindow.setPosition(new PhysicalPosition(pos.x, pos.y)); }); }); Modal.setAppElement("#root"); function App() { const [codes, setCodes] = useState>(); const [subTopic, setSubTopic] = useState(""); const [configModalOpen, setConfigModalOpen] = useState(false); const [droneId, setDroneId] = useState(() => { let storedId = localStorage.getItem("droneId"); return (storedId ? storedId : "0000"); }); if (!codes){ resolveResource("resources/codes.json").then((path: string) => { readTextFile(path).then((codesJson: string) => { setCodes(JSON.parse(codesJson)); }); }); } const getSubjects = (input: Array) => { // TODO: Put all single-level elements at bottom let subjects: Array = []; input.forEach((code: Code) => { if (!subjects.includes(code.type)) { subjects.push(code.type) } }); return subjects; }; const getTopicChildren = (topic: string) => { let topicChildren: Array = []; if (codes){ codes.forEach((code: Code) => { if (code.type == topic) { if (!topicChildren.includes(code.text)) { topicChildren.push(code.text) } } }); } return topicChildren; }; const generatePayload = (code: Code) => { let assembledStr: string = droneId.toString(); let codeId: string = code.code.toString(); if (code.code < 10) { codeId = "00" + codeId; }else if (code.code < 100) { codeId = "0" + codeId; } assembledStr += " :: Code " + codeId; assembledStr += " :: " + code.type; if (code.text != "."){ assembledStr += " :: " + code.text; } console.log(assembledStr); return assembledStr; }; const handleSubClick = (topic: string, index: number) => { if (codes){ let text = getTopicChildren(topic)[index]; let filtered = codes.filter((code: Code) => code.type === topic && code.text === text); if (filtered.length == 1) { let payload: string = generatePayload(filtered[0]); setSubTopic(""); invoke('type_str', {input: payload}); } } }; const SubMenu = () => { if (subTopic == "") { return (
); } else { return (
{getTopicChildren(subTopic).map((text: string, index: number, topics: Array) => { let displayText: string = text; displayText = displayText.replace(/.*:: /, ""); if (displayText == "") { displayText = "..."; } return(
{ handleSubClick(subTopic, index) }} > {displayText}
) })}
); } } const handleTopicClick = (type: string) => { if (codes) { let filtered = codes.filter((code: Code) => code.type === type); if (filtered.length == 1) { let assembledStr: string = generatePayload(filtered[0]); setSubTopic(""); invoke('type_str', {input: assembledStr}); }else{ // Open Submenu for type setSubTopic(type); } } }; const buildMenu = () => { if (codes) { return (
{getSubjects(codes).map((type: string, index: number) => { let dispType: string = type; let filtered = codes.filter((code: Code) => code.type === type); if (filtered.length == 1) { dispType += ((filtered[0].text == "")?" :: ...":((filtered[0].text == ".")?"":(" :: "+filtered[0].text))); } return ( { handleTopicClick(type) }} value={dispType} /> ); })}
); }else{ return (
); } }; return (

Hexcorp Drone Interface

Drone ID
{ setDroneId(e.target.value); localStorage.setItem("droneId", e.target.value); }} />
{/* TODO: Optional auto-send on pre-made messages */}
{ let url = "https://git.corrupt.link/liz/AllenWrench"; platform().then((os: string) => { switch(os) { case "win32": new Command("open-link-win", ["-Command", "Start-Process", `${url}`]).spawn(); break; case "linux": new Command("open-link-linux", ["-c", "xdg-open", `${url}`]).spawn(); break; case "macos": new Command("open-link-macos", ["-c", "open", `${url}`]).spawn(); break; default: console.log(`Unknown OS: ${os}`); break; } setConfigModalOpen(false); }); }}>
{ setConfigModalOpen(false); }}> Close
{buildMenu()}
{ setSubTopic(""); console.log(`Config modal open ${configModalOpen}`); setConfigModalOpen(!configModalOpen); }}> {droneId}
); } export default App;