Skip to content

Commit 2d060b3

Browse files
Feat/auto import assets (#1152)
* feat: upload file to assets * feat: Set correct route on import * feat: Add missing extesion to file name * feat: Reorder imports * feat: add missing deps on useefect
1 parent 25a7f83 commit 2d060b3

1 file changed

Lines changed: 34 additions & 19 deletions

File tree

packages/@dcl/inspector/src/components/ui/FileUploadField/FileUploadField.tsx

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import { VscFolderOpened as FolderIcon } from 'react-icons/vsc'
66

77
import { selectAssetCatalog, selectUploadFile, updateUploadFile } from '../../../redux/app'
88
import { useAppDispatch, useAppSelector } from '../../../redux/hooks'
9+
import { importAsset } from '../../../redux/data-layer'
910
import { DropTypesEnum, LocalAssetDrop, getNode } from '../../../lib/sdk/drag-drop'
10-
import { EXTENSIONS, withAssetDir } from '../../../lib/data-layer/host/fs-utils'
11+
import { DIRECTORY, EXTENSIONS, withAssetDir } from '../../../lib/data-layer/host/fs-utils'
1112

13+
import { buildAssetPath, convertAssetToBinary, processAssets } from '../../ImportAsset/utils'
1214
import { isModel } from '../../EntityInspector/GltfInspector/utils'
1315
import { isAudio } from '../../EntityInspector/AudioSourceInspector/utils'
1416
import { isModel as isTexture } from '../../EntityInspector/MaterialInspector/Texture/utils'
@@ -126,24 +128,6 @@ const FileUploadField: React.FC<Props> = ({
126128
inputRef.current?.click()
127129
}, [inputRef])
128130

129-
const handleChange = useCallback(
130-
(event: React.ChangeEvent<HTMLInputElement>) => {
131-
const file = event.target.files?.[0]
132-
if (file && isValidFileName(file.name)) {
133-
setDropError(false)
134-
const newUploadFile = { ...uploadFile }
135-
newUploadFile[id.current] = file
136-
dispatch(updateUploadFile(newUploadFile))
137-
} else {
138-
setDropError(true)
139-
}
140-
141-
// Clear input value
142-
if (inputRef.current) inputRef.current.value = ''
143-
},
144-
[inputRef, setPath, setDropError]
145-
)
146-
147131
const handleChangeTextField = useCallback(
148132
(event: React.ChangeEvent<HTMLInputElement>) => {
149133
const { value } = event.target
@@ -161,6 +145,37 @@ const FileUploadField: React.FC<Props> = ({
161145
[addBase, setPath, setDropError, acceptURLs, onChange]
162146
)
163147

148+
const handleChange = useCallback(
149+
async (event: React.ChangeEvent<HTMLInputElement>) => {
150+
const file = event.target.files?.[0]
151+
if (file && isValidFileName(file.name)) {
152+
setDropError(false)
153+
const [newAsset] = await processAssets([file])
154+
const basePath = withAssetDir(DIRECTORY.SCENE)
155+
const assetPackageName = buildAssetPath(newAsset)
156+
const content = await convertAssetToBinary(newAsset)
157+
const newUploadFile = { ...uploadFile }
158+
newUploadFile[id.current] = file
159+
const assetPath = `${basePath}/${assetPackageName}/${newAsset.name}.${newAsset.extension}`
160+
dispatch(
161+
importAsset({
162+
content,
163+
basePath,
164+
assetPackageName,
165+
reload: true
166+
})
167+
)
168+
dispatch(updateUploadFile(newUploadFile))
169+
setPath(assetPath)
170+
onDrop && onDrop(assetPath)
171+
} else {
172+
setDropError(true)
173+
}
174+
if (inputRef.current) inputRef.current.value = ''
175+
},
176+
[inputRef, setPath, setDropError, uploadFile, onDrop]
177+
)
178+
164179
const hasError = useMemo(() => {
165180
return error || dropError
166181
}, [error, dropError])

0 commit comments

Comments
 (0)