diff --git a/web/src/App.tsx b/web/src/App.tsx index f14801c..b301169 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -1,5 +1,5 @@ import { Fragment, lazy, Suspense, useEffect, useRef, useState } from "react"; -import { Route, Switch, Redirect, useLocation, Router } from "wouter"; +import { Route, Switch, Redirect, Router } from "wouter"; import { useSession, useViewingDelegation } from "@/store/session"; import { notifyOwnWhileAway, ownAccountAway, useMail } from "@/store/mail"; import { scheduleSupported, useScheduled } from "@/store/scheduled"; @@ -120,7 +120,6 @@ export function App() { function AuthedApp() { const accountId = useSession((s) => s.accountId); const viewing = useSession((s) => s.viewing); - const [location] = useLocation(); /* * Settings that live with the account rather than the browser. @@ -366,10 +365,13 @@ function AuthedApp() { {(p) => } {(p) => } {(p) => } + {/* Replace, not push: a page that only redirects isn't one to go Back to (#96). */} - + + + + - {location === "/" ? : } diff --git a/web/src/lib/__tests__/backStack.test.ts b/web/src/lib/__tests__/backStack.test.ts new file mode 100644 index 0000000..8e32379 --- /dev/null +++ b/web/src/lib/__tests__/backStack.test.ts @@ -0,0 +1,25 @@ +import { describe, expect, it } from "vitest"; +import { isHome, sidewaysReplaces } from "../backStack"; + +describe("Back in the installed app (#96)", () => { + it("treats the inbox, by path or by id, as home", () => { + expect(isHome("/mail", "inbox1")).toBe(true); + expect(isHome("/mail/", "inbox1")).toBe(true); + expect(isHome("/mail/inbox1", "inbox1")).toBe(true); + expect(isHome("/mail/inbox1/thread9", "inbox1")).toBe(false); + expect(isHome("/mail/archive2", "inbox1")).toBe(false); + expect(isHome("/calendar", "inbox1")).toBe(false); + }); + + it("keeps the inbox in the history when leaving it, and replaces every other page", () => { + expect(sidewaysReplaces("/mail/inbox1", "inbox1", true)).toBe(false); + expect(sidewaysReplaces("/mail/archive2", "inbox1", true)).toBe(true); + expect(sidewaysReplaces("/calendar", "inbox1", true)).toBe(true); + expect(sidewaysReplaces("/mail/archive2/thread9", "inbox1", true)).toBe(true); + }); + + it("leaves a browser tab's history alone", () => { + expect(sidewaysReplaces("/mail/archive2", "inbox1", false)).toBe(false); + expect(sidewaysReplaces("/calendar", "inbox1", false)).toBe(false); + }); +}); diff --git a/web/src/lib/backStack.ts b/web/src/lib/backStack.ts new file mode 100644 index 0000000..cd1812d --- /dev/null +++ b/web/src/lib/backStack.ts @@ -0,0 +1,33 @@ +import { useLocation } from "wouter"; +import { useMail } from "@/store/mail"; +import { isInstalledApp } from "@/lib/mailhandler"; + +/* + * What Back does in the installed app (#96). A browser tab keeps every page + * it visited, as a web page should. An installed app is expected to work + * like one: Back goes to where you came from in the app, and from the inbox + * it closes the app. So there, moving between folders and sections replaces + * the page in the history, except when leaving the inbox, which is the one + * page Back should return to. The stack stays inbox → folder → message. + */ + +/** Whether `location` is the inbox, the app's home. */ +export function isHome(location: string, inboxId: string | undefined): boolean { + const path = location.replace(/\/+$/, "") || "/"; + return path === "/" || path === "/mail" || (inboxId !== undefined && path === `/mail/${inboxId}`); +} + +/** Whether a move to another folder or section should replace the current page. */ +export function sidewaysReplaces(location: string, inboxId: string | undefined, installed: boolean): boolean { + return installed && !isHome(location, inboxId); +} + +/** sidewaysReplaces for the page being shown. Pass the result as a link's or navigate's `replace`. */ +export function useSidewaysReplace(): boolean { + const [location] = useLocation(); + const inboxId = useMail((s) => { + for (const m of Object.values(s.mailboxes)) if (m.role === "inbox") return m.id; + return undefined; + }); + return sidewaysReplaces(location, inboxId, isInstalledApp()); +} diff --git a/web/src/views/AppShell.tsx b/web/src/views/AppShell.tsx index 1128a49..4d82022 100644 --- a/web/src/views/AppShell.tsx +++ b/web/src/views/AppShell.tsx @@ -28,6 +28,7 @@ import { t } from "@/lib/i18n"; import { hasAdministration } from "@/lib/admin/adminAccess"; import { usePermissions } from "./admin/usePermissions"; import { AdminNav } from "./admin/AdminNav"; +import { useSidewaysReplace } from "@/lib/backStack"; // The other sections' sidebars load with the section, as their views already do. const FilesTree = lazy(() => import("./files/FilesTree").then((m) => ({ default: m.FilesTree }))); @@ -52,6 +53,7 @@ const PUSH_LABEL = { export function AppShell({ children }: { children: ReactNode }) { const [location, navigate] = useLocation(); + const sideways = useSidewaysReplace(); const isMobile = useIsMobile(); const collapsed = useSettings((s) => s.settings.sidebarCollapsed); const inboxUnread = useMail((s) => Object.values(s.mailboxes).find((m) => m.role === "inbox")?.unreadEmails ?? 0); @@ -489,22 +491,22 @@ export function AppShell({ children }: { children: ReactNode }) { )}