2023-12-15 04:19:42 -05:00
|
|
|
import {
|
|
|
|
createContext,
|
|
|
|
FC,
|
|
|
|
useCallback,
|
|
|
|
useContext,
|
|
|
|
useEffect,
|
|
|
|
useMemo,
|
|
|
|
useRef,
|
|
|
|
useState,
|
|
|
|
} from 'react'
|
|
|
|
import { useProjectContext } from '@/shared/context/project-context'
|
|
|
|
import { useIdeReactContext } from '@/features/ide-react/context/ide-react-context'
|
|
|
|
import { useEditorManagerContext } from '@/features/ide-react/context/editor-manager-context'
|
2023-12-05 05:19:00 -05:00
|
|
|
import { useSelectFileTreeEntity } from '@/features/ide-react/hooks/use-select-file-tree-entity'
|
|
|
|
import useScopeValue from '@/shared/hooks/use-scope-value'
|
2023-12-15 04:19:42 -05:00
|
|
|
import { BinaryFile } from '@/features/file-view/types/binary-file'
|
2023-11-02 07:36:04 -04:00
|
|
|
import {
|
|
|
|
FileTreeDocumentFindResult,
|
|
|
|
FileTreeFileRefFindResult,
|
|
|
|
FileTreeFindResult,
|
|
|
|
} from '@/features/ide-react/types/file-tree'
|
2023-12-05 05:19:00 -05:00
|
|
|
import { debugConsole } from '@/utils/debugging'
|
2023-12-15 04:19:42 -05:00
|
|
|
import { convertFileRefToBinaryFile } from '@/features/ide-react/util/file-view'
|
2023-11-02 07:36:04 -04:00
|
|
|
|
2023-12-15 04:19:42 -05:00
|
|
|
const FileTreeOpenContext = createContext<
|
|
|
|
| {
|
|
|
|
selectedEntityCount: number
|
|
|
|
openEntity: FileTreeDocumentFindResult | FileTreeFileRefFindResult | null
|
|
|
|
handleFileTreeInit: () => void
|
|
|
|
handleFileTreeSelect: (selectedEntities: FileTreeFindResult[]) => void
|
|
|
|
handleFileTreeDelete: (entity: FileTreeFindResult) => void
|
|
|
|
}
|
|
|
|
| undefined
|
|
|
|
>(undefined)
|
|
|
|
|
|
|
|
export const FileTreeOpenProvider: FC = ({ children }) => {
|
2023-11-03 08:15:36 -04:00
|
|
|
const { rootDocId } = useProjectContext()
|
2023-12-15 04:19:42 -05:00
|
|
|
const { eventEmitter, projectJoined } = useIdeReactContext()
|
2023-11-03 08:15:36 -04:00
|
|
|
const {
|
|
|
|
openDocId: openDocWithId,
|
|
|
|
currentDocumentId: openDocId,
|
|
|
|
openInitialDoc,
|
|
|
|
} = useEditorManagerContext()
|
2023-11-15 06:35:18 -05:00
|
|
|
const { selectEntity } = useSelectFileTreeEntity()
|
2023-11-03 08:15:36 -04:00
|
|
|
const [, setOpenFile] = useScopeValue<BinaryFile | null>('openFile')
|
2023-11-02 07:36:04 -04:00
|
|
|
const [openEntity, setOpenEntity] = useState<
|
|
|
|
FileTreeDocumentFindResult | FileTreeFileRefFindResult | null
|
|
|
|
>(null)
|
|
|
|
const [selectedEntityCount, setSelectedEntityCount] = useState(0)
|
2023-10-26 04:57:00 -04:00
|
|
|
const [fileTreeReady, setFileTreeReady] = useState(false)
|
|
|
|
|
|
|
|
const handleFileTreeInit = useCallback(() => {
|
|
|
|
setFileTreeReady(true)
|
|
|
|
}, [])
|
|
|
|
|
2023-11-15 06:35:18 -05:00
|
|
|
// Open a document in the editor when one is selected in the file tree
|
2023-10-26 04:57:00 -04:00
|
|
|
const handleFileTreeSelect = useCallback(
|
|
|
|
(selectedEntities: FileTreeFindResult[]) => {
|
2023-11-02 07:36:04 -04:00
|
|
|
debugConsole.log('File tree selection changed', selectedEntities)
|
|
|
|
setSelectedEntityCount(selectedEntities.length)
|
|
|
|
if (selectedEntities.length !== 1) {
|
|
|
|
setOpenEntity(null)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const [selected] = selectedEntities
|
|
|
|
|
|
|
|
if (selected.type === 'folder') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
setOpenEntity(selected)
|
2023-11-03 08:15:36 -04:00
|
|
|
if (selected.type === 'doc' && fileTreeReady) {
|
|
|
|
openDocWithId(selected.entity._id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep openFile scope value in sync with the file tree
|
|
|
|
const openFile =
|
|
|
|
selected.type === 'fileRef'
|
|
|
|
? convertFileRefToBinaryFile(selected.entity)
|
|
|
|
: null
|
|
|
|
setOpenFile(openFile)
|
|
|
|
if (openFile) {
|
|
|
|
window.dispatchEvent(new CustomEvent('file-view:file-opened'))
|
2023-11-02 07:36:04 -04:00
|
|
|
}
|
2023-10-26 04:57:00 -04:00
|
|
|
},
|
2023-11-03 08:15:36 -04:00
|
|
|
[fileTreeReady, setOpenFile, openDocWithId]
|
2023-10-26 04:57:00 -04:00
|
|
|
)
|
|
|
|
|
2023-12-15 04:19:42 -05:00
|
|
|
const handleFileTreeDelete = useCallback(
|
|
|
|
(entity: FileTreeFindResult) => {
|
2023-11-02 07:36:04 -04:00
|
|
|
eventEmitter.emit('entity:deleted', entity)
|
|
|
|
// Select the root document if the current document was deleted
|
|
|
|
if (entity.entity._id === openDocId) {
|
2024-01-26 04:23:48 -05:00
|
|
|
openDocWithId(rootDocId!)
|
2023-11-02 07:36:04 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
[eventEmitter, openDocId, openDocWithId, rootDocId]
|
|
|
|
)
|
|
|
|
|
2023-11-27 06:26:35 -05:00
|
|
|
const openDocIdRef = useRef<typeof openDocId | null>(null)
|
|
|
|
|
2023-11-02 07:36:04 -04:00
|
|
|
// Synchronize the file tree when openDoc or openDocId is called on the editor
|
|
|
|
// manager context from elsewhere. If the file tree does change, it will
|
|
|
|
// trigger the onSelect handler in this component, which will update the local
|
|
|
|
// state.
|
|
|
|
useEffect(() => {
|
2023-11-27 06:26:35 -05:00
|
|
|
if (openDocId !== openDocIdRef.current) {
|
|
|
|
debugConsole.log(`openDocId changed to ${openDocId}`)
|
|
|
|
openDocIdRef.current = openDocId
|
|
|
|
if (openDocId !== null) {
|
|
|
|
selectEntity(openDocId)
|
|
|
|
}
|
2023-11-02 07:36:04 -04:00
|
|
|
}
|
2023-11-15 06:35:18 -05:00
|
|
|
}, [openDocId, selectEntity])
|
2023-11-02 07:36:04 -04:00
|
|
|
|
2023-11-08 07:10:07 -05:00
|
|
|
// Open a document once the file tree and project are ready
|
2023-11-03 08:15:36 -04:00
|
|
|
const initialOpenDoneRef = useRef(false)
|
2023-11-02 07:36:04 -04:00
|
|
|
useEffect(() => {
|
2024-01-26 04:23:48 -05:00
|
|
|
if (
|
|
|
|
rootDocId &&
|
|
|
|
fileTreeReady &&
|
|
|
|
projectJoined &&
|
|
|
|
!initialOpenDoneRef.current
|
|
|
|
) {
|
2023-11-03 08:15:36 -04:00
|
|
|
initialOpenDoneRef.current = true
|
|
|
|
openInitialDoc(rootDocId)
|
2023-11-02 07:36:04 -04:00
|
|
|
}
|
2023-11-08 07:10:07 -05:00
|
|
|
}, [fileTreeReady, openInitialDoc, projectJoined, rootDocId])
|
2023-11-03 08:15:36 -04:00
|
|
|
|
2023-12-15 04:19:42 -05:00
|
|
|
const value = useMemo(() => {
|
|
|
|
return {
|
|
|
|
selectedEntityCount,
|
|
|
|
openEntity,
|
|
|
|
handleFileTreeInit,
|
|
|
|
handleFileTreeSelect,
|
|
|
|
handleFileTreeDelete,
|
|
|
|
}
|
|
|
|
}, [
|
|
|
|
handleFileTreeDelete,
|
2023-12-05 05:19:00 -05:00
|
|
|
handleFileTreeInit,
|
|
|
|
handleFileTreeSelect,
|
2023-12-15 04:19:42 -05:00
|
|
|
openEntity,
|
|
|
|
selectedEntityCount,
|
|
|
|
])
|
2023-12-05 04:34:21 -05:00
|
|
|
|
2023-12-15 04:19:42 -05:00
|
|
|
return (
|
|
|
|
<FileTreeOpenContext.Provider value={value}>
|
|
|
|
{children}
|
|
|
|
</FileTreeOpenContext.Provider>
|
|
|
|
)
|
2023-12-05 05:19:00 -05:00
|
|
|
}
|
2023-10-26 04:57:00 -04:00
|
|
|
|
2023-12-15 04:19:42 -05:00
|
|
|
export const useFileTreeOpenContext = () => {
|
|
|
|
const context = useContext(FileTreeOpenContext)
|
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
throw new Error(
|
|
|
|
'useFileTreeOpenContext is only available inside FileTreeOpenProvider'
|
|
|
|
)
|
2023-12-05 05:19:00 -05:00
|
|
|
}
|
2023-12-15 04:19:42 -05:00
|
|
|
|
|
|
|
return context
|
2023-10-26 04:57:00 -04:00
|
|
|
}
|