Skip to content

Commit 93f22e6

Browse files
Rokt33rDavy-c
authored andcommitted
Extract new doc row
1 parent 7c978e0 commit 93f22e6

2 files changed

Lines changed: 120 additions & 86 deletions

File tree

src/cloud/components/Views/Table/TableViewContentManager.tsx

Lines changed: 7 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { getDocLinkHref } from '../../Link/DocLink'
3535
import PropPicker from '../../Props/PropPicker'
3636
import DocTagsList from '../../DocPage/DocTagsList'
3737
import { getFormattedBoosthubDateTime } from '../../../lib/date'
38-
import { mdiFileDocumentOutline, mdiPlus } from '@mdi/js'
38+
import { mdiFileDocumentOutline } from '@mdi/js'
3939
import NavigationItem from '../../../../design/components/molecules/Navigation/NavigationItem'
4040
import TableAddPropertyContext from './TableAddPropertyContext'
4141
import { TableViewActionsRef } from '../../../lib/hooks/views/tableView'
@@ -48,17 +48,14 @@ import Button from '../../../../design/components/atoms/Button'
4848
import TablePropertiesContext from './TablePropertiesContext'
4949
import TableViewContentManagerFolderRow from './TableViewContentManagerFolderRow'
5050
import TableViewContentManagerRow from './TableViewContentManagerRow'
51-
import TableContentManagerRow from './TableContentManagerRow'
5251
import { overflowEllipsis } from '../../../../design/lib/styled/styleFunctions'
5352
import { usePreferences } from '../../../lib/stores/preferences'
5453
import SortingOption, {
5554
sortingOrders,
5655
} from '../../ContentManager/SortingOption'
5756
import { FormSelectOption } from '../../../../design/components/molecules/Form/atoms/FormSelect'
58-
import FormInput from '../../../../design/components/molecules/Form/atoms/FormInput'
59-
import { useCloudApi } from '../../../lib/hooks/useCloudApi'
60-
import Spinner from '../../../../design/components/atoms/Spinner'
6157
import TableViewContentManagerNewFolderRow from './TableViewContentmanagerNewFolderRow'
58+
import TableViewContentManagerNewDocRow from './TableViewContentManagerNewDocRow'
6259

6360
interface ContentManagerProps {
6461
team: SerializedTeam
@@ -86,7 +83,6 @@ const TableViewContentManager = ({
8683
}: ContentManagerProps) => {
8784
const { translate } = useI18n()
8885
const { openContextModal, closeAllModals } = useModal()
89-
const { createDoc } = useCloudApi()
9086
const { push } = useRouter()
9187
const { preferences, setPreferences } = usePreferences()
9288
const [order, setOrder] = useState<typeof sortingOrders[number]['value']>(
@@ -255,36 +251,6 @@ const TableViewContentManager = ({
255251
[setPreferences]
256252
)
257253

258-
const [newDocRowState, setNewDocRowState] = useState<
259-
'idle' | 'editing' | 'submitting'
260-
>('idle')
261-
const [newDocName, setNewDocName] = useState('')
262-
const newDocCompositionStateRef = useRef(false)
263-
const newDocInputRef = useRef<HTMLInputElement>(null)
264-
265-
useEffect(() => {
266-
if (newDocInputRef.current != null && newDocRowState === 'editing') {
267-
newDocInputRef.current.focus()
268-
}
269-
}, [newDocRowState])
270-
271-
const createNewDoc = useCallback(async () => {
272-
setNewDocRowState('submitting')
273-
if (currentWorkspaceId != null) {
274-
await createDoc(
275-
team,
276-
{
277-
title: newDocName,
278-
workspaceId: currentWorkspaceId,
279-
parentFolderId: currentFolderId,
280-
},
281-
{ skipRedirect: true }
282-
)
283-
}
284-
setNewDocName('')
285-
setNewDocRowState('idle')
286-
}, [currentWorkspaceId, createDoc, team, newDocName, currentFolderId])
287-
288254
return (
289255
<Container>
290256
<Scroller className='cm__scroller'>
@@ -475,56 +441,11 @@ const TableViewContentManager = ({
475441
/>
476442
{orderedDocs.length === 0 && <EmptyRow label='No Documents' />}
477443
{currentWorkspaceId != null && (
478-
<TableContentManagerRow>
479-
{newDocRowState === 'idle' ? (
480-
<Button
481-
className='content__manager--no-padding'
482-
variant='transparent'
483-
iconPath={mdiPlus}
484-
onClick={() => setNewDocRowState('editing')}
485-
>
486-
{translate(lngKeys.ModalsCreateNewDocument)}
487-
</Button>
488-
) : newDocRowState === 'editing' ? (
489-
<FormInput
490-
ref={newDocInputRef}
491-
value={newDocName}
492-
onChange={(event) => {
493-
setNewDocName(event.target.value)
494-
}}
495-
onCompositionStart={() => {
496-
newDocCompositionStateRef.current = true
497-
}}
498-
onCompositionEnd={() => {
499-
newDocCompositionStateRef.current = false
500-
if (newDocInputRef.current != null) {
501-
newDocInputRef.current.focus()
502-
}
503-
}}
504-
onKeyPress={(event) => {
505-
if (newDocCompositionStateRef.current) {
506-
return
507-
}
508-
switch (event.key) {
509-
case 'Escape':
510-
event.preventDefault()
511-
setNewDocRowState('idle')
512-
setNewDocName('')
513-
return
514-
case 'Enter':
515-
event.preventDefault()
516-
createNewDoc()
517-
return
518-
}
519-
}}
520-
onBlur={() => {
521-
createNewDoc()
522-
}}
523-
/>
524-
) : (
525-
<Spinner />
526-
)}
527-
</TableContentManagerRow>
444+
<TableViewContentManagerNewDocRow
445+
team={team}
446+
workspaceId={currentWorkspaceId}
447+
folderId={currentFolderId}
448+
/>
528449
)}
529450

530451
{folders != null && (
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import React, { useState, useRef, useEffect, useCallback } from 'react'
2+
import { mdiPlus } from '@mdi/js'
3+
import Button from '../../../../design/components/atoms/Button'
4+
import TableContentManagerRow from './TableContentManagerRow'
5+
import { useCloudApi } from '../../../lib/hooks/useCloudApi'
6+
import { SerializedTeam } from '../../../interfaces/db/team'
7+
import FormInput from '../../../../design/components/molecules/Form/atoms/FormInput'
8+
import Spinner from '../../../../design/components/atoms/Spinner'
9+
import { useI18n } from '../../../lib/hooks/useI18n'
10+
import { lngKeys } from '../../../lib/i18n/types'
11+
12+
interface TableViewContentManagerNewFolderRowProps {
13+
team: SerializedTeam
14+
workspaceId: string
15+
folderId?: string
16+
className?: string
17+
}
18+
19+
const TableViewContentManagerNewDocRow = ({
20+
team,
21+
workspaceId,
22+
folderId,
23+
className,
24+
}: TableViewContentManagerNewFolderRowProps) => {
25+
const { translate } = useI18n()
26+
const { createDoc } = useCloudApi()
27+
const [formState, setFormState] = useState<'idle' | 'editing' | 'submitting'>(
28+
'idle'
29+
)
30+
const [newTitle, setNewTitle] = useState('')
31+
const compositionStateRef = useRef(false)
32+
const newTitleInputRef = useRef<HTMLInputElement>(null)
33+
34+
useEffect(() => {
35+
if (newTitleInputRef.current != null && formState === 'editing') {
36+
newTitleInputRef.current.focus()
37+
}
38+
}, [formState])
39+
40+
const createNewDoc = useCallback(async () => {
41+
setFormState('submitting')
42+
if (workspaceId != null) {
43+
await createDoc(
44+
team,
45+
{
46+
title: newTitle,
47+
workspaceId,
48+
parentFolderId: folderId,
49+
},
50+
{ skipRedirect: true }
51+
)
52+
}
53+
setNewTitle('')
54+
setFormState('idle')
55+
}, [workspaceId, folderId, createDoc, team, newTitle])
56+
57+
const cancelEditing = useCallback(() => {
58+
setFormState('idle')
59+
setNewTitle('')
60+
}, [])
61+
62+
return (
63+
<TableContentManagerRow className={className}>
64+
{formState === 'idle' ? (
65+
<Button
66+
className='content__manager--no-padding'
67+
variant='transparent'
68+
iconPath={mdiPlus}
69+
onClick={() => setFormState('editing')}
70+
>
71+
{translate(lngKeys.ModalsCreateNewDocument)}
72+
</Button>
73+
) : formState === 'editing' ? (
74+
<FormInput
75+
ref={newTitleInputRef}
76+
value={newTitle}
77+
onChange={(event) => {
78+
setNewTitle(event.target.value)
79+
}}
80+
onCompositionStart={() => {
81+
compositionStateRef.current = true
82+
}}
83+
onCompositionEnd={() => {
84+
compositionStateRef.current = false
85+
if (newTitleInputRef.current != null) {
86+
newTitleInputRef.current.focus()
87+
}
88+
}}
89+
onKeyPress={(event) => {
90+
if (compositionStateRef.current) {
91+
return
92+
}
93+
switch (event.key) {
94+
case 'Escape':
95+
event.preventDefault()
96+
cancelEditing()
97+
return
98+
case 'Enter':
99+
event.preventDefault()
100+
createNewDoc()
101+
return
102+
}
103+
}}
104+
onBlur={cancelEditing}
105+
/>
106+
) : (
107+
<Spinner />
108+
)}
109+
</TableContentManagerRow>
110+
)
111+
}
112+
113+
export default TableViewContentManagerNewDocRow

0 commit comments

Comments
 (0)