mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 13:05:14 +03:00
feat(app): mark sessions read or unread from their menus (#62889)
This commit is contained in:
committed by
GitHub
Unverified
parent
be6b87ef48
commit
ed0171f08a
@@ -334,10 +334,11 @@ for (const [tabLayout, direction, label] of [
|
||||
await tabB.click({ button: "right" })
|
||||
await expect(menu.getByRole("menuitem")).toHaveText([
|
||||
"Rename",
|
||||
"Export…",
|
||||
"Close tab",
|
||||
"Close other tabs",
|
||||
label,
|
||||
"Delete session…",
|
||||
"Delete…",
|
||||
])
|
||||
await menu.getByRole("menuitem", { name: "Close other tabs", exact: true }).click()
|
||||
await expect(titles).toHaveText([b.title])
|
||||
@@ -439,7 +440,7 @@ for (const tabLayout of ["horizontal", "vertical"] as const) {
|
||||
await expect(title).toHaveText(a.title)
|
||||
|
||||
await title.click({ button: "right" })
|
||||
await page.getByRole("menuitem", { name: "Delete session…", exact: true }).click()
|
||||
await page.getByRole("menuitem", { name: "Delete…", exact: true }).click()
|
||||
|
||||
const dialog = page.getByRole("dialog")
|
||||
await dialog.getByRole("button", { name: "Cancel", exact: true }).click()
|
||||
@@ -447,7 +448,7 @@ for (const tabLayout of ["horizontal", "vertical"] as const) {
|
||||
await expect(title).toHaveText(a.title)
|
||||
|
||||
await title.click({ button: "right" })
|
||||
await page.getByRole("menuitem", { name: "Delete session…", exact: true }).click()
|
||||
await page.getByRole("menuitem", { name: "Delete…", exact: true }).click()
|
||||
await dialog.getByRole("button", { name: "Delete session", exact: true }).click()
|
||||
await remove
|
||||
await expect(title).toHaveCount(0)
|
||||
@@ -668,6 +669,60 @@ test("an unread tab whose run failed shows the failed dot, as the inbox does", a
|
||||
await expect(tab(b.id).locator('[data-mark="unread"]')).toBeHidden()
|
||||
await expect(tab(c.id).locator('[data-mark="unread"]')).toBeVisible()
|
||||
await expect(tab(c.id).locator('[data-mark="failed"]')).toBeHidden()
|
||||
|
||||
await tab(c.id).locator("[data-titlebar-tab-link]").click({ button: "right" })
|
||||
await page.getByRole("menuitem", { name: "Mark as read", exact: true }).click()
|
||||
await expect(tab(c.id).locator('[data-mark="unread"]')).toBeHidden()
|
||||
await tab(c.id).locator("[data-titlebar-tab-link]").click({ button: "right" })
|
||||
await expect(page.getByRole("menu").getByRole("menuitem")).toHaveText([
|
||||
"Rename",
|
||||
"Export…",
|
||||
"Mark as unread",
|
||||
"Close tab",
|
||||
"Close other tabs",
|
||||
"Close tabs to the right",
|
||||
"Delete…",
|
||||
])
|
||||
await page.getByRole("menuitem", { name: "Mark as unread", exact: true }).click()
|
||||
await expect(tab(c.id).locator('[data-mark="unread"]')).toBeVisible()
|
||||
await tab(c.id).locator("[data-titlebar-tab-link]").click()
|
||||
await expectPath(page, sessionHref(c.id))
|
||||
await tab(c.id).locator("[data-titlebar-tab-link]").click({ button: "right" })
|
||||
await page.getByRole("menuitem", { name: "Mark as unread", exact: true }).click()
|
||||
await page.mouse.move(0, 0)
|
||||
await expect(tab(c.id).locator('[data-mark="unread"]')).toBeVisible()
|
||||
await expect(tab(c.id).locator('[data-slot="tab-status"]')).toHaveCSS("opacity", "1")
|
||||
await tab(a.id).locator("[data-titlebar-tab-link]").click()
|
||||
await expectPath(page, sessionHref(a.id))
|
||||
await expect(tab(c.id).locator('[data-mark="unread"]')).toBeVisible()
|
||||
await tab(c.id).locator("[data-titlebar-tab-link]").click()
|
||||
await expectPath(page, sessionHref(c.id))
|
||||
await expect(tab(c.id).locator('[data-mark="unread"]')).toBeHidden()
|
||||
await tab(a.id).locator("[data-titlebar-tab-link]").click()
|
||||
await expectPath(page, sessionHref(a.id))
|
||||
await expect(tab(c.id).locator('[data-mark="unread"]')).toBeHidden()
|
||||
})
|
||||
|
||||
test("an unfocused window preserves a restored manual unread reminder", async ({ page }) => {
|
||||
// Playwright emulates focus for each page, so model a background window explicitly.
|
||||
await page.addInitScript(() => {
|
||||
document.hasFocus = () => false
|
||||
})
|
||||
await mockWorkspace(page, {
|
||||
name: "BackgroundUnread",
|
||||
sessions: [a, { ...c, time: { created: T0, updated: T0, idle: T0, viewed: T0 } }],
|
||||
seed: { storage: { "opencode.global.dat:notification.manual-unread": { sessions: [c.id] } } },
|
||||
})
|
||||
await page.goto(sessionHref(c.id))
|
||||
await expectSessionTitle(page, c.title)
|
||||
const tab = page.locator(`[data-titlebar-tab-slot]:has(a[href="${sessionHref(c.id)}"])`)
|
||||
|
||||
await expect(tab.locator('[data-mark="unread"]')).toBeVisible()
|
||||
await page.evaluate(() => {
|
||||
document.hasFocus = Document.prototype.hasFocus.bind(document)
|
||||
window.dispatchEvent(new Event("focus"))
|
||||
})
|
||||
await expect(tab.locator('[data-mark="unread"]')).toBeHidden()
|
||||
})
|
||||
|
||||
test("vertical tabs resize, scroll, show shortcut hints, and navigate", async ({ page }) => {
|
||||
@@ -1803,6 +1858,39 @@ test("opening a session from the inbox keeps the list's scroll position and rows
|
||||
expect(await list.evaluate((element) => element.scrollTop)).toBe(offset)
|
||||
})
|
||||
|
||||
test("renaming an inbox card updates the same session on Home", async ({ page }) => {
|
||||
await mockWorkspace(page, {
|
||||
name: "RenameInbox",
|
||||
sessions: [
|
||||
{ id: "ses_rename_inbox", title: "Original inbox title" },
|
||||
{ id: "ses_rename_kept", title: "Unchanged title" },
|
||||
],
|
||||
seed: { settings: { appearance: { tabLayout: "inbox" } }, tabs: [] },
|
||||
})
|
||||
await page.goto("/")
|
||||
const inbox = page.locator('[data-slot="sidebar-inbox"]')
|
||||
|
||||
const home = page.locator('[data-component="home-session-row-container"][data-session-id="ses_rename_inbox"]')
|
||||
|
||||
await expect(home).toContainText("Original inbox title")
|
||||
await inbox
|
||||
.locator('[data-slot="sidebar-inbox-item"]')
|
||||
.filter({ hasText: "Original inbox title" })
|
||||
.click({ button: "right" })
|
||||
await page.getByRole("menuitem", { name: "Rename", exact: true }).click()
|
||||
const dialog = page.getByRole("dialog")
|
||||
|
||||
await dialog.getByRole("textbox").fill("Renamed inbox title")
|
||||
await dialog.getByRole("button", { name: "Save", exact: true }).click()
|
||||
await expect(dialog).toBeHidden()
|
||||
await expect(
|
||||
inbox.locator('[data-slot="sidebar-inbox-item"]').filter({ hasText: "Renamed inbox title" }),
|
||||
).toHaveCount(1)
|
||||
await expect(inbox.getByText("Original inbox title", { exact: true })).toHaveCount(0)
|
||||
await expect(inbox.getByText("Unchanged title", { exact: true })).toBeVisible()
|
||||
await expect(home).toContainText("Renamed inbox title")
|
||||
})
|
||||
|
||||
test("the inbox Archived dialog searches only archived sessions and restores the selected session", async ({
|
||||
page,
|
||||
}) => {
|
||||
@@ -2066,6 +2154,17 @@ test("the inbox keeps every unread result separate from expandable Recent, inclu
|
||||
await inbox.getByRole("button", { name: "Show less", exact: true }).click()
|
||||
await expect(recent).toHaveCount(10)
|
||||
await expect(unread).toHaveCount(12)
|
||||
|
||||
const marked = page.getByText("Read session 1", { exact: true })
|
||||
|
||||
await recent.filter({ has: marked }).click({ button: "right" })
|
||||
await page.getByRole("menuitem", { name: "Mark as unread", exact: true }).click()
|
||||
await expect(unread.filter({ has: marked })).toHaveCount(1)
|
||||
await expect(recent.filter({ has: marked })).toHaveCount(0)
|
||||
await unread.filter({ has: marked }).click({ button: "right" })
|
||||
await page.getByRole("menuitem", { name: "Mark as read", exact: true }).click()
|
||||
await expect(recent.filter({ has: marked })).toHaveCount(1)
|
||||
await expect(unread.filter({ has: marked })).toHaveCount(0)
|
||||
})
|
||||
|
||||
test("the inbox waits for initial requests and orders attention by waiting age rather than last update", async ({
|
||||
|
||||
@@ -388,12 +388,7 @@ const Group = HttpApiGroup.make("mock")
|
||||
.add(
|
||||
HttpApiEndpoint.post("sessionView", "/api/session/:sessionID/view", {
|
||||
params: SessionParams,
|
||||
success: NoContent,
|
||||
}),
|
||||
)
|
||||
.add(
|
||||
HttpApiEndpoint.post("sessionView", "/api/session/:sessionID/view", {
|
||||
params: SessionParams,
|
||||
payload: Schema.Struct({ idle: Schema.Number }),
|
||||
success: NoContent,
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -1263,7 +1263,25 @@ function mockHandlers(
|
||||
}),
|
||||
sessionRemove: () => noContent,
|
||||
sessionShell: () => noContent,
|
||||
sessionView: () => noContent,
|
||||
sessionView: (ctx) =>
|
||||
Effect.sync(() => {
|
||||
const session = config.sessions.find((item) => item.id === ctx.params.sessionID)
|
||||
|
||||
if (!session) return
|
||||
const time = currentSession(session, config.directory).time
|
||||
|
||||
session.time = { ...time, viewed: Math.max(time.viewed ?? 0, ctx.payload.idle) }
|
||||
state.emit([
|
||||
{
|
||||
id: `evt_viewed_${session.id}_${ctx.payload.idle}`,
|
||||
created: Date.now(),
|
||||
type: "session.viewed",
|
||||
durable: { aggregateID: session.id, seq: 1, version: 1 },
|
||||
location: { directory: requestDirectory(config, ctx.request) },
|
||||
data: { sessionID: session.id, idle: ctx.payload.idle },
|
||||
},
|
||||
])
|
||||
}).pipe(Effect.andThen(noContent)),
|
||||
sessionForm: (ctx) =>
|
||||
Effect.succeed({
|
||||
data: resolve(config.forms ?? []).filter(
|
||||
|
||||
@@ -16,6 +16,7 @@ import { ServerConnection } from "@/runtime/server/registry"
|
||||
import { SessionTabAvatarView } from "@/shell/layout/session-tab-avatar"
|
||||
import { showToast } from "@/shell/notifications/toast"
|
||||
import { sessionLabel } from "@/session/title"
|
||||
import { SessionReadMenuItem } from "@/session/session-read-menu-item"
|
||||
import { shouldOpenSessionInBackground } from "./open"
|
||||
import { createAltHold } from "./alt-hold"
|
||||
import "./view.css"
|
||||
@@ -867,6 +868,7 @@ function HomeSessionRow(
|
||||
>
|
||||
{props.language.t("common.export")}…
|
||||
</Menu.Item>
|
||||
<SessionReadMenuItem server={props.server} session={props.record.session} />
|
||||
<Menu.Separator />
|
||||
<Menu.Item onSelect={() => props.onDeleteSession(props.server, props.record.session)}>
|
||||
{props.language.t("common.delete")}…
|
||||
|
||||
@@ -1210,6 +1210,9 @@ export const dict = {
|
||||
"settings.permissions.tool.doom_loop.title": "Doom Loop",
|
||||
"settings.permissions.tool.doom_loop.description": "Detect repeated tool calls with identical input",
|
||||
|
||||
"session.markRead": "Mark as read",
|
||||
"session.markUnread": "Mark as unread",
|
||||
|
||||
"session.delete.failed.title": "Failed to delete session",
|
||||
"session.delete.title": "Delete session",
|
||||
"session.delete.confirm": 'Delete session "{{name}}"?',
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import type { SessionInfo } from "@opencode/client/promise"
|
||||
import { useQueryClient } from "@tanstack/solid-query"
|
||||
import { useLanguage } from "@/runtime/i18n/language"
|
||||
import { ServerConnection } from "@/runtime/server/registry"
|
||||
import { useGlobal } from "@/runtime/server/runtime"
|
||||
|
||||
export function createSessionRenamer() {
|
||||
const global = useGlobal()
|
||||
|
||||
const language = useLanguage()
|
||||
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return async (server: ServerConnection.Key, session: SessionInfo, title: string) => {
|
||||
const ctx = global.serverCtx(server)
|
||||
|
||||
const conn = global.servers.list().find((item) => ServerConnection.key(item) === server)
|
||||
|
||||
if (!ctx || !conn) throw new Error(language.t("common.requestFailed"))
|
||||
await ctx.sdk.api.session.update({ sessionID: session.id, title })
|
||||
ctx.data.session.remember({ ...(ctx.data.session.get(session.id) ?? session), title })
|
||||
ctx.data.session.invalidate(session.id)
|
||||
void ctx.data.session.sync(session.id).catch(() => undefined)
|
||||
void queryClient.invalidateQueries({ queryKey: ["home-sessions", conn], exact: true })
|
||||
void queryClient.invalidateQueries({ queryKey: ["inbox-sessions", conn], exact: true })
|
||||
void queryClient.invalidateQueries({ queryKey: ["inbox-archived-sessions", conn], exact: true })
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,7 @@ import { createSessionResolution } from "./session-resolution"
|
||||
import { SessionScreenView } from "./screen"
|
||||
import { PreparingComposer } from "./preparing-composer"
|
||||
import { ComposerFocusProvider } from "./composer/focus"
|
||||
import { INITIAL_SESSION_VIEWED_STATE, transitionSessionViewed, type SessionViewedState } from "./viewed-machine"
|
||||
|
||||
export function TargetSessionRouteContent() {
|
||||
const params = useParams<{ serverKey: string; id: string }>()
|
||||
@@ -255,6 +256,23 @@ function MarkSessionViewed(props: { sessionID: () => string | undefined }) {
|
||||
makeEventListener(window, "blur", update)
|
||||
makeEventListener(document, "visibilitychange", update)
|
||||
|
||||
const viewed = createMemo<SessionViewedState>(
|
||||
(previous) =>
|
||||
transitionSessionViewed(previous, {
|
||||
type: "changed",
|
||||
sessionID: props.sessionID(),
|
||||
focused: focus.window,
|
||||
ready: notification.session.ready(),
|
||||
}),
|
||||
INITIAL_SESSION_VIEWED_STATE,
|
||||
)
|
||||
|
||||
createEffect(
|
||||
on(viewed, (state) => {
|
||||
if (state.status === "viewed") notification.session.opened(state.sessionID)
|
||||
}),
|
||||
)
|
||||
|
||||
// Viewed state is shared by every client, so only a focused window reports what its user has seen.
|
||||
// markViewed reads the session's idle watermark, so a turn finishing while it stays open re-runs this.
|
||||
createEffect(() => {
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import { createMemo, Show } from "solid-js"
|
||||
import type { SessionInfo } from "@opencode/client/promise"
|
||||
import { Menu } from "@opencode/ui/menu"
|
||||
import { useLanguage } from "@/runtime/i18n/language"
|
||||
import { ServerConnection } from "@/runtime/server/registry"
|
||||
import { useGlobal } from "@/runtime/server/runtime"
|
||||
import { useSessionTabAvatarState } from "@/shell/layout/project-avatar-state"
|
||||
|
||||
export function SessionReadMenuItem(props: { server: ServerConnection.Key; session: SessionInfo | undefined }) {
|
||||
const language = useLanguage()
|
||||
|
||||
const global = useGlobal()
|
||||
|
||||
const session = createMemo(() => {
|
||||
const snapshot = props.session
|
||||
|
||||
return snapshot ? (global.serverCtx(props.server)?.data.session.get(snapshot.id) ?? snapshot) : undefined
|
||||
})
|
||||
|
||||
const status = useSessionTabAvatarState(
|
||||
() => props.server,
|
||||
() => session()?.id ?? "",
|
||||
() => true,
|
||||
session,
|
||||
)
|
||||
|
||||
const select = () => {
|
||||
const ctx = global.serverCtx(props.server)
|
||||
|
||||
const current = session()
|
||||
|
||||
if (!ctx || !current) return
|
||||
|
||||
// Home and inbox indexes may have a session that has not been loaded yet.
|
||||
if (!ctx.data.session.get(current.id)) ctx.data.session.remember(current)
|
||||
|
||||
if (status.unread()) return ctx.notification.session.markRead(current.id)
|
||||
ctx.notification.session.markUnread(current.id)
|
||||
}
|
||||
|
||||
return (
|
||||
<Show
|
||||
when={
|
||||
session() &&
|
||||
global.serverCtx(props.server)?.notification.session.ready() &&
|
||||
!status.loading() &&
|
||||
!status.attention() &&
|
||||
(status.unread() || session()?.time.idle !== undefined)
|
||||
}
|
||||
>
|
||||
<Menu.Item onSelect={select}>{language.t(status.unread() ? "session.markRead" : "session.markUnread")}</Menu.Item>
|
||||
</Show>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { expect, test } from "bun:test"
|
||||
import { INITIAL_SESSION_VIEWED_STATE, transitionSessionViewed } from "./viewed-machine"
|
||||
|
||||
test("a visit is viewed once, after focus and storage are ready", () => {
|
||||
const background = transitionSessionViewed(INITIAL_SESSION_VIEWED_STATE, {
|
||||
type: "changed",
|
||||
sessionID: "a",
|
||||
focused: false,
|
||||
ready: true,
|
||||
})
|
||||
|
||||
expect(background).toEqual({ status: "waiting", sessionID: "a" })
|
||||
const loading = transitionSessionViewed(background, { type: "changed", sessionID: "a", focused: true, ready: false })
|
||||
|
||||
expect(loading).toBe(background)
|
||||
const viewed = transitionSessionViewed(loading, { type: "changed", sessionID: "a", focused: true, ready: true })
|
||||
|
||||
expect(viewed).toEqual({ status: "viewed", sessionID: "a" })
|
||||
expect(transitionSessionViewed(viewed, { type: "changed", sessionID: "a", focused: false, ready: true })).toBe(viewed)
|
||||
expect(transitionSessionViewed(viewed, { type: "changed", sessionID: "a", focused: true, ready: true })).toBe(viewed)
|
||||
const left = transitionSessionViewed(viewed, { type: "changed", sessionID: undefined, focused: true, ready: true })
|
||||
|
||||
expect(left).toBe(INITIAL_SESSION_VIEWED_STATE)
|
||||
expect(transitionSessionViewed(left, { type: "changed", sessionID: "a", focused: true, ready: true })).not.toBe(
|
||||
viewed,
|
||||
)
|
||||
})
|
||||
@@ -0,0 +1,24 @@
|
||||
export type SessionViewedState =
|
||||
| { readonly status: "waiting"; readonly sessionID?: string }
|
||||
| { readonly status: "viewed"; readonly sessionID: string }
|
||||
|
||||
export type SessionViewedEvent = {
|
||||
readonly type: "changed"
|
||||
readonly sessionID: string | undefined
|
||||
readonly focused: boolean
|
||||
readonly ready: boolean
|
||||
}
|
||||
|
||||
export const INITIAL_SESSION_VIEWED_STATE: SessionViewedState = Object.freeze({ status: "waiting" })
|
||||
|
||||
export function transitionSessionViewed(state: SessionViewedState, event: SessionViewedEvent): SessionViewedState {
|
||||
if (!event.sessionID) return INITIAL_SESSION_VIEWED_STATE
|
||||
|
||||
if (state.status === "viewed" && state.sessionID === event.sessionID) return state
|
||||
|
||||
if (event.focused && event.ready) return { status: "viewed", sessionID: event.sessionID }
|
||||
|
||||
if (state.sessionID === event.sessionID) return state
|
||||
|
||||
return { status: "waiting", sessionID: event.sessionID }
|
||||
}
|
||||
@@ -71,6 +71,9 @@ const LEGACY_MIGRATION_PAGE = 200
|
||||
|
||||
const LEGACY_MIGRATION_CONCURRENCY = 4
|
||||
|
||||
// Manual unread is client-local: the server's viewed watermark can only move forwards.
|
||||
const ManualUnreadStore = Persistence.struct({ sessions: Persistence.array(Schema.String) })
|
||||
|
||||
export function createServerNotificationState(input: {
|
||||
sdk: ServerSDK
|
||||
data: Data
|
||||
@@ -129,6 +132,15 @@ export function createServerNotificationState(input: {
|
||||
|
||||
const legacyTarget = Persist.serverGlobal(input.sdk.scope, "notification")
|
||||
const [legacy, setLegacy, , legacyReady] = persisted(legacyTarget, LegacyNotificationStore, {}, platform)
|
||||
|
||||
const [manual, setManual, , manualReady] = persisted(
|
||||
Persist.serverGlobal(input.sdk.scope, "notification.manual-unread"),
|
||||
ManualUnreadStore,
|
||||
{ sessions: [] },
|
||||
platform,
|
||||
)
|
||||
|
||||
const manuallyUnread = createMemo(() => new Set(manual.sessions))
|
||||
const [legacyState, setLegacyState] = createSignal(INITIAL_LEGACY_MIGRATION_STATE)
|
||||
const discovered = Date.now()
|
||||
let legacyRun: AbortController | undefined
|
||||
@@ -241,7 +253,20 @@ export function createServerNotificationState(input: {
|
||||
if (online) reports.forEach((_, root) => dispatchView(root, { type: "connected" }))
|
||||
})
|
||||
|
||||
const unread = (session?: Pick<SessionInfo, "id" | "time">) => selectSessionUnread(legacyState(), session)
|
||||
const unread = (session?: Pick<SessionInfo, "id" | "time">) =>
|
||||
(!!session && manuallyUnread().has(session.id)) || selectSessionUnread(legacyState(), session)
|
||||
|
||||
const updateManual = (sessionID: string, unread: boolean) => {
|
||||
const apply = () => {
|
||||
if (disposed.value || manuallyUnread().has(sessionID) === unread) return
|
||||
setManual("sessions", (sessions) =>
|
||||
unread ? [...sessions, sessionID] : sessions.filter((id) => id !== sessionID),
|
||||
)
|
||||
}
|
||||
|
||||
if (manualReady()) return apply()
|
||||
void manualReady.promise?.then(apply, apply)
|
||||
}
|
||||
|
||||
const roots = (directory: string) =>
|
||||
input.data.session
|
||||
@@ -260,6 +285,13 @@ export function createServerNotificationState(input: {
|
||||
dispatchView(root, { type: "view", idle })
|
||||
}
|
||||
|
||||
const opened = (sessionID: string) => updateManual(input.data.session.root(sessionID), false)
|
||||
|
||||
const read = (sessionID: string) => {
|
||||
opened(sessionID)
|
||||
view(sessionID)
|
||||
}
|
||||
|
||||
// Loading the session keeps a background completion visible in the server-derived unread state.
|
||||
const lookup = async (sessionID: string) => {
|
||||
const session = input.data.session.get(sessionID)
|
||||
@@ -368,6 +400,12 @@ export function createServerNotificationState(input: {
|
||||
}
|
||||
|
||||
const unsub = input.sdk.event.listen((event) => {
|
||||
if (event.type === "session.deleted") {
|
||||
updateManual(event.data.sessionID, false)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (event.type === "permission.asked") {
|
||||
void handleRequest("permission", event.data.sessionID, event.data.id)
|
||||
|
||||
@@ -402,6 +440,7 @@ export function createServerNotificationState(input: {
|
||||
|
||||
return {
|
||||
session: {
|
||||
ready: manualReady,
|
||||
/**
|
||||
* Whether the session's root has an execution result no client has viewed yet. `snapshot` stands in for a root
|
||||
* session that isn't loaded, such as a row from the fetched home index.
|
||||
@@ -409,15 +448,23 @@ export function createServerNotificationState(input: {
|
||||
unread(sessionID: string, snapshot?: SessionInfo) {
|
||||
return unread(input.data.session.get(input.data.session.root(sessionID)) ?? snapshot)
|
||||
},
|
||||
manuallyUnread(sessionID: string) {
|
||||
return manuallyUnread().has(input.data.session.root(sessionID))
|
||||
},
|
||||
/** Reports the session's latest idle transition to the server as viewed. */
|
||||
markViewed: view,
|
||||
opened,
|
||||
markRead: read,
|
||||
markUnread(sessionID: string) {
|
||||
updateManual(input.data.session.root(sessionID), true)
|
||||
},
|
||||
},
|
||||
project: {
|
||||
hasUnread(directory: string) {
|
||||
return roots(directory).some((session) => unread(session))
|
||||
},
|
||||
markViewed(directory: string) {
|
||||
roots(directory).forEach((session) => view(session.id))
|
||||
roots(directory).forEach((session) => read(session.id))
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import { sessionFormRequest, sessionPermissionRequest, sessionTreeIDs } from "@/
|
||||
import { notifySessionTabsRemoved } from "./session-events"
|
||||
import { errorMessage } from "@/shell/layout/helpers"
|
||||
import { showToast } from "@/shell/notifications/toast"
|
||||
import { createSessionRenamer } from "@/session/commands/rename-session"
|
||||
import { workingSince } from "./inbox-working"
|
||||
import {
|
||||
INITIAL_INBOX_BRANCH_STATE,
|
||||
@@ -67,6 +68,8 @@ export function createSessionInbox(options: { archived?: boolean } = {}) {
|
||||
const language = useLanguage()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const rename = createSessionRenamer()
|
||||
|
||||
const [clock, setClock] = createStore({ second: Date.now(), minute: Date.now() })
|
||||
const timer = setInterval(() => setClock("minute", Date.now()), 60_000)
|
||||
onCleanup(() => clearInterval(timer))
|
||||
@@ -454,8 +457,9 @@ export function createSessionInbox(options: { archived?: boolean } = {}) {
|
||||
// Marking viewed reads the loaded session, and rows from the index usually are not loaded, so load it first.
|
||||
markViewed: (entry: InboxEntry) => {
|
||||
if (!entry.ctx.data.session.get(entry.session.id)) entry.ctx.data.session.remember(entry.session)
|
||||
entry.ctx.notification.session.markViewed(entry.session.id)
|
||||
entry.ctx.notification.session.markRead(entry.session.id)
|
||||
},
|
||||
rename: (entry: InboxEntry, title: string) => rename(entry.server, entry.session, title),
|
||||
archive: (entry: InboxEntry) =>
|
||||
entry.ctx.sdk.api.session
|
||||
.update({ sessionID: entry.session.id, archived: true })
|
||||
|
||||
@@ -29,7 +29,10 @@ import { Persist, persisted } from "@/runtime/persistence/storage"
|
||||
import { SessionTabAvatarView } from "@/shell/layout/session-tab-avatar"
|
||||
import type { LocalProject } from "@/shell/state/layout"
|
||||
import { sessionTabTitle } from "./tab-title"
|
||||
import { sessionTitle } from "@/session/title"
|
||||
import { sessionLabel, sessionTitle } from "@/session/title"
|
||||
import { createSessionExporter } from "@/session/commands/export-session"
|
||||
import { SessionReadMenuItem } from "@/session/session-read-menu-item"
|
||||
import { RenameDialog } from "./project-session-row"
|
||||
import { createSessionInbox, inboxArchived, type InboxEntry, type InboxStatus } from "./inbox-model"
|
||||
import { compareWorking, workingSince } from "./inbox-working"
|
||||
|
||||
@@ -49,6 +52,8 @@ export function SidebarInbox() {
|
||||
const servers = useServers()
|
||||
const inbox = createSessionInbox({ archived: true })
|
||||
|
||||
const exporter = createSessionExporter()
|
||||
|
||||
const [state, setState, , ready] = persisted(Persist.global("sidebar.inbox"), InboxState, {
|
||||
workingExpanded: true,
|
||||
recentExpanded: false,
|
||||
@@ -177,6 +182,32 @@ export function SidebarInbox() {
|
||||
branch={inbox.branch(entry())}
|
||||
worktree={inbox.inWorktree(entry())}
|
||||
archived={inboxArchived(entry())}
|
||||
actions={
|
||||
<>
|
||||
<Menu.Item
|
||||
onSelect={() => {
|
||||
const current = entry()
|
||||
|
||||
void dialog.show(() => (
|
||||
<RenameDialog
|
||||
title={sessionLabel(current.session)}
|
||||
onRename={(title) => inbox.rename(current, title)}
|
||||
/>
|
||||
))
|
||||
}}
|
||||
>
|
||||
{language.t("common.rename")}
|
||||
</Menu.Item>
|
||||
<Menu.Item
|
||||
disabled={exporter.exporting({ server: entry().server, sessionID: entry().session.id })}
|
||||
onSelect={() => exporter.export({ server: entry().server, sessionID: entry().session.id })}
|
||||
>
|
||||
{language.t("common.export")}…
|
||||
</Menu.Item>
|
||||
<SessionReadMenuItem server={entry().server} session={entry().session} />
|
||||
<Menu.Separator />
|
||||
</>
|
||||
}
|
||||
onOpen={() => inbox.open(entry())}
|
||||
onArchive={() => void inbox.archive(entry())}
|
||||
onUnarchive={() => void inbox.unarchive(entry())}
|
||||
@@ -556,6 +587,7 @@ export function InboxSessionCard(props: {
|
||||
onArchive: () => void
|
||||
onUnarchive: () => void
|
||||
onDelete: () => void
|
||||
actions?: JSX.Element
|
||||
}) {
|
||||
const language = useLanguage()
|
||||
const navigation = useContext(InboxNavigation)
|
||||
@@ -671,6 +703,7 @@ export function InboxSessionCard(props: {
|
||||
</Menu.Context.Trigger>
|
||||
<Menu.Context.Portal>
|
||||
<Menu.Context.Content>
|
||||
{props.actions}
|
||||
<Show
|
||||
when={props.archived}
|
||||
fallback={<Menu.Item onSelect={props.onArchive}>{language.t("common.archive")}</Menu.Item>}
|
||||
|
||||
@@ -16,6 +16,9 @@ import { errorMessage } from "@/shell/layout/helpers"
|
||||
import { showToast } from "@/shell/notifications/toast"
|
||||
import { sessionTreeIDs } from "@/session/requests/session-request-tree"
|
||||
import { sessionLabel } from "@/session/title"
|
||||
import { createSessionExporter } from "@/session/commands/export-session"
|
||||
import { createSessionRenamer } from "@/session/commands/rename-session"
|
||||
import { SessionReadMenuItem } from "@/session/session-read-menu-item"
|
||||
import { notifySessionTabsRemoved } from "./session-events"
|
||||
import {
|
||||
createSessionRenameDialogState,
|
||||
@@ -45,6 +48,12 @@ export function ProjectSessionRow(props: {
|
||||
|
||||
const ctx = () => global.serverCtx(props.server)
|
||||
|
||||
const exporter = createSessionExporter()
|
||||
|
||||
const rename = createSessionRenamer()
|
||||
|
||||
const exportTarget = () => ({ server: props.server, sessionID: props.session.id })
|
||||
|
||||
const status = useSessionTabAvatarState(
|
||||
() => props.server,
|
||||
() => props.session.id,
|
||||
@@ -52,18 +61,6 @@ export function ProjectSessionRow(props: {
|
||||
() => props.session,
|
||||
)
|
||||
|
||||
// Refreshes the title even when the dialog already stopped waiting, so a late save still shows.
|
||||
const rename = async (title: string) => {
|
||||
const current = ctx()
|
||||
|
||||
if (!current) throw new Error(language.t("common.requestFailed"))
|
||||
await current.sdk.api.session.update({ sessionID: props.session.id, title })
|
||||
current.data.session.remember({ ...(current.data.session.get(props.session.id) ?? props.session), title })
|
||||
current.data.session.invalidate(props.session.id)
|
||||
void current.data.session.sync(props.session.id).catch(() => undefined)
|
||||
void queryClient.invalidateQueries({ queryKey: ["home-sessions", connection()], exact: true })
|
||||
}
|
||||
|
||||
function DeleteDialog() {
|
||||
const confirm = () => {
|
||||
const current = ctx()
|
||||
@@ -164,12 +161,26 @@ export function ProjectSessionRow(props: {
|
||||
>
|
||||
<Menu.Item
|
||||
disabled={!ctx()}
|
||||
onSelect={() => dialog.show(() => <RenameDialog title={sessionLabel(props.session)} onRename={rename} />)}
|
||||
onSelect={() =>
|
||||
dialog.show(() => (
|
||||
<RenameDialog
|
||||
title={sessionLabel(props.session)}
|
||||
onRename={(title) => rename(props.server, props.session, title)}
|
||||
/>
|
||||
))
|
||||
}
|
||||
>
|
||||
{language.t("common.rename")}
|
||||
</Menu.Item>
|
||||
<Menu.Item
|
||||
disabled={!ctx() || exporter.exporting(exportTarget())}
|
||||
onSelect={() => exporter.export(exportTarget())}
|
||||
>
|
||||
{language.t("common.export")}…
|
||||
</Menu.Item>
|
||||
<SessionReadMenuItem server={props.server} session={props.session} />
|
||||
<Menu.Separator />
|
||||
<Menu.Item data-variant="critical" disabled={!ctx()} onSelect={() => dialog.show(() => <DeleteDialog />)}>
|
||||
<Menu.Item disabled={!ctx()} onSelect={() => dialog.show(() => <DeleteDialog />)}>
|
||||
{language.t("common.delete")}…
|
||||
</Menu.Item>
|
||||
</Menu.Context.Content>
|
||||
@@ -178,7 +189,7 @@ export function ProjectSessionRow(props: {
|
||||
)
|
||||
}
|
||||
|
||||
function RenameDialog(props: { title: string; onRename: (title: string) => Promise<void> }) {
|
||||
export function RenameDialog(props: { title: string; onRename: (title: string) => Promise<void> }) {
|
||||
const language = useLanguage()
|
||||
|
||||
const dialog = useDialog()
|
||||
|
||||
@@ -14,6 +14,8 @@ import { displayName } from "@opencode/ui/project-avatar"
|
||||
import { SessionTabAvatarView } from "@/shell/layout/session-tab-avatar"
|
||||
import type { SessionInfo } from "@opencode/client/promise"
|
||||
import { showToast } from "@/shell/notifications/toast"
|
||||
import { createSessionExporter } from "@/session/commands/export-session"
|
||||
import { SessionReadMenuItem } from "@/session/session-read-menu-item"
|
||||
import { sessionTabTitle } from "./tab-title"
|
||||
import { TabPreviewPopover } from "./tab-popover"
|
||||
import {
|
||||
@@ -103,11 +105,15 @@ export function TabNavItem(
|
||||
const servers = useServers()
|
||||
const serverCtx = useServerCtx(() => servers.list.find((item) => ServerConnection.key(item) === props.server))
|
||||
|
||||
const exporter = createSessionExporter()
|
||||
|
||||
// The project icon stays put; status shows where the close button appears on hover.
|
||||
const status = () => {
|
||||
if (props.preparing || props.loading) return "busy"
|
||||
|
||||
if (!props.session || props.active) return
|
||||
if (!props.session) return
|
||||
|
||||
if (props.active && !serverCtx()?.notification.session.manuallyUnread(props.session.id)) return
|
||||
|
||||
if (props.attention) return "attention"
|
||||
|
||||
@@ -571,6 +577,18 @@ export function TabNavItem(
|
||||
<Menu.Item disabled={!props.session} onSelect={() => startRename("menu")}>
|
||||
{language.t("common.rename")}
|
||||
</Menu.Item>
|
||||
<Menu.Item
|
||||
disabled={!props.session || exporter.exporting({ server: props.server, sessionID: props.session.id })}
|
||||
onSelect={() => {
|
||||
const session = props.session
|
||||
|
||||
if (session) exporter.export({ server: props.server, sessionID: session.id })
|
||||
}}
|
||||
>
|
||||
{language.t("common.export")}…
|
||||
</Menu.Item>
|
||||
<SessionReadMenuItem server={props.server} session={props.session} />
|
||||
<Menu.Separator />
|
||||
<TabCloseMenuItems
|
||||
orientation={props.orientation}
|
||||
onClose={props.onClose}
|
||||
@@ -578,8 +596,8 @@ export function TabNavItem(
|
||||
onCloseAfter={props.onCloseAfter}
|
||||
/>
|
||||
<Menu.Separator />
|
||||
<Menu.Item data-variant="critical" disabled={!props.session} onSelect={props.onDelete}>
|
||||
{language.t("session.delete.title")}…
|
||||
<Menu.Item disabled={!props.session} onSelect={props.onDelete}>
|
||||
{language.t("common.delete")}…
|
||||
</Menu.Item>
|
||||
</Menu.Context.Content>
|
||||
</Menu.Context.Portal>
|
||||
|
||||
@@ -6,6 +6,7 @@ import type { ServerConnection } from "@/runtime/server/registry"
|
||||
import { ServerScope } from "@/runtime/server/scope"
|
||||
import type { Tab } from "@/shell/tabs/tabs"
|
||||
import type { createNotificationCoordinator } from "@/shell/notifications/coordinator"
|
||||
import type { AsyncStorage } from "@solid-primitives/storage"
|
||||
|
||||
type NotificationDelivery = Parameters<ReturnType<typeof createNotificationCoordinator>["system"]>[1]
|
||||
|
||||
@@ -22,6 +23,8 @@ const clicks: Array<() => void> = []
|
||||
|
||||
const selected: string[] = []
|
||||
|
||||
const desktopStorage = new Map<string, AsyncStorage>()
|
||||
|
||||
const tabs = {
|
||||
store: new Array<Tab>(),
|
||||
addSessionTab: (input: { server: ServerConnection.Key; sessionId: string }) => ({
|
||||
@@ -63,7 +66,9 @@ beforeAll(async () => {
|
||||
mock.module("@/runtime/platform/platform", () => ({
|
||||
...real.platform,
|
||||
usePlatform: () => ({
|
||||
platform: "web",
|
||||
platform: desktopStorage.has("desktop") ? "desktop" : "web",
|
||||
windowID: "notification-test",
|
||||
storage: () => desktopStorage.get("desktop"),
|
||||
notify: async (title: string, _description?: string, onClick?: () => void) => {
|
||||
alerts.push(title)
|
||||
|
||||
@@ -109,6 +114,7 @@ beforeEach(() => {
|
||||
sounds.length = 0
|
||||
clicks.length = 0
|
||||
selected.length = 0
|
||||
desktopStorage.clear()
|
||||
Object.assign(preferences, {
|
||||
autoApprove: false,
|
||||
agent: true,
|
||||
@@ -292,6 +298,155 @@ test.each([
|
||||
dispose()
|
||||
})
|
||||
|
||||
test("manual unread edits wait for desktop storage to load", async () => {
|
||||
const loaded = Promise.withResolvers<string | null>()
|
||||
|
||||
const hydrated = Promise.withResolvers<void>()
|
||||
|
||||
desktopStorage.set("desktop", {
|
||||
getItem: (key) => (key === "notification.manual-unread" ? loaded.promise : Promise.resolve(null)),
|
||||
setItem: async () => {},
|
||||
removeItem: async () => {},
|
||||
})
|
||||
|
||||
const mounted = createRoot((dispose) => {
|
||||
const state = createState({
|
||||
sdk: {
|
||||
scope: ServerScope.local,
|
||||
connection: { status: () => "connected" },
|
||||
event: { listen: () => () => {} },
|
||||
},
|
||||
data: {
|
||||
session: {
|
||||
root: (id: string) => id,
|
||||
get: () => ({ id: "desktop-marked", time: { idle: 1, viewed: 1 } }),
|
||||
},
|
||||
},
|
||||
coordinator: {},
|
||||
})
|
||||
|
||||
createEffect(() => {
|
||||
if (state.session.ready()) hydrated.resolve()
|
||||
})
|
||||
|
||||
return { dispose, state }
|
||||
})
|
||||
|
||||
try {
|
||||
expect(mounted.state.session.ready()).toBe(false)
|
||||
mounted.state.session.opened("desktop-marked")
|
||||
mounted.state.session.markUnread("desktop-new")
|
||||
loaded.resolve(JSON.stringify({ sessions: ["desktop-marked", "desktop-other"] }))
|
||||
await hydrated.promise
|
||||
expect(mounted.state.session.manuallyUnread("desktop-marked")).toBe(false)
|
||||
expect(mounted.state.session.manuallyUnread("desktop-other")).toBe(true)
|
||||
expect(mounted.state.session.manuallyUnread("desktop-new")).toBe(true)
|
||||
} finally {
|
||||
mounted.dispose()
|
||||
}
|
||||
})
|
||||
|
||||
test("manual unread marks survive remounts and clear on read, reopen or deletion", () => {
|
||||
const views: { sessionID: string; idle: number }[] = []
|
||||
|
||||
const listeners = new Set<(event: OpenCodeEvent) => void>()
|
||||
|
||||
const [sessions, setSessions] = createStore({
|
||||
marked: { id: "manual-marked", time: { idle: 3, viewed: 3 }, location: { directory: "/manual" } },
|
||||
other: { id: "manual-other", time: { idle: 4, viewed: 4 }, location: { directory: "/manual" } },
|
||||
})
|
||||
|
||||
const start = () =>
|
||||
createRoot((dispose) => ({
|
||||
dispose,
|
||||
state: createState({
|
||||
sdk: {
|
||||
scope: ServerScope.local,
|
||||
connection: { status: () => "connected" },
|
||||
event: {
|
||||
listen: (listener: (event: OpenCodeEvent) => void) => {
|
||||
listeners.add(listener)
|
||||
|
||||
return () => listeners.delete(listener)
|
||||
},
|
||||
},
|
||||
api: {
|
||||
session: {
|
||||
view: async (input: { sessionID: string; idle: number }) => {
|
||||
views.push(input)
|
||||
setSessions("marked", "time", "viewed", input.idle)
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
data: {
|
||||
session: {
|
||||
root: (id: string) => (id === "manual-child" ? sessions.marked.id : id),
|
||||
get: (id: string) => Object.values(sessions).find((session) => session.id === id),
|
||||
list: () => Object.values(sessions),
|
||||
},
|
||||
},
|
||||
coordinator: {},
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.useFakeTimers()
|
||||
const first = start()
|
||||
|
||||
try {
|
||||
first.state.session.markUnread("manual-child")
|
||||
first.state.session.markUnread(sessions.marked.id)
|
||||
first.state.session.markUnread(sessions.other.id)
|
||||
expect(first.state.session.unread("manual-child")).toBe(true)
|
||||
expect(first.state.project.hasUnread("/manual")).toBe(true)
|
||||
expect(views).toEqual([])
|
||||
vi.runOnlyPendingTimers()
|
||||
first.dispose()
|
||||
|
||||
const second = start()
|
||||
|
||||
try {
|
||||
expect(second.state.session.unread(sessions.marked.id)).toBe(true)
|
||||
second.state.session.markRead("manual-child")
|
||||
expect(second.state.session.unread(sessions.marked.id)).toBe(false)
|
||||
expect(second.state.session.unread(sessions.other.id)).toBe(true)
|
||||
expect(views).toEqual([])
|
||||
second.state.session.markUnread(sessions.marked.id)
|
||||
setSessions("marked", "time", "idle", 5)
|
||||
second.state.session.markViewed("manual-child")
|
||||
expect(second.state.session.unread(sessions.marked.id)).toBe(true)
|
||||
second.state.session.opened("manual-child")
|
||||
expect(second.state.session.unread(sessions.marked.id)).toBe(false)
|
||||
second.state.session.markUnread(sessions.marked.id)
|
||||
listeners.forEach((listener) =>
|
||||
listener({
|
||||
type: "session.deleted",
|
||||
id: "manual-deleted",
|
||||
created: 0,
|
||||
location: sessions.marked.location,
|
||||
data: { sessionID: sessions.marked.id },
|
||||
}),
|
||||
)
|
||||
expect(second.state.session.unread(sessions.marked.id)).toBe(false)
|
||||
second.state.project.markViewed("/manual")
|
||||
expect(second.state.project.hasUnread("/manual")).toBe(false)
|
||||
|
||||
setSessions("marked", "time", "idle", 6)
|
||||
second.state.session.markRead(sessions.marked.id)
|
||||
expect(views).toEqual([
|
||||
{ sessionID: sessions.marked.id, idle: 5 },
|
||||
{ sessionID: sessions.marked.id, idle: 6 },
|
||||
])
|
||||
expect(second.state.session.unread(sessions.marked.id)).toBe(false)
|
||||
} finally {
|
||||
second.dispose()
|
||||
}
|
||||
} finally {
|
||||
first.dispose()
|
||||
vi.useRealTimers()
|
||||
}
|
||||
})
|
||||
|
||||
test("unread state and viewing follow the server's idle and viewed watermarks", async () => {
|
||||
const views: { sessionID: string; idle: number }[] = []
|
||||
const failures = { remaining: 1 }
|
||||
|
||||
Reference in New Issue
Block a user