Files
supabase/apps/studio/components/interfaces/Explorer/MarkdownCell.tsx
T
Saxon FletcherandJoshen Lim d9742d707f chore(studio): refine Explorer sidebar breadcrumbs (#50188)
Moves Explorer’s back navigation and create actions into a reusable
sidebar breadcrumb header. Reduces product-menu headings globally to
`text-sm` and keeps breadcrumb links free of padding, borders, and
backgrounds.

### How to test

1. Open `/project/<ref>/explorer` and confirm the header shows Explorer
and the SQL Editor switch action.
2. Open Notebooks and Chats. Confirm the header shows `Explorer >
Notebooks/Chats` and the corresponding create action works.
3. Return using the Explorer breadcrumb with a click or Tab + Enter.
Check that the label stays aligned and has no hover background.
4. Open another product, such as Database, and confirm its sidebar
heading uses the smaller font size.

5. At a mobile viewport, open the menu and repeat the notebook/chat
actions and back navigation without closing the sheet. Confirm the
header stays current and disappears when returning to the main menu or
opening another product.

Validation: 18 focused tests, typecheck, formatting, and lint ratchet
passed.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Added a shared Explorer sidebar header with breadcrumbs and contextual
actions for creating notebooks and chats.
- Added keyboard-accessible navigation between the Explorer overview and
notebook or chat sections.
- Added support for customized product menu headers across project
layouts.

- **Improvements**
- Centralized Explorer navigation and actions in the shared sidebar
layout.
- Improved mobile menu updates when navigating between Explorer
resources.
- Refined Explorer home layout and drag-handle behavior across screen
sizes.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2026-09-10 15:38:19 +08:00

154 lines
5.5 KiB
TypeScript

import { Edit } from 'lucide-react'
import { useState } from 'react'
import { Button, cn } from 'ui'
import { Markdown } from '../Markdown'
import { AddCellDropdown } from './AddCellDropdown'
import { MoveCellDropdownContent } from './MoveCellDropdownContent'
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { CodeEditor } from '@/components/ui/CodeEditor/CodeEditor'
import { SortableSection } from '@/components/ui/SortableSection'
import { type MarkdownCell as MarkdownCellSchema } from '@/data/content/notebooks/notebook-schema'
import { useLatest } from '@/hooks/misc/useLatest'
import { useCurrentNotebook, useNotebooksStateSnapshot } from '@/state/notebooks/notebooks-state'
interface MarkdownCellProps {
cell: MarkdownCellSchema
onEdit?: () => void
}
// [Joshen] handleUpdateMarkdown could be shifted into notebook-state as a updateCell action
export const MarkdownCell = ({ cell, onEdit }: MarkdownCellProps) => {
const snap = useNotebooksStateSnapshot()
const currentNotebook = useCurrentNotebook()
const cells = currentNotebook?.notebook.content?.cells ?? []
const [value, setValue] = useState(cell.text)
const [isEditing, setIsEditing] = useState(false)
const valueRef = useLatest(value)
const handleStartEditing = () => {
setValue(cell.text)
setIsEditing(true)
}
const handleCancel = () => {
setIsEditing(false)
}
const handleUpdateMarkdown = (cellId: string, text: string) => {
const notebookId = currentNotebook?.notebook.id
if (!notebookId) return
onEdit?.()
const nextCells = cells.map((c) => (c._id === cellId ? { ...c, text } : c))
snap.updateCells({ id: notebookId, cells: nextCells })
setIsEditing(false)
}
const handleUpdateMarkdownRef = useLatest(handleUpdateMarkdown)
return (
<SortableSection
id={cell._id}
actions={<AddCellDropdown cellId={cell._id} />}
gripDropdownContent={<MoveCellDropdownContent cellId={cell._id} />}
gripClassName="mt-1.5 sm:opacity-0 group-hover:opacity-100 has-[[data-state=open]]:opacity-100 transition"
>
{isEditing ? (
<div
className={cn('w-full max-w-3xl mx-auto transition', 'overflow-hidden border rounded-md')}
>
<CodeEditor
hideLineNumbers
language="markdown"
value={value}
onInputChange={(e) => setValue(e ?? '')}
className="h-[150px]"
options={{ quickSuggestions: false, wordBasedSuggestions: 'off' }}
onMount={(editor, monaco) => {
editor.addCommand(
monaco.KeyCode.Escape,
handleCancel,
[
'editorTextFocus',
'!editorHasSelection',
'!editorHasMultipleSelections',
'!suggestWidgetVisible',
'!findWidgetVisible',
'!parameterHintsVisible',
'!renameInputVisible',
'!inSnippetMode',
'!accessibilityHelpWidgetVisible',
'!inlineSuggestionVisible',
].join(' && ')
)
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter, () =>
handleUpdateMarkdownRef.current(cell._id, valueRef.current)
)
editor.onDidBlurEditorWidget(() =>
handleUpdateMarkdownRef.current(cell._id, valueRef.current)
)
}}
/>
<div className="border-t flex items-center justify-between pl-3 pr-1 py-1">
<p className="text-xs text-foreground-lighter">Markdown</p>
<div className="flex items-center gap-x-1">
<Button variant="text" onMouseDown={(e) => e.preventDefault()} onClick={handleCancel}>
Cancel
</Button>
<Button
variant="text"
onMouseDown={(e) => e.preventDefault()}
onClick={() => handleUpdateMarkdown(cell._id, value)}
>
Done
</Button>
</div>
</div>
</div>
) : (
<div
onDoubleClick={handleStartEditing}
className={cn(
'group/mdcell relative w-full max-w-3xl mx-auto px-3 py-2 transition',
'hover:bg-alternative/50',
'border border-transparent rounded-md hover:border-default'
)}
>
<ButtonTooltip
variant="text"
className={cn(
'absolute right-1 top-1 px-1',
'opacity-0 group-hover/mdcell:opacity-100 focus-visible:opacity-100'
)}
icon={<Edit size={14} />}
onClick={handleStartEditing}
tooltip={{ content: { side: 'bottom', text: 'Edit' } }}
/>
{cell.text ? (
<Markdown
className={cn(
'prose prose-sm max-w-none text-muted-foreground prose-headings:text-foreground',
'[&>h1]:mb-2 [&>h2]:mb-2',
'[&_ol>li]:pl-3',
'[--tw-prose-body:var(--foreground-muted)]',
'[--tw-prose-headings:var(--foreground-default)]',
'[--tw-prose-links:var(--foreground-muted)]',
'[--tw-prose-bold:var(--foreground-muted)]',
'[--tw-prose-quotes:var(--foreground-muted)]'
)}
>
{cell.text}
</Markdown>
) : (
<p className="text-foreground-lighter text-sm italic">This cell has no content</p>
)}
</div>
)}
</SortableSection>
)
}