Skip to content

Commit fcccb17

Browse files
committed
feat(app): mark a home session unread from the context menu
Add a "Mark as unread" item to the home session list context menu. The mark is client-local and clears when the session is viewed. The server's viewed watermark is monotone, so a manual unread cannot be expressed by rewriting time.viewed; merge the mark into the existing unread computation instead. Refs anomalyco#58192
1 parent cdf1c20 commit fcccb17

7 files changed

Lines changed: 121 additions & 4 deletions

File tree

‎packages/app/src/home/sessions/controller.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,9 @@ export function createHomeSessionsController(home: HomeController) {
376376
}),
377377
})
378378
},
379+
markUnread: (session: SessionInfo) => {
380+
home.server.focusedContext()?.notification.session.markUnread(session.id)
381+
},
379382
rename,
380383
export: exportSession,
381384
showDelete: (server: ServerConnection.Key, session: SessionInfo) =>
@@ -408,7 +411,10 @@ function groupSessions(records: HomeSessionRecord[], language: ReturnType<typeof
408411
const now = new Date()
409412
const today = localDay(now)
410413
const yesterday = localDay(new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1))
411-
const day = (record: HomeSessionRecord) => localDay(new Date(record.session.time.updated ?? record.session.time.created))
414+
415+
const day = (record: HomeSessionRecord) =>
416+
localDay(new Date(record.session.time.updated ?? record.session.time.created))
417+
412418
const todaySessions = records.filter((record) => day(record) === today)
413419
const yesterdaySessions = records.filter((record) => day(record) === yesterday)
414420
const olderSessions = records.filter((record) => day(record) !== today && day(record) !== yesterday)

‎packages/app/src/home/sessions/region.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export function HomeSessions(props: {
3434
onCreateSession={props.sessions.session.create}
3535
onOpenSession={props.sessions.session.open}
3636
onArchiveSession={props.sessions.session.archive}
37+
onMarkUnread={props.sessions.session.markUnread}
3738
onRenameSession={props.sessions.session.rename}
3839
onExportSession={props.sessions.session.export}
3940
onDeleteSession={props.sessions.session.showDelete}

‎packages/app/src/home/sessions/view.tsx‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const HOME_SESSION_LONG_PRESS_MS = 500
3838
function isBackgroundOpen(event: MouseEvent) {
3939
return shouldOpenSessionInBackground({
4040
button: event.button,
41-
mac: typeof navigator === "object" && /(Mac|iPod|iPhone|iPad)/.test(navigator.platform),
41+
mac: /(Mac|iPod|iPhone|iPad)/.test(globalThis.navigator?.platform ?? ""),
4242
meta: event.metaKey,
4343
ctrl: event.ctrlKey,
4444
shift: event.shiftKey,
@@ -68,6 +68,7 @@ export type HomeSessionsViewProps = {
6868
onCreateSession: () => void
6969
onOpenSession: (session: SessionInfo, options?: OpenSessionOptions) => void
7070
onArchiveSession: (session: SessionInfo) => Promise<void>
71+
onMarkUnread: (session: SessionInfo) => void
7172
onRenameSession: (server: ServerConnection.Key, session: SessionInfo, title: string) => Promise<boolean>
7273
onExportSession: (server: ServerConnection.Key, session: SessionInfo) => Promise<void>
7374
onDeleteSession: (server: ServerConnection.Key, session: SessionInfo) => void
@@ -779,6 +780,18 @@ function HomeSessionRow(
779780
requestAnimationFrame(() => rowButton()?.focus())
780781
}}
781782
>
783+
<HomeSessionStatusController
784+
server={props.server}
785+
record={props.record}
786+
isOpenTab={props.isOpenTab}
787+
render={(state) => (
788+
<Show when={!state.unread()}>
789+
<Menu.Item onSelect={() => props.onMarkUnread(props.record.session)}>
790+
{props.language.t("common.markUnread")}
791+
</Menu.Item>
792+
</Show>
793+
)}
794+
/>
782795
<Menu.Item onSelect={openEditor}>{props.language.t("common.rename")}</Menu.Item>
783796
<Menu.Item onSelect={() => void props.onExportSession(props.server, props.record.session)}>
784797
{props.language.t("common.export")}…

‎packages/app/src/runtime/i18n/en.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ export const dict = {
617617
"common.moreOptions": "More options",
618618
"common.learnMore": "Learn more",
619619
"common.rename": "Rename",
620+
"common.markUnread": "Mark as unread",
620621
"common.export": "Export",
621622
"common.reset": "Reset",
622623
"common.archive": "Archive",

‎packages/app/src/shell/notifications/notification.tsx‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ const LegacyNotificationStore = Persistence.struct({
4242
started: Schema.optional(Schema.Finite),
4343
})
4444

45+
// Sessions marked unread by hand. This is client-local on purpose: the server's viewed watermark is monotone, so a
46+
// manual mark is merged into the computed state instead of rewriting `time.viewed`.
47+
const ManualUnreadStore = Persistence.struct({
48+
sessions: Persistence.array(Schema.String),
49+
})
50+
4551
// The carry-over waits this long after connecting so it doesn't compete with startup requests.
4652
const LEGACY_MIGRATION_DELAY = 5_000
4753

@@ -74,6 +80,8 @@ export function createServerNotificationState(input: {
7480

7581
const legacyTarget = Persist.serverGlobal(input.sdk.scope, "notification")
7682
const [legacy, setLegacy, , legacyReady] = persisted(legacyTarget, LegacyNotificationStore, {}, platform)
83+
const manualTarget = Persist.serverGlobal(input.sdk.scope, "notification.manual-unread")
84+
const [manual, setManual] = persisted(manualTarget, ManualUnreadStore, { sessions: [] }, platform)
7785
const started = Date.now()
7886
const legacyStarted = () => legacy.started ?? started
7987

@@ -89,11 +97,17 @@ export function createServerNotificationState(input: {
8997
// Idle watermarks the carry-over reported, so rows drawn from an older fetched copy of a session stay read.
9098
const legacyReported = new Map<string, number>()
9199

100+
const manuallyUnread = (sessionID: string) => manual.sessions.includes(sessionID)
101+
92102
const unread = (session?: Pick<SessionInfo, "id" | "time">) => {
93-
const idle = session?.time.idle
103+
if (!session) return false
104+
105+
if (manuallyUnread(session.id)) return true
106+
107+
const idle = session.time.idle
94108

95109
// Until the old list is read, a session it had read could flash unread.
96-
if (!legacyReady() || !session || idle === undefined || !isSessionUnread(session)) return false
110+
if (!legacyReady() || idle === undefined || !isSessionUnread(session)) return false
97111

98112
if (idle <= (legacyReported.get(session.id) ?? -Infinity)) return false
99113
const unseen = legacyUnseen()
@@ -177,6 +191,10 @@ export function createServerNotificationState(input: {
177191
const view = (sessionID: string) => {
178192
void state.viewRetry
179193
const root = input.data.session.root(sessionID)
194+
195+
// Opening a manually marked session clears the mark before the usual viewed-watermark logic runs.
196+
if (manuallyUnread(root)) setManual("sessions", (sessions) => sessions.filter((id) => id !== root))
197+
180198
const session = input.data.session.get(root)
181199
const idle = session?.time.idle
182200

@@ -204,6 +222,12 @@ export function createServerNotificationState(input: {
204222
)
205223
}
206224

225+
// Only a confirmed deletion drops a mark. Rows disappearing from a filtered or paginated list are not deletions.
226+
const forget = (sessionID: string) => {
227+
if (!manual.sessions.includes(sessionID)) return
228+
setManual("sessions", (sessions) => sessions.filter((id) => id !== sessionID))
229+
}
230+
207231
// Loading the session keeps a background completion visible in the server-derived unread state.
208232
const lookup = async (sessionID: string) => {
209233
const session = input.data.session.get(sessionID)
@@ -265,6 +289,12 @@ export function createServerNotificationState(input: {
265289
}
266290

267291
const unsub = input.sdk.event.listen((event) => {
292+
if (event.type === "session.deleted") {
293+
forget(event.data.sessionID)
294+
295+
return
296+
}
297+
268298
if (event.type === "session.execution.failed") {
269299
handleSessionError(event.data.sessionID, event.id)
270300

@@ -290,6 +320,13 @@ export function createServerNotificationState(input: {
290320
},
291321
/** Reports the session's latest idle transition to the server as viewed. */
292322
markViewed: view,
323+
/** Marks the session's root unread until this client views it. */
324+
markUnread(sessionID: string) {
325+
const root = input.data.session.root(sessionID)
326+
327+
if (manuallyUnread(root)) return
328+
setManual("sessions", (sessions) => [...sessions, root])
329+
},
293330
},
294331
project: {
295332
hasUnread(directory: string) {

‎packages/app/src/shell/titlebar/tab-nav.tsx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ export function TabNavItem(props: {
6060

6161
const unread = () => !!props.session && !props.active && props.unread
6262

63+
const markUnread = () => {
64+
const session = props.session
65+
66+
if (!session) return
67+
serverCtx()?.notification.session.markUnread(session.id)
68+
}
69+
6370
const project = createMemo(() => {
6471
const session = props.session
6572

@@ -412,6 +419,9 @@ export function TabNavItem(props: {
412419
openRename()
413420
}}
414421
>
422+
<Show when={!!props.session && !props.active && !unread()}>
423+
<Menu.Item onSelect={markUnread}>{language.t("common.markUnread")}</Menu.Item>
424+
</Show>
415425
<Menu.Item disabled={!props.session || rename.isPending} onSelect={() => setMenu("rename", true)}>
416426
{language.t("common.rename")}
417427
</Menu.Item>

‎packages/app/test-browser/server-notification-state.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,47 @@ test.each([
100100
dispose()
101101
})
102102

103+
test("deleting a session drops its manual unread mark", async () => {
104+
const listeners = new Array<(event: OpenCodeEvent) => void>()
105+
const session = { id: "marked", time: { idle: 2, viewed: 2 }, location: { directory: "/project" } }
106+
107+
const dispose = createRoot((dispose) => {
108+
const state = createState({
109+
sdk: {
110+
scope: ServerScope.local,
111+
event: {
112+
listen: (listener: (event: OpenCodeEvent) => void) => {
113+
listeners.push(listener)
114+
115+
return () => {}
116+
},
117+
},
118+
},
119+
data: {
120+
session: {
121+
get: (id: string) => (id === session.id ? session : undefined),
122+
root: (id: string) => id,
123+
},
124+
},
125+
coordinator: {},
126+
})
127+
128+
return { dispose, state }
129+
})
130+
131+
const state = dispose.state
132+
state.session.markUnread("marked")
133+
expect(state.session.unread("marked")).toBe(true)
134+
135+
listeners.forEach((listener) =>
136+
// SAFETY: the deleted event only carries the session ID the state reads.
137+
listener({ type: "session.deleted", id: "event-1", data: { sessionID: "marked" } } as never),
138+
)
139+
140+
expect(state.session.unread("marked")).toBe(false)
141+
dispose.dispose()
142+
})
143+
103144
test("unread state and viewing follow the server's idle and viewed watermarks", async () => {
104145
const views: { sessionID: string; idle: number }[] = []
105146
const failures = { remaining: 1 }
@@ -160,6 +201,14 @@ test("unread state and viewing follow the server's idle and viewed watermarks",
160201
expect(["root", "child", "read", "other"].map((id) => state.session.unread(id))).toEqual([true, true, false, true])
161202
expect(state.project.hasUnread("/project")).toBe(true)
162203

204+
// A manual mark makes a read session unread until the client views it again.
205+
state.session.markUnread("read")
206+
expect(state.session.unread("read")).toBe(true)
207+
setStore("viewing", "read")
208+
await new Promise((resolve) => setTimeout(resolve, 0))
209+
expect(state.session.unread("read")).toBe(false)
210+
expect(views).toEqual([])
211+
163212
// Viewing a child acknowledges its root's idle watermark; a failed report retries after a backoff.
164213
setStore("viewing", "child")
165214
await new Promise((resolve) => setTimeout(resolve, 0))

0 commit comments

Comments
 (0)