11'use client'
22
3+ import { mutate } from 'swr'
34import { ImageIcon , Smile , X } from 'lucide-react'
45import { ElementRef , useRef , useState } from 'react'
56import TextareaAutosize from 'react-textarea-autosize'
@@ -8,38 +9,39 @@ import { Button } from './ui/button'
89import IconPicker from './icon-picker'
910import { useDocument } from '@/stores/use-document'
1011import { useCoverImage } from '@/stores/use-cover-image'
11- import { Doc , removeIcon , update } from '@/api/document'
12+ import { removeIcon , update } from '@/api/document'
1213
1314interface ToolbarProps {
14- initialData : Doc
1515 preview ?: boolean
1616}
1717
18- const Toolbar = ( { initialData , preview } : ToolbarProps ) => {
18+ const Toolbar = ( { preview } : ToolbarProps ) => {
1919 const inputRef = useRef < ElementRef < 'textarea' > > ( null )
2020 const [ isEditing , setIsEditing ] = useState ( false )
21- const [ value , setValue ] = useState ( initialData . title )
22- const { onSetDocument } = useDocument ( )
21+ const { document, onSetDocument } = useDocument ( )
2322
2423 const coverImage = useCoverImage ( )
2524 const enableInput = ( ) => {
2625 if ( preview ) return
2726 setIsEditing ( true )
2827
2928 setTimeout ( ( ) => {
30- setValue ( initialData . title )
3129 inputRef . current ?. focus ( )
3230 } , 0 )
3331 }
3432
3533 const disableInput = async ( ) => {
3634 setIsEditing ( false )
3735 const response = await update ( {
38- _id : initialData . _id ,
39- title : value || 'Untitled' ,
36+ _id : document ? ._id ! ,
37+ title : document ?. title || 'Untitled' ,
4038 } )
4139 const newDocument = response . data
4240 onSetDocument ( newDocument )
41+ mutate (
42+ ( key ) =>
43+ typeof key === 'string' && key . startsWith ( '/api/document/sidebar' )
44+ )
4345 }
4446
4547 const onKeyDown = ( event : React . KeyboardEvent < HTMLTextAreaElement > ) => {
@@ -50,27 +52,27 @@ const Toolbar = ({ initialData, preview }: ToolbarProps) => {
5052 }
5153 const onIconSelect = async ( icon : string ) => {
5254 const response = await update ( {
53- _id : initialData . _id ,
55+ _id : document ? ._id ! ,
5456 icon,
5557 } )
5658 const newDocument = response . data
5759 onSetDocument ( newDocument )
5860 }
5961
6062 const onRemoveIcon = async ( ) => {
61- const response = await removeIcon ( initialData . _id )
63+ const response = await removeIcon ( document ? ._id ! )
6264 const newDocument = response . data
6365 onSetDocument ( newDocument )
6466 }
6567
6668 return (
6769 < div className = "group relative pl-[54px]" >
68- { ! ! initialData . icon && ! preview && (
70+ { ! ! document ? .icon && ! preview && (
6971 < div className = "group/icon flex items-center gap-x-2 pt-6" >
7072 < IconPicker onChange = { onIconSelect } >
7173 { /* FIXME:浏览器无法正确渲染emoji */ }
7274 < p className = "text-6xl transition hover:opacity-75" >
73- { initialData . icon }
75+ { document ? .icon }
7476 </ p >
7577 </ IconPicker >
7678 < Button
@@ -82,11 +84,11 @@ const Toolbar = ({ initialData, preview }: ToolbarProps) => {
8284 </ Button >
8385 </ div >
8486 ) }
85- { ! ! initialData . icon && preview && (
86- < p className = "pt-6 text-6xl" > { initialData . icon } </ p >
87+ { ! ! document ? .icon && preview && (
88+ < p className = "pt-6 text-6xl" > { document ? .icon } </ p >
8789 ) }
8890 < div className = "flex items-center gap-x-1 py-4 opacity-0 group-hover:opacity-100" >
89- { ! initialData . icon && ! preview && (
91+ { ! document ? .icon && ! preview && (
9092 < IconPicker asChild onChange = { onIconSelect } >
9193 < Button
9294 className = "text-xs text-muted-foreground"
@@ -97,7 +99,7 @@ const Toolbar = ({ initialData, preview }: ToolbarProps) => {
9799 </ Button >
98100 </ IconPicker >
99101 ) }
100- { ! initialData . coverImage && ! preview && (
102+ { ! document ? .coverImage && ! preview && (
101103 < Button
102104 onClick = { coverImage . onOpen }
103105 className = "text-xs text-muted-foreground"
@@ -113,15 +115,17 @@ const Toolbar = ({ initialData, preview }: ToolbarProps) => {
113115 ref = { inputRef }
114116 onBlur = { disableInput }
115117 onKeyDown = { onKeyDown }
116- value = { value }
117- onChange = { ( e ) => setValue ( e . target . value ) }
118+ value = { document ?. title }
119+ onChange = { ( e ) =>
120+ onSetDocument ( { ...document ! , title : e . target . value } )
121+ }
118122 className = "resize-none break-words bg-transparent text-5xl font-bold text-[#3F3F3F] outline-none dark:text-[#CFCFCF]"
119123 />
120124 ) : (
121125 < div
122126 onClick = { enableInput }
123127 className = "break-words pb-[11.5px] text-5xl font-bold text-[#3F3F3F] outline-none dark:text-[#CFCFCF]" >
124- { value }
128+ { document ?. title }
125129 </ div >
126130 ) }
127131 </ div >
0 commit comments