diff --git a/src/components/Providers/SystemIOProviderDesktop.tsx b/src/components/Providers/SystemIOProviderDesktop.tsx index c2f0c7441..8560778fe 100644 --- a/src/components/Providers/SystemIOProviderDesktop.tsx +++ b/src/components/Providers/SystemIOProviderDesktop.tsx @@ -1,6 +1,8 @@ +import { useFileSystemWatcher } from '@src/hooks/useFileSystemWatcher' import { PATHS } from '@src/lib/paths' import { systemIOActor, useSettings } from '@src/machines/appMachine' import { + useHasListedProjects, useProjectDirectoryPath, useRequestedFileName, useRequestedProjectName, @@ -13,6 +15,7 @@ export function SystemIOMachineLogicListener() { const requestedProjectName = useRequestedProjectName() const requestedFileName = useRequestedFileName() const projectDirectoryPath = useProjectDirectoryPath() + const hasListedProjects = useHasListedProjects() const navigate = useNavigate() const settings = useSettings() @@ -68,5 +71,25 @@ export function SystemIOMachineLogicListener() { }) }, [settings.projects.defaultProjectName.current]) + useFileSystemWatcher( + async () => { + // Gotcha: Chokidar is buggy. It will emit addDir or add on files that did not get created. + // This means while the application initialize and Chokidar initializes you cannot tell if + // a directory or file is actually created or they are buggy signals. This means you must + // ignore all signals during initialization because it is ambiguous. Once those signals settle + // you can actually start listening to real signals. + // If someone creates folders or files during initialization we ignore those events! + if (!hasListedProjects) { + return + } + systemIOActor.send({ + type: SystemIOMachineEvents.readFoldersFromProjectDirectory, + }) + }, + settings.app.projectDirectory.current + ? [settings.app.projectDirectory.current] + : [] + ) + return null } diff --git a/src/machines/appMachine.ts b/src/machines/appMachine.ts index ea8ee3b8d..31d8aa616 100644 --- a/src/machines/appMachine.ts +++ b/src/machines/appMachine.ts @@ -79,8 +79,10 @@ export const useSettings = () => return settings }) +// TODO: Debugging export const systemIOActor = appActor.getSnapshot().children.systemIO! +window.systemIOActor = systemIOActor export const engineStreamActor = appActor.system.get( ENGINE_STREAM ) as EngineStreamActor diff --git a/src/machines/systemIO/hooks.ts b/src/machines/systemIO/hooks.ts index c4d84c1d7..575740c07 100644 --- a/src/machines/systemIO/hooks.ts +++ b/src/machines/systemIO/hooks.ts @@ -14,3 +14,5 @@ export const useCanReadWriteProjectDirectory = () => systemIOActor, (state) => state.context.canReadWriteProjectDirectory ) +export const useHasListedProjects = () => + useSelector(systemIOActor, (state) => state.context.hasListedProjects) diff --git a/src/machines/systemIO/systemIOMachine.ts b/src/machines/systemIO/systemIOMachine.ts index af962811a..ac7d7968c 100644 --- a/src/machines/systemIO/systemIOMachine.ts +++ b/src/machines/systemIO/systemIOMachine.ts @@ -258,7 +258,10 @@ export const systemIOMachine = setup({ }, onDone: { target: SystemIOMachineStates.idle, - actions: [SystemIOMachineActions.setFolders], + actions: [ + SystemIOMachineActions.setFolders, + assign({ hasListedProjects: true }), + ], }, onError: { target: SystemIOMachineStates.idle, @@ -278,7 +281,14 @@ export const systemIOMachine = setup({ }, onDone: { target: SystemIOMachineStates.readingFolders, - actions: [SystemIOMachineActions.toastSuccess], + actions: [ + assign({ + requestedProjectName: ({ event }) => { + return { name: event.output.name } + }, + }), + SystemIOMachineActions.toastSuccess, + ], }, onError: { target: SystemIOMachineStates.idle,