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
}
// [Joshen] handleUpdateMarkdown could be shifted into notebook-state as a updateCell action
export const MarkdownCell = ({ cell }: 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
const nextCells = cells.map((c) => (c.id === cellId ? { ...c, text } : c))
snap.updateCells({ id: notebookId, cells: nextCells })
setIsEditing(false)
}
const handleUpdateMarkdownRef = useLatest(handleUpdateMarkdown)
return (
Markdown
This cell has no content
)}