Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

feat(community): remember per-server last-opened channel (client navigation memory)

Generdb2ab9e13d95
diff --git a/src/web/src/app/c/channels/[serverId]/[channelId]/page.tsx b/src/web/src/app/c/channels/[serverId]/[channelId]/page.tsx
index 79c5e6404..b10f42b47 100644
--- a/src/web/src/app/c/channels/[serverId]/[channelId]/page.tsx
+++ b/src/web/src/app/c/channels/[serverId]/[channelId]/page.tsx
@@ -20,6 +20,7 @@ import { canManageServer } from "@/components/community/_types"
import type { MentionType } from "@alook/shared"
import { isForum as isForumType, deriveThreadName, USE_SERVER_DEFAULT } from "@alook/shared"
import { resolveRowPresence } from "@/lib/community/presence"
+import { setLastChannel } from "@/lib/community/last-channel"
import { makeUserNameResolver } from "@/lib/community/display-name"
import { avatarInitial } from "@/lib/community/avatar"
import {
@@ -91,6 +92,12 @@ function ChannelView() {
const searchParams = useSearchParams()
const serverId = decodeURIComponent(params.serverId)
const channelId = params.channelId
+ // Remember this as the server's last-opened channel (per-browser navigation
+ // memory) so re-entering the server restores here instead of the default.
+ // Pure localStorage write; failures are swallowed in the helper.
+ useEffect(() => {
+ setLastChannel(serverId, channelId)
+ }, [serverId, channelId])
// Cross-channel "jump to message" target, captured ONCE at mount from `?msg=`.
// `ChannelView` is keyed by `serverId/channelId`, so a fresh jump remounts and
// re-reads this. The param is stripped from the URL right after (below) so a
diff --git a/src/web/src/app/c/channels/[serverId]/page.tsx b/src/web/src/app/c/channels/[serverId]/page.tsx
index d1813a9c9..f7f689787 100644
--- a/src/web/src/app/c/channels/[serverId]/page.tsx
+++ b/src/web/src/app/c/channels/[serverId]/page.tsx
@@ -5,13 +5,15 @@ import { useParams, useRouter } from "next/navigation"
import { MessageList } from "@/components/community/message-list"
import { Skeleton } from "@/components/ui/skeleton"
import { useServer } from "@/hooks/community/use-servers"
+import { getLastChannel, pickServerLandingChannel } from "@/lib/community/last-channel"
/**
* /c/channels/:serverId
*
- * Redirects to the first channel by position. Shows a channel-shell skeleton
- * while waiting for the server detail so the transition feels like a reveal
- * rather than a swap.
+ * Restores the last channel opened in this server (per-browser memory), falling
+ * back to the first channel by position. Shows a channel-shell skeleton while
+ * waiting for the server detail so the transition feels like a reveal rather
+ * than a swap.
*/
export default function ServerDefaultPage() {
const params = useParams<{ serverId: string }>()
@@ -22,9 +24,14 @@ export default function ServerDefaultPage() {
useEffect(() => {
if (!currentServer) return
const allChannels = currentServer.categories.flatMap((cat) => cat.channels)
- const first = allChannels[0]
- if (first) {
- router.replace(`/c/channels/${serverId}/${first.id}`)
+ // Restore the remembered last channel when it still exists + is visible
+ // here; otherwise the first channel. See `pickServerLandingChannel`.
+ const target = pickServerLandingChannel(
+ allChannels.map((c) => c.id),
+ getLastChannel(serverId),
+ )
+ if (target) {
+ router.replace(`/c/channels/${serverId}/${target}`)
}
}, [currentServer, serverId, router])
diff --git a/src/web/src/lib/community/last-channel.test.ts b/src/web/src/lib/community/last-channel.test.ts
new file mode 100644
index 000000000..2c6f132e4
--- /dev/null
+++ b/src/web/src/lib/community/last-channel.test.ts
@@ -0,0 +1,93 @@
+import { describe, it, expect, beforeEach, vi } from "vitest"
+import {
+ lastChannelKey,
+ getLastChannel,
+ setLastChannel,
+ pickServerLandingChannel,
+} from "./last-channel"
+
+describe("last-channel", () => {
+ let storage: Record<string, string>
+
+ beforeEach(() => {
+ storage = {}
+ vi.unstubAllGlobals()
+ vi.stubGlobal("window", {})
+ vi.stubGlobal("localStorage", {
+ getItem: vi.fn((key: string) => storage[key] ?? null),
+ setItem: vi.fn((key: string, value: string) => {
+ storage[key] = value
+ }),
+ removeItem: vi.fn((key: string) => {
+ delete storage[key]
+ }),
+ })
+ })
+
+ it("namespaces the key under the last-channel prefix", () => {
+ expect(lastChannelKey("srv_1")).toBe("community:lastChannel:srv_1")
+ })
+
+ it("returns null when nothing is stored for the server", () => {
+ expect(getLastChannel("srv_1")).toBeNull()
+ })
+
+ it("round-trips a channel id per server", () => {
+ setLastChannel("srv_1", "ch_9")
+ setLastChannel("srv_2", "ch_3")
+ expect(getLastChannel("srv_1")).toBe("ch_9")
+ expect(getLastChannel("srv_2")).toBe("ch_3")
+ })
+
+ it("overwrites the prior channel for the same server", () => {
+ setLastChannel("srv_1", "ch_1")
+ setLastChannel("srv_1", "ch_2")
+ expect(getLastChannel("srv_1")).toBe("ch_2")
+ })
+
+ it("returns null (never throws) when localStorage.getItem throws", () => {
+ vi.stubGlobal("localStorage", {
+ getItem: vi.fn(() => {
+ throw new Error("SecurityError: localStorage unavailable")
+ }),
+ setItem: vi.fn(),
+ })
+ expect(getLastChannel("srv_1")).toBeNull()
+ })
+
+ it("swallows a throwing localStorage.setItem (best-effort, no throw)", () => {
+ vi.stubGlobal("localStorage", {
+ getItem: vi.fn(() => null),
+ setItem: vi.fn(() => {
+ throw new Error("QuotaExceededError")
+ }),
+ })
+ expect(() => setLastChannel("srv_1", "ch_1")).not.toThrow()
+ })
+
+ it("is a no-op returning null under SSR (no window)", () => {
+ vi.stubGlobal("window", undefined)
+ expect(getLastChannel("srv_1")).toBeNull()
+ expect(() => setLastChannel("srv_1", "ch_1")).not.toThrow()
+ })
+})
+
+describe("pickServerLandingChannel", () => {
+ it("restores the remembered last channel when it still exists", () => {
+ expect(pickServerLandingChannel(["ch_1", "ch_2", "ch_3"], "ch_2")).toBe("ch_2")
+ })
+
+ it("falls back to the first channel when there is no memory", () => {
+ expect(pickServerLandingChannel(["ch_1", "ch_2"], null)).toBe("ch_1")
+ })
+
+ it("falls back to the first channel when the remembered id is gone (deleted / no access)", () => {
+ // The graceful fallback: a stale id simply isn't in the visible list.
+ expect(pickServerLandingChannel(["ch_1", "ch_2"], "ch_deleted")).toBe("ch_1")
+ })
+
+ it("returns undefined when the server has no channels", () => {
+ expect(pickServerLandingChannel([], "ch_1")).toBeUndefined()
+ expect(pickServerLandingChannel([], null)).toBeUndefined()
+ })
+})
diff --git a/src/web/src/lib/community/last-channel.ts b/src/web/src/lib/community/last-channel.ts
new file mode 100644
index 000000000..8ef8949df
--- /dev/null
+++ b/src/web/src/lib/community/last-channel.ts
@@ -0,0 +1,53 @@
+// Per-server "last opened channel" navigation memory (pure client). Re-entering
+// a server otherwise always lands on the first channel, discarding where you
+// were; this remembers, per browser, the last channel/post opened in each
+// server so the server-landing page can restore to it.
+//
+// Deliberately narrow: it stores ONLY which channel (a serverId -> channelId
+// map), never scroll position or read-to-seq — those are server read-state, a
+// separate concern this must not touch. localStorage-only (no backend, no
+// daemon, no cross-device sync). Pure guarded wrappers (SSR + privacy-mode safe)
+// mirroring `composer-draft.ts` — any failure degrades to "no memory", i.e.
+// exactly today's default-channel behavior, never a throw.
+
+const PREFIX = "community:lastChannel:"
+
+export function lastChannelKey(serverId: string): string {
+ return `${PREFIX}${serverId}`
+}
+
+export function getLastChannel(serverId: string): string | null {
+ if (typeof window === "undefined") return null
+ try {
+ return localStorage.getItem(lastChannelKey(serverId))
+ } catch {
+ return null
+ }
+}
+
+export function setLastChannel(serverId: string, channelId: string): void {
+ if (typeof window === "undefined") return
+ try {
+ localStorage.setItem(lastChannelKey(serverId), channelId)
+ } catch {
+ // Best-effort: private-mode / quota failures just mean no memory this
+ // session, which falls back to the default channel — never surface it.
+ }
+}
+
+/**
+ * Pick which channel the server-landing page should redirect to: the remembered
+ * `last` channel when it still exists among the server's currently-visible
+ * `channelIds` (a deleted / no-longer-accessible id isn't in the list → falls
+ * through — this IS the graceful fallback, no error path needed), else the
+ * first channel by position. `undefined` when the server has no channels.
+ * Extracted pure so the selection rule is unit-tested without a page-render
+ * harness.
+ */
+export function pickServerLandingChannel(
+ channelIds: readonly string[],
+ last: string | null,
+): string | undefined {
+ if (last !== null && channelIds.includes(last)) return last
+ return channelIds[0]
+}