Skip to content

Commit

Permalink
force timezone user pref at a minimum
Browse files Browse the repository at this point in the history
  • Loading branch information
medentem committed Dec 19, 2024
1 parent b8134ec commit bf54d3e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
4 changes: 3 additions & 1 deletion src/components/CustomizeFirmware.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ export default function CustomizeFirmware(props: CustomizeFirmwareProps) {
if (value === "" || value === "off") {
continue;
}
const option = firmwareCustomizationOptions.find((x) => x.name === key);
options.push({
name: key,
value: (value as string) === "on" ? "true" : (value as string),
type: option?.type,
});
}
console.log(options);
Expand Down Expand Up @@ -132,7 +134,7 @@ export default function CustomizeFirmware(props: CustomizeFirmwareProps) {
<button
type="submit"
disabled={isCompiling}
className={`rounded-md ${isCompiling ? "bg-gray-400 hover:bg-gray-400" : "bg-meshtastic-green hover:bg-meshtastic-green/70"} px-3 py-2 text-sm font-semibold text-gray-800 shadow-sm focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600`}
className={`rounded-md ${isCompiling || hasCustomFirmware() ? "bg-gray-400 hover:bg-gray-400" : "bg-meshtastic-green hover:bg-meshtastic-green/70"} px-3 py-2 text-sm font-semibold text-gray-800 shadow-sm focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600`}
>
{isCompiling
? buildProgress
Expand Down
51 changes: 30 additions & 21 deletions src/stores/firmwareStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,24 @@ export const useFirmwareStore = create<FirmwareState>((set, get) => ({
) => {
set({ isCompiling: true });

if (
options &&
options.length > 0 &&
options.findIndex((x) => x.name === "USERPREFS_TZ_STRING") === -1
) {
options.push({
name: "USERPREFS_TZ_STRING",
value: "tzplaceholder ",
});
}

const json =
options && options.length > 0
? JSON.stringify(
Object.fromEntries(
options.map((opt) => [
opt.name,
isBase64(opt.value)
opt.type === "arrayOfHexValues"
? base64ToHexArrayString(opt.value)
: opt.value,
]),
Expand Down Expand Up @@ -174,27 +185,25 @@ export const useFirmwareStore = create<FirmwareState>((set, get) => ({
},
}));

function isBase64(str: unknown): str is string {
if (typeof str !== "string") return false;
// A simple base64 regex check: base64 typically only contains A-Z, a-z, 0-9, +, / and possibly '=' padding
return /^[A-Za-z0-9+/]+={0,2}$/.test(str);
}

function base64ToHexArrayString(base64Str: string): string {
// Decode Base64 into a binary string
const binaryStr = atob(base64Str);

// Convert each character's char code into a byte (0-255)
const byteValues = [];
for (let i = 0; i < binaryStr.length; i++) {
byteValues.push(binaryStr.charCodeAt(i));
}
try {
// Decode Base64 into a binary string
const binaryStr = atob(base64Str);

// Convert each character's char code into a byte (0-255)
const byteValues = [];
for (let i = 0; i < binaryStr.length; i++) {
byteValues.push(binaryStr.charCodeAt(i));
}

// Convert each byte to a hex string like "0xd4"
const hexValues = byteValues.map(
(byte) => `0x${byte.toString(16).padStart(2, "0")}`,
);
// Convert each byte to a hex string like "0xd4"
const hexValues = byteValues.map(
(byte) => `0x${byte.toString(16).padStart(2, "0")}`,
);

// Join them with commas and wrap with braces
return `{ ${hexValues.join(", ")} }`;
// Join them with commas and wrap with braces
return `{ ${hexValues.join(", ")} }`;
} catch {
return base64Str;
}
}

0 comments on commit bf54d3e

Please sign in to comment.