client: stop unnecessary title re-renders in /new

This commit is contained in:
Max Leiter 2022-03-20 21:43:04 -07:00
parent 2fbcb41cdd
commit 3f8511e0c1
No known key found for this signature in database
GPG key ID: A3512F2F2F17EBDA
3 changed files with 14 additions and 14 deletions

View file

@ -1,5 +1,5 @@
import { Button, Text, useTheme, useToasts } from '@geist-ui/core' import { Button, Text, useTheme, useToasts } from '@geist-ui/core'
import { useCallback, useEffect } from 'react' import { memo, useCallback, useEffect } from 'react'
import { useDropzone } from 'react-dropzone' import { useDropzone } from 'react-dropzone'
import styles from './drag-and-drop.module.css' import styles from './drag-and-drop.module.css'
import { Document } from '../' import { Document } from '../'
@ -170,4 +170,4 @@ function FileDropzone({ setDocs }: { setDocs: ((docs: Document[]) => void) }) {
) )
} }
export default FileDropzone export default memo(FileDropzone)

View file

@ -24,6 +24,7 @@ const Post = () => {
content: '', content: '',
id: generateUUID() id: generateUUID()
}]) }])
const [isSubmitting, setSubmitting] = useState(false) const [isSubmitting, setSubmitting] = useState(false)
const remove = (id: string) => { const remove = (id: string) => {
@ -63,11 +64,10 @@ const Post = () => {
setDocs(docs.map((doc) => doc.id === id ? { ...doc, content } : doc)) setDocs(docs.map((doc) => doc.id === id ? { ...doc, content } : doc))
}, [docs]) }, [docs])
const uploadDocs = (files: Document[]) => { const uploadDocs = useCallback((files: Document[]) => {
// if no title is set and the only document is empty, // if no title is set and the only document is empty,
const isFirstDocEmpty = docs.length === 1 && docs[0].title === '' && docs[0].content === '' const isFirstDocEmpty = docs.length === 1 && docs[0].title === '' && docs[0].content === ''
const shouldSetTitle = !title && isFirstDocEmpty const shouldSetTitle = !title && isFirstDocEmpty
console.log(shouldSetTitle, title, isFirstDocEmpty)
if (shouldSetTitle) { if (shouldSetTitle) {
if (files.length === 1) { if (files.length === 1) {
setTitle(files[0].title) setTitle(files[0].title)
@ -78,15 +78,15 @@ const Post = () => {
if (isFirstDocEmpty) setDocs(files) if (isFirstDocEmpty) setDocs(files)
else setDocs([...docs, ...files]) else setDocs([...docs, ...files])
} }, [docs, title])
return ( return (
<div> <div>
<Title title={title} handleChange={(e) => setTitle(e.target.value)} /> <Title title={title} setTitle={setTitle} />
<FileDropzone setDocs={uploadDocs} /> <FileDropzone setDocs={uploadDocs} />
{ {
docs.map(({ id }) => { docs.map(({ content, id, title }) => {
const doc = docs.find((doc) => doc.id === id)
return ( return (
<Document <Document
remove={() => remove(id)} remove={() => remove(id)}
@ -94,8 +94,8 @@ const Post = () => {
editable={true} editable={true}
setContent={(content) => updateContent(content, id)} setContent={(content) => updateContent(content, id)}
setTitle={(title) => updateTitle(title, id)} setTitle={(title) => updateTitle(title, id)}
content={doc?.content} content={content}
title={doc?.title} title={title}
/> />
) )
}) })

View file

@ -14,11 +14,11 @@ const titlePlaceholders = [
] ]
type props = { type props = {
handleChange?: (event: ChangeEvent<HTMLInputElement>) => void setTitle: (title: string) => void
title?: string title?: string
} }
const Title = ({ handleChange, title }: props) => { const Title = ({ setTitle, title }: props) => {
return (<div className={styles.title}> return (<div className={styles.title}>
<Text h1 width={"150px"} className={styles.drift}>Drift</Text> <Text h1 width={"150px"} className={styles.drift}>Drift</Text>
@ -26,7 +26,7 @@ const Title = ({ handleChange, title }: props) => {
<Input <Input
placeholder={titlePlaceholders[Math.floor(Math.random() * titlePlaceholders.length)]} placeholder={titlePlaceholders[Math.floor(Math.random() * titlePlaceholders.length)]}
value={title || ""} value={title || ""}
onChange={handleChange} onChange={(event) => setTitle(event.target.value)}
height={"55px"} height={"55px"}
font={1.5} font={1.5}
label="Post title" label="Post title"
@ -36,4 +36,4 @@ const Title = ({ handleChange, title }: props) => {
</div>) </div>)
} }
export default Title export default memo(Title)