Skip to content

Commit 56a29e4

Browse files
committed
fix: Adding a theme to a preset no longer breaks the button
1 parent ceab36c commit 56a29e4

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

components/Presets/AddThemeToPresetButton.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
import { themeContext } from "@contexts/themeContext";
22
import { Modal, RadioDropdown } from "..";
3-
import { useContext, useState } from "react";
3+
import { useContext, useState, useMemo, useEffect } from "react";
44
import { Flags } from "ThemeTypes";
5-
import { generatePreset, generatePresetFromThemeNames } from "backend";
5+
import { generatePresetFromThemeNames } from "backend";
66
import { twMerge } from "tailwind-merge";
77

88
export function AddThemeToPresetButton() {
99
const { themes, refreshThemes, selectedPreset } = useContext(themeContext);
10-
const dropdownOptions = themes
11-
.filter(
12-
(e) => !selectedPreset?.dependencies.includes(e.name) && !e.flags.includes(Flags.isPreset)
13-
)
14-
.map((e) => e.name);
10+
const dropdownOptions = useMemo(
11+
() =>
12+
themes
13+
.filter(
14+
(e) => !selectedPreset?.dependencies.includes(e.name) && !e.flags.includes(Flags.isPreset)
15+
)
16+
.map((e) => e.name),
17+
[themes]
18+
);
19+
20+
// This fixes the issue where if you selected an option and added it, the value would not reset to a new one
21+
// There may be a way to do this that doesn't involve a useEffect
22+
useEffect(() => {
23+
if (selectedPreset?.dependencies.includes(themeToAdd)) {
24+
setThemeToAdd(dropdownOptions[0]);
25+
}
26+
}, [themes, selectedPreset]);
1527

16-
console.log(themes);
1728
const [themeToAdd, setThemeToAdd] = useState<string>(dropdownOptions[0]);
1829

1930
if (selectedPreset) {

0 commit comments

Comments
 (0)