-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcommandHandler.js
More file actions
92 lines (82 loc) · 2.44 KB
/
commandHandler.js
File metadata and controls
92 lines (82 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
The command handler that takes in exposed commands to be called by from
the spotlight
*/
// 3rd Party Imports
import { useHotkeys } from "@mantine/hooks"
import { useEffect, useState } from "react"
import { useNavigate } from "react-router"
import { useSettings } from "../../helpers/settings"
import { useRebootCallback } from "../../helpers/useRebootCallback"
let commands = []
export function Commands() {
let navigate = useNavigate()
const [isMac, setIsMac] = useState(false)
const rebootCallback = useRebootCallback()
useEffect(() => {
window.ipcRenderer.invoke("app:is-mac").then((result) => {
setIsMac(result)
})
}, [])
commands = []
const { open } = useSettings()
// Define commands
AddCommand("goto_dashboard", () => {
navigate("/")
})
AddCommand("goto_missions", () => {
navigate("/missions")
})
AddCommand("goto_graphs", () => {
navigate("/graphs")
})
AddCommand("goto_params", () => {
navigate("/params")
})
AddCommand("goto_config", () => {
navigate("/config")
})
AddCommand("goto_fla", () => {
navigate("/fla")
})
AddCommand("force_refresh", () => {
window.ipcRenderer.send("window:force-reload")
})
AddCommand("open_settings", () => {
open()
})
AddCommand("reboot_autopilot", rebootCallback)
// Register hotkeys
useHotkeys([
[isMac ? "meta+1" : "alt+1", () => RunCommand("goto_dashboard")],
[isMac ? "meta+2" : "alt+2", () => RunCommand("goto_missions")],
[isMac ? "meta+3" : "alt+3", () => RunCommand("goto_graphs")],
[isMac ? "meta+4" : "alt+4", () => RunCommand("goto_params")],
[isMac ? "meta+5" : "alt+5", () => RunCommand("goto_config")],
[isMac ? "meta+6" : "alt+6", () => RunCommand("goto_fla")],
["mod+shift+r", () => RunCommand("force_refresh")],
["mod+,", () => RunCommand("open_settings")],
])
}
export function AddCommand(id, command, shortcut = null, macShortcut = null) {
// Used to expose a command
if (commands.find((entry) => entry.id == id) == undefined) {
commands.push({
id: id,
command: command,
shortcut: shortcut,
macShortcut: macShortcut,
})
} else {
console.error(`Attempting to add command that already exists: ${id}`)
}
}
export function RunCommand(id) {
// Search for a command by id
var cmd = commands.find((entry) => entry.id == id)
if (cmd !== undefined) {
cmd.command()
} else {
console.error(`Couldn't find command ${id} to run`)
}
}