Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(today): add a session menu to pinned and recent rows (#108929)

Shy Alter80d277a5cd28
diff --git a/frontend/src/layout/today/TodaySessionMenu.tsx b/frontend/src/layout/today/TodaySessionMenu.tsx
new file mode 100644
index 000000000000..349046ff45c6
--- /dev/null
+++ b/frontend/src/layout/today/TodaySessionMenu.tsx
@@ -0,0 +1,63 @@
+import { useActions, useValues } from 'kea'
+
+import { IconArchive, IconEllipsis, IconFolderMove, IconPencil, IconPin, IconPinFilled } from '@posthog/icons'
+import { LemonButton, LemonMenu } from '@posthog/lemon-ui'
+
+import { todaySessionMenuLogic } from './todaySessionMenuLogic'
+import { spaceLabel, todaySpacesLogic } from './todaySpacesLogic'
+
+interface TodaySessionMenuProps {
+ sessionId: string
+ pinned: boolean
+ spaceId: string | null
+}
+
+export function TodaySessionMenu({ sessionId, pinned, spaceId }: TodaySessionMenuProps): JSX.Element {
+ const { sortedSpaces } = useValues(todaySpacesLogic)
+ const { pendingSessionIds } = useValues(todaySessionMenuLogic)
+ const { setSessionPinned, startRenaming, archiveSession, moveSession } = useActions(todaySessionMenuLogic)
+ const disabledReason = pendingSessionIds.includes(sessionId) ? 'Saving your last change' : undefined
+ const otherSpaces = sortedSpaces.filter((space) => space.id !== spaceId)
+
+ return (
+ <LemonMenu
+ items={[
+ {
+ label: pinned ? 'Unpin' : 'Pin',
+ icon: pinned ? <IconPinFilled /> : <IconPin />,
+ onClick: () => setSessionPinned(sessionId, !pinned),
+ disabledReason,
+ 'data-attr': 'today-session-pin',
+ },
+ {
+ label: 'Rename',
+ icon: <IconPencil />,
+ onClick: () => startRenaming(sessionId),
+ disabledReason,
+ 'data-attr': 'today-session-rename',
+ },
+ otherSpaces.length
+ ? {
+ label: 'Move to space',
+ icon: <IconFolderMove />,
+ items: otherSpaces.map((space) => ({
+ label: spaceLabel(space),
+ onClick: () => moveSession(sessionId, space.id),
+ disabledReason,
+ 'data-attr': 'today-session-move',
+ })),
+ }
+ : null,
+ {
+ label: 'Archive',
+ icon: <IconArchive />,
+ onClick: () => archiveSession(sessionId, true),
+ disabledReason,
+ 'data-attr': 'today-session-archive',
+ },
+ ]}
+ >
+ <LemonButton size="xsmall" icon={<IconEllipsis />} tooltip="More" data-attr="today-session-menu" />
+ </LemonMenu>
+ )
+}
diff --git a/frontend/src/layout/today/TodaySessionRenameInput.tsx b/frontend/src/layout/today/TodaySessionRenameInput.tsx
new file mode 100644
index 000000000000..1e6dd062a57f
--- /dev/null
+++ b/frontend/src/layout/today/TodaySessionRenameInput.tsx
@@ -0,0 +1,55 @@
+import { useActions } from 'kea'
+import { useRef, useState } from 'react'
+
+import { LemonInput } from '@posthog/lemon-ui'
+
+import { cn } from 'lib/utils/css-classes'
+
+import { todaySessionMenuLogic } from './todaySessionMenuLogic'
+
+interface TodaySessionRenameInputProps {
+ sessionId: string
+ title: string
+ depth?: number
+}
+
+export function TodaySessionRenameInput({ sessionId, title, depth = 0 }: TodaySessionRenameInputProps): JSX.Element {
+ const [value, setValue] = useState(title)
+ const finished = useRef(false)
+ const { renameSession, stopRenaming } = useActions(todaySessionMenuLogic)
+
+ const save = (): void => {
+ if (finished.current) {
+ return
+ }
+ finished.current = true
+ const next = value.trim()
+ if (next && next !== title) {
+ renameSession(sessionId, next)
+ } else {
+ stopRenaming()
+ }
+ }
+
+ return (
+ <div className={cn('TodayPaneRow', depth > 0 && 'TodayPaneRow--nested')}>
+ <LemonInput
+ size="xsmall"
+ fullWidth
+ autoFocus
+ value={value}
+ onChange={setValue}
+ onPressEnter={save}
+ onBlur={save}
+ onKeyDown={(event) => {
+ if (event.key === 'Escape') {
+ finished.current = true
+ stopRenaming()
+ }
+ }}
+ aria-label="Session title"
+ data-attr="today-session-rename-input"
+ />
+ </div>
+ )
+}
diff --git a/frontend/src/layout/today/TodayShell.scss b/frontend/src/layout/today/TodayShell.scss
index d15f088accd4..b958bca89df2 100644
--- a/frontend/src/layout/today/TodayShell.scss
+++ b/frontend/src/layout/today/TodayShell.scss
@@ -234,6 +234,17 @@
white-space: nowrap;
}
+.TodayPaneRow__wrap:has(.TodayPaneRow__meta) {
+ .TodayPaneRow--withAction {
+ padding-right: 8px;
+ }
+
+ &:hover .TodayPaneRow__meta,
+ &:focus-within .TodayPaneRow__meta {
+ visibility: hidden;
+ }
+}
+
.TodayPaneRow__chevron {
flex: 0 0 14px;
width: 14px;
diff --git a/frontend/src/layout/today/TodaySpacesSidebar.tsx b/frontend/src/layout/today/TodaySpacesSidebar.tsx
index 3ccc236344a6..5230b4071f12 100644
--- a/frontend/src/layout/today/TodaySpacesSidebar.tsx
+++ b/frontend/src/layout/today/TodaySpacesSidebar.tsx
@@ -11,6 +11,9 @@ import { urls } from 'scenes/urls'
import { TodayPaneRow } from './TodayPaneRow'
import { TodayPaneSection } from './TodayPaneSection'
+import { TodaySessionMenu } from './TodaySessionMenu'
+import { todaySessionMenuLogic } from './todaySessionMenuLogic'
+import { TodaySessionRenameInput } from './TodaySessionRenameInput'
import { spaceLabel, todaySpacesLogic } from './todaySpacesLogic'
import { TodayWorkItem, shortTimeAgo } from './todayWorkItems'
@@ -33,20 +36,30 @@ export function TodaySpacesSidebar(): JSX.Element {
} = useValues(todaySpacesLogic)
const { toggleSpace, loadSpaces, loadSpaceTasks, loadRecentTasks, toggleSection, setBrowsingSpaces } =
useActions(todaySpacesLogic)
+ const { renamingSessionId } = useValues(todaySessionMenuLogic)
const { location, searchParams } = useValues(router)
const onAi = location.pathname.endsWith('/ai')
+ const pinnedIds = new Set(pinnedItems.map((item) => item.id))
- const renderItem = (item: TodayWorkItem, dataAttr: string): JSX.Element => (
- <TodayPaneRow
- key={`${item.kind}-${item.id}`}
- label={item.title || (item.kind === 'chat' ? 'Untitled chat' : 'Untitled session')}
- icon={<span className="TodayPane__dot" data-kind={item.kind} data-status={item.status ?? undefined} />}
- meta={shortTimeAgo(item.timestamp)}
- to={item.kind === 'chat' ? urls.ai(item.id) : urls.aiTask(item.id)}
- active={onAi && (item.kind === 'chat' ? searchParams.chat : searchParams.task) === item.id}
- dataAttr={dataAttr}
- />
- )
+ const renderItem = (item: TodayWorkItem, dataAttr: string): JSX.Element =>
+ item.kind === 'session' && renamingSessionId === item.id ? (
+ <TodaySessionRenameInput key={`${item.kind}-${item.id}`} sessionId={item.id} title={item.title} />
+ ) : (
+ <TodayPaneRow
+ key={`${item.kind}-${item.id}`}
+ label={item.title || (item.kind === 'chat' ? 'Untitled chat' : 'Untitled session')}
+ icon={<span className="TodayPane__dot" data-kind={item.kind} data-status={item.status ?? undefined} />}
+ meta={shortTimeAgo(item.timestamp)}
+ to={item.kind === 'chat' ? urls.ai(item.id) : urls.aiTask(item.id)}
+ active={onAi && (item.kind === 'chat' ? searchParams.chat : searchParams.task) === item.id}
+ dataAttr={dataAttr}
+ action={
+ item.kind === 'session' ? (
+ <TodaySessionMenu sessionId={item.id} pinned={pinnedIds.has(item.id)} spaceId={item.channel} />
+ ) : null
+ }
+ />
+ )
const hasPinned = pinnedItems.length > 0
@@ -100,19 +113,34 @@ export function TodaySpacesSidebar(): JSX.Element {
) : !recentItems.length ? (
<div className="TodayPane__state">Sessions and chats you open show up here.</div>
) : (
- recentGroups.map((group, index) => (
- <Fragment key={group.key}>
- <div className={cn('TodayPane__group', index === 0 && 'TodayPane__group--first')}>
- {group.label}
+ <>
+ {recentTasksUnavailable && (
+ <div className="TodayPane__state">
+ <span>Some sessions didn’t load.</span>
+ <LemonButton
+ size="xsmall"
+ type="secondary"
+ onClick={() => loadRecentTasks()}
+ data-attr="today-recent-retry"
+ >
+ Try again
+ </LemonButton>
</div>
- {group.items.map((item) =>
- renderItem(
- item,
- item.kind === 'chat' ? 'today-recent-chat' : 'today-recent-session'
- )
- )}
- </Fragment>
- ))
+ )}
+ {recentGroups.map((group, index) => (
+ <Fragment key={group.key}>
+ <div className={cn('TodayPane__group', index === 0 && 'TodayPane__group--first')}>
+ {group.label}
+ </div>
+ {group.items.map((item) =>
+ renderItem(
+ item,
+ item.kind === 'chat' ? 'today-recent-chat' : 'today-recent-session'
+ )
+ )}
+ </Fragment>
+ ))}
+ </>
)}
</TodayPaneSection>
<TodayPaneSection
diff --git a/frontend/src/layout/today/todaySessionMenuLogic.test.ts b/frontend/src/layout/today/todaySessionMenuLogic.test.ts
new file mode 100644
index 000000000000..e93c05c40204
--- /dev/null
+++ b/frontend/src/layout/today/todaySessionMenuLogic.test.ts
@@ -0,0 +1,67 @@
+import { expectLogic } from 'kea-test-utils'
+
+import { useMocks } from '~/mocks/jest'
+import { initKeaTests } from '~/test/init'
+
+import { todaySessionMenuLogic } from './todaySessionMenuLogic'
+import { todaySpacesLogic } from './todaySpacesLogic'
+
+describe('todaySessionMenuLogic', () => {
+ let sessionSpace = 'space-a'
+ let failPatch = false
+
+ beforeEach(() => {
+ sessionSpace = 'space-a'
+ failPatch = false
+ useMocks({
+ get: {
+ '/api/projects/:team_id/task_channels/': [],
+ '/api/projects/:team_id/tasks/': ({ request }) => {
+ const channel = new URL(request.url).searchParams.get('channel')
+ const results = channel === sessionSpace ? [{ id: 'task-1', title: 'Session', channel }] : []
+ return [200, { results, count: results.length }]
+ },
+ },
+ patch: {
+ '/api/projects/:team_id/tasks/:id/': async ({ request }) => {
+ if (failPatch) {
+ return [500, { detail: 'Server error' }]
+ }
+ const body = (await request.json()) as { channel?: string }
+ sessionSpace = body.channel ?? sessionSpace
+ return [200, { id: 'task-1', channel: sessionSpace }]
+ },
+ },
+ })
+ initKeaTests()
+ })
+
+ it('reloads every cached space after a move, so the session leaves its old space', async () => {
+ const spaces = todaySpacesLogic()
+ spaces.mount()
+ const logic = todaySessionMenuLogic()
+ logic.mount()
+ spaces.actions.loadSpaceTasks('space-a')
+ spaces.actions.loadSpaceTasks('space-b')
+ await expectLogic(spaces).toFinishAllListeners()
+
+ logic.actions.moveSession('task-1', 'space-b')
+ await expectLogic(logic).toFinishAllListeners()
+ await expectLogic(spaces).toFinishAllListeners()
+
+ expect(spaces.values.spaceTasks['space-a']).toEqual([])
+ expect(spaces.values.spaceTasks['space-b'].map((task) => task.id)).toEqual(['task-1'])
+ expect(logic.values.pendingSessionIds).toEqual([])
+ })
+
+ it('clears the pending state when an update fails, so the menu works again', async () => {
+ failPatch = true
+ const logic = todaySessionMenuLogic()
+ logic.mount()
+
+ logic.actions.archiveSession('task-1', true)
+ await expectLogic(logic).toDispatchActions(['archiveSession', 'sessionUpdateFailed'])
+
+ expect(logic.values.pendingSessionIds).toEqual([])
+ })
+})
diff --git a/frontend/src/layout/today/todaySessionMenuLogic.ts b/frontend/src/layout/today/todaySessionMenuLogic.ts
new file mode 100644
index 000000000000..74ba16809a11
--- /dev/null
+++ b/frontend/src/layout/today/todaySessionMenuLogic.ts
@@ -0,0 +1,162 @@
+import { MakeLogicType, actions, connect, kea, listeners, path, reducers } from 'kea'
+
+import { lemonToast } from '@posthog/lemon-ui'
+
+import { teamLogic } from 'scenes/teamLogic'
+
+import { tasksPartialUpdate, tasksPinCreate } from 'products/tasks/frontend/generated/api'
+import { PatchedTaskWriteApi, TaskListItemApi } from 'products/tasks/frontend/generated/api.schemas'
+
+import { todaySpacesLogic } from './todaySpacesLogic'
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface todaySessionMenuLogicValues {
+ currentTeamId: number | null // teamLogic
+ spaceTasks: Record<string, TaskListItemApi[]> // todaySpacesLogic
+ pendingSessionIds: string[]
+ renamingSessionId: string | null
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface todaySessionMenuLogicActions {
+ loadPinnedTasks: () => any // todaySpacesLogic
+ loadRecentTasks: () => any // todaySpacesLogic
+ loadSpaceTasks: (spaceId: string) => {
+ spaceId: string
+ } // todaySpacesLogic
+ archiveSession: (
+ sessionId: string,
+ archived: boolean
+ ) => {
+ archived: boolean
+ sessionId: string
+ }
+ moveSession: (
+ sessionId: string,
+ spaceId: string
+ ) => {
+ sessionId: string
+ spaceId: string
+ }
+ renameSession: (
+ sessionId: string,
+ title: string
+ ) => {
+ sessionId: string
+ title: string
+ }
+ sessionUpdateFailed: (sessionId: string) => {
+ sessionId: string
+ }
+ sessionUpdated: (sessionId: string) => {
+ sessionId: string
+ }
+ setSessionPinned: (
+ sessionId: string,
+ pinned: boolean
+ ) => {
+ pinned: boolean
+ sessionId: string
+ }
+ startRenaming: (sessionId: string) => {
+ sessionId: string
+ }
+ stopRenaming: () => {
+ value: true
+ }
+}
+
+export type todaySessionMenuLogicType = MakeLogicType<todaySessionMenuLogicValues, todaySessionMenuLogicActions>
+
+export const todaySessionMenuLogic = kea<todaySessionMenuLogicType>([
+ path(['layout', 'today', 'todaySessionMenuLogic']),
+ connect(() => ({
+ values: [teamLogic, ['currentTeamId'], todaySpacesLogic, ['spaceTasks']],
+ actions: [todaySpacesLogic, ['loadPinnedTasks', 'loadRecentTasks', 'loadSpaceTasks']],
+ })),
+ actions({
+ setSessionPinned: (sessionId: string, pinned: boolean) => ({ sessionId, pinned }),
+ renameSession: (sessionId: string, title: string) => ({ sessionId, title }),
+ archiveSession: (sessionId: string, archived: boolean) => ({ sessionId, archived }),
+ moveSession: (sessionId: string, spaceId: string) => ({ sessionId, spaceId }),
+ sessionUpdated: (sessionId: string) => ({ sessionId }),
+ sessionUpdateFailed: (sessionId: string) => ({ sessionId }),
+ startRenaming: (sessionId: string) => ({ sessionId }),
+ stopRenaming: true,
+ }),
+ reducers({
+ renamingSessionId: [
+ null as string | null,
+ {
+ startRenaming: (_, { sessionId }) => sessionId,
+ stopRenaming: () => null,
+ renameSession: () => null,
+ },
+ ],
+ pendingSessionIds: [
+ [] as string[],
+ {
+ setSessionPinned: (state, { sessionId }) => [...state, sessionId],
+ renameSession: (state, { sessionId }) => [...state, sessionId],
+ archiveSession: (state, { sessionId }) => [...state, sessionId],
+ moveSession: (state, { sessionId }) => [...state, sessionId],
+ sessionUpdated: (state, { sessionId }) => state.filter((id) => id !== sessionId),
+ sessionUpdateFailed: (state, { sessionId }) => state.filter((id) => id !== sessionId),
+ },
+ ],
+ }),
+ listeners(({ actions, values }) => {
+ const update = async (sessionId: string, patch: PatchedTaskWriteApi, failure: string): Promise<boolean> => {
+ try {
+ await tasksPartialUpdate(String(values.currentTeamId), sessionId, patch)
+ actions.sessionUpdated(sessionId)
+ return true
+ } catch {
+ lemonToast.error(failure)
+ actions.sessionUpdateFailed(sessionId)
+ return false
+ }
+ }
+
+ return {
+ setSessionPinned: async ({ sessionId, pinned }) => {
+ try {
+ await tasksPinCreate(String(values.currentTeamId), sessionId, { pinned })
+ actions.sessionUpdated(sessionId)
+ } catch {
+ lemonToast.error(`Couldn’t ${pinned ? 'pin' : 'unpin'} this session. Try again.`)
+ actions.sessionUpdateFailed(sessionId)
+ }
+ },
+ renameSession: async ({ sessionId, title }) => {
+ await update(sessionId, { title, title_manually_set: true }, 'Couldn’t rename this session. Try again.')
+ },
+ archiveSession: async ({ sessionId, archived }) => {
+ const done = await update(
+ sessionId,
+ { archived },
+ `Couldn’t ${archived ? 'archive' : 'restore'} this session. Try again.`
+ )
+ if (done && archived) {
+ lemonToast.success('Session archived', {
+ button: {
+ label: 'Undo',
+ action: () => actions.archiveSession(sessionId, false),
+ dataAttr: 'today-session-archive-undo',
+ },
+ })
+ }
+ },
+ moveSession: async ({ sessionId, spaceId }) => {
+ await update(sessionId, { channel: spaceId }, 'Couldn’t move this session. Try again.')
+ },
+ sessionUpdated: () => {
+ actions.loadPinnedTasks()
+ actions.loadRecentTasks()
+ for (const spaceId of Object.keys(values.spaceTasks)) {
+ actions.loadSpaceTasks(spaceId)
+ }
+ },
+ }
+ }),
+])
diff --git a/frontend/src/layout/today/todayWorkItems.ts b/frontend/src/layout/today/todayWorkItems.ts
index 330efaa1877f..09f0fb2b8225 100644
--- a/frontend/src/layout/today/todayWorkItems.ts
+++ b/frontend/src/layout/today/todayWorkItems.ts
@@ -12,6 +12,7 @@ export interface TodayWorkItem {
title: string
timestamp: string | null
status: string | null
+ channel: string | null
}
export interface TodayWorkGroup {
@@ -27,6 +28,7 @@ export function sessionItem(task: TaskListItemApi): TodayWorkItem {
title: task.title,
timestamp: task.last_activity_at ?? task.updated_at ?? task.created_at ?? null,
status: task.latest_run?.status ?? null,
+ channel: task.channel ?? null,
}
}
@@ -37,6 +39,7 @@ export function chatItem(conversation: ConversationDetail): TodayWorkItem {
title: conversation.title ?? '',
timestamp: conversation.updated_at ?? conversation.created_at,
status: null,
+ channel: null,
}
}