From f36e5a3a81dfd7396a56ba4de6da65470231b137 Mon Sep 17 00:00:00 2001 From: Elvy <88881326+EepyElvyra@users.noreply.github.com> Date: Sat, 4 Jan 2025 18:35:34 +0100 Subject: [PATCH] do it nicer --- .../components/SettingArrayComponent.tsx | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/components/PluginSettings/components/SettingArrayComponent.tsx b/src/components/PluginSettings/components/SettingArrayComponent.tsx index 002ee1b94..abcecae18 100644 --- a/src/components/PluginSettings/components/SettingArrayComponent.tsx +++ b/src/components/PluginSettings/components/SettingArrayComponent.tsx @@ -11,7 +11,7 @@ import { Margins } from "@utils/margins"; import { wordsFromCamel, wordsToTitle } from "@utils/text"; import { OptionType, PluginOptionList } from "@utils/types"; import { findByCodeLazy, findComponentByCodeLazy } from "@webpack"; -import { Avatar, Button, ChannelStore, Forms, GuildStore, IconUtils, React, Text, TextInput, useState } from "@webpack/common"; +import { Avatar, Button, ChannelStore, Forms, GuildStore, IconUtils, React, Text, TextInput, useEffect, useState } from "@webpack/common"; import { Channel, Guild } from "discord-types/general"; import { JSX } from "react"; @@ -52,9 +52,12 @@ export function SettingArrayComponent({ id }: ISettingElementProps) { const [error, setError] = useState(null); - const [items, setItems] = useState([]); + useEffect(() => { + pluginSettings[id] = items; + }, [items, pluginSettings, id]); + if (items.length === 0 && pluginSettings[id].length !== 0) { setItems(pluginSettings[id]); } @@ -89,9 +92,7 @@ export function SettingArrayComponent({ pluginSettings[id] = []; return; } - const newItems = items.filter((_, i) => i !== index); - setItems(newItems); - pluginSettings[id] = newItems; + setItems(items.filter((_, i) => i !== index)); }; function renderGuildView() { @@ -254,11 +255,8 @@ export function SettingArrayComponent({ return; } - const newItems = [...items, inputElement.value]; + setItems([...items, inputElement.value]); - setItems(newItems); - - pluginSettings[id] = newItems; inputElement.value = ""; }