From 6a16c8ce90d485cb46ebc130de0776b958ddf396 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Wed, 28 Feb 2024 16:26:25 -0800 Subject: [PATCH] Chrysalis will now backup your configuration as a downloaded json file every time you update your firmware --- product-status.md | 1 + .../Sidebar/LayoutSharing/ExportToFile.js | 51 ++++--------------- src/renderer/screens/FirmwareUpdate.js | 4 +- .../utils/exportKeyboardConfigToFile.js | 38 ++++++++++++++ 4 files changed, 53 insertions(+), 41 deletions(-) create mode 100644 src/renderer/utils/exportKeyboardConfigToFile.js diff --git a/product-status.md b/product-status.md index afa0f7469..14995458f 100644 --- a/product-status.md +++ b/product-status.md @@ -32,6 +32,7 @@ Chrysalis requires a browser with WebSerial support. Right now, this means Chrom ## Recent updates +- Chrysalis will now back up your configuration as a download every time you update your firmware - "Report an issue" now provides system logs for reports - Corrections for some incorrect key identifiers, particularly for dynamic macros diff --git a/src/renderer/screens/Editor/Sidebar/LayoutSharing/ExportToFile.js b/src/renderer/screens/Editor/Sidebar/LayoutSharing/ExportToFile.js index 66cbd2bd6..fb98d9e9e 100644 --- a/src/renderer/screens/Editor/Sidebar/LayoutSharing/ExportToFile.js +++ b/src/renderer/screens/Editor/Sidebar/LayoutSharing/ExportToFile.js @@ -1,51 +1,22 @@ import Box from "@mui/material/Box"; import Button from "@mui/material/Button"; -import { toast } from "@renderer/components/Toast"; -import jsonStringify from "json-stringify-pretty-compact"; import React from "react"; import { useTranslation } from "react-i18next"; -import Focus from "@api/focus"; +import exportKeyboardConfigToFile from "@renderer/utils/exportKeyboardConfigToFile"; +import { GlobalContext } from "@renderer/components/GlobalContext"; + export const ExportToFile = (props) => { const { t } = useTranslation(); - - const focus = new Focus(); - - const exportToFile = async () => { - const { keymap, colormap } = props; - const backupData = await focus.readKeyboardConfiguration(); - // delete the eeprom contents - delete backupData["eeprom.contents"]; - const data = { - keymaps: keymap.custom, - colormaps: colormap.colorMap, - palette: colormap.palette, - deviceConfiguration: backupData, - }; - const content = jsonStringify(data); - const blob = new Blob([content], { type: "application/json" }); - const url = URL.createObjectURL(blob); - - const link = document.createElement("a"); - link.href = url; - const currentDate = new Date(); - const year = currentDate.getFullYear(); - const month = String(currentDate.getMonth() + 1).padStart(2, "0"); // Months are 0-based - const day = String(currentDate.getDate()).padStart(2, "0"); - const hours = String(currentDate.getHours()).padStart(2, "0"); - const minutes = String(currentDate.getMinutes()).padStart(2, "0"); - const seconds = String(currentDate.getSeconds()).padStart(2, "0"); - - const device_name = focus.focusDeviceDescriptor.info.displayName.replace(/ /g, "-"); - - link.download = `Chrysalis_${device_name}_layout_${year}-${month}-${day}_${hours}-${minutes}-${seconds}.json`; - link.click(); - - URL.revokeObjectURL(url); - }; - + const globalContext = React.useContext(GlobalContext); + const [activeDevice] = globalContext.state.activeDevice; return ( - diff --git a/src/renderer/screens/FirmwareUpdate.js b/src/renderer/screens/FirmwareUpdate.js index 46ca37025..31db7024f 100644 --- a/src/renderer/screens/FirmwareUpdate.js +++ b/src/renderer/screens/FirmwareUpdate.js @@ -45,13 +45,14 @@ import { FlashNotification } from "./FirmwareUpdate/FlashNotification"; import FlashSteps from "./FirmwareUpdate/FlashSteps"; import UpdateDescription from "./FirmwareUpdate/UpdateDescription"; import logger from "@renderer/utils/Logger"; +import exportKeyboardConfigToFile from "../utils/exportKeyboardConfigToFile"; const delay = (ms) => new Promise((res) => setTimeout(res, ms)); const FirmwareUpdate = (props) => { const focus = new Focus(); const { state } = useContext(GlobalContext); - const [activeDevice, setActiveDevice] = state.state.activeDevice; + const [activeDevice, setActiveDevice] = state.activeDevice; const [usbDevice, setUsbDevice] = useState(null); const [firmwareFilename, setFirmwareFilename] = useState(""); @@ -162,6 +163,7 @@ const FirmwareUpdate = (props) => { } else { logger.log("about to save eeprom"); await onStepChange("saveEEPROM"); + await exportKeyboardConfigToFile(activeDevice); const saveKey = await activeDevice.saveEEPROM(); logger.log("Done saving eeprom"); await onStepChange("bootloader"); diff --git a/src/renderer/utils/exportKeyboardConfigToFile.js b/src/renderer/utils/exportKeyboardConfigToFile.js new file mode 100644 index 000000000..5fbfac20a --- /dev/null +++ b/src/renderer/utils/exportKeyboardConfigToFile.js @@ -0,0 +1,38 @@ +import jsonStringify from "json-stringify-pretty-compact"; + +const exportKeyboardConfigToFile = async (activeDevice) => { + const keymap = await activeDevice.keymap(); + const colormap = await activeDevice.colormap(); + + const backupData = await activeDevice.focus.readKeyboardConfiguration(); + // delete the eeprom contents + delete backupData["eeprom.contents"]; + const data = { + keymaps: keymap.custom, + colormaps: colormap.colorMap, + palette: colormap.palette, + deviceConfiguration: backupData, + }; + const content = jsonStringify(data); + const blob = new Blob([content], { type: "application/json" }); + const url = URL.createObjectURL(blob); + + const link = document.createElement("a"); + link.href = url; + const currentDate = new Date(); + const year = currentDate.getFullYear(); + const month = String(currentDate.getMonth() + 1).padStart(2, "0"); // Months are 0-based + const day = String(currentDate.getDate()).padStart(2, "0"); + const hours = String(currentDate.getHours()).padStart(2, "0"); + const minutes = String(currentDate.getMinutes()).padStart(2, "0"); + const seconds = String(currentDate.getSeconds()).padStart(2, "0"); + + const device_name = activeDevice.focus.focusDeviceDescriptor.info.displayName.replace(/ /g, "-"); + + link.download = `Chrysalis_${device_name}_layout_${year}-${month}-${day}_${hours}-${minutes}-${seconds}.json`; + link.click(); + + URL.revokeObjectURL(url); +}; + +export default exportKeyboardConfigToFile;