Skip to content

Commit 28a5c58

Browse files
committed
Refactor useColorsDecoder
1 parent a3e9ad5 commit 28a5c58

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

client/src/App.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,15 @@ const App: React.FC = () => {
136136

137137
const { preferredTheme, isDarkMode, unscheduleClassesByDefault, convertToLocalTimezone } = useGetUserSettingsQuery();
138138

139-
const decodedAssignedColors = useColorsDecoder(assignedColors, preferredTheme);
139+
const decodedColors = useColorsDecoder(Object.values(assignedColors), preferredTheme);
140+
const decodedAssignedColors = useMemo(
141+
() =>
142+
Object.keys(assignedColors).reduce<Record<string, string>>((object, key, index) => {
143+
object[key] = decodedColors[index];
144+
return object;
145+
}, {}),
146+
[assignedColors, decodedColors],
147+
);
140148

141149
setDropzoneRange(days.length, earliestStartTime, latestEndTime);
142150

client/src/components/controls/ColorOptions.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ const ColorOptions: FC<ColorOptionsProps> = ({
4040
[isDarkMode, preferredTheme],
4141
);
4242

43-
const decodedColors = useColorsDecoder(
44-
colors.reduce((obj, color, index) => ({ ...obj, [index]: color }), {}),
45-
preferredTheme,
46-
);
43+
const decodedColors = useColorsDecoder(colors, preferredTheme);
4744

4845
return (
4946
<List sx={{ display: 'flex', flexDirection: 'column', gap: 1 }}>

client/src/hooks/useColorDecoder.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,15 @@ const decodeColor = (assignedColor: string, preferredTheme: string) => {
1111
};
1212

1313
/**
14-
* Decodes all assigned colours using the `useColorDecoder` function.
15-
* Converts each assigned colour key to its corresponding color value in the current or preview theme.
14+
* Decodes all colours using the `useColorDecoder` function.
15+
* Converts each colour to its corresponding color value in the current or preview theme.
1616
*
17-
* @param {Record<string, string>} assignedColors A record of assigned colour keys (e.g., { event1: 'default-1' }).
17+
* @param {string[]} colors A record of assigned colour keys (e.g., { event1: 'default-1' }).
1818
* @param {string} [previewTheme] An optional theme to use for decoding instead of the current theme.
19-
* @returns {Record<string, string>} A record of decoded colour values (e.g., { event1: 'oklch(0.8 0.1 200)' }).
19+
* @returns {string[]} A record of decoded colour values (e.g., { event1: 'oklch(0.8 0.1 200)' }).
2020
*/
21-
export const useColorsDecoder = (assignedColors: Record<string, string>, preferredTheme: string) => {
22-
const decodedColors = Object.fromEntries(
23-
Object.entries(assignedColors).map(([key, color]) => {
24-
const decodedColor = decodeColor(color, preferredTheme);
25-
return [key, decodedColor];
26-
}),
27-
);
28-
29-
return decodedColors;
21+
export const useColorsDecoder = (colors: string[], preferredTheme: string): string[] => {
22+
return colors.map((color) => decodeColor(color, preferredTheme));
3023
};
3124

3225
/**

0 commit comments

Comments
 (0)