Skip to content

Commit 94b8317

Browse files
authored
Merge pull request #26 from mlhiter/dev
fix: data title error
2 parents f7b806a + 673efba commit 94b8317

4 files changed

Lines changed: 48 additions & 37 deletions

File tree

app/(main)/(routes)/documents/[documentId]/page.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
'use client'
22

33
import { debounce } from 'lodash'
4-
import { useEffect, useState } from 'react'
4+
import { useEffect } from 'react'
55

66
import Cover from '@/components/cover'
77
import Toolbar from '@/components/toolbar'
88
import { Skeleton } from '@/components/ui/skeleton'
99
import { EditorWrapper } from '@/components/editor/editor-wrapper'
1010

1111
import { useDocument } from '@/stores/use-document'
12-
import { Doc, getById, update } from '@/api/document'
12+
import { getById, update } from '@/api/document'
1313

1414
interface DocumentIdPageProps {
1515
params: {
@@ -18,9 +18,7 @@ interface DocumentIdPageProps {
1818
}
1919

2020
const DocumentIdPage = ({ params }: DocumentIdPageProps) => {
21-
const { onSetDocument } = useDocument()
22-
const [document, setDocument] = useState<Doc>()
23-
21+
const { document, onSetDocument } = useDocument()
2422
const onChange = async (content: string) => {
2523
await update({
2624
_id: params.documentId,
@@ -34,13 +32,11 @@ const DocumentIdPage = ({ params }: DocumentIdPageProps) => {
3432
const fetchDocument = async () => {
3533
const response = await getById(params.documentId)
3634
const document = response.data
37-
console.log('fetchDocument', document)
3835
onSetDocument(document)
39-
setDocument(document)
4036
}
4137

4238
fetchDocument()
43-
}, [])
39+
}, [params.documentId])
4440

4541
if (document === undefined) {
4642
return (
@@ -62,12 +58,11 @@ const DocumentIdPage = ({ params }: DocumentIdPageProps) => {
6258
return <div>Not found</div>
6359
}
6460

65-
// FIXME: data will disappear when we navigate to another page.
6661
return (
6762
<div className="pb-40">
6863
<Cover url={document.coverImage} />
6964
<div className="mx-auto md:max-w-3xl lg:max-w-4xl">
70-
<Toolbar initialData={document} />
65+
<Toolbar />
7166
<EditorWrapper
7267
onChange={debounceOnChange}
7368
documentId={params.documentId}

app/(main)/components/document-list.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
'use client'
22

3-
import useSWR from 'swr'
43
import { useState } from 'react'
54
import { FileIcon } from 'lucide-react'
65
import { useParams, useRouter } from 'next/navigation'
76

87
import Item from './item'
9-
import axios from '@/lib/axios'
108
import { cn } from '@/lib/utils'
119
import { Doc } from '@/api/document'
10+
import { useSidebar } from '@/hooks/use-sidebar'
1211

1312
interface DocumentListProps {
1413
parentDocumentId?: string
@@ -36,12 +35,7 @@ const DocumentList = ({
3635

3736
parentDocumentId = parentDocumentId ? parentDocumentId : ''
3837

39-
const fetcher = (url: string) => axios.get(url).then((res) => res.data)
40-
41-
const { data: documents } = useSWR(
42-
`/api/document/sidebar?parentDocument=${parentDocumentId}&type=${type}`,
43-
fetcher
44-
)
38+
const { documents } = useSidebar(parentDocumentId, type)
4539

4640
// function: 点击重定向到文档详情页
4741
const onRedirect = (documentId: string) => {

components/toolbar.tsx

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use client'
22

3+
import { mutate } from 'swr'
34
import { ImageIcon, Smile, X } from 'lucide-react'
45
import { ElementRef, useRef, useState } from 'react'
56
import TextareaAutosize from 'react-textarea-autosize'
@@ -8,38 +9,39 @@ import { Button } from './ui/button'
89
import IconPicker from './icon-picker'
910
import { useDocument } from '@/stores/use-document'
1011
import { useCoverImage } from '@/stores/use-cover-image'
11-
import { Doc, removeIcon, update } from '@/api/document'
12+
import { removeIcon, update } from '@/api/document'
1213

1314
interface 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>

hooks/use-sidebar.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import useSWR from 'swr'
2+
3+
import axios from '@/lib/axios'
4+
import { Doc } from '@/api/document'
5+
6+
const fetcher = (url: string) => axios.get(url).then((res) => res.data)
7+
8+
export const useSidebar = (parentDocumentId: string, type: string) => {
9+
const { data: documents, mutate } = useSWR<Doc[]>(
10+
`/api/document/sidebar?parentDocument=${parentDocumentId}&type=${type}`,
11+
fetcher
12+
)
13+
14+
return {
15+
documents,
16+
mutate,
17+
}
18+
}

0 commit comments

Comments
 (0)