Skip to content

Commit f508da9

Browse files
Rokt33rDavy-c
authored andcommitted
Improve doc creation
1 parent 12c6874 commit f508da9

1 file changed

Lines changed: 79 additions & 23 deletions

File tree

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

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ import Button from '../../../../design/components/atoms/Button'
4848
import TablePropertiesContext from './TablePropertiesContext'
4949
import TableViewContentManagerFolderRow from './TableViewContentManagerFolderRow'
5050
import TableViewContentManagerRow from './TableViewContentManagerRow'
51-
import { useCloudResourceModals } from '../../../lib/hooks/useCloudResourceModals'
5251
import TableContentManagerRow from './TableContentManagerRow'
5352
import { overflowEllipsis } from '../../../../design/lib/styled/styleFunctions'
5453
import { usePreferences } from '../../../lib/stores/preferences'
@@ -86,8 +85,7 @@ const TableViewContentManager = ({
8685
}: ContentManagerProps) => {
8786
const { translate } = useI18n()
8887
const { openContextModal, closeAllModals } = useModal()
89-
const { openNewDocForm } = useCloudResourceModals()
90-
const { createFolder } = useCloudApi()
88+
const { createFolder, createDoc } = useCloudApi()
9189
const { push } = useRouter()
9290
const { preferences, setPreferences } = usePreferences()
9391
const [order, setOrder] = useState<typeof sortingOrders[number]['value']>(
@@ -264,7 +262,7 @@ const TableViewContentManager = ({
264262
const newFolderInputRef = useRef<HTMLInputElement>(null)
265263

266264
useEffect(() => {
267-
if (newFolderInputRef.current != null) {
265+
if (newFolderInputRef.current != null && newFolderRowState === 'editing') {
268266
newFolderInputRef.current.focus()
269267
}
270268
}, [newFolderRowState])
@@ -287,6 +285,36 @@ const TableViewContentManager = ({
287285
setNewFolderRowState('idle')
288286
}, [currentWorkspaceId, currentFolderId, createFolder, team, newFolderName])
289287

288+
const [newDocRowState, setNewDocRowState] = useState<
289+
'idle' | 'editing' | 'submitting'
290+
>('idle')
291+
const [newDocName, setNewDocName] = useState('')
292+
const newDocCompositionStateRef = useRef(false)
293+
const newDocInputRef = useRef<HTMLInputElement>(null)
294+
295+
useEffect(() => {
296+
if (newDocInputRef.current != null && newDocRowState === 'editing') {
297+
newDocInputRef.current.focus()
298+
}
299+
}, [newDocRowState])
300+
301+
const createNewDoc = useCallback(async () => {
302+
setNewDocRowState('submitting')
303+
if (currentWorkspaceId != null) {
304+
await createDoc(
305+
team,
306+
{
307+
title: newDocName,
308+
workspaceId: currentWorkspaceId,
309+
parentFolderId: currentFolderId,
310+
},
311+
{ skipRedirect: true }
312+
)
313+
}
314+
setNewDocName('')
315+
setNewDocRowState('idle')
316+
}, [currentWorkspaceId, createDoc, team, newDocName, currentFolderId])
317+
290318
return (
291319
<Container>
292320
<Scroller className='cm__scroller'>
@@ -478,26 +506,54 @@ const TableViewContentManager = ({
478506
{orderedDocs.length === 0 && <EmptyRow label='No Documents' />}
479507
{currentWorkspaceId != null && (
480508
<TableContentManagerRow>
481-
<Button
482-
className='content__manager--no-padding'
483-
variant='transparent'
484-
iconPath={mdiPlus}
485-
onClick={() =>
486-
openNewDocForm(
487-
{
488-
team,
489-
parentFolderId: currentFolderId,
490-
workspaceId: currentWorkspaceId,
491-
},
492-
{
493-
skipRedirect: true,
494-
precedingRows: [],
509+
{newDocRowState === 'idle' ? (
510+
<Button
511+
className='content__manager--no-padding'
512+
variant='transparent'
513+
iconPath={mdiPlus}
514+
onClick={() => setNewDocRowState('editing')}
515+
>
516+
{translate(lngKeys.ModalsCreateNewDocument)}
517+
</Button>
518+
) : newDocRowState === 'editing' ? (
519+
<FormInput
520+
ref={newDocInputRef}
521+
value={newDocName}
522+
onChange={(event) => {
523+
setNewDocName(event.target.value)
524+
}}
525+
onCompositionStart={() => {
526+
newDocCompositionStateRef.current = true
527+
}}
528+
onCompositionEnd={() => {
529+
newDocCompositionStateRef.current = false
530+
if (newDocInputRef.current != null) {
531+
newDocInputRef.current.focus()
495532
}
496-
)
497-
}
498-
>
499-
{translate(lngKeys.ModalsCreateNewDocument)}
500-
</Button>
533+
}}
534+
onKeyPress={(event) => {
535+
if (newDocCompositionStateRef.current) {
536+
return
537+
}
538+
switch (event.key) {
539+
case 'Escape':
540+
event.preventDefault()
541+
setNewDocRowState('idle')
542+
setNewDocName('')
543+
return
544+
case 'Enter':
545+
event.preventDefault()
546+
createNewDoc()
547+
return
548+
}
549+
}}
550+
onBlur={() => {
551+
createNewDoc()
552+
}}
553+
/>
554+
) : (
555+
<Spinner />
556+
)}
501557
</TableContentManagerRow>
502558
)}
503559

0 commit comments

Comments
 (0)