From: abias Date: Wed, 17 May 2023 00:34:15 +0000 (-0400) Subject: Refactor how error data is passed from server to client X-Git-Url: http://these/git/%7B%60https:/%7B%60%24%7BrepoUrl%7D?a=commitdiff_plain;h=4f1d357b5b3ff82f32fc70d0b7debff9dd747dcf;p=lemmy-ui.git Refactor how error data is passed from server to client --- diff --git a/src/server/index.tsx b/src/server/index.tsx index 746c962..c8099cc 100644 --- a/src/server/index.tsx +++ b/src/server/index.tsx @@ -130,6 +130,7 @@ server.get("/*", async (req, res) => { // in order to remove the jwt on the browser. Necessary for wrong jwts let site: GetSiteResponse | undefined = undefined; let routeData: any[] = []; + let errorPageData: ErrorPageData | undefined; try { let try_site: any = await client.getSite(getSiteForm); if (try_site.error == "not_logged_in") { @@ -165,7 +166,7 @@ server.get("/*", async (req, res) => { } } } catch (error) { - routeData = getErrorRouteData(error, site); + errorPageData = getErrorRouteData(error, site); } // Redirect to the 404 if there's an API error @@ -175,7 +176,7 @@ server.get("/*", async (req, res) => { if (error === "instance_is_private") { return res.redirect(`/signup`); } else { - routeData = getErrorRouteData(error, site); + errorPageData = getErrorRouteData(error, site); } } @@ -183,6 +184,7 @@ server.get("/*", async (req, res) => { path, site_res: site, routeData, + errorPageData, }; const wrapper = ( @@ -293,7 +295,7 @@ async function fetchIconPng(iconUrl: string) { } function getErrorRouteData(error: string, site?: GetSiteResponse) { - const errorPageData: ErrorPageData = { type: "error" }; + const errorPageData: ErrorPageData = {}; // Exact error should only be seen in a development environment. Users // in production will get a more generic message. @@ -308,7 +310,7 @@ function getErrorRouteData(error: string, site?: GetSiteResponse) { errorPageData.adminMatrixIds = adminMatrixIds; } - return [errorPageData]; + return errorPageData; } async function createSsrHtml(root: string, isoData: IsoDataOptionalSite) { diff --git a/src/shared/components/app/error-page.tsx b/src/shared/components/app/error-page.tsx index 7c7bfce..12472dd 100644 --- a/src/shared/components/app/error-page.tsx +++ b/src/shared/components/app/error-page.tsx @@ -1,7 +1,7 @@ import { Component } from "inferno"; import { Link } from "inferno-router"; import { IsoDataOptionalSite } from "shared/interfaces"; -import { ErrorPageData, setIsoData } from "../../utils"; +import { setIsoData } from "../../utils"; export class ErrorPage extends Component { private isoData: IsoDataOptionalSite = setIsoData(this.context); @@ -11,7 +11,7 @@ export class ErrorPage extends Component { } render() { - const errorPageData = this.getErrorPageData(); + const { errorPageData } = this.isoData; return (
@@ -65,15 +65,4 @@ export class ErrorPage extends Component {
); } - - private getErrorPageData() { - const errorPageData = this.isoData.routeData[0] as - | ErrorPageData - | undefined; - if (errorPageData?.type === "error") { - return errorPageData; - } - - return undefined; - } } diff --git a/src/shared/components/common/error-guard.tsx b/src/shared/components/common/error-guard.tsx index db7f578..3012154 100644 --- a/src/shared/components/common/error-guard.tsx +++ b/src/shared/components/common/error-guard.tsx @@ -1,5 +1,5 @@ import { Component } from "inferno"; -import { ErrorPageData, setIsoData } from "../../utils"; +import { setIsoData } from "../../utils"; import { ErrorPage } from "../app/error-page"; class ErrorGuard extends Component { @@ -10,12 +10,10 @@ class ErrorGuard extends Component { } render() { - const errorPageData = this.isoData.routeData[0] as - | ErrorPageData - | undefined; + const errorPageData = this.isoData.errorPageData; const siteRes = this.isoData.site_res; - if (errorPageData?.type === "error" || !siteRes) { + if (errorPageData || !siteRes) { return ; } else { return this.props.children; diff --git a/src/shared/interfaces.ts b/src/shared/interfaces.ts index fe8a79d..a6b2ae4 100644 --- a/src/shared/interfaces.ts +++ b/src/shared/interfaces.ts @@ -1,5 +1,6 @@ import { CommentView, GetSiteResponse, LemmyHttp } from "lemmy-js-client"; import type { ParsedQs } from "qs"; +import { ErrorPageData } from "./utils"; /** * This contains serialized data, it needs to be deserialized before use. @@ -8,6 +9,7 @@ export interface IsoData { path: string; routeData: any[]; site_res: GetSiteResponse; + errorPageData?: ErrorPageData; } export type IsoDataOptionalSite = Partial & diff --git a/src/shared/utils.ts b/src/shared/utils.ts index 54002ec..c405fc5 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -106,7 +106,6 @@ export type ThemeColor = | "gray-dark"; export interface ErrorPageData { - type: "error"; error?: string; adminMatrixIds?: string[]; }