Files
modeling-app/src/hooks/useProjectsLoader.tsx
Kevin Nadro fdeb2b3f49 Feature: Implement read write access checking on Project Directory and report any issues in home page (#5676)
* chore: skeleton to detect read write directories and if we have access to notify user

* chore: adding buttont to easily change project directory

* chore: cleaning up home page error bar layout and button

* fix: adding clearer comments

* fix: ugly console debugging but I need to save off progress

* fix: removing project dir check on empty string

* fix: debug progress to save off listProjects once. Still bugged...

* fix: more hard coded debugging to get project loading optimizted

* fix: yarp, we got another one bois

* fix: cleaning up code

* fix: massive bug comment to warn devs about chokidar bugs

* fix: returning error instead of throwing

* fix: cleaning up PR

* fix: fixed loading the projects when the project directory changes

* fix: remove testing code

* fix: only skip directories if you can access the project directory since we don't need to view them

* fix: unit tests, turning off noisey localhost vitest garbage

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores)

* fix: deleted testing state

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Pierre Jacquier <pierrejacquier39@gmail.com>
Co-authored-by: Pierre Jacquier <pierre@zoo.dev>
2025-03-24 19:57:01 +00:00

44 lines
1.4 KiB
TypeScript

import { trap } from 'lib/trap'
import { useState, useEffect } from 'react'
import { ensureProjectDirectoryExists, listProjects } from 'lib/desktop'
import { loadAndValidateSettings } from 'lib/settings/settingsUtils'
import { Project } from 'lib/project'
import { isDesktop } from 'lib/isDesktop'
// Gotcha: This should be ported to the ProjectMachine and keep track of
// projectDirs and projectPaths in the context when it internally calls listProjects
// Hook uses [number] to give users familiarity. It is meant to mimic a
// dependency array, but is intended to only ever be used with 1 value.
export const useProjectsLoader = (deps?: [number]) => {
const [lastTs, setLastTs] = useState(-1)
const [projectPaths, setProjectPaths] = useState<Project[]>([])
const [projectsDir, setProjectsDir] = useState<string | undefined>(undefined)
useEffect(() => {
// Useless on web, until we get fake filesystems over there.
if (!isDesktop()) return
if (deps && deps[0] === lastTs) return
if (deps) {
setLastTs(deps[0])
}
;(async () => {
const { configuration } = await loadAndValidateSettings()
const _projectsDir = await ensureProjectDirectoryExists(configuration)
setProjectsDir(_projectsDir)
if (projectsDir) {
const _projectPaths = await listProjects(configuration)
setProjectPaths(_projectPaths)
}
})().catch(trap)
}, deps ?? [])
return {
projectPaths,
projectsDir,
}
}