2024-03-14 15:56:45 -04:00
|
|
|
import { ActionFunction, LoaderFunction, redirect } from 'react-router-dom'
|
2024-04-25 00:13:09 -07:00
|
|
|
import { FileLoaderData, HomeLoaderData, IndexLoaderData } from './types'
|
2024-08-09 02:47:25 -04:00
|
|
|
import { getProjectMetaByRouteId, PATHS } from './paths'
|
2024-08-16 07:15:42 -04:00
|
|
|
import { isDesktop } from './isDesktop'
|
2024-04-02 10:29:34 -04:00
|
|
|
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 makeUrlPathRelative from './makeUrlPathRelative'
|
2024-08-20 22:16:44 -04:00
|
|
|
import { codeManager } from 'lib/singletons'
|
2024-03-14 15:56:45 -04:00
|
|
|
import { fileSystemManager } from 'lang/std/fileSystemManager'
|
2024-10-03 13:02:57 -04:00
|
|
|
import { getProjectInfo } from './desktop'
|
2024-04-25 00:13:09 -07:00
|
|
|
import { createSettings } from './settings/initialSettings'
|
2024-10-17 23:42:24 -04:00
|
|
|
import { normalizeLineEndings } from 'lib/codeEditor'
|
2024-03-14 15:56:45 -04:00
|
|
|
|
|
|
|
// 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,
|
2024-04-25 05:52:08 -07:00
|
|
|
}): Promise<
|
|
|
|
ReturnType<typeof createSettings> | ReturnType<typeof redirect>
|
|
|
|
> => {
|
2024-04-25 11:55:11 -07:00
|
|
|
let { settings, configuration } = await loadAndValidateSettings()
|
2024-04-02 10:29:34 -04:00
|
|
|
|
|
|
|
// 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) {
|
2024-04-25 11:55:11 -07:00
|
|
|
const projectPathData = await getProjectMetaByRouteId(
|
|
|
|
params.id,
|
|
|
|
configuration
|
|
|
|
)
|
2024-04-02 10:29:34 -04:00
|
|
|
if (projectPathData) {
|
2024-08-22 13:38:53 -04:00
|
|
|
const { projectPath } = projectPathData
|
2024-05-22 14:22:07 -04:00
|
|
|
const { settings: s } = await loadAndValidateSettings(
|
2024-08-22 13:38:53 -04:00
|
|
|
projectPath || undefined
|
2024-05-22 14:22:07 -04:00
|
|
|
)
|
2024-08-16 07:15:42 -04:00
|
|
|
return s
|
2024-04-02 10:29:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) => {
|
2024-04-25 00:13:09 -07:00
|
|
|
const { settings } = await loadAndValidateSettings()
|
2024-04-02 10:29:34 -04:00
|
|
|
const onboardingStatus = settings.app.onboardingStatus.current || ''
|
|
|
|
const notEnRouteToOnboarding = !args.request.url.includes(
|
2024-08-09 02:47:25 -04:00
|
|
|
PATHS.ONBOARDING.INDEX
|
2024-04-02 10:29:34 -04:00
|
|
|
)
|
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(
|
2024-08-09 02:47:25 -04:00
|
|
|
makeUrlPathRelative(PATHS.ONBOARDING.INDEX) + onboardingStatus.slice(1)
|
2024-03-14 15:56:45 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-04-02 10:29:34 -04:00
|
|
|
return settingsLoader(args)
|
2024-03-14 15:56:45 -04:00
|
|
|
}
|
|
|
|
|
2024-08-16 07:15:42 -04:00
|
|
|
export const fileLoader: LoaderFunction = async (
|
|
|
|
routerData
|
|
|
|
): Promise<FileLoaderData | Response> => {
|
|
|
|
const { params } = routerData
|
2024-04-25 11:55:11 -07:00
|
|
|
let { configuration } = await loadAndValidateSettings()
|
2024-03-14 15:56:45 -04:00
|
|
|
|
2024-04-25 11:55:11 -07:00
|
|
|
const projectPathData = await getProjectMetaByRouteId(
|
|
|
|
params.id,
|
|
|
|
configuration
|
|
|
|
)
|
2024-04-02 10:29:34 -04:00
|
|
|
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) {
|
2024-08-22 13:38:53 -04:00
|
|
|
const { projectName, projectPath, currentFileName, currentFilePath } =
|
2024-04-02 10:29:34 -04:00
|
|
|
projectPathData
|
2024-03-14 15:56:45 -04:00
|
|
|
|
2024-08-16 07:15:42 -04:00
|
|
|
const urlObj = new URL(routerData.request.url)
|
|
|
|
let code = ''
|
|
|
|
|
|
|
|
if (!urlObj.pathname.endsWith('/settings')) {
|
2024-08-28 06:38:14 -04:00
|
|
|
const fallbackFile = isDesktop()
|
|
|
|
? (await getProjectInfo(projectPath)).default_file
|
|
|
|
: ''
|
|
|
|
let fileExists = isDesktop()
|
|
|
|
if (currentFilePath && fileExists) {
|
|
|
|
try {
|
|
|
|
await window.electron.stat(currentFilePath)
|
|
|
|
} catch (e) {
|
|
|
|
if (e === 'ENOENT') {
|
|
|
|
fileExists = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fileExists || !currentFileName || !currentFilePath || !projectName) {
|
2024-08-16 07:15:42 -04:00
|
|
|
return redirect(
|
|
|
|
`${PATHS.FILE}/${encodeURIComponent(
|
2024-08-28 06:38:14 -04:00
|
|
|
isDesktop() ? fallbackFile : params.id + '/' + PROJECT_ENTRYPOINT
|
2024-08-16 07:15:42 -04:00
|
|
|
)}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-10-18 10:43:01 -04:00
|
|
|
code = await window.electron.readFile(currentFilePath, {
|
|
|
|
encoding: 'utf-8',
|
|
|
|
})
|
2024-08-22 13:38:53 -04:00
|
|
|
code = normalizeLineEndings(code)
|
2024-08-16 07:15:42 -04:00
|
|
|
|
|
|
|
// Update both the state and the editor's code.
|
|
|
|
// We explicitly do not write to the file here since we are loading from
|
|
|
|
// the file system and not the editor.
|
2024-08-22 13:38:53 -04:00
|
|
|
codeManager.updateCurrentFilePath(currentFilePath)
|
2024-08-16 07:15:42 -04:00
|
|
|
codeManager.updateCodeStateEditor(code)
|
2024-04-02 10:29:34 -04:00
|
|
|
}
|
2024-03-14 15:56:45 -04:00
|
|
|
|
|
|
|
// Set the file system manager to the project path
|
|
|
|
// So that WASM gets an updated path for operations
|
2024-08-22 13:38:53 -04:00
|
|
|
fileSystemManager.dir = projectPath
|
2024-03-14 15:56:45 -04:00
|
|
|
|
2024-08-16 07:15:42 -04:00
|
|
|
const defaultProjectData = {
|
2024-08-22 13:38:53 -04:00
|
|
|
name: projectName || 'unnamed',
|
|
|
|
path: projectPath,
|
2024-08-16 07:15:42 -04:00
|
|
|
children: [],
|
|
|
|
kcl_file_count: 0,
|
|
|
|
directory_count: 0,
|
|
|
|
metadata: null,
|
2024-08-22 13:38:53 -04:00
|
|
|
default_file: projectPath,
|
2024-08-16 07:15:42 -04:00
|
|
|
}
|
|
|
|
|
2024-08-20 22:16:44 -04:00
|
|
|
const maybeProjectInfo = isDesktop()
|
2024-08-22 13:38:53 -04:00
|
|
|
? await getProjectInfo(projectPath)
|
2024-08-20 22:16:44 -04:00
|
|
|
: null
|
|
|
|
|
|
|
|
console.log('maybeProjectInfo', {
|
|
|
|
maybeProjectInfo,
|
|
|
|
defaultProjectData,
|
|
|
|
projectPathData,
|
|
|
|
})
|
|
|
|
|
2024-04-02 10:29:34 -04:00
|
|
|
const projectData: IndexLoaderData = {
|
2024-03-14 15:56:45 -04:00
|
|
|
code,
|
2024-08-20 22:16:44 -04:00
|
|
|
project: maybeProjectInfo ?? defaultProjectData,
|
2024-03-14 15:56:45 -04:00
|
|
|
file: {
|
2024-08-22 13:38:53 -04:00
|
|
|
name: currentFileName || '',
|
|
|
|
path: currentFilePath || '',
|
2024-04-25 00:13:09 -07:00
|
|
|
children: [],
|
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-04-25 00:13:09 -07:00
|
|
|
children: [],
|
2024-04-02 10:29:34 -04:00
|
|
|
},
|
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
|
|
|
|
> => {
|
2024-08-16 07:15:42 -04:00
|
|
|
if (!isDesktop()) {
|
2024-08-09 02:47:25 -04:00
|
|
|
return redirect(PATHS.FILE + '/%2F' + BROWSER_PROJECT_NAME)
|
2024-03-14 15:56:45 -04:00
|
|
|
}
|
2024-10-03 13:02:57 -04:00
|
|
|
return {}
|
2024-03-14 15:56:45 -04:00
|
|
|
}
|