|
| 1 | +import ActionBar from "src/features/action-bar/action-bar"; |
| 2 | +import Tray from "src/features/tray/tray"; |
| 3 | +import crel from "crel"; |
| 4 | +import limitTrayHeight from "src/utils/limit-tray-height"; |
| 5 | +import stateHandler from "src/utils/state"; |
| 6 | +import tracer from "src/utils/tracer.js"; |
| 7 | + |
| 8 | +export default function App({ root, stateId, actions, actionsCorner, hardReload }) { |
| 9 | + const state = stateHandler(stateId); |
| 10 | + |
| 11 | + // Default tray height |
| 12 | + let trayHeight = state.getCache("tray-height", window.innerHeight * 0.25); |
| 13 | + trayHeight = limitTrayHeight(trayHeight, !!state.get("actions")); |
| 14 | + |
| 15 | + // SET STATE |
| 16 | + |
| 17 | + state.set("log", []); |
| 18 | + state.set("actions", actions); |
| 19 | + state.set("actions-corner", actionsCorner); |
| 20 | + state.set("reload-action-should-refresh-cache", hardReload); |
| 21 | + state.setCache("tray-open", state.getCache("tray-open", true)); |
| 22 | + state.setCache("tray-height", trayHeight); |
| 23 | + |
| 24 | + /** |
| 25 | + * ACTIONS |
| 26 | + */ |
| 27 | + |
| 28 | + function toggleTray() { |
| 29 | + state.setCache("tray-open", !state.getCache("tray-open")); |
| 30 | + render(); |
| 31 | + } |
| 32 | + |
| 33 | + function resizeTray(offset) { |
| 34 | + const height = limitTrayHeight(state.getCache("tray-height") + offset, !!state.get("actions")); |
| 35 | + |
| 36 | + if (height !== state.getCache("tray-height")) { |
| 37 | + state.setCache("tray-height", height); |
| 38 | + render(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + function addLogEntry({ message, filePath, fileName, lineNumber, type } = {}) { |
| 43 | + const log = state.get("log"); |
| 44 | + const entry = { message, filePath, fileName, lineNumber, type, amount: 1 }; |
| 45 | + const lastEntry = log[log.length - 1]; |
| 46 | + |
| 47 | + if (lastEntry && lastEntry.message === message) { |
| 48 | + lastEntry.amount = lastEntry.amount + 1; |
| 49 | + log[log.length - 1] = lastEntry; |
| 50 | + } else { |
| 51 | + log.push(entry); |
| 52 | + } |
| 53 | + |
| 54 | + state.set("log", log); |
| 55 | + render(); |
| 56 | + } |
| 57 | + |
| 58 | + // In the event the window is resized |
| 59 | + window.addEventListener("resize", () => { |
| 60 | + resizeTray(state.getCache("tray-height")); |
| 61 | + }); |
| 62 | + |
| 63 | + // Display logs |
| 64 | + const log = console.log; |
| 65 | + console.log = message => { |
| 66 | + log(message); |
| 67 | + const { filePath, fileName, lineNumber } = tracer(new Error()); |
| 68 | + addLogEntry({ message, filePath, fileName, lineNumber }); |
| 69 | + }; |
| 70 | + const error = console.error; |
| 71 | + console.error = message => { |
| 72 | + error(message); |
| 73 | + const { filePath, fileName, lineNumber } = tracer(new Error()); |
| 74 | + addLogEntry({ message, filePath, fileName, lineNumber, type: "error" }); |
| 75 | + }; |
| 76 | + const assert = console.assert; |
| 77 | + console.assert = (assertion, message) => { |
| 78 | + assert(assertion, message); |
| 79 | + |
| 80 | + if (!assertion) { |
| 81 | + message = |
| 82 | + "Assertion failed: " + (typeof message !== "undefined" ? message : "console.assert"); |
| 83 | + const { filePath, fileName, lineNumber } = tracer(new Error()); |
| 84 | + addLogEntry({ message, filePath, fileName, lineNumber, type: "error" }); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + // Display error messages |
| 89 | + window.onerror = (message, filePath, lineNumber) => { |
| 90 | + addLogEntry({ message, filePath, fileName: "", lineNumber, type: "error" }); |
| 91 | + }; |
| 92 | + |
| 93 | + // render |
| 94 | + |
| 95 | + function render() { |
| 96 | + root.innerHTML = ""; |
| 97 | + |
| 98 | + // render action bar |
| 99 | + crel( |
| 100 | + root, |
| 101 | + ActionBar({ |
| 102 | + actions, |
| 103 | + corner: state.get("actions-corner"), |
| 104 | + shouldRefreshCache: state.get("reload-action-should-refresh-cache"), |
| 105 | + trayIsOpen: state.getCache("tray-open"), |
| 106 | + onToggleTray: toggleTray |
| 107 | + }) |
| 108 | + ); |
| 109 | + |
| 110 | + // render tray |
| 111 | + if (state.get("actions").includes("toggle-tray")) { |
| 112 | + crel( |
| 113 | + root, |
| 114 | + Tray({ |
| 115 | + state, |
| 116 | + trayIsOpen: state.getCache("tray-open"), |
| 117 | + trayHeight: state.getCache("tray-height"), |
| 118 | + log: state.get("log"), |
| 119 | + onResizeTray: resizeTray |
| 120 | + }) |
| 121 | + ); |
| 122 | + } |
| 123 | + } |
| 124 | + render(); |
| 125 | +} |
0 commit comments