-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor analytics and deep-linking code
- Loading branch information
1 parent
f054751
commit e581ca2
Showing
6 changed files
with
247 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { dialog } from "electron"; | ||
import { openInMainWindow } from "../utils/openInMainWindow"; | ||
import { getConfig, patchConfig } from "../ipc/config"; | ||
import { | ||
ActionType, | ||
Event, | ||
eventTypeReadableMap, | ||
} from "../../shared/config/config_types"; | ||
|
||
export async function handleOpenWhitelisted( | ||
url: URL, | ||
_groups: RegExpMatchArray | null, | ||
) { | ||
await openInMainWindow(`${url.pathname}${url.search}`); | ||
} | ||
|
||
export async function handlePatchConfig( | ||
url: URL, | ||
_groups: RegExpMatchArray | null, | ||
) { | ||
try { | ||
const config = JSON.parse(url.searchParams.get("config") ?? "{}"); | ||
|
||
if (!config) return; | ||
|
||
const { response } = await dialog.showMessageBox({ | ||
type: "question", | ||
defaultId: 0, | ||
buttons: ["Apply config changes", "Cancel"], | ||
title: "Do you want to apply config changes?", | ||
message: "Do you want to apply config changes?", | ||
detail: `Config patch:\n\n${JSON.stringify(config, null, 2)}`, | ||
}); | ||
|
||
if (response === 1) return; | ||
|
||
await patchConfig(config); | ||
} catch (e) { | ||
dialog.showErrorBox("Error", "Invalid config"); | ||
} | ||
} | ||
|
||
export async function handleAddEvent( | ||
url: URL, | ||
_groups: RegExpMatchArray | null, | ||
) { | ||
const getReadableEvent = (event: Event) => { | ||
const name = event.name; | ||
const triggers = event.triggers | ||
.map((trigger) => eventTypeReadableMap[trigger]) | ||
.join(", "); | ||
|
||
const actions = event.actions | ||
.map((action, index) => { | ||
let actionString = `${index + 1}. ${action.type}`; | ||
switch (action.type) { | ||
case ActionType.On: | ||
actionString += `: ${action.color ? `RGB = ${action.color.r}, ${action.color.g}, ${action.color.b}` : "N/A"}, Brightness = ${action.brightness ?? "N/A"}`; | ||
break; | ||
case ActionType.Off: | ||
break; | ||
case ActionType.Delay: | ||
actionString += `: ${action.delay ?? "N/A"}ms`; | ||
break; | ||
case ActionType.GoBackToCurrentStatus: | ||
break; | ||
default: | ||
break; | ||
} | ||
return actionString; | ||
}) | ||
.join("\n"); | ||
|
||
return `Event:\n\nName: ${name}\nTriggers: ${triggers}\nActions:\n${actions}`; | ||
}; | ||
|
||
try { | ||
const event = JSON.parse(url.searchParams.get("event") ?? "{}"); | ||
|
||
if (!event) return; | ||
|
||
const { response } = await dialog.showMessageBox({ | ||
type: "question", | ||
defaultId: 0, | ||
buttons: ["Apply config changes", "Cancel"], | ||
title: "Add shared event?", | ||
message: "Do you want to add this shared event to your config?", | ||
detail: getReadableEvent(event), | ||
}); | ||
|
||
if (response === 1) return; | ||
|
||
const config = await getConfig(); | ||
const events = config.events ?? []; | ||
const nextId = Math.max(...events.map((e) => e.id)) + 1; | ||
await patchConfig({ | ||
events: [...events, { ...event, id: nextId }], | ||
}); | ||
} catch (e) { | ||
dialog.showErrorBox("Error", "Invalid config"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import path from "path"; | ||
import { app, ipcMain } from "electron"; | ||
import { createMainWindow, mainWindow } from ".."; | ||
import { | ||
handleAddEvent, | ||
handleOpenWhitelisted, | ||
handlePatchConfig, | ||
} from "./app"; | ||
|
||
interface IRoute { | ||
host: string; | ||
match: RegExp; | ||
handler: (uri: URL, groups: RegExpMatchArray | null) => void; | ||
} | ||
|
||
const protocols = ["f1mvlightsintegration", "f1mvli", "f1mvlights"]; | ||
const uriRegex = new RegExp(`^(${protocols.join("|")})://(.*)$`); | ||
|
||
const router: IRoute[] = [ | ||
{ | ||
host: "app", | ||
match: /^\/settings/i, | ||
handler: handleOpenWhitelisted, | ||
}, | ||
{ | ||
host: "app", | ||
match: /^\/config\/patch/i, | ||
handler: handlePatchConfig, | ||
}, | ||
{ | ||
host: "app", | ||
match: /^\/event-editor/i, | ||
handler: handleOpenWhitelisted, | ||
}, | ||
{ | ||
host: "app", | ||
match: /^\/config\/add-event/i, | ||
handler: handleAddEvent, | ||
}, | ||
]; | ||
|
||
function handleOpenURL(event: Electron.Event | undefined, url: string) { | ||
event?.preventDefault(); | ||
|
||
const uri = new URL(url); | ||
const { pathname, host } = uri; | ||
|
||
const route = router.find((r) => r.host === host && r.match.test(pathname)); | ||
if (!route) return; | ||
|
||
route.handler(uri, pathname.match(route.match)); | ||
} | ||
|
||
function handleSecondInstance(event: Electron.Event, argv: string[]) { | ||
if (process.platform === "win32" || process.platform === "linux") { | ||
const uri = argv.find((arg) => uriRegex.test(arg)); | ||
if (uri) handleOpenURL(event, uri); | ||
} | ||
|
||
if (!mainWindow) createMainWindow(); | ||
if (mainWindow?.isMinimized()) mainWindow.restore(); | ||
|
||
mainWindow?.focus(); | ||
} | ||
|
||
export function handleDeepLink(argv: string[]) { | ||
const uri = argv.find((arg) => uriRegex.test(arg)); | ||
if (uri) handleOpenURL(undefined, uri); | ||
} | ||
|
||
export function registerDeepLink() { | ||
try { | ||
if (process.env.NODE_ENV !== "development") { | ||
if (process.defaultApp) { | ||
if (process.argv.length >= 2) { | ||
protocols.forEach((protocol) => { | ||
app.setAsDefaultProtocolClient(protocol, process.execPath, [ | ||
path.resolve(process.argv[1]), | ||
]); | ||
}); | ||
} | ||
} else { | ||
protocols.forEach((protocol) => { | ||
app.setAsDefaultProtocolClient(protocol); | ||
}); | ||
} | ||
} | ||
|
||
app.on("open-url", handleOpenURL); | ||
app.on("second-instance", handleSecondInstance); | ||
|
||
ipcMain.handle("f1mvli:deep-link:open-url", handleOpenURL); | ||
} catch (e: any) { | ||
console.error("Failed to register F1MV Lights Integration protocol:", e); | ||
} | ||
|
||
return () => { | ||
app.off("open-url", handleOpenURL); | ||
app.off("second-instance", handleSecondInstance); | ||
|
||
ipcMain.removeHandler("f1mvli:deep-link:open-url"); | ||
|
||
protocols.forEach((protocol) => { | ||
app.removeAsDefaultProtocolClient(protocol); | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { mainWindow, availablePort } from ".."; | ||
|
||
export async function openInMainWindow(pathname: string) { | ||
const baseUrl = | ||
process.env.VITE_DEV_SERVER_URL || `http://localhost:${availablePort}`; | ||
const url = new URL(baseUrl); | ||
url.hash = `#${pathname}${url.search}`; | ||
await mainWindow?.loadURL(url.toString()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters