import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import TrayPanel from "./components/TrayPanel";
import "./index.css";

// The webview's own context menu offers Reload, Back and Inspect — page
// actions in something that is not meant to read as a page. Text fields keep
// theirs so copy and paste stay reachable.
document.addEventListener("contextmenu", (e) => {
  const el = e.target as HTMLElement | null;
  const editable =
    el?.closest("input, textarea, [contenteditable='true']") !== null &&
    el?.closest("input, textarea, [contenteditable='true']") !== undefined;
  if (!editable) e.preventDefault();
});

// The menu bar panel is a second window on the same bundle, told apart by
// the hash it is opened with.
const panel = window.location.hash === "#tray";

ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
  <React.StrictMode>{panel ? <TrayPanel /> : <App />}</React.StrictMode>,
);
