Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WEB-1431] fix: update custom theme post sign-in #4586

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions web/helpers/theme.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ const calculateShades = (hexValue: string): TShades => {
return shades as TShades;
};

export const applyTheme = (palette: string, isDarkPalette: boolean) => {
export const applyTheme = (palette: string, isDarkPalette: boolean, dom: HTMLElement | null) => {
if (!palette) return;
const dom = document?.querySelector<HTMLElement>("[data-theme='custom']");
// palette: [bg, text, primary, sidebarBg, sidebarText]
const values: string[] = palette.split(",");
values.push(isDarkPalette ? "dark" : "light");
Expand Down
35 changes: 28 additions & 7 deletions web/lib/wrappers/store-wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const StoreWrapper: FC<TStoreWrapper> = observer((props) => {
const { sidebarCollapsed, toggleSidebar } = useAppTheme();
const { data: userProfile } = useUserProfile();
// states
const [dom, setDom] = useState<undefined | HTMLElement>();
const [dom, setDom] = useState<HTMLElement | null>(null);

/**
* Sidebar collapsed fetching from local storage
Expand All @@ -38,19 +38,40 @@ const StoreWrapper: FC<TStoreWrapper> = observer((props) => {
* Setting up the theme of the user by fetching it from local storage
*/
useEffect(() => {
setTheme(userProfile?.theme?.theme || "system");
if (!userProfile?.theme?.theme) return;
if (window) setDom(() => window.document?.querySelector<HTMLElement>("[data-theme='custom']") || undefined);

setTheme(userProfile?.theme?.theme || "system");
if (userProfile?.theme?.theme === "custom" && userProfile?.theme?.palette && dom)
if (userProfile?.theme?.theme === "custom" && userProfile?.theme?.palette) {
applyTheme(
userProfile?.theme?.palette !== ",,,,"
? userProfile?.theme?.palette
: "#0d101b,#c5c5c5,#3f76ff,#0d101b,#c5c5c5",
false
false,
dom
);
else unsetCustomCssVariables();
}, [userProfile, userProfile?.theme?.theme, userProfile?.theme?.palette, setTheme, dom]);
} else unsetCustomCssVariables();
}, [userProfile, userProfile?.theme, userProfile?.theme?.palette, setTheme, dom]);

useEffect(() => {
if (dom) return;

const observer = new MutationObserver((mutationsList, observer) => {
for (const mutation of mutationsList) {
if (mutation.type === "childList") {
const customThemeElement = window.document?.querySelector<HTMLElement>("[data-theme='custom']");
if (customThemeElement) {
setDom(customThemeElement);
observer.disconnect();
break;
}
}
}
});

observer.observe(document.body, { childList: true, subtree: true });

return () => observer.disconnect();
}, [dom]);

useEffect(() => {
if (!router.query) return;
Expand Down
Loading