Only call openDoc if the creation was successful (#28153)

GitOrigin-RevId: a545e9ee9b5cfa5d81401e06a92e900ff6d06bae
This commit is contained in:
Alf Eaton
2025-09-04 12:44:33 +01:00
committed by Copybot
parent 309097a038
commit 19866f48a8
3 changed files with 62 additions and 20 deletions

View File

@@ -30,7 +30,9 @@ export default function FileTreeCreateNewDoc() {
extension: name.split('.').length > 1 ? name.split('.').pop() : '',
})
openDoc(doc)
if (doc) {
openDoc(doc)
}
},
[finishCreatingDoc, name, openDoc]
)

View File

@@ -16,6 +16,9 @@ import {
syncDelete,
syncMove,
syncCreateEntity,
NewDocEntity,
NewLinkedFileEntity,
NewEntity,
} from '../util/sync-mutation'
import { findInTree, findInTreeOrThrow } from '../util/find-in-tree'
import { isNameUniqueInFolder } from '../util/is-name-unique-in-folder'
@@ -35,6 +38,7 @@ import { Folder } from '../../../../../types/folder'
import { useReferencesContext } from '@/features/ide-react/context/references-context'
import { usePermissionsContext } from '@/features/ide-react/context/permissions-context'
import { FileTreeEntity } from '@ol-types/file-tree-entity'
import { Doc } from '@ol-types/doc'
type DroppedFile = File & {
relativePath?: string
@@ -73,8 +77,12 @@ const FileTreeActionableContext = createContext<
finishCreatingFolder: any
startCreatingDocOrFile: any
startUploadingDocOrFile: any
finishCreatingDoc: any
finishCreatingLinkedFile: any
finishCreatingDoc: (
entity: Omit<NewDocEntity, 'endpoint'>
) => Promise<Doc | undefined>
finishCreatingLinkedFile: (
entity: Omit<NewLinkedFileEntity, 'endpoint'>
) => Promise<{ new_file_id: string } | undefined>
cancel: () => void
droppedFiles: { files: File[]; targetFolderId: string } | null
setDroppedFiles: (value: DroppedFiles | null) => void
@@ -403,7 +411,7 @@ export const FileTreeActionableProvider: FC<React.PropsWithChildren> = ({
}, [fileTreeData, selectedEntityIds])
const finishCreatingEntity = useCallback(
(entity: any) => {
(entity: NewEntity) => {
const error = validateCreate(fileTreeData, parentFolderId, entity)
if (error) {
return Promise.reject(error)
@@ -415,7 +423,7 @@ export const FileTreeActionableProvider: FC<React.PropsWithChildren> = ({
)
const finishCreatingFolder = useCallback(
(name: any) => {
(name: string) => {
dispatch({ type: ACTION_TYPES.CREATING_FOLDER })
return finishCreatingEntity({ endpoint: 'folder', name })
.then(() => {
@@ -440,8 +448,16 @@ export const FileTreeActionableProvider: FC<React.PropsWithChildren> = ({
startCreatingFile('upload')
}, [startCreatingFile])
type FinishCreatingDocOrFileReturn<T> = T extends NewDocEntity
? Promise<Doc | undefined>
: T extends NewLinkedFileEntity
? Promise<{ new_file_id: string } | undefined>
: never
const finishCreatingDocOrFile = useCallback(
(entity: any) => {
<T extends NewDocEntity | NewLinkedFileEntity>(
entity: T
): FinishCreatingDocOrFileReturn<T> => {
dispatch({ type: ACTION_TYPES.CREATING_FILE })
return finishCreatingEntity(entity)
@@ -451,23 +467,21 @@ export const FileTreeActionableProvider: FC<React.PropsWithChildren> = ({
})
.catch(error => {
dispatch({ type: ACTION_TYPES.ERROR, error })
})
}) as FinishCreatingDocOrFileReturn<T>
},
[finishCreatingEntity]
)
const finishCreatingDoc = useCallback(
(entity: any) => {
entity.endpoint = 'doc'
return finishCreatingDocOrFile(entity)
(entity: Omit<NewDocEntity, 'endpoint'>) => {
return finishCreatingDocOrFile({ ...entity, endpoint: 'doc' })
},
[finishCreatingDocOrFile]
)
const finishCreatingLinkedFile = useCallback(
(entity: any) => {
entity.endpoint = 'linked_file'
return finishCreatingDocOrFile(entity)
(entity: Omit<NewLinkedFileEntity, 'endpoint'>) => {
return finishCreatingDocOrFile({ ...entity, endpoint: 'linked_file' })
},
[finishCreatingDocOrFile]
)

View File

@@ -1,4 +1,6 @@
import { postJSON, deleteJSON } from '../../../infrastructure/fetch-json'
import { Folder } from '@ol-types/folder'
import { Doc } from '@ol-types/doc'
export function syncRename(
projectId: string,
@@ -42,21 +44,45 @@ export function syncMove(
)
}
export function syncCreateEntity(
export type NewDocEntity = {
endpoint: 'doc'
name: string
}
export type NewFolderEntity = {
endpoint: 'folder'
name: string
}
export type NewLinkedFileEntity = {
endpoint: 'linked_file'
name: string
provider: string
data: Record<string, any>
}
export type NewEntity = NewDocEntity | NewFolderEntity | NewLinkedFileEntity
type SyncCreateEntityReturn<T> = T extends NewDocEntity
? Promise<Doc>
: T extends NewFolderEntity
? Promise<Folder>
: T extends NewLinkedFileEntity
? Promise<{ new_file_id: string }>
: never
export function syncCreateEntity<T extends NewEntity>(
projectId: string,
parentFolderId: string,
newEntityData: {
endpoint: 'doc' | 'folder' | 'linked-file'
[key: string]: unknown
}
) {
newEntityData: T
): SyncCreateEntityReturn<T> {
const { endpoint, ...newEntity } = newEntityData
return postJSON(`/project/${projectId}/${endpoint}`, {
body: {
parent_folder_id: parentFolderId,
...newEntity,
},
})
}) as SyncCreateEntityReturn<T>
}
function getEntityPathName(entityType: string) {