fix: use file system watcher, navigate to project after creation via the requestProjectName

This commit is contained in:
Kevin Nadro
2025-04-15 12:45:46 -06:00
parent 01fc2c43c2
commit 80af10a988
4 changed files with 39 additions and 2 deletions

View File

@ -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
}

View File

@ -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

View File

@ -14,3 +14,5 @@ export const useCanReadWriteProjectDirectory = () =>
systemIOActor,
(state) => state.context.canReadWriteProjectDirectory
)
export const useHasListedProjects = () =>
useSelector(systemIOActor, (state) => state.context.hasListedProjects)

View File

@ -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,