Skip to content

Commit

Permalink
fix: comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoecheza committed May 10, 2023
1 parent ead2a5e commit ca99ea3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/@dcl/inspector/src/components/Block/Block.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
flex-grow: 1;
}

.Block.broken {
.Block.error {
border: 1px var(--primary) dashed;
padding: 0 5px 8px 5px;
border-radius: 2px;
Expand Down
4 changes: 2 additions & 2 deletions packages/@dcl/inspector/src/components/Block/Block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { Props } from './types'

import './Block.css'

const Block = React.forwardRef<null, React.PropsWithChildren<Props>>(({ label, broken, children }, ref) => {
const Block = React.forwardRef<null, React.PropsWithChildren<Props>>(({ label, error, children }, ref) => {
return (
<div ref={ref} className={cx("Block", { broken })}>
<div ref={ref} className={cx("Block", { error })}>
{label && <label>{label}</label>}
<div className="content">{children}</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion packages/@dcl/inspector/src/components/Block/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type Props = {
label?: string
broken?: boolean
error?: boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ export default withSdk<Props>(

const hasGltf = useHasComponent(entity, GltfContainer)
const handleInputValidation = useCallback(({ src }: { src: string }) => isValidInput(files, src), [files])
const { getInputProps, monitor } = useComponentInput(entity, GltfContainer, fromGltf, toGltf, handleInputValidation, [files])
const { getInputProps, isValid } = useComponentInput(entity, GltfContainer, fromGltf, toGltf, handleInputValidation, [files])

const handleRemove = useCallback(() => GltfContainer.deleteFrom(entity), [])
const handleDrop = useCallback((src: string) => {
GltfContainer.createOrReplace(entity, { src })
const handleDrop = useCallback(async (src: string) => {
const { operations } = sdk
operations.updateValue(GltfContainer, entity, { src })
await operations.dispatch()
}, [])

const [{ isHover }, drop] = useDrop(
Expand Down Expand Up @@ -65,7 +67,7 @@ export default withSdk<Props>(
<DeleteIcon /> Delete
</Item>
</Menu>
<Block label="Path" ref={drop} broken={init && !monitor.isValid}>
<Block label="Path" ref={drop} error={init && !isValid}>
<TextField type="text" {...inputProps} />
</Block>
</Container>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Entity } from '@dcl/ecs'
import { useCallback, useState } from 'react'
import { IoIosArrowDown, IoIosArrowForward, IoIosImage } from 'react-icons/io'
import { AiFillFolder } from 'react-icons/ai'
Expand All @@ -21,14 +22,20 @@ type Props = {
folders: AssetNodeFolder[]
}

interface ModalState {
isOpen: boolean
value: string
entities: Entity[]
}

export const ROOT = 'File System'

export type TreeNode = Omit<AssetNode, 'children'> & { children?: string[] }

function ProjectView({ folders, onImportAsset }: Props) {
const sdk = useSdk()
const [open, setOpen] = useState(new Set<string>())
const [modal, setModal] = useState<{ isOpen: boolean; value: string } | undefined>(undefined)
const [modal, setModal] = useState<ModalState | undefined>(undefined)
const getTree = useCallback(() => {
const tree = new Map<string, TreeNode>()
tree.set(ROOT, { children: folders.map(f => f.name), name: ROOT, type: 'folder', parent: null })
Expand Down Expand Up @@ -63,32 +70,37 @@ function ProjectView({ folders, onImportAsset }: Props) {
setOpen(new Set(open))
}, [open, setOpen])

const shouldOpenModal = useCallback((value: string) => {
if (!sdk || modal?.isOpen) return false
const getEntitiesWithAsset = useCallback((value: string): Entity[] => {
if (!sdk || modal?.isOpen) return []
const entitiesWithAsset: Entity[] = []
const { GltfContainer } = sdk.components
for (const [_, _value] of sdk.engine.getEntitiesWith(GltfContainer)) {
for (const [entity, _value] of sdk.engine.getEntitiesWith(GltfContainer)) {
if (_value.src === value) {
return true
entitiesWithAsset.push(entity)
}
}
return false
return entitiesWithAsset
}, [])

const handleRemove = useCallback(async (value: string) => {
const path = getFullNodePath(tree.get(value)!).slice(1)
if (shouldOpenModal(path)) return setModal({ isOpen: true, value: path })
const entitiesWithAsset = getEntitiesWithAsset(path)
if (entitiesWithAsset.length) return setModal({ isOpen: true, value: path, entities: entitiesWithAsset })
await removeAsset(path)
}, [open, setOpen])

const removeAsset = useCallback(async (path: string) => {
const removeAsset = useCallback(async (path: string, entities: Entity[] = []) => {
if (!sdk) return
await sdk.dataLayer.removeAsset({ path })
const { dataLayer, components, operations } = sdk
await dataLayer.removeAsset({ path })
fileSystemEvent.emit('change')
entities.forEach(($) => operations.updateValue(components.GltfContainer, $, { src: '' }))
await operations.dispatch()
}, [])

const handleConfirm = useCallback(async () => {
if (!modal) return
await removeAsset(modal.value)
await removeAsset(modal.value, modal.entities)
setModal(undefined)
}, [modal, setModal])

Expand Down
8 changes: 3 additions & 5 deletions packages/@dcl/inspector/src/hooks/sdk/useComponentInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const useComponentInput = <ComponentValueType extends object, InputType e
)
const [isFocused, setIsFocused] = useState(false)
const [skipSync, setSkipSync] = useState(false)
const [monitor, setMonitor] = useState({ isValid: false })
const [isValid, setIsValid] = useState(false)

const updateInputs = useCallback((value: InputType | null, skipSync = false) => {
setSkipSync(skipSync)
Expand Down Expand Up @@ -83,9 +83,7 @@ export const useComponentInput = <ComponentValueType extends object, InputType e
}, [componentValue])

useEffect(() => {
setMonitor({
isValid: validate(input)
})
setIsValid(validate(input))
}, [input, ...deps])

const getProps = useCallback(
Expand All @@ -104,5 +102,5 @@ export const useComponentInput = <ComponentValueType extends object, InputType e
[handleUpdate, handleFocus, handleBlur, input]
)

return { getInputProps: getProps, monitor }
return { getInputProps: getProps, isValid }
}

0 comments on commit ca99ea3

Please sign in to comment.