A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
web: small refactor to make the build pass (#9761)
Abdullah Atta7c9e3e79dfee
diff --git a/apps/web/src/navigation/auth-routes.ts b/apps/web/src/navigation/auth-routes.tsnew file mode 100644index 000000000..b018c01e3--- /dev/null+++ b/apps/web/src/navigation/auth-routes.ts@@ -0,0 +1,57 @@+/*+This file is part of the Notesnook project (https://notesnook.com/)++Copyright (C) 2023 Streetwriters (Private) Limited++This program is free software: you can redistribute it and/or modify+it under the terms of the GNU General Public License as published by+the Free Software Foundation, either version 3 of the License, or+(at your option) any later version.++This program is distributed in the hope that it will be useful,+but WITHOUT ANY WARRANTY; without even the implied warranty of+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+GNU General Public License for more details.++You should have received a copy of the GNU General Public License+along with this program. If not, see <http://www.gnu.org/licenses/>.+*/++import { getCurrentPath } from "./index";++export type AuthRoutes =+ | "sessionExpiry"+ | "login:email"+ | "login:password"+ | "signup"+ | "recover"+ | "mfa:code"+ | "mfa:select";++export const authRoutes: Record<AuthRoutes, string> = {+ "login:email": "/login",+ "login:password": "/login/password",+ "mfa:code": "/login/mfa/code",+ "mfa:select": "/login/mfa/select",+ recover: "/recover",+ sessionExpiry: "/sessionexpired",+ signup: "/signup"+};++const unauthorizedRoutes: AuthRoutes[] = [+ "login:email",+ "login:password",+ "signup",+ "mfa:code",+ "mfa:select",+ "recover"+];++export function isAuthRoute() {+ const path = getCurrentPath();+ return Object.values(authRoutes).includes(path);+}++export function isUnauthorizedRoute(route: AuthRoutes) {+ return unauthorizedRoutes.includes(route);+}diff --git a/apps/web/src/navigation/routes.tsx b/apps/web/src/navigation/routes.tsxindex 4cdf7d4d8..ffea0659f 100644--- a/apps/web/src/navigation/routes.tsx+++ b/apps/web/src/navigation/routes.tsx@@ -23,7 +23,6 @@ import Notes from "../views/notes";import { NotebookHeader } from "../components/notebook-header";import Trash from "../views/trash";import { useStore as useNoteStore } from "../stores/note-store";-import { useStore as useAppStore } from "../stores/app-store";import Reminders from "../views/reminders";import { RouteResult, defineRoutes } from "./types";import { CREATE_BUTTON_MAP } from "../common";diff --git a/apps/web/src/root.tsx b/apps/web/src/root.tsxindex 4510b3ee0..24de28114 100644--- a/apps/web/src/root.tsx+++ b/apps/web/src/root.tsx@@ -28,12 +28,13 @@ import {import { desktop } from "./common/desktop-bridge";import { useKeyStore } from "./interfaces/key-store";import { usePromise } from "@notesnook/common";-import { AuthProps, isAuthRouteActive } from "./views/auth";+import { AuthProps } from "./views/auth";import { loadDatabase } from "./hooks/use-database";import AppLock from "./views/app-lock";import { Text } from "@theme-ui/components";import { EV, EVENTS } from "@notesnook/core";import { useEffect, useState } from "react";+import { isAuthRoute } from "./navigation/auth-routes";export async function startApp(children?: React.ReactNode) {const rootElement = document.getElementById("root");@@ -57,7 +58,7 @@ export async function startApp(children?: React.ReactNode) {root.render(<>- <TitleBar force={isAuthRouteActive()} />+ <TitleBar force={isAuthRoute()} /><ErrorBoundary><GlobalErrorHandler><BaseThemeProviderdiff --git a/apps/web/src/views/auth.tsx b/apps/web/src/views/auth.tsxindex 2c1e3379c..580bb2d05 100644--- a/apps/web/src/views/auth.tsx+++ b/apps/web/src/views/auth.tsx@@ -30,12 +30,7 @@ import {Warn} from "../components/icons";import Field, { FieldProps } from "../components/field";-import {- getCurrentPath,- getQueryParams,- hardNavigate,- makeURL-} from "../navigation";+import { getQueryParams, hardNavigate, makeURL } from "../navigation";import { store as userstore } from "../stores/user-store";import { db } from "../common/db";import Config from "../utils/config";@@ -49,6 +44,11 @@ import { showLogoutConfirmation } from "../dialogs/confirm";import { TaskManager } from "../common/task-manager";import { strings } from "@notesnook/intl";import { ScrollContainer } from "@notesnook/ui";+import {+ authRoutes,+ AuthRoutes,+ isUnauthorizedRoute+} from "../navigation/auth-routes";type EmailFormData = {email: string;@@ -117,14 +117,7 @@ type BaseAuthComponentProps<TRoute extends AuthRoutes> = {navigate: NavigateFunction;formData?: AuthFormData[TRoute];};-type AuthRoutes =- | "sessionExpiry"- | "login:email"- | "login:password"- | "signup"- | "recover"- | "mfa:code"- | "mfa:select";+export type AuthProps = {route: AuthRoutes;isolated?: boolean;@@ -158,30 +151,6 @@ function getRouteComponent<TRoute extends AuthRoutes>(return undefined;}-const routePaths: Record<AuthRoutes, string> = {- "login:email": "/login",- "login:password": "/login/password",- "mfa:code": "/login/mfa/code",- "mfa:select": "/login/mfa/select",- recover: "/recover",- sessionExpiry: "/sessionexpired",- signup: "/signup"-};--export function isAuthRouteActive() {- const path = getCurrentPath();- return Object.values(routePaths).includes(path);-}--const authorizedRoutes: AuthRoutes[] = [- "login:email",- "login:password",- "signup",- "mfa:code",- "mfa:select",- "recover"-];-function Auth(props: AuthProps) {return (<AuthContainer>@@ -201,12 +170,12 @@ export function HeadlessAuth(props: AuthProps) {const Route = useMemo(() => getRouteComponent(route), [route]);useEffect(() => {if (isolated) return;- window.history.replaceState({}, "", makeURL(routePaths[route]));+ window.history.replaceState({}, "", makeURL(authRoutes[route]));}, [route, isolated]);useEffect(() => {db.user.getUser().then((user) => {- if (user && authorizedRoutes.includes(route) && !isSessionExpired())+ if (user && isUnauthorizedRoute(route) && !isSessionExpired())return openURL("/", { authenticated: true });performance.mark("load:auth");setIsReady(true);