Skip to content

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

joshen/fe 4113 explorer markdown cells (#48846)

Joshen Limb648db233b60
diff --git a/apps/studio/components/interfaces/Explorer/MarkdownCell.tsx b/apps/studio/components/interfaces/Explorer/MarkdownCell.tsx
new file mode 100644
index 00000000000..bdcc73f03f9
--- /dev/null
+++ b/apps/studio/components/interfaces/Explorer/MarkdownCell.tsx
@@ -0,0 +1,121 @@
+import { Edit } from 'lucide-react'
+import { useState } from 'react'
+import { Button, cn } from 'ui'
+
+import { Markdown } from '../Markdown'
+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'
+
+interface MarkdownCellProps {
+ cell: MarkdownCellSchema
+ onCommitChanges: (text: string) => void
+}
+
+export const MarkdownCell = ({ cell, onCommitChanges }: MarkdownCellProps) => {
+ const [value, setValue] = useState(cell.text)
+ const [isEditing, setIsEditing] = useState(false)
+
+ const valueRef = useLatest(value)
+ const onCommitChangesRef = useLatest(onCommitChanges)
+
+ const handleStartEditing = () => {
+ setValue(cell.text)
+ setIsEditing(true)
+ }
+
+ const handleCancel = () => {
+ setIsEditing(false)
+ }
+
+ const handleCommit = () => {
+ onCommitChangesRef.current(valueRef.current)
+ setIsEditing(false)
+ }
+
+ return (
+ <SortableSection gripClassName="mt-2.5" id={cell.id}>
+ {isEditing ? (
+ <div
+ className={cn('w-full max-w-2xl 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, handleCommit)
+ editor.onDidBlurEditorWidget(handleCommit)
+ }}
+ />
+ <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={handleCommit}>
+ Save
+ </Button>
+ </div>
+ </div>
+ </div>
+ ) : (
+ <div
+ onDoubleClick={handleStartEditing}
+ className={cn(
+ 'group relative w-full max-w-2xl 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:opacity-100 focus-visible:opacity-100'
+ )}
+ icon={<Edit size={14} />}
+ onClick={handleStartEditing}
+ tooltip={{ content: { side: 'bottom', text: 'Edit' } }}
+ />
+ <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>
+ </div>
+ )}
+ </SortableSection>
+ )
+}
diff --git a/apps/studio/components/interfaces/Explorer/NotebookEditor.tsx b/apps/studio/components/interfaces/Explorer/NotebookEditor.tsx
index d0c58f861f7..11f42cf233c 100644
--- a/apps/studio/components/interfaces/Explorer/NotebookEditor.tsx
+++ b/apps/studio/components/interfaces/Explorer/NotebookEditor.tsx
@@ -1,3 +1,17 @@
+import {
+ DndContext,
+ DragEndEvent,
+ KeyboardSensor,
+ PointerSensor,
+ useSensor,
+ useSensors,
+} from '@dnd-kit/core'
+import {
+ arrayMove,
+ SortableContext,
+ sortableKeyboardCoordinates,
+ verticalListSortingStrategy,
+} from '@dnd-kit/sortable'
import { useParams } from 'common'
import { Edit, Notebook, NotebookText, Play, Save } from 'lucide-react'
import { useEffect, useEffectEvent, useState } from 'react'
@@ -5,6 +19,7 @@ import { AiIconAnimation, Button } from 'ui'
import { Input } from 'ui-patterns/DataInputs/Input'
import { EmptyStatePresentational } from 'ui-patterns/EmptyStatePresentational'
+import { MarkdownCell } from './MarkdownCell'
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { useNotebooksStateSnapshot } from '@/state/notebooks/notebooks-state'
import { createTabId, useTabsStateSnapshot } from '@/state/tabs'
@@ -20,16 +35,40 @@ export const NotebookEditor = () => {
const [titleValue, setTitleValue] = useState<string>(name ?? '')
const [isEditingTitle, setIsEditingTitle] = useState(false)
+ const cells = content?.cells ?? []
+
+ const sensors = useSensors(
+ useSensor(PointerSensor, { activationConstraint: { distance: 8 } }),
+ useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates })
+ )
+
const handleSaveTitle = () => {
const trimmedName = titleValue.trim()
if (id && trimmedName && trimmedName !== name) {
snap.renameNotebook({ id, name: trimmedName })
tabs.updateTab(createTabId('notebook', { id }), { label: trimmedName })
}
-
setIsEditingTitle(false)
}
+ const handleUpdateCellText = (cellId: string, text: string) => {
+ if (!id) return
+
+ const nextCells = cells.map((cell) => (cell.id === cellId ? { ...cell, text } : cell))
+ snap.updateCells({ id, cells: nextCells })
+ }
+
+ const handleDragEnd = (event: DragEndEvent) => {
+ const { active, over } = event
+ if (!id || !over || active.id === over.id) return
+
+ const oldIndex = cells.findIndex((cell) => cell.id === active.id)
+ const newIndex = cells.findIndex((cell) => cell.id === over.id)
+ if (oldIndex === -1 || newIndex === -1) return
+
+ snap.updateCells({ id, cells: arrayMove([...cells], oldIndex, newIndex) })
+ }
+
const registerTab = useEffectEvent(() => {
if (!id) return
tabs.addTab({
@@ -105,7 +144,7 @@ export const NotebookEditor = () => {
<div className="w-full mx-auto flex-grow min-h-0 overflow-y-auto">
<div className="p-4">
- {content?.cells.length === 0 && (
+ {cells.length === 0 && (
<EmptyStatePresentational
icon={<Notebook className="text-foreground-lighter" />}
title="This notebook is empty"
@@ -118,6 +157,33 @@ export const NotebookEditor = () => {
</div>
</EmptyStatePresentational>
)}
+ {cells.length > 0 && (
+ <DndContext sensors={sensors} onDragEnd={handleDragEnd}>
+ <SortableContext
+ items={cells.map((cell) => cell.id)}
+ strategy={verticalListSortingStrategy}
+ >
+ <div className="flex flex-col gap-y-2">
+ {cells.map((cell) => {
+ switch (cell._tag) {
+ case 'markdown_cell':
+ return (
+ <MarkdownCell
+ key={cell.id}
+ cell={cell}
+ onCommitChanges={(text) => handleUpdateCellText(cell.id, text)}
+ />
+ )
+ case 'database_cell':
+ case 'log_cell':
+ // [Joshen] Will eventually hook it up
+ return null
+ }
+ })}
+ </div>
+ </SortableContext>
+ </DndContext>
+ )}
</div>
</div>
</div>
diff --git a/apps/studio/components/interfaces/Explorer/hooks.ts b/apps/studio/components/interfaces/Explorer/hooks.ts
index f3883d21961..cdb0f932839 100644
--- a/apps/studio/components/interfaces/Explorer/hooks.ts
+++ b/apps/studio/components/interfaces/Explorer/hooks.ts
@@ -27,7 +27,28 @@ export const useCreateNotebook = () => {
favorite: false,
content: {
schema_version: 1,
- cells: [],
+ // [Joshen] Just adding sample data to play around with, keep for now - clean up at the end
+ cells: [
+ {
+ _tag: 'markdown_cell',
+ id: '1',
+ text: `
+# Title
+A brief description on what this notebook is about
+ `.trim(),
+ },
+ {
+ _tag: 'markdown_cell',
+ id: '2',
+ text: `
+## Section
+This is a sample paragraph to demonstrate the Markdown cells
+1. List item 1
+2. List item 2
+3. List item 3
+ `,
+ },
+ ],
},
owner_id: profile.id,
project_id: project.id,
diff --git a/apps/studio/components/interfaces/ProjectHome/Home.tsx b/apps/studio/components/interfaces/ProjectHome/Home.tsx
index 35c6c12d72b..cde13bf4c2d 100644
--- a/apps/studio/components/interfaces/ProjectHome/Home.tsx
+++ b/apps/studio/components/interfaces/ProjectHome/Home.tsx
@@ -23,16 +23,18 @@ import { CustomReportSection } from './CustomReportSection'
import { DEFAULT_SECTION_ORDER, mergeSectionOrder } from './Home.utils'
import { ProjectUsageSection } from './ProjectUsageSection'
import { ProjectUsageSectionDeltas } from './ProjectUsageSectionDeltas'
-import { SortableSection } from '@/components/interfaces/ProjectHome/SortableSection'
import { TopSection } from '@/components/interfaces/ProjectHome/TopSection'
import { ProjectNeedsSecuring } from '@/components/layouts/ProjectNeedsSecuring/ProjectNeedsSecuring'
import { ScaffoldContainer, ScaffoldSection } from '@/components/layouts/Scaffold'
+import { SortableSection } from '@/components/ui/SortableSection'
import { useLocalStorage } from '@/hooks/misc/useLocalStorage'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { IS_PLATFORM, PROJECT_STATUS } from '@/lib/constants'
import { useTrack } from '@/lib/telemetry/track'
import { useAppStateSnapshot } from '@/state/app-state'
+const SORT_GRIP_CLASS = 'absolute -left-6 top-1.5'
+
export const ProjectHome = () => {
const { enableBranching } = useParams()
const snap = useAppStateSnapshot()
@@ -123,7 +125,7 @@ export const ProjectHome = () => {
key={id}
className={cn(isComingUp && 'opacity-60 pointer-events-none')}
>
- <SortableSection id={id}>
+ <SortableSection gripClassName={SORT_GRIP_CLASS} id={id}>
<UsageSection />
</SortableSection>
</div>
@@ -131,7 +133,7 @@ export const ProjectHome = () => {
}
if (id === 'connect' && showConnectSection) {
return (
- <SortableSection key={id} id={id}>
+ <SortableSection gripClassName={SORT_GRIP_CLASS} key={id} id={id}>
<ConnectSection />
</SortableSection>
)
@@ -142,7 +144,7 @@ export const ProjectHome = () => {
key={id}
className={cn(isComingUp && 'opacity-60 pointer-events-none')}
>
- <SortableSection id={id}>
+ <SortableSection gripClassName={SORT_GRIP_CLASS} id={id}>
<AdvisorSection showEmptyState={isComingUp} />
</SortableSection>
</div>
@@ -154,7 +156,7 @@ export const ProjectHome = () => {
key={id}
className={cn(isComingUp && 'opacity-60 pointer-events-none')}
>
- <SortableSection id={id}>
+ <SortableSection gripClassName={SORT_GRIP_CLASS} id={id}>
<CustomReportSection />
</SortableSection>
</div>
diff --git a/apps/studio/components/interfaces/ProjectHome/SortableSection.tsx b/apps/studio/components/ui/SortableSection.tsx
similarity index 54%
rename from apps/studio/components/interfaces/ProjectHome/SortableSection.tsx
rename to apps/studio/components/ui/SortableSection.tsx
index 9502472e12f..09295a0b94d 100644
--- a/apps/studio/components/interfaces/ProjectHome/SortableSection.tsx
+++ b/apps/studio/components/ui/SortableSection.tsx
@@ -1,14 +1,13 @@
import { useSortable } from '@dnd-kit/sortable'
import { GripVertical } from 'lucide-react'
-import type { CSSProperties, ReactNode } from 'react'
+import type { CSSProperties, PropsWithChildren } from 'react'
import { cn } from 'ui'
-type SortableSectionProps = {
- id: string
- children: ReactNode
-}
-
-export const SortableSection = ({ id, children }: SortableSectionProps) => {
+export const SortableSection = ({
+ id,
+ children,
+ gripClassName,
+}: PropsWithChildren<{ id: string; gripClassName?: string }>) => {
const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({
id,
})
@@ -21,13 +20,18 @@ export const SortableSection = ({ id, children }: SortableSectionProps) => {
}
return (
- <div ref={setNodeRef} style={style} className="relative will-change-transform">
+ <div
+ ref={setNodeRef}
+ style={style}
+ className="relative will-change-transform flex items-start gap-x-2"
+ >
<button
type="button"
aria-label="Drag to reorder section"
className={cn(
- 'absolute -left-6 top-2 text-foreground-muted hover:text-foreground cursor-grab active:cursor-grabbing',
- 'rounded-sm focus-ring'
+ 'text-foreground-muted hover:text-foreground cursor-grab active:cursor-grabbing',
+ 'rounded-sm focus-ring',
+ gripClassName
)}
{...attributes}
{...listeners}
@@ -35,7 +39,7 @@ export const SortableSection = ({ id, children }: SortableSectionProps) => {
>
<GripVertical size={14} />
</button>
- <div className={cn(isDragging && 'opacity-70')}>{children}</div>
+ <div className={cn('w-full', isDragging && 'opacity-70')}>{children}</div>
</div>
)
}