From e03fe66a214318f021332d1eed3537310e900e81 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Orvar=20Segerstr=C3=B6m?= Date: Mon, 26 Oct 2020 08:09:44 +0100 Subject: [PATCH] Don't flash default theme on page load --- src/client/index.tsx | 4 +++ src/server/index.tsx | 23 ++++++++++++++- src/shared/components/app.tsx | 2 +- src/shared/components/navbar.tsx | 15 ---------- src/shared/initialize.ts | 9 ++++++ src/shared/utils.ts | 48 +++++++++++++++++--------------- 6 files changed, 61 insertions(+), 40 deletions(-) create mode 100644 src/shared/initialize.ts diff --git a/src/client/index.tsx b/src/client/index.tsx index dfa9ad7..b7f216d 100644 --- a/src/client/index.tsx +++ b/src/client/index.tsx @@ -1,7 +1,11 @@ import { hydrate } from 'inferno-hydrate'; import { BrowserRouter } from 'inferno-router'; +import { initializeSite } from '../shared/initialize'; import { App } from '../shared/components/app'; +const site = window.isoData.site; +initializeSite(site); + const wrapper = ( diff --git a/src/server/index.tsx b/src/server/index.tsx index ef6ce08..6a7ffbe 100644 --- a/src/server/index.tsx +++ b/src/server/index.tsx @@ -13,6 +13,7 @@ import { lemmyHttp, setAuth } from '../shared/utils'; import { GetSiteForm, GetSiteResponse } from 'lemmy-js-client'; import process from 'process'; import { Helmet } from 'inferno-helmet'; +import { initializeSite } from '../shared/initialize'; const server = express(); const port = 1234; @@ -59,6 +60,19 @@ server.get('/*', async (req, res) => { lang, }; + initializeSite(site); + const user = site.my_user; + const userTheme = + user && + user.theme && + ` + + `; + const wrapper = ( @@ -95,8 +109,15 @@ server.get('/*', async (req, res) => { + ${userTheme || ''} + ${ + userTheme + ? '' + : ` + ` + } @@ -105,7 +126,7 @@ server.get('/*', async (req, res) => { Javascript is disabled. Actions will not work. - +
${root}
diff --git a/src/shared/components/app.tsx b/src/shared/components/app.tsx index 53d5c15..a435070 100644 --- a/src/shared/components/app.tsx +++ b/src/shared/components/app.tsx @@ -8,6 +8,7 @@ import { Navbar } from '../../shared/components/navbar'; import { Footer } from '../../shared/components/footer'; import { Symbols } from '../../shared/components/symbols'; import { GetSiteResponse } from 'lemmy-js-client'; +import { UserService } from '../../shared/services'; import './styles.scss'; export interface AppProps { @@ -18,7 +19,6 @@ export class App extends Component { constructor(props: any, context: any) { super(props, context); } - render() { return ( <> diff --git a/src/shared/components/navbar.tsx b/src/shared/components/navbar.tsx index 63bc230..795368f 100644 --- a/src/shared/components/navbar.tsx +++ b/src/shared/components/navbar.tsx @@ -72,21 +72,6 @@ export class Navbar extends Component { this.parseMessage = this.parseMessage.bind(this); this.subscription = wsSubscribe(this.parseMessage); - - // The login - // TODO this needs some work - UserService.Instance.user = this.props.site.my_user; - i18n.changeLanguage(getLanguage()); - if (UserService.Instance.user) { - setTheme(UserService.Instance.user.theme); - } - - // if (!!this.props.site.my_user) { - // UserService.Instance.this.props.site.my_user); - // // UserService.Instance.user = this.props.site.my_user; - // } else { - // UserService.Instance.setUser(undefined); - // } } componentDidMount() { diff --git a/src/shared/initialize.ts b/src/shared/initialize.ts new file mode 100644 index 0000000..555da5e --- /dev/null +++ b/src/shared/initialize.ts @@ -0,0 +1,9 @@ +import { GetSiteResponse } from 'lemmy-js-client'; +import { UserService } from './services'; +import { i18n } from './i18next'; +import { getLanguage } from './utils'; + +export const initializeSite = (site: GetSiteResponse) => { + UserService.Instance.user = site.my_user; + i18n.changeLanguage(getLanguage()); +}; diff --git a/src/shared/utils.ts b/src/shared/utils.ts index 33ba140..228aa9a 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -443,32 +443,34 @@ export function getMomentLanguage(): string { } export function setTheme(theme: string, forceReload: boolean = false) { - if (isBrowser() && (theme !== 'browser' || forceReload)) { - // This is only run on a force reload - if (theme == 'browser') { - theme = 'darkly'; - } + if (!isBrowser()) { + return; + } + if (theme === 'browser' && !forceReload) { + return; + } + // This is only run on a force reload + if (theme == 'browser') { + theme = 'darkly'; + } - // Unload all the other themes - for (var i = 0; i < themes.length; i++) { - let styleSheet = document.getElementById(themes[i]); - if (styleSheet) { - styleSheet.setAttribute('disabled', 'disabled'); - } + // Unload all the other themes + for (var i = 0; i < themes.length; i++) { + let styleSheet = document.getElementById(themes[i]); + if (styleSheet) { + styleSheet.setAttribute('disabled', 'disabled'); } - - document - .getElementById('default-light') - .setAttribute('disabled', 'disabled'); - document - .getElementById('default-dark') - .setAttribute('disabled', 'disabled'); - - // Load the theme dynamically - let cssLoc = `/static/assets/css/themes/${theme}.min.css`; - loadCss(theme, cssLoc); - document.getElementById(theme).removeAttribute('disabled'); } + + document + .getElementById('default-light') + ?.setAttribute('disabled', 'disabled'); + document.getElementById('default-dark')?.setAttribute('disabled', 'disabled'); + + // Load the theme dynamically + let cssLoc = `/static/assets/css/themes/${theme}.min.css`; + loadCss(theme, cssLoc); + document.getElementById(theme).removeAttribute('disabled'); } export function loadCss(id: string, loc: string) { -- 2.44.1