Update bs theme when site theme is changed (#2226)

This commit is contained in:
SleeplessOne1917 2023-11-17 14:35:43 +00:00 committed by GitHub
parent 8485a2c5dc
commit dd3842655a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 47 additions and 28 deletions

View file

@ -2,7 +2,6 @@ import { initializeSite, setupDateFns } from "@utils/app";
import { hydrate } from "inferno-hydrate"; import { hydrate } from "inferno-hydrate";
import { BrowserRouter } from "inferno-router"; import { BrowserRouter } from "inferno-router";
import { App } from "../shared/components/app/app"; import { App } from "../shared/components/app/app";
import { UserService } from "../shared/services";
import "bootstrap/js/dist/collapse"; import "bootstrap/js/dist/collapse";
import "bootstrap/js/dist/dropdown"; import "bootstrap/js/dist/dropdown";
@ -15,7 +14,7 @@ async function startClient() {
const wrapper = ( const wrapper = (
<BrowserRouter> <BrowserRouter>
<App user={UserService.Instance.myUserInfo} /> <App />
</BrowserRouter> </BrowserRouter>
); );

View file

@ -117,7 +117,7 @@ export default async (req: Request, res: Response) => {
const wrapper = ( const wrapper = (
<StaticRouter location={url} context={isoData}> <StaticRouter location={url} context={isoData}>
<App user={site?.my_user} /> <App />
</StaticRouter> </StaticRouter>
); );

View file

@ -3,10 +3,9 @@ import { dataBsTheme } from "@utils/browser";
import { Component, RefObject, createRef, linkEvent } from "inferno"; import { Component, RefObject, createRef, linkEvent } from "inferno";
import { Provider } from "inferno-i18next-dess"; import { Provider } from "inferno-i18next-dess";
import { Route, Switch } from "inferno-router"; import { Route, Switch } from "inferno-router";
import { MyUserInfo } from "lemmy-js-client";
import { IsoDataOptionalSite } from "../../interfaces"; import { IsoDataOptionalSite } from "../../interfaces";
import { routes } from "../../routes"; import { routes } from "../../routes";
import { FirstLoadService, I18NextService, UserService } from "../../services"; import { FirstLoadService, I18NextService } from "../../services";
import AuthGuard from "../common/auth-guard"; import AuthGuard from "../common/auth-guard";
import ErrorGuard from "../common/error-guard"; import ErrorGuard from "../common/error-guard";
import { ErrorPage } from "./error-page"; import { ErrorPage } from "./error-page";
@ -17,14 +16,10 @@ import { Theme } from "./theme";
import AnonymousGuard from "../common/anonymous-guard"; import AnonymousGuard from "../common/anonymous-guard";
import { CodeTheme } from "./code-theme"; import { CodeTheme } from "./code-theme";
interface AppProps { export class App extends Component<any, any> {
user?: MyUserInfo;
}
export class App extends Component<AppProps, any> {
private isoData: IsoDataOptionalSite = setIsoData(this.context); private isoData: IsoDataOptionalSite = setIsoData(this.context);
private readonly mainContentRef: RefObject<HTMLElement>; private readonly mainContentRef: RefObject<HTMLElement>;
constructor(props: AppProps, context: any) { constructor(props: any, context: any) {
super(props, context); super(props, context);
this.mainContentRef = createRef(); this.mainContentRef = createRef();
} }
@ -34,8 +29,6 @@ export class App extends Component<AppProps, any> {
this.mainContentRef.current?.focus(); this.mainContentRef.current?.focus();
} }
user = UserService.Instance.myUserInfo;
render() { render() {
const siteRes = this.isoData.site_res; const siteRes = this.isoData.site_res;
const siteView = siteRes?.site_view; const siteView = siteRes?.site_view;
@ -46,7 +39,7 @@ export class App extends Component<AppProps, any> {
<div <div
id="app" id="app"
className="lemmy-site" className="lemmy-site"
data-bs-theme={dataBsTheme(this.props.user)} data-bs-theme={dataBsTheme(siteRes)}
> >
<button <button
type="button" type="button"

View file

@ -1,5 +1,5 @@
import { setIsoData } from "@utils/app"; import { setIsoData } from "@utils/app";
import { isBrowser } from "@utils/browser"; import { isBrowser, updateDataBsTheme } from "@utils/browser";
import { getQueryParams } from "@utils/helpers"; import { getQueryParams } from "@utils/helpers";
import { Component, linkEvent } from "inferno"; import { Component, linkEvent } from "inferno";
import { RouteComponentProps } from "inferno-router/dist/Route"; import { RouteComponentProps } from "inferno-router/dist/Route";
@ -47,6 +47,7 @@ async function handleLoginSuccess(i: Login, loginRes: LoginResponse) {
if (site.state === "success") { if (site.state === "success") {
UserService.Instance.myUserInfo = site.data.my_user; UserService.Instance.myUserInfo = site.data.my_user;
updateDataBsTheme(site.data);
} }
const { prev } = getLoginQueryParams(); const { prev } = getLoginQueryParams();

View file

@ -61,6 +61,7 @@ import { PersonListing } from "./person-listing";
import { InitialFetchRequest } from "../../interfaces"; import { InitialFetchRequest } from "../../interfaces";
import TotpModal from "../common/totp-modal"; import TotpModal from "../common/totp-modal";
import { LoadingEllipses } from "../common/loading-ellipses"; import { LoadingEllipses } from "../common/loading-ellipses";
import { updateDataBsTheme } from "@utils/browser";
type SettingsData = RouteDataResponse<{ type SettingsData = RouteDataResponse<{
instancesRes: GetFederatedInstancesResponse; instancesRes: GetFederatedInstancesResponse;
@ -1621,6 +1622,7 @@ export class Settings extends Component<any, SettingsState> {
} = siteRes.data.my_user!.local_user_view; } = siteRes.data.my_user!.local_user_view;
UserService.Instance.myUserInfo = siteRes.data.my_user; UserService.Instance.myUserInfo = siteRes.data.my_user;
updateDataBsTheme(siteRes.data);
i.setState(prev => ({ i.setState(prev => ({
...prev, ...prev,

View file

@ -1,9 +1,11 @@
import { GetSiteResponse } from "lemmy-js-client"; import { GetSiteResponse } from "lemmy-js-client";
import { setupEmojiDataModel, setupMarkdown } from "../../markdown"; import { setupEmojiDataModel, setupMarkdown } from "../../markdown";
import { I18NextService, UserService } from "../../services"; import { I18NextService, UserService } from "../../services";
import { updateDataBsTheme } from "@utils/browser";
export default function initializeSite(site?: GetSiteResponse) { export default function initializeSite(site?: GetSiteResponse) {
UserService.Instance.myUserInfo = site?.my_user; UserService.Instance.myUserInfo = site?.my_user;
updateDataBsTheme(site);
I18NextService.i18n.changeLanguage(); I18NextService.i18n.changeLanguage();
if (site) { if (site) {
setupEmojiDataModel(site.custom_emojis ?? []); setupEmojiDataModel(site.custom_emojis ?? []);

View file

@ -1,5 +1,5 @@
import { fetchThemeList } from "@utils/app"; 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) { export default async function setTheme(theme: string, forceReload = false) {
if (!isBrowser()) { if (!isBrowser()) {
@ -32,5 +32,8 @@ export default async function setTheme(theme: string, forceReload = false) {
const cssLoc = `/css/themes/${theme}.css`; const cssLoc = `/css/themes/${theme}.css`;
loadCss(theme, cssLoc); loadCss(theme, cssLoc);
document
.getElementById("app")
?.setAttribute("data-bs-theme", dataBsTheme(theme));
document.getElementById(theme)?.removeAttribute("disabled"); document.getElementById(theme)?.removeAttribute("disabled");
} }

View file

@ -1,17 +1,23 @@
import { MyUserInfo } from "lemmy-js-client"; import { GetSiteResponse } from "lemmy-js-client";
import isDark from "./is-dark"; import isDark from "./is-dark";
export default function dataBsTheme(user?: MyUserInfo) { export default function dataBsTheme(siteResOrTheme?: GetSiteResponse | string) {
return (isDark() && user?.local_user_view.local_user.theme === "browser") || const theme =
(user && typeof siteResOrTheme === "string"
[ ? siteResOrTheme
"darkly", : siteResOrTheme?.my_user?.local_user_view.local_user.theme ??
"darkly-red", siteResOrTheme?.site_view.local_site.default_theme ??
"darkly-pureblack", "browser";
"darkly-compact",
"i386", return (isDark() && theme === "browser") ||
"vaporwave-dark", [
].includes(user.local_user_view.local_user.theme)) "darkly",
"darkly-red",
"darkly-pureblack",
"darkly-compact",
"i386",
"vaporwave-dark",
].includes(theme)
? "dark" ? "dark"
: "light"; : "light";
} }

View file

@ -9,6 +9,7 @@ import restoreScrollPosition from "./restore-scroll-position";
import saveScrollPosition from "./save-scroll-position"; import saveScrollPosition from "./save-scroll-position";
import setAuthCookie from "./set-auth-cookie"; import setAuthCookie from "./set-auth-cookie";
import share from "./share"; import share from "./share";
import updateDataBsTheme from "./update-data-bs-theme";
export { export {
canShare, canShare,
@ -22,4 +23,5 @@ export {
saveScrollPosition, saveScrollPosition,
setAuthCookie, setAuthCookie,
share, share,
updateDataBsTheme,
}; };

View file

@ -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));
}
}