(props: ImportsTrayProps)
| 70 | } |
| 71 | |
| 72 | export const ImportsTray = (props: ImportsTrayProps) => { |
| 73 | const { rootNode, startDrag, addImports, moduleTrayLoader } = props |
| 74 | const editorContext = useContext<EditorState>(EditorStateContext) |
| 75 | |
| 76 | const [importsList, setImportsList] = useState<Import[]>([]) |
| 77 | |
| 78 | const refreshModuleList = useCallback(() => { |
| 79 | const scopeImportNames = rootNode.getScope().getImportedModules() |
| 80 | |
| 81 | const scopeImports = scopeImportNames |
| 82 | .filter((moduleName) => dynamicallyInstallable.get(moduleName)) |
| 83 | .map((name): Import => { |
| 84 | return { |
| 85 | name: name, |
| 86 | isDeletable: false, |
| 87 | } |
| 88 | }) |
| 89 | |
| 90 | const projectImports = editorContext.project.dependencies.map((dep): Import => { |
| 91 | // TODO(harrison): ideally you can't delete external modules when they're being used? |
| 92 | const isDeletable = true // !scopeImportNames.includes(dep.name) |
| 93 | |
| 94 | return { |
| 95 | name: dep.name, |
| 96 | isDeletable, |
| 97 | } |
| 98 | }) |
| 99 | |
| 100 | setImportsList([...scopeImports, ...projectImports]) |
| 101 | }, [rootNode]) |
| 102 | |
| 103 | useEffect(() => { |
| 104 | refreshModuleList() |
| 105 | const scopeObserver: ScopeObserver = { |
| 106 | handleScopeMutation: (mutation: ScopeMutation) => { |
| 107 | if (mutation.type === ScopeMutationType.IMPORT_MODULE) { |
| 108 | refreshModuleList() |
| 109 | } |
| 110 | }, |
| 111 | } |
| 112 | |
| 113 | const projectObserver: ProjectObserver = { |
| 114 | handleProjectMutation(mutation) { |
| 115 | if (mutation.type === ProjectMutationType.UPDATE_DEPENDENCIES) { |
| 116 | refreshModuleList() |
| 117 | } |
| 118 | }, |
| 119 | } |
| 120 | |
| 121 | globalMutationDispatcher.registerProjectObserver(projectObserver) |
| 122 | globalMutationDispatcher.registerScopeObserver(scopeObserver) |
| 123 | return () => { |
| 124 | globalMutationDispatcher.deregisterScopeObserver(scopeObserver) |
| 125 | globalMutationDispatcher.deregisterProjectObserver(projectObserver) |
| 126 | } |
| 127 | }, [rootNode]) |
| 128 | |
| 129 | return ( |
nothing calls this directly
no test coverage detected