Back in the installed app goes back through the app, not every folder #99

Merged
jcoffey-dev merged 1 commits from fix/96-back-button-history into main 2026-11-16 16:26:38 +00:00
6 changed files with 80 additions and 12 deletions

No files matched your search

+6 -4
View File
@@ -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() {
<Route path="/files/:nodeId?">{(p) => <FilesView nodeId={p.nodeId} />}</Route>
<Route path="/settings/:section?">{(p) => <SettingsView section={p.section} />}</Route>
<Route path="/admin/:section?/:id?">{(p) => <AdminView section={p.section} id={p.id} />}</Route>
{/* Replace, not push: a page that only redirects isn't one to go Back to (#96). */}
<Route path="/login">
<Redirect to="/mail" />
<Redirect to="/mail" replace />
</Route>
<Route>
<Redirect to="/mail" replace />
</Route>
<Route>{location === "/" ? <Redirect to="/mail" /> : <Redirect to="/mail" />}</Route>
</Switch>
</Suspense>
<ComposerDock />
+25
View File
@@ -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);
});
});
+33
View File
@@ -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());
}
+11 -7
View File
@@ -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 }) {
</button>
)}
<nav className="mobile-tabbar" aria-label={t("Sections")}>
<Link href="/mail" className={section === "mail" || section === "search" ? "active" : ""}>
<Link href="/mail" replace={sideways} className={section === "mail" || section === "search" ? "active" : ""}>
<Mail size={22} />
{t("Mail")}
</Link>
<Link href="/calendar" className={section === "calendar" ? "active" : ""}>
<Link href="/calendar" replace={sideways} className={section === "calendar" ? "active" : ""}>
<Calendar size={22} />
{t("Calendar")}
</Link>
<Link href="/contacts" className={section === "contacts" ? "active" : ""}>
<Link href="/contacts" replace={sideways} className={section === "contacts" ? "active" : ""}>
<Users size={22} />
{t("Contacts")}
</Link>
<Link href="/files" className={section === "files" ? "active" : ""}>
<Link href="/files" replace={sideways} className={section === "files" ? "active" : ""}>
<FolderOpen size={22} />
{t("Files")}
@@ -520,7 +522,7 @@ export function AppShell({ children }: { children: ReactNode }) {
still somewhere worth going. */
need="mayReadItems"
onClose={() => setGoFolder(false)}
onPick={(id) => { setGoFolder(false); navigate(`/mail/${id}`); }}
onPick={(id) => { setGoFolder(false); navigate(`/mail/${id}`, { replace: sideways }); }}
/>
)}
</div>
@@ -529,8 +531,9 @@ export function AppShell({ children }: { children: ReactNode }) {
/** The app rail down the left edge, which replaces the module bar on a wide screen. */
function RailLink({ href, icon, label, active, badge = 0 }: { href: string; icon: ReactNode; label: string; active: boolean; badge?: number }) {
const sideways = useSidewaysReplace();
return (
<Link href={href} className={`rail-link ${active ? "active" : ""}`} title={label} aria-label={label} aria-current={active ? "page" : undefined}>
<Link href={href} replace={sideways} className={`rail-link ${active ? "active" : ""}`} title={label} aria-label={label} aria-current={active ? "page" : undefined}>
{icon}
{badge > 0 && <span className="rail-badge">{badge > 999 ? "999+" : badge}</span>}
</Link>
@@ -539,8 +542,9 @@ function RailLink({ href, icon, label, active, badge = 0 }: { href: string; icon
/** Outlook-style module switcher at the bottom of the folder pane. */
function ModuleLink({ href, icon, label, active }: { href: string; icon: ReactNode; label: string; active: boolean }) {
const sideways = useSidewaysReplace();
return (
<Link href={href} className={`module-link ${active ? "active" : ""}`} title={label} aria-label={label} aria-current={active ? "page" : undefined}>
<Link href={href} replace={sideways} className={`module-link ${active ? "active" : ""}`} title={label} aria-label={label} aria-current={active ? "page" : undefined}>
{icon}
<span className="module-label">{label}</span>
</Link>
+2 -1
View File
@@ -66,7 +66,8 @@ export function LoginPage() {
setBusy(true);
if (trustDevice && !direct) localStorage.setItem("ihasmail:lastUser", username.trim());
const params = new URLSearchParams({ ...(direct ? {} : { username: username.trim() }), ...(trustDevice ? { remember: "1" } : {}), theme: signInTheme() });
window.location.assign(withBase(`/api/auth/oauth/start?${params}`));
// Replace: Back after signing in shouldn't land on this form (#96).
window.location.replace(withBase(`/api/auth/oauth/start?${params}`));
return;
}
if (!username || !password) return;
+3
View File
@@ -19,6 +19,7 @@ import { haptic, useTouchRow } from "@/lib/input/touch";
import { plural, t } from "@/lib/i18n";
import { mailboxDisplayName } from "@/lib/mailbox/mailboxName";
import { useSession } from "@/store/session";
import { useSidewaysReplace } from "@/lib/backStack";
// Loaded when first opened: it is not needed to show mail, and it is not small.
const ShareDialog = lazy(() => import("../settings/ShareDialog").then((m) => ({ default: m.ShareDialog })));
@@ -316,6 +317,7 @@ export function MailboxTree() {
function FolderRow({ mailbox: m, label, depth, hasChildren, open, hiddenUnread, childUnread, onToggle, onDrillIn, currentId, onMenu, dragging, acceptsFolder, onFolderDragStart, onFolderDragEnd, onFolderDrop, canPlace, onFolderPlace }: { mailbox: Mailbox; label: string; depth: number; hasChildren: boolean; open: boolean; hiddenUnread: number; childUnread: number; onToggle: () => void; onDrillIn?: () => void; currentId?: string; onMenu: (m: Mailbox, e: { currentTarget: Element }) => void; dragging: boolean; acceptsFolder: boolean; onFolderDragStart: () => void; onFolderDragEnd: () => void; onFolderDrop: (id: Id) => void; canPlace: (placement: Placement) => boolean; onFolderPlace: (id: Id, placement: Placement) => void }) {
/** Where a drop here would land: in this folder, or just above or below it. */
const [drop, setDrop] = useState<"into" | Placement | null>(null);
const sideways = useSidewaysReplace();
/** Expanding in place and drilling in are the same relationship; only one shows. */
const twisty = hasChildren && !onDrillIn;
// Scheduled counts like Drafts: everything in it is already read, so the
@@ -400,6 +402,7 @@ function FolderRow({ mailbox: m, label, depth, hasChildren, open, hiddenUnread,
return (
<Link
href={`/mail/${m.id}`}
replace={sideways}
className={`nav-item folder-row depth-${Math.min(depth, 4)} ${currentId === m.id ? "active" : ""} ${unread ? "unread" : ""} ${drop === "into" ? "drop-target" : drop ? `drop-${drop}` : ""} ${dragging ? "dragging" : ""}`}
title={label}
{...press}