Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Hook up recently updated list (#49296)

Joshen Lim9738035fec07
diff --git a/apps/studio/components/layouts/ExplorerLayout/ExplorerNavHome.tsx b/apps/studio/components/layouts/ExplorerLayout/ExplorerNavHome.tsx
index e25e74a697d..dc8c0bd2892 100644
--- a/apps/studio/components/layouts/ExplorerLayout/ExplorerNavHome.tsx
+++ b/apps/studio/components/layouts/ExplorerLayout/ExplorerNavHome.tsx
@@ -1,5 +1,7 @@
+import { useParams } from 'common'
import { motion } from 'framer-motion'
import { ChevronRight } from 'lucide-react'
+import Link from 'next/link'
import {
EXPLORER_SECTIONS,
@@ -8,12 +10,25 @@ import {
LEVEL_TRANSITION,
rowClassName,
} from './ExplorerLayout.constants'
+import { formatRelativeTimeShort, getRecentlyUpdatedItems } from './ExplorerNavHome.utils'
+import { useCreateChat } from '@/components/interfaces/Explorer/hooks'
+import { useNotebooksInfiniteQuery } from '@/data/content/notebooks/notebooks-infinite-query'
+import { useAiAssistantChatList } from '@/state/ai-assistant-state'
export const ExplorerNavHome = ({
onSelectSection,
}: {
onSelectSection: (section: ExplorerResourceType) => void
}) => {
+ const { ref } = useParams()
+ const { openChat } = useCreateChat()
+
+ const { data: notebooksData } = useNotebooksInfiniteQuery({ projectRef: ref, limit: 100 })
+ const notebooks = notebooksData?.pages.flatMap((page) => page.content) ?? []
+ const chats = useAiAssistantChatList()
+
+ const recentItems = getRecentlyUpdatedItems({ notebooks, chats })
+
return (
<motion.div
key="root"
@@ -48,7 +63,42 @@ export const ExplorerNavHome = ({
<h3 className="mb-2 px-3 font-mono text-sm font-normal uppercase text-foreground-lighter">
Recently updated
</h3>
- <p className="px-3 text-xs text-foreground-lighter">Nothing edited yet</p>
+ {recentItems.length === 0 ? (
+ <p className="px-3 text-xs text-foreground-lighter">Nothing edited yet</p>
+ ) : (
+ recentItems.map((item) => {
+ const Icon = EXPLORER_SECTIONS.find((section) => section.type === item.type)?.icon
+ const content = (
+ <>
+ {Icon && <Icon size={14} className="shrink-0" aria-hidden="true" />}
+ <span className="flex-1 truncate text-left">{item.label}</span>
+ <span className="shrink-0 text-xs text-foreground-lighter">
+ {formatRelativeTimeShort(item.updatedAt)}
+ </span>
+ </>
+ )
+
+ return item.type === 'chat' ? (
+ <button
+ key={item.id}
+ type="button"
+ tabIndex={0}
+ className={rowClassName(false)}
+ onClick={() => openChat(item.id)}
+ >
+ {content}
+ </button>
+ ) : (
+ <Link
+ key={item.id}
+ href={`/project/${ref}/explorer/notebook/${item.id}`}
+ className={rowClassName(false)}
+ >
+ {content}
+ </Link>
+ )
+ })
+ )}
</section>
</motion.div>
)
diff --git a/apps/studio/components/layouts/ExplorerLayout/ExplorerNavHome.utils.test.ts b/apps/studio/components/layouts/ExplorerLayout/ExplorerNavHome.utils.test.ts
new file mode 100644
index 00000000000..bb5ac52a217
--- /dev/null
+++ b/apps/studio/components/layouts/ExplorerLayout/ExplorerNavHome.utils.test.ts
@@ -0,0 +1,101 @@
+import { describe, expect, it } from 'vitest'
+
+import { formatRelativeTimeShort, getRecentlyUpdatedItems } from './ExplorerNavHome.utils'
+import type { NotebookRow } from '@/data/content/notebooks/notebooks-infinite-query'
+import type { ChatSession } from '@/state/ai-assistant-state'
+
+const notebook = (overrides: Partial<NotebookRow> = {}): NotebookRow =>
+ ({
+ id: 'notebook-1',
+ name: 'Signup funnel',
+ updated_at: '2026-01-01T00:00:00.000Z',
+ ...overrides,
+ }) as NotebookRow
+
+const chat = (overrides: Partial<ChatSession> = {}): ChatSession =>
+ ({
+ id: 'chat-1',
+ name: 'Investigate errors',
+ messages: [],
+ createdAt: new Date('2026-01-01T00:00:00.000Z'),
+ updatedAt: new Date('2026-01-01T00:00:00.000Z'),
+ ...overrides,
+ }) as ChatSession
+
+describe('getRecentlyUpdatedItems', () => {
+ it('merges notebooks and chats sorted by recency, most recent first', () => {
+ const notebooks = [notebook({ id: 'n1', updated_at: '2026-01-01T00:00:00.000Z' })]
+ const chats = [chat({ id: 'c1', updatedAt: new Date('2026-02-01T00:00:00.000Z') })]
+
+ expect(getRecentlyUpdatedItems({ notebooks, chats }).map((item) => item.id)).toEqual([
+ 'c1',
+ 'n1',
+ ])
+ })
+
+ it('filters out support chats', () => {
+ const chats = [
+ chat({
+ id: 'support',
+ supportMetadata: { isSupportChat: true } as ChatSession['supportMetadata'],
+ }),
+ ]
+
+ expect(getRecentlyUpdatedItems({ notebooks: [], chats })).toEqual([])
+ })
+
+ it('sorts a chat with no updatedAt yet (not hydrated) after everything else', () => {
+ const chats = [
+ chat({ id: 'rehydrating', updatedAt: undefined as unknown as Date }),
+ chat({ id: 'recent', updatedAt: new Date('2026-01-01T00:00:00.000Z') }),
+ ]
+
+ expect(getRecentlyUpdatedItems({ notebooks: [], chats }).map((item) => item.id)).toEqual([
+ 'recent',
+ 'rehydrating',
+ ])
+ })
+
+ it('truncates to the given limit', () => {
+ const notebooks = Array.from({ length: 10 }, (_, i) =>
+ notebook({ id: `n${i}`, updated_at: new Date(2026, 0, i + 1).toISOString() })
+ )
+
+ const result = getRecentlyUpdatedItems({ notebooks, chats: [], limit: 3 })
+
+ expect(result).toHaveLength(3)
+ expect(result.map((item) => item.id)).toEqual(['n9', 'n8', 'n7'])
+ })
+
+ it('defaults to a limit of 5', () => {
+ const notebooks = Array.from({ length: 10 }, (_, i) =>
+ notebook({ id: `n${i}`, updated_at: new Date(2026, 0, i + 1).toISOString() })
+ )
+
+ expect(getRecentlyUpdatedItems({ notebooks, chats: [] })).toHaveLength(5)
+ })
+})
+
+describe('formatRelativeTimeShort', () => {
+ const now = new Date('2026-01-01T12:00:00.000Z').getTime()
+
+ it('shows minutes under an hour', () => {
+ const timestamp = now - 12 * 60 * 1000
+ expect(formatRelativeTimeShort(timestamp, now)).toBe('12m ago')
+ })
+
+ it('shows "just now" for under a minute', () => {
+ const timestamp = now - 30 * 1000
+ expect(formatRelativeTimeShort(timestamp, now)).toBe('just now')
+ })
+
+ it('shows hours under a day', () => {
+ const timestamp = now - 21 * 60 * 60 * 1000
+ expect(formatRelativeTimeShort(timestamp, now)).toBe('21h ago')
+ })
+
+ it('shows days beyond a day', () => {
+ const timestamp = now - 2 * 24 * 60 * 60 * 1000
+ expect(formatRelativeTimeShort(timestamp, now)).toBe('2d ago')
+ })
+})
diff --git a/apps/studio/components/layouts/ExplorerLayout/ExplorerNavHome.utils.ts b/apps/studio/components/layouts/ExplorerLayout/ExplorerNavHome.utils.ts
new file mode 100644
index 00000000000..3a92dfe3e2a
--- /dev/null
+++ b/apps/studio/components/layouts/ExplorerLayout/ExplorerNavHome.utils.ts
@@ -0,0 +1,64 @@
+import dayjs from 'dayjs'
+
+import type { NotebookRow } from '@/data/content/notebooks/notebooks-infinite-query'
+import type { ChatSession } from '@/state/ai-assistant-state'
+
+export const RECENTLY_UPDATED_ITEMS_LIMIT = 5
+
+export interface RecentlyUpdatedItem {
+ id: string
+ type: 'notebook' | 'chat'
+ label: string
+ updatedAt: number
+}
+
+/**
+ * Merges notebooks and chats into one recency-sorted list. Chats can reach here before
+ * their persisted state has hydrated (no `updatedAt` yet), so those sort last rather than
+ * throwing or floating to the top.
+ */
+export function getRecentlyUpdatedItems({
+ notebooks,
+ chats,
+ limit = RECENTLY_UPDATED_ITEMS_LIMIT,
+}: {
+ notebooks: NotebookRow[]
+ chats: ChatSession[]
+ limit?: number
+}): RecentlyUpdatedItem[] {
+ const notebookItems: RecentlyUpdatedItem[] = notebooks.map((notebook) => ({
+ id: notebook.id,
+ type: 'notebook',
+ label: notebook.name,
+ updatedAt: new Date(notebook.updated_at).getTime(),
+ }))
+
+ const chatItems: RecentlyUpdatedItem[] = chats
+ .filter((chat) => !chat.supportMetadata?.isSupportChat)
+ .map((chat) => ({
+ id: chat.id,
+ type: 'chat',
+ label: chat.name,
+ updatedAt: chat.updatedAt?.getTime() ?? 0,
+ }))
+
+ return [...notebookItems, ...chatItems].sort((a, b) => b.updatedAt - a.updatedAt).slice(0, limit)
+}
+
+/** Compact relative time (`12m`, `21h`, `2d`) for the narrow recent-items row. */
+export function formatRelativeTimeShort(
+ timestamp: number,
+ now: number = Date.now()
+): string | undefined {
+ if (timestamp === 0) return undefined
+
+ const diffMinutes = dayjs(now).diff(timestamp, 'minute')
+ if (diffMinutes < 1) return 'just now'
+ if (diffMinutes < 60) return `${diffMinutes}m ago`
+
+ const diffHours = dayjs(now).diff(timestamp, 'hour')
+ if (diffHours < 24) return `${diffHours}h ago`
+
+ const diffDays = dayjs(now).diff(timestamp, 'day')
+ return `${diffDays}d ago`
+}