mirror of
https://github.com/LemmyNet/lemmy-ui.git
synced 2024-12-26 04:41:26 +00:00
Don't flash default theme on page load
This commit is contained in:
parent
2753b01923
commit
e03fe66a21
6 changed files with 63 additions and 42 deletions
|
@ -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 = (
|
||||
<BrowserRouter>
|
||||
<App site={window.isoData.site} />
|
||||
|
|
|
@ -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 &&
|
||||
`
|
||||
<link
|
||||
rel="stylesheet"
|
||||
type="text/css"
|
||||
href="/static/assets/css/themes/${user.theme}.min.css"
|
||||
/>
|
||||
`;
|
||||
|
||||
const wrapper = (
|
||||
<StaticRouter location={req.url} context={isoData}>
|
||||
<App site={isoData.site} />
|
||||
|
@ -95,8 +109,15 @@ server.get('/*', async (req, res) => {
|
|||
|
||||
<!-- Styles -->
|
||||
<link rel="stylesheet" type="text/css" href="/static/styles/styles.css" />
|
||||
${userTheme || ''}
|
||||
${
|
||||
userTheme
|
||||
? ''
|
||||
: `
|
||||
<link rel="stylesheet" type="text/css" href="/static/assets/css/themes/litely.min.css" id="default-light" media="(prefers-color-scheme: light)" />
|
||||
<link rel="stylesheet" type="text/css" href="/static/assets/css/themes/darkly.min.css" id="default-dark" media="(prefers-color-scheme: no-preference), (prefers-color-scheme: dark)" />
|
||||
`
|
||||
}
|
||||
</head>
|
||||
|
||||
<body ${helmet.bodyAttributes.toString()}>
|
||||
|
@ -105,7 +126,7 @@ server.get('/*', async (req, res) => {
|
|||
<b>Javascript is disabled. Actions will not work.</b>
|
||||
</div>
|
||||
</noscript>
|
||||
|
||||
|
||||
<div id='root'>${root}</div>
|
||||
<script defer src='/static/js/client.js'></script>
|
||||
</body>
|
||||
|
|
|
@ -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<AppProps, any> {
|
|||
constructor(props: any, context: any) {
|
||||
super(props, context);
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<>
|
||||
|
|
|
@ -72,21 +72,6 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
|
|||
|
||||
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() {
|
||||
|
|
9
src/shared/initialize.ts
Normal file
9
src/shared/initialize.ts
Normal file
|
@ -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());
|
||||
};
|
|
@ -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';
|
||||
}
|
||||
|
||||
// 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');
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
|
|
Loading…
Reference in a new issue