From cde3c266f2785dca8976cde8cf37aa83c67eb870 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Mon, 7 Sep 2020 10:32:07 -0500 Subject: [PATCH] Better resolver, communities mostly done. --- src/server/index.tsx | 18 +++++++---- src/shared/components/app.tsx | 41 ++++++++++++------------- src/shared/components/communities.tsx | 41 +++++++------------------ src/shared/components/navbar.tsx | 43 +++++++-------------------- src/shared/components/sponsors.tsx | 2 -- src/shared/interfaces.ts | 2 ++ src/shared/utils.ts | 30 ++++++++++++++++--- 7 files changed, 82 insertions(+), 95 deletions(-) diff --git a/src/server/index.tsx b/src/server/index.tsx index 3a698e9..3eace28 100644 --- a/src/server/index.tsx +++ b/src/server/index.tsx @@ -10,7 +10,7 @@ import { IsoData } from '../shared/interfaces'; import { routes } from '../shared/routes'; import IsomorphicCookie from 'isomorphic-cookie'; import { lemmyHttp, setAuth } from '../shared/utils'; -import { GetSiteForm } from 'lemmy-js-client'; +import { GetSiteForm, GetSiteResponse } from 'lemmy-js-client'; const server = express(); const port = 1234; @@ -38,15 +38,23 @@ server.get('/*', async (req, res) => { } let resolver = await Promise.all(promises); + let site: GetSiteResponse = resolver[0]; + let routeData = resolver.slice(1, resolver.length); + + let acceptLang = req.headers['accept-language'].split(',')[0]; + let lang = !!site.my_user + ? site.my_user.lang == 'browser' + ? acceptLang + : 'en' + : acceptLang; let isoData: IsoData = { path: req.path, - site: resolver[0], - routeData: resolver.slice(1, resolver.length), + site, + routeData, + lang, }; - console.log(activeRoute.path); - const wrapper = ( diff --git a/src/shared/components/app.tsx b/src/shared/components/app.tsx index 8c7443c..48cfd6a 100644 --- a/src/shared/components/app.tsx +++ b/src/shared/components/app.tsx @@ -1,7 +1,7 @@ import { Component } from 'inferno'; import { Route, Switch } from 'inferno-router'; -/* import { Provider } from 'inferno-i18next'; */ -/* import { i18n } from './i18next'; */ +import { Provider } from 'inferno-i18next'; +import { i18n } from '../i18next'; import { routes } from '../../shared/routes'; import { Navbar } from '../../shared/components/navbar'; import { Footer } from '../../shared/components/footer'; @@ -18,28 +18,29 @@ export class App extends Component { super(props, context); } - /* */ render() { return ( <> -
- -
- - {routes.map(({ path, exact, component: C, ...rest }) => ( - } - /> - ))} - {/* } /> */} - - + +
+ +
+ + {routes.map(({ path, exact, component: C, ...rest }) => ( + } + /> + ))} + {/* } /> */} + + +
+
-
-
+ ); } diff --git a/src/shared/components/communities.tsx b/src/shared/components/communities.tsx index 2429599..a6b9726 100644 --- a/src/shared/components/communities.tsx +++ b/src/shared/components/communities.tsx @@ -1,7 +1,6 @@ import { Component, linkEvent } from 'inferno'; import { Helmet } from 'inferno-helmet'; import { Subscription } from 'rxjs'; -import { retryWhen, delay, take } from 'rxjs/operators'; import { UserOperation, Community, @@ -11,7 +10,6 @@ import { ListCommunitiesForm, SortType, WebSocketJsonResponse, - GetSiteResponse, Site, } from 'lemmy-js-client'; import { WebSocketService } from '../services'; @@ -22,10 +20,11 @@ import { isBrowser, lemmyHttp, setAuth, + setIsoData, + wsSubscribe, } from '../utils'; import { CommunityLink } from './community-link'; import { i18n } from '../i18next'; -import { IsoData } from 'shared/interfaces'; const communityLimit = 100; @@ -42,36 +41,24 @@ interface CommunitiesProps { export class Communities extends Component { private subscription: Subscription; + private isoData = setIsoData(this.context); private emptyState: CommunitiesState = { communities: [], loading: true, page: getPageFromProps(this.props), - site: undefined, + site: this.isoData.site.site, }; constructor(props: any, context: any) { super(props, context); this.state = this.emptyState; - let isoData: IsoData; + this.parseMessage = this.parseMessage.bind(this); - if (isBrowser()) { - this.subscription = WebSocketService.Instance.subject - .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10)))) - .subscribe( - msg => this.parseMessage(msg), - err => console.error(err), - () => console.log('complete') - ); - isoData = window.isoData; - } else { - isoData = this.context.router.staticContext; - } - - this.state.site = isoData.site.site; + this.subscription = wsSubscribe(this.parseMessage); // Only fetch the data if coming from another route - if (isoData.path == this.context.router.route.match.path) { - this.state.communities = isoData.routeData[0].communities; + if (this.isoData.path == this.context.router.route.match.url) { + this.state.communities = this.isoData.routeData[0].communities; this.state.loading = false; } else { this.refetch(); @@ -98,11 +85,7 @@ export class Communities extends Component { } get documentTitle(): string { - if (this.state.site) { - return `${i18n.t('communities')} - ${this.state.site.name}`; - } else { - return 'Lemmy'; - } + return `${i18n.t('communities')} - ${this.state.site.name}`; } render() { @@ -250,7 +233,7 @@ export class Communities extends Component { static fetchInitialData(auth: string, path: string): Promise[] { let pathSplit = path.split('/'); - let page = pathSplit[2] ? Number(pathSplit[2]) : 1; + let page = pathSplit[3] ? Number(pathSplit[3]) : 1; let listCommunitiesForm: ListCommunitiesForm = { sort: SortType.TopAll, limit: communityLimit, @@ -282,10 +265,6 @@ export class Communities extends Component { found.subscribed = data.community.subscribed; found.number_of_subscribers = data.community.number_of_subscribers; this.setState(this.state); - } else if (res.op == UserOperation.GetSite) { - let data = res.data as GetSiteResponse; - this.state.site = data.site; - this.setState(this.state); } } } diff --git a/src/shared/components/navbar.tsx b/src/shared/components/navbar.tsx index 72505b5..56829b5 100644 --- a/src/shared/components/navbar.tsx +++ b/src/shared/components/navbar.tsx @@ -46,7 +46,6 @@ interface NavbarState { unreadCount: number; searchParam: string; toggleSearch: boolean; - siteRes: GetSiteResponse; onSiteBanner?(url: string): any; } @@ -62,7 +61,6 @@ export class Navbar extends Component { mentions: [], messages: [], expanded: false, - siteRes: this.props.site, // TODO this could probably go away searchParam: '', toggleSearch: false, }; @@ -80,14 +78,15 @@ export class Navbar extends Component { () => console.log('complete') ); - // WebSocketService.Instance.getSite(); - this.searchTextField = createRef(); } // The login + // TODO this needs some work if (this.props.site.my_user) { + console.log(this.props.site.my_user); UserService.Instance.user = this.props.site.my_user; + // i18n.changeLanguage(getLanguage()); if (isBrowser()) { WebSocketService.Instance.userJoin(); @@ -97,6 +96,7 @@ export class Navbar extends Component { this.fetchUnreads(); // setTheme(data.my_user.theme, true); // i18n.changeLanguage(getLanguage()); + // i18n.changeLanguage('de'); } } } @@ -113,7 +113,7 @@ export class Navbar extends Component { this.state.isLoggedIn = false; } console.log('a new login'); - WebSocketService.Instance.getSite(); + // WebSocketService.Instance.getSite(); this.setState(this.state); }); @@ -184,19 +184,19 @@ export class Navbar extends Component {