diff --git a/src/client/index.tsx b/src/client/index.tsx index ffe52ce8a..a7af06def 100644 --- a/src/client/index.tsx +++ b/src/client/index.tsx @@ -2,7 +2,6 @@ import { initializeSite, setupDateFns } from "@utils/app"; import { hydrate } from "inferno-hydrate"; import { BrowserRouter } from "inferno-router"; import { App } from "../shared/components/app/app"; -import { UserService } from "../shared/services"; import "bootstrap/js/dist/collapse"; import "bootstrap/js/dist/dropdown"; @@ -15,7 +14,7 @@ async function startClient() { const wrapper = ( - + ); diff --git a/src/server/handlers/catch-all-handler.tsx b/src/server/handlers/catch-all-handler.tsx index 3fd9327a8..aacbc21dd 100644 --- a/src/server/handlers/catch-all-handler.tsx +++ b/src/server/handlers/catch-all-handler.tsx @@ -117,7 +117,7 @@ export default async (req: Request, res: Response) => { const wrapper = ( - + ); diff --git a/src/shared/components/app/app.tsx b/src/shared/components/app/app.tsx index c573ea41a..5709f7669 100644 --- a/src/shared/components/app/app.tsx +++ b/src/shared/components/app/app.tsx @@ -3,10 +3,9 @@ import { dataBsTheme } from "@utils/browser"; import { Component, RefObject, createRef, linkEvent } from "inferno"; import { Provider } from "inferno-i18next-dess"; import { Route, Switch } from "inferno-router"; -import { MyUserInfo } from "lemmy-js-client"; import { IsoDataOptionalSite } from "../../interfaces"; import { routes } from "../../routes"; -import { FirstLoadService, I18NextService, UserService } from "../../services"; +import { FirstLoadService, I18NextService } from "../../services"; import AuthGuard from "../common/auth-guard"; import ErrorGuard from "../common/error-guard"; import { ErrorPage } from "./error-page"; @@ -17,14 +16,10 @@ import { Theme } from "./theme"; import AnonymousGuard from "../common/anonymous-guard"; import { CodeTheme } from "./code-theme"; -interface AppProps { - user?: MyUserInfo; -} - -export class App extends Component { +export class App extends Component { private isoData: IsoDataOptionalSite = setIsoData(this.context); private readonly mainContentRef: RefObject; - constructor(props: AppProps, context: any) { + constructor(props: any, context: any) { super(props, context); this.mainContentRef = createRef(); } @@ -34,8 +29,6 @@ export class App extends Component { this.mainContentRef.current?.focus(); } - user = UserService.Instance.myUserInfo; - render() { const siteRes = this.isoData.site_res; const siteView = siteRes?.site_view; @@ -46,7 +39,7 @@ export class App extends Component { { } = siteRes.data.my_user!.local_user_view; UserService.Instance.myUserInfo = siteRes.data.my_user; + updateDataBsTheme(siteRes.data); i.setState(prev => ({ ...prev, diff --git a/src/shared/utils/app/initialize-site.ts b/src/shared/utils/app/initialize-site.ts index ac6d9cb61..565067032 100644 --- a/src/shared/utils/app/initialize-site.ts +++ b/src/shared/utils/app/initialize-site.ts @@ -1,9 +1,11 @@ import { GetSiteResponse } from "lemmy-js-client"; import { setupEmojiDataModel, setupMarkdown } from "../../markdown"; import { I18NextService, UserService } from "../../services"; +import { updateDataBsTheme } from "@utils/browser"; export default function initializeSite(site?: GetSiteResponse) { UserService.Instance.myUserInfo = site?.my_user; + updateDataBsTheme(site); I18NextService.i18n.changeLanguage(); if (site) { setupEmojiDataModel(site.custom_emojis ?? []); diff --git a/src/shared/utils/app/set-theme.ts b/src/shared/utils/app/set-theme.ts index 9c6488f4e..348e0ba6d 100644 --- a/src/shared/utils/app/set-theme.ts +++ b/src/shared/utils/app/set-theme.ts @@ -1,5 +1,5 @@ import { fetchThemeList } from "@utils/app"; -import { isBrowser, loadCss } from "@utils/browser"; +import { dataBsTheme, isBrowser, loadCss } from "@utils/browser"; export default async function setTheme(theme: string, forceReload = false) { if (!isBrowser()) { @@ -32,5 +32,8 @@ export default async function setTheme(theme: string, forceReload = false) { const cssLoc = `/css/themes/${theme}.css`; loadCss(theme, cssLoc); + document + .getElementById("app") + ?.setAttribute("data-bs-theme", dataBsTheme(theme)); document.getElementById(theme)?.removeAttribute("disabled"); } diff --git a/src/shared/utils/browser/data-bs-theme.ts b/src/shared/utils/browser/data-bs-theme.ts index 974333668..7ae0b69b5 100644 --- a/src/shared/utils/browser/data-bs-theme.ts +++ b/src/shared/utils/browser/data-bs-theme.ts @@ -1,17 +1,23 @@ -import { MyUserInfo } from "lemmy-js-client"; +import { GetSiteResponse } from "lemmy-js-client"; import isDark from "./is-dark"; -export default function dataBsTheme(user?: MyUserInfo) { - return (isDark() && user?.local_user_view.local_user.theme === "browser") || - (user && - [ - "darkly", - "darkly-red", - "darkly-pureblack", - "darkly-compact", - "i386", - "vaporwave-dark", - ].includes(user.local_user_view.local_user.theme)) +export default function dataBsTheme(siteResOrTheme?: GetSiteResponse | string) { + const theme = + typeof siteResOrTheme === "string" + ? siteResOrTheme + : siteResOrTheme?.my_user?.local_user_view.local_user.theme ?? + siteResOrTheme?.site_view.local_site.default_theme ?? + "browser"; + + return (isDark() && theme === "browser") || + [ + "darkly", + "darkly-red", + "darkly-pureblack", + "darkly-compact", + "i386", + "vaporwave-dark", + ].includes(theme) ? "dark" : "light"; } diff --git a/src/shared/utils/browser/index.ts b/src/shared/utils/browser/index.ts index 41701207e..8aac65aa3 100644 --- a/src/shared/utils/browser/index.ts +++ b/src/shared/utils/browser/index.ts @@ -9,6 +9,7 @@ import restoreScrollPosition from "./restore-scroll-position"; import saveScrollPosition from "./save-scroll-position"; import setAuthCookie from "./set-auth-cookie"; import share from "./share"; +import updateDataBsTheme from "./update-data-bs-theme"; export { canShare, @@ -22,4 +23,5 @@ export { saveScrollPosition, setAuthCookie, share, + updateDataBsTheme, }; diff --git a/src/shared/utils/browser/update-data-bs-theme.ts b/src/shared/utils/browser/update-data-bs-theme.ts new file mode 100644 index 000000000..a277572bb --- /dev/null +++ b/src/shared/utils/browser/update-data-bs-theme.ts @@ -0,0 +1,11 @@ +import { GetSiteResponse } from "lemmy-js-client"; +import isBrowser from "./is-browser"; +import dataBsTheme from "./data-bs-theme"; + +export default function updateDataBsTheme(siteRes?: GetSiteResponse) { + if (isBrowser()) { + document + .getElementById("app") + ?.setAttribute("data-bs-theme", dataBsTheme(siteRes)); + } +}