2024-03-14 15:56:45 -04:00
|
|
|
import { ActionFunction, LoaderFunction, redirect } from 'react-router-dom'
|
2024-04-02 10:29:34 -04:00
|
|
|
import { FileLoaderData, HomeLoaderData, IndexLoaderData } from './types'
|
2024-03-14 15:56:45 -04:00
|
|
|
import { isTauri } from './isTauri'
|
2024-04-02 10:29:34 -04:00
|
|
|
import { getProjectMetaByRouteId, paths } from './paths'
|
|
|
|
import { BROWSER_PATH } from 'lib/paths'
|
|
|
|
import {
|
|
|
|
BROWSER_FILE_NAME,
|
|
|
|
BROWSER_PROJECT_NAME,
|
|
|
|
PROJECT_ENTRYPOINT,
|
|
|
|
} from 'lib/constants'
|
2024-03-14 15:56:45 -04:00
|
|
|
import { loadAndValidateSettings } from './settings/settingsUtils'
|
|
|
|
import {
|
|
|
|
getInitialDefaultDir,
|
|
|
|
getProjectsInDir,
|
|
|
|
initializeProjectDirectory,
|
|
|
|
} from './tauriFS'
|
|
|
|
import makeUrlPathRelative from './makeUrlPathRelative'
|
|
|
|
import { sep } from '@tauri-apps/api/path'
|
|
|
|
import { readDir, readTextFile } from '@tauri-apps/api/fs'
|
|
|
|
import { metadata } from 'tauri-plugin-fs-extra-api'
|
2024-03-22 16:55:30 +11:00
|
|
|
import { kclManager } from 'lib/singletons'
|
2024-03-14 15:56:45 -04:00
|
|
|
import { fileSystemManager } from 'lang/std/fileSystemManager'
|
|
|
|
|
|
|
|
// The root loader simply resolves the settings and any errors that
|
|
|
|
// occurred during the settings load
|
2024-04-02 10:29:34 -04:00
|
|
|
export const settingsLoader: LoaderFunction = async ({
|
|
|
|
params,
|
|
|
|
}): ReturnType<typeof loadAndValidateSettings> => {
|
|
|
|
let settings = await loadAndValidateSettings()
|
|
|
|
|
|
|
|
// I don't love that we have to read the settings again here,
|
|
|
|
// but we need to get the project path to load the project settings
|
|
|
|
if (params.id) {
|
|
|
|
const defaultDir = settings.app.projectDirectory.current || ''
|
|
|
|
const projectPathData = getProjectMetaByRouteId(params.id, defaultDir)
|
|
|
|
if (projectPathData) {
|
|
|
|
const { projectPath } = projectPathData
|
|
|
|
settings = await loadAndValidateSettings(projectPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return settings
|
2024-03-14 15:56:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect users to the appropriate onboarding page if they haven't completed it
|
2024-04-02 10:29:34 -04:00
|
|
|
export const onboardingRedirectLoader: ActionFunction = async (args) => {
|
|
|
|
const settings = await loadAndValidateSettings()
|
|
|
|
const onboardingStatus = settings.app.onboardingStatus.current || ''
|
|
|
|
const notEnRouteToOnboarding = !args.request.url.includes(
|
|
|
|
paths.ONBOARDING.INDEX
|
|
|
|
)
|
2024-03-14 15:56:45 -04:00
|
|
|
// '' is the initial state, 'done' and 'dismissed' are the final states
|
|
|
|
const hasValidOnboardingStatus =
|
|
|
|
onboardingStatus.length === 0 ||
|
|
|
|
!(onboardingStatus === 'done' || onboardingStatus === 'dismissed')
|
|
|
|
const shouldRedirectToOnboarding =
|
|
|
|
notEnRouteToOnboarding && hasValidOnboardingStatus
|
|
|
|
|
|
|
|
if (shouldRedirectToOnboarding) {
|
|
|
|
return redirect(
|
|
|
|
makeUrlPathRelative(paths.ONBOARDING.INDEX) + onboardingStatus.slice(1)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-04-02 10:29:34 -04:00
|
|
|
return settingsLoader(args)
|
2024-03-14 15:56:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export const fileLoader: LoaderFunction = async ({
|
|
|
|
params,
|
2024-04-02 10:29:34 -04:00
|
|
|
}): Promise<FileLoaderData | Response> => {
|
|
|
|
let settings = await loadAndValidateSettings()
|
2024-03-14 15:56:45 -04:00
|
|
|
|
2024-04-02 10:29:34 -04:00
|
|
|
const defaultDir = settings.app.projectDirectory.current || '/'
|
|
|
|
const projectPathData = getProjectMetaByRouteId(params.id, defaultDir)
|
|
|
|
const isBrowserProject = params.id === decodeURIComponent(BROWSER_PATH)
|
2024-03-14 15:56:45 -04:00
|
|
|
|
2024-04-02 10:29:34 -04:00
|
|
|
if (!isBrowserProject && projectPathData) {
|
|
|
|
const { projectName, projectPath, currentFileName, currentFilePath } =
|
|
|
|
projectPathData
|
2024-03-14 15:56:45 -04:00
|
|
|
|
2024-04-02 10:29:34 -04:00
|
|
|
if (!currentFileName || !currentFilePath) {
|
2024-03-14 15:56:45 -04:00
|
|
|
return redirect(
|
|
|
|
`${paths.FILE}/${encodeURIComponent(
|
2024-04-02 10:29:34 -04:00
|
|
|
`${params.id}${isTauri() ? sep : '/'}${PROJECT_ENTRYPOINT}`
|
2024-03-14 15:56:45 -04:00
|
|
|
)}`
|
|
|
|
)
|
2024-04-02 10:29:34 -04:00
|
|
|
}
|
2024-03-14 15:56:45 -04:00
|
|
|
|
|
|
|
// TODO: PROJECT_ENTRYPOINT is hardcoded
|
|
|
|
// until we support setting a project's entrypoint file
|
2024-04-02 10:29:34 -04:00
|
|
|
const code = await readTextFile(currentFilePath)
|
2024-03-14 15:56:45 -04:00
|
|
|
const entrypointMetadata = await metadata(
|
|
|
|
projectPath + sep + PROJECT_ENTRYPOINT
|
|
|
|
)
|
|
|
|
const children = await readDir(projectPath, { recursive: true })
|
|
|
|
kclManager.setCodeAndExecute(code, false)
|
|
|
|
|
|
|
|
// Set the file system manager to the project path
|
|
|
|
// So that WASM gets an updated path for operations
|
|
|
|
fileSystemManager.dir = projectPath
|
|
|
|
|
2024-04-02 10:29:34 -04:00
|
|
|
const projectData: IndexLoaderData = {
|
2024-03-14 15:56:45 -04:00
|
|
|
code,
|
|
|
|
project: {
|
|
|
|
name: projectName,
|
|
|
|
path: projectPath,
|
|
|
|
children,
|
|
|
|
entrypointMetadata,
|
|
|
|
},
|
|
|
|
file: {
|
|
|
|
name: currentFileName,
|
2024-04-02 10:29:34 -04:00
|
|
|
path: currentFilePath,
|
2024-03-14 15:56:45 -04:00
|
|
|
},
|
|
|
|
}
|
2024-04-02 10:29:34 -04:00
|
|
|
|
|
|
|
return {
|
|
|
|
...projectData,
|
|
|
|
}
|
2024-03-14 15:56:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
code: '',
|
2024-04-02 10:29:34 -04:00
|
|
|
project: {
|
|
|
|
name: BROWSER_PROJECT_NAME,
|
|
|
|
path: '/' + BROWSER_PROJECT_NAME,
|
|
|
|
children: [],
|
|
|
|
},
|
|
|
|
file: {
|
|
|
|
name: BROWSER_FILE_NAME,
|
|
|
|
path: decodeURIComponent(BROWSER_PATH),
|
|
|
|
},
|
2024-03-14 15:56:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loads the settings and by extension the projects in the default directory
|
|
|
|
// and returns them to the Home route, along with any errors that occurred
|
|
|
|
export const homeLoader: LoaderFunction = async (): Promise<
|
|
|
|
HomeLoaderData | Response
|
|
|
|
> => {
|
|
|
|
if (!isTauri()) {
|
2024-04-02 10:29:34 -04:00
|
|
|
return redirect(paths.FILE + '/' + BROWSER_PROJECT_NAME)
|
2024-03-14 15:56:45 -04:00
|
|
|
}
|
2024-04-02 10:29:34 -04:00
|
|
|
const settings = await loadAndValidateSettings()
|
|
|
|
|
2024-03-14 15:56:45 -04:00
|
|
|
const projectDir = await initializeProjectDirectory(
|
2024-04-02 10:29:34 -04:00
|
|
|
settings.app.projectDirectory.current || (await getInitialDefaultDir())
|
2024-03-14 15:56:45 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
if (projectDir.path) {
|
|
|
|
const projects = await getProjectsInDir(projectDir.path)
|
|
|
|
|
|
|
|
return {
|
|
|
|
projects,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
projects: [],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|