Files
supabase/apps/studio/components/ui/SortableSection.tsx
Joshen Lim 93d9d80535 joshen/fe 4149 explorer support adding removing cells in notebook (#49037)
## Context

Related to Explorer/Notebooks - this PR adds the functionality to add /
remove cells

Separately - am thinking we can shift a lot of the "cell update" logic
into notebook-state, mainly so that each UI component doesn't need to be
aware of the notebook's `cells` but just its own cell.

I'll do it separately though to prevent bloating this PR, already left
comments where i think can be refactored

<img width="250" alt="image"
src="https://github.com/user-attachments/assets/91aae5f9-07b9-4327-8e21-8e9d71098aa6"
/>
<img width="250" alt="image"
src="https://github.com/user-attachments/assets/677de208-5c52-4155-90c6-bc0879070693"
/>
<img width="250" alt="image"
src="https://github.com/user-attachments/assets/17c3dae7-87bc-40bb-bfa0-15df101006ce"
/>
<img width="1091" height="262" alt="image"
src="https://github.com/user-attachments/assets/a09c3d23-696a-4b55-a1f8-4602e56a60dc"
/>



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

## New Features
- Add query and Markdown cells directly within notebooks.
- Move, remove, and reorder cells using drag-and-drop or cell controls.
- Add cells from empty states and notebook toolbar actions.
- Edit Markdown cells, mark edits as complete, and see placeholders for
empty content.

## Improvements
- Cell controls and hover interactions are more consistent and
responsive.
- Moving cells is disabled at the top or bottom of a notebook.
- Sample cells now use standardized content and formatting.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-08-14 00:17:29 +07:00

94 lines
2.7 KiB
TypeScript

import { useDndMonitor } from '@dnd-kit/core'
import { useSortable } from '@dnd-kit/sortable'
import { GripVertical } from 'lucide-react'
import type { CSSProperties, PropsWithChildren, ReactNode } from 'react'
import { useEffect, useRef, useState } from 'react'
import { Button, cn, DropdownMenu, DropdownMenuTrigger } from 'ui'
export const SortableSection = ({
id,
children,
actions,
gripClassName,
gripDropdownContent,
}: PropsWithChildren<{
id: string
gripClassName?: string
actions?: ReactNode
gripDropdownContent?: ReactNode
}>) => {
const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({
id,
})
const [menuOpen, setMenuOpen] = useState(false)
const isDraggingRef = useRef(false)
const openTimeoutRef = useRef<ReturnType<typeof setTimeout>>(undefined)
useDndMonitor({
onDragStart: (event) => {
if (event.active.id === id) isDraggingRef.current = true
},
onDragEnd: (event) => {
if (event.active.id === id) isDraggingRef.current = false
},
onDragCancel: (event) => {
if (event.active.id === id) isDraggingRef.current = false
},
})
useEffect(() => () => clearTimeout(openTimeoutRef.current), [])
const style: CSSProperties = {
transform: transform
? `translate3d(${Math.round(transform.x)}px, ${Math.round(transform.y)}px, 0)`
: undefined,
transition,
}
return (
<div
ref={setNodeRef}
style={style}
className="group relative will-change-transform flex items-start gap-x-4"
>
<div className={cn('flex items-center', gripClassName)}>
{actions}
<DropdownMenu
open={menuOpen}
onOpenChange={(open) => {
clearTimeout(openTimeoutRef.current)
if (!open) {
setMenuOpen(false)
return
}
openTimeoutRef.current = setTimeout(() => {
if (!isDraggingRef.current) setMenuOpen(true)
}, 150)
}}
>
<DropdownMenuTrigger asChild>
<Button
type="button"
variant="text"
aria-label="Drag to reorder section"
className={cn(
'w-6 text-foreground-muted hover:text-foreground cursor-grab active:cursor-grabbing',
'rounded-sm focus-ring'
)}
{...attributes}
{...listeners}
tabIndex={0}
icon={<GripVertical />}
/>
</DropdownMenuTrigger>
{gripDropdownContent}
</DropdownMenu>
</div>
<div className={cn('w-full', isDragging && 'opacity-70')}>{children}</div>
</div>
)
}