From 673efba01ffc9d2788abd216103fbc7d97705c10 Mon Sep 17 00:00:00 2001 From: Alex Lee <3076438032@qq.com> Date: Thu, 25 Apr 2024 23:45:10 +0800 Subject: [PATCH] fix: data title error --- .../(routes)/documents/[documentId]/page.tsx | 15 +++---- app/(main)/components/document-list.tsx | 10 +---- components/toolbar.tsx | 42 ++++++++++--------- hooks/use-sidebar.tsx | 18 ++++++++ 4 files changed, 48 insertions(+), 37 deletions(-) create mode 100644 hooks/use-sidebar.tsx diff --git a/app/(main)/(routes)/documents/[documentId]/page.tsx b/app/(main)/(routes)/documents/[documentId]/page.tsx index 96204de..2f12752 100644 --- a/app/(main)/(routes)/documents/[documentId]/page.tsx +++ b/app/(main)/(routes)/documents/[documentId]/page.tsx @@ -1,7 +1,7 @@ 'use client' import { debounce } from 'lodash' -import { useEffect, useState } from 'react' +import { useEffect } from 'react' import Cover from '@/components/cover' import Toolbar from '@/components/toolbar' @@ -9,7 +9,7 @@ import { Skeleton } from '@/components/ui/skeleton' import { EditorWrapper } from '@/components/editor/editor-wrapper' import { useDocument } from '@/stores/use-document' -import { Doc, getById, update } from '@/api/document' +import { getById, update } from '@/api/document' interface DocumentIdPageProps { params: { @@ -18,9 +18,7 @@ interface DocumentIdPageProps { } const DocumentIdPage = ({ params }: DocumentIdPageProps) => { - const { onSetDocument } = useDocument() - const [document, setDocument] = useState() - + const { document, onSetDocument } = useDocument() const onChange = async (content: string) => { await update({ _id: params.documentId, @@ -34,13 +32,11 @@ const DocumentIdPage = ({ params }: DocumentIdPageProps) => { const fetchDocument = async () => { const response = await getById(params.documentId) const document = response.data - console.log('fetchDocument', document) onSetDocument(document) - setDocument(document) } fetchDocument() - }, []) + }, [params.documentId]) if (document === undefined) { return ( @@ -62,12 +58,11 @@ const DocumentIdPage = ({ params }: DocumentIdPageProps) => { return
Not found
} - // FIXME: data will disappear when we navigate to another page. return (
- + axios.get(url).then((res) => res.data) - - const { data: documents } = useSWR( - `/api/document/sidebar?parentDocument=${parentDocumentId}&type=${type}`, - fetcher - ) + const { documents } = useSidebar(parentDocumentId, type) // function: 点击重定向到文档详情页 const onRedirect = (documentId: string) => { diff --git a/components/toolbar.tsx b/components/toolbar.tsx index 05cde06..83f0287 100644 --- a/components/toolbar.tsx +++ b/components/toolbar.tsx @@ -1,5 +1,6 @@ 'use client' +import { mutate } from 'swr' import { ImageIcon, Smile, X } from 'lucide-react' import { ElementRef, useRef, useState } from 'react' import TextareaAutosize from 'react-textarea-autosize' @@ -8,18 +9,16 @@ import { Button } from './ui/button' import IconPicker from './icon-picker' import { useDocument } from '@/stores/use-document' import { useCoverImage } from '@/stores/use-cover-image' -import { Doc, removeIcon, update } from '@/api/document' +import { removeIcon, update } from '@/api/document' interface ToolbarProps { - initialData: Doc preview?: boolean } -const Toolbar = ({ initialData, preview }: ToolbarProps) => { +const Toolbar = ({ preview }: ToolbarProps) => { const inputRef = useRef>(null) const [isEditing, setIsEditing] = useState(false) - const [value, setValue] = useState(initialData.title) - const { onSetDocument } = useDocument() + const { document, onSetDocument } = useDocument() const coverImage = useCoverImage() const enableInput = () => { @@ -27,7 +26,6 @@ const Toolbar = ({ initialData, preview }: ToolbarProps) => { setIsEditing(true) setTimeout(() => { - setValue(initialData.title) inputRef.current?.focus() }, 0) } @@ -35,11 +33,15 @@ const Toolbar = ({ initialData, preview }: ToolbarProps) => { const disableInput = async () => { setIsEditing(false) const response = await update({ - _id: initialData._id, - title: value || 'Untitled', + _id: document?._id!, + title: document?.title || 'Untitled', }) const newDocument = response.data onSetDocument(newDocument) + mutate( + (key) => + typeof key === 'string' && key.startsWith('/api/document/sidebar') + ) } const onKeyDown = (event: React.KeyboardEvent) => { @@ -50,7 +52,7 @@ const Toolbar = ({ initialData, preview }: ToolbarProps) => { } const onIconSelect = async (icon: string) => { const response = await update({ - _id: initialData._id, + _id: document?._id!, icon, }) const newDocument = response.data @@ -58,19 +60,19 @@ const Toolbar = ({ initialData, preview }: ToolbarProps) => { } const onRemoveIcon = async () => { - const response = await removeIcon(initialData._id) + const response = await removeIcon(document?._id!) const newDocument = response.data onSetDocument(newDocument) } return (
- {!!initialData.icon && !preview && ( + {!!document?.icon && !preview && (
{/* FIXME:浏览器无法正确渲染emoji */}

- {initialData.icon} + {document?.icon}

)} - {!!initialData.icon && preview && ( -

{initialData.icon}

+ {!!document?.icon && preview && ( +

{document?.icon}

)}
- {!initialData.icon && !preview && ( + {!document?.icon && !preview && ( )} - {!initialData.coverImage && !preview && ( + {!document?.coverImage && !preview && (
diff --git a/hooks/use-sidebar.tsx b/hooks/use-sidebar.tsx new file mode 100644 index 0000000..4aa324a --- /dev/null +++ b/hooks/use-sidebar.tsx @@ -0,0 +1,18 @@ +import useSWR from 'swr' + +import axios from '@/lib/axios' +import { Doc } from '@/api/document' + +const fetcher = (url: string) => axios.get(url).then((res) => res.data) + +export const useSidebar = (parentDocumentId: string, type: string) => { + const { data: documents, mutate } = useSWR( + `/api/document/sidebar?parentDocument=${parentDocumentId}&type=${type}`, + fetcher + ) + + return { + documents, + mutate, + } +}