Fix settings switches to handle missing storage keys safely

This commit is contained in:
kmmiio99o 2025-09-27 21:22:17 +02:00
parent d756744160
commit 9261c983aa

View file

@ -1,4 +1,6 @@
import { plugin } from "@vendetta";
import { storage } from "@vendetta/plugin";
import { useProxy } from "@vendetta/storage";
import { getAssetIDByName } from "@vendetta/ui/assets";
import { React } from "@vendetta/metro/common";
import { findByProps } from "@vendetta/metro";
@ -13,7 +15,7 @@ const { TableRowGroup, TableSwitchRow, Stack, TableRow } = findByProps(
"TableRow",
);
// Initialize default settings if not exist
// Initialize storage with default values
if (!storage.factSettings) {
storage.factSettings = {
sendAsReply: true,
@ -49,7 +51,7 @@ export default function Settings() {
useProxy(storage);
const [rerender, forceRerender] = React.useReducer((x) => x + 1, 0);
// Check for changes when component unmounts
// Check for pending restart when unmounting
React.useEffect(() => {
return () => {
if (storage.pendingRestart) {
@ -67,7 +69,10 @@ export default function Settings() {
return (
<View style={{ flex: 1 }}>
<ScrollView style={{ flex: 1, paddingBottom: 38 }}>
<ScrollView
style={{ flex: 1 }}
contentContainerStyle={{ paddingBottom: 38 }}
>
<Stack spacing={8}>
{/* Facts Commands */}
<TableRowGroup title="Facts Commands">
@ -75,7 +80,7 @@ export default function Settings() {
label="Send as Reply"
subLabel="Send facts as a reply to the command message"
icon={getAssetIDByName("ic_reply_24px")}
value={storage.factSettings.sendAsReply}
value={storage.factSettings?.sendAsReply ?? true}
onValueChange={(v: boolean) => {
storage.factSettings.sendAsReply = v;
forceRerender();
@ -85,7 +90,7 @@ export default function Settings() {
label="Include Source Citation"
subLabel="Include the source of facts when available"
icon={getAssetIDByName("ic_info")}
value={storage.factSettings.includeCitation}
value={storage.factSettings?.includeCitation ?? false}
onValueChange={(v: boolean) => {
storage.factSettings.includeCitation = v;
forceRerender();
@ -95,21 +100,21 @@ export default function Settings() {
label="/catfact"
subLabel="Get random cat facts"
icon={getAssetIDByName("ic_info")}
value={storage.enabledCommands.catfact}
value={storage.enabledCommands?.catfact ?? true}
onValueChange={(v) => handleCommandToggle("catfact", v)}
/>
<TableSwitchRow
label="/dogfact"
subLabel="Get random dog facts"
icon={getAssetIDByName("ic_info")}
value={storage.enabledCommands.dogfact}
value={storage.enabledCommands?.dogfact ?? true}
onValueChange={(v) => handleCommandToggle("dogfact", v)}
/>
<TableSwitchRow
label="/useless"
subLabel="Get random useless facts"
icon={getAssetIDByName("ic_info")}
value={storage.enabledCommands.useless}
value={storage.enabledCommands?.useless ?? true}
onValueChange={(v) => handleCommandToggle("useless", v)}
/>
</TableRowGroup>
@ -120,7 +125,7 @@ export default function Settings() {
label="Always Send Detailed Plugin List"
subLabel="Always use detailed mode when listing plugins"
icon={getAssetIDByName("ic_message_copy")}
value={storage.listSettings.pluginListAlwaysDetailed}
value={storage.listSettings?.pluginListAlwaysDetailed ?? false}
onValueChange={(v: boolean) => {
storage.listSettings.pluginListAlwaysDetailed = v;
forceRerender();
@ -130,7 +135,7 @@ export default function Settings() {
label="Always Send Detailed Theme List"
subLabel="Always use detailed mode when listing themes"
icon={getAssetIDByName("ic_theme")}
value={storage.listSettings.themeListAlwaysDetailed}
value={storage.listSettings?.themeListAlwaysDetailed ?? false}
onValueChange={(v: boolean) => {
storage.listSettings.themeListAlwaysDetailed = v;
forceRerender();
@ -140,14 +145,14 @@ export default function Settings() {
label="/plugin-list"
subLabel="List all installed plugins"
icon={getAssetIDByName("ic_plugins")}
value={storage.enabledCommands.pluginList}
value={storage.enabledCommands?.pluginList ?? true}
onValueChange={(v) => handleCommandToggle("pluginList", v)}
/>
<TableSwitchRow
label="/theme-list"
subLabel="List all installed themes"
icon={getAssetIDByName("ic_theme")}
value={storage.enabledCommands.themeList}
value={storage.enabledCommands?.themeList ?? true}
onValueChange={(v) => handleCommandToggle("themeList", v)}
/>
</TableRowGroup>
@ -158,7 +163,7 @@ export default function Settings() {
label="/petpet"
subLabel="Create pet-pet GIF of a user"
icon={getAssetIDByName("ic_image")}
value={storage.enabledCommands.petpet}
value={storage.enabledCommands?.petpet ?? true}
onValueChange={(v) => handleCommandToggle("petpet", v)}
/>
</TableRowGroup>
@ -169,14 +174,14 @@ export default function Settings() {
label="/konoself"
subLabel="Get random image from KonoChan (private)"
icon={getAssetIDByName("ic_image")}
value={storage.enabledCommands.konoself}
value={storage.enabledCommands?.konoself ?? true}
onValueChange={(v) => handleCommandToggle("konoself", v)}
/>
<TableSwitchRow
label="/konosend"
subLabel="Send random image from KonoChan to channel"
icon={getAssetIDByName("ic_image")}
value={storage.enabledCommands.konosend}
value={storage.enabledCommands?.konosend ?? true}
onValueChange={(v) => handleCommandToggle("konosend", v)}
/>
</TableRowGroup>
@ -209,6 +214,7 @@ export default function Settings() {
<TableRowGroup title="Actions">
<TableRow
label="Reload Discord"
subLabel="Apply command changes"
icon={getAssetIDByName("ic_message_retry")}
onPress={() => BundleUpdaterManager.reload()}
/>