Skip to content

Commit

Permalink
Fix creating local projects (#10371)
Browse files Browse the repository at this point in the history
- Fix enso-org/cloud-v2#1331

# Important Notes
None
  • Loading branch information
somebody1234 authored Jun 25, 2024
1 parent f6c79e8 commit 2638ba8
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,10 @@ export default function ProjectIcon(props: ProjectIconProps) {
[user, setItem]
)
const [spinnerState, setSpinnerState] = React.useState(spinner.SpinnerState.initial)
const [shouldOpenWhenReady, setShouldOpenWhenReady] = React.useState(false)
const shouldOpenWhenReadyRef = React.useRef(false)
const [isRunningInBackground, setIsRunningInBackground] = React.useState(
item.projectState.executeAsync ?? false
)
const [shouldSwitchPage, setShouldSwitchPage] = React.useState(false)
const doAbortOpeningRef = React.useRef(() => {})
const doOpenEditorRef = React.useRef(doOpenEditor)
doOpenEditorRef.current = doOpenEditor
Expand All @@ -131,36 +130,38 @@ export default function ProjectIcon(props: ProjectIconProps) {
const getProjectDetailsMutate = getProjectDetailsMutation.mutateAsync

const openEditorMutation = reactQuery.useMutation({
mutationKey: ['openEditor', item.id],
mutationKey: ['openEditor'],
networkMode: 'always',
mutationFn: async () => {
mutationFn: async (item2: backendModule.ProjectAsset) => {
const abortController = new AbortController()
doAbortOpeningRef.current = () => {
abortController.abort()
}
const projectPromise = openProjectMutate([
item.id,
{ executeAsync: false, parentId: item.parentId, cognitoCredentials: session },
item.title,
]).then(() =>
waitUntilProjectIsReadyMutation.mutateAsync([
item.id,
item.parentId,
item.title,
abortController.signal,
const projectPromise = openProjectMutation
.mutateAsync([
item2.id,
{ executeAsync: false, parentId: item2.parentId, cognitoCredentials: session },
item2.title,
])
)
.then(() =>
waitUntilProjectIsReadyMutation.mutateAsync([
item2.id,
item2.parentId,
item2.title,
abortController.signal,
])
)
setProjectStartupInfo({
project: projectPromise,
projectAsset: item,
projectAsset: item2,
setProjectAsset: setItem,
backendType: backend.type,
accessToken: session?.accessToken ?? null,
})
await projectPromise
if (!abortController.signal.aborted) {
setState(backendModule.ProjectState.opened)
if (shouldOpenWhenReady) {
if (shouldOpenWhenReadyRef.current) {
doOpenEditor()
}
}
Expand All @@ -174,7 +175,7 @@ export default function ProjectIcon(props: ProjectIconProps) {
try {
if (!shouldRunInBackground) {
setState(backendModule.ProjectState.openInProgress)
openEditorMutate()
openEditorMutate(item)
} else {
setState(backendModule.ProjectState.opened)
await openProjectMutate([
Expand Down Expand Up @@ -225,14 +226,17 @@ export default function ProjectIcon(props: ProjectIconProps) {
case AssetEventType.openProject: {
if (event.id !== item.id) {
if (!event.runInBackground && !isRunningInBackground) {
setShouldOpenWhenReady(false)
shouldOpenWhenReadyRef.current = false
if (!isOtherUserUsingProject && backendModule.IS_OPENING_OR_OPENED[state]) {
doAbortOpeningRef.current()
void closeProject()
}
}
} else {
if (backendModule.IS_OPENING_OR_OPENED[state]) {
if (
backendModule.IS_OPENING_OR_OPENED[state] &&
state !== backendModule.ProjectState.placeholder
) {
const projectPromise = waitUntilProjectIsReadyMutation.mutateAsync([
item.id,
item.parentId,
Expand All @@ -245,12 +249,11 @@ export default function ProjectIcon(props: ProjectIconProps) {
backendType: backend.type,
accessToken: session?.accessToken ?? null,
})
if (!isRunningInBackground && event.shouldAutomaticallySwitchPage) {
if (!isRunningInBackground) {
doOpenEditor()
}
} else {
setShouldOpenWhenReady(!event.runInBackground)
setShouldSwitchPage(event.shouldAutomaticallySwitchPage)
shouldOpenWhenReadyRef.current = !event.runInBackground
setIsRunningInBackground(event.runInBackground)
void openProject(event.runInBackground)
}
Expand All @@ -259,7 +262,7 @@ export default function ProjectIcon(props: ProjectIconProps) {
}
case AssetEventType.closeProject: {
if (event.id === item.id) {
setShouldOpenWhenReady(false)
shouldOpenWhenReadyRef.current = false
void closeProject()
}
break
Expand All @@ -273,20 +276,11 @@ export default function ProjectIcon(props: ProjectIconProps) {
}
})

React.useEffect(() => {
if (state === backendModule.ProjectState.opened) {
if (shouldOpenWhenReady) {
doOpenEditorRef.current()
setShouldOpenWhenReady(false)
}
}
}, [shouldOpenWhenReady, shouldSwitchPage, state])

const closeProject = async () => {
if (!isRunningInBackground) {
doCloseEditor(item.id)
}
setShouldOpenWhenReady(false)
shouldOpenWhenReadyRef.current = false
setState(backendModule.ProjectState.closing)
await closeProjectMutation.mutateAsync([item.id, item.title])
setState(backendModule.ProjectState.closed)
Expand All @@ -310,7 +304,6 @@ export default function ProjectIcon(props: ProjectIconProps) {
dispatchAssetEvent({
type: AssetEventType.openProject,
id: item.id,
shouldAutomaticallySwitchPage: true,
runInBackground: false,
})
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ export default function ProjectNameColumn(props: ProjectNameColumnProps) {
dispatchAssetEvent({
type: AssetEventType.openProject,
id: createdProject.projectId,
shouldAutomaticallySwitchPage: true,
runInBackground: false,
})
} catch (error) {
Expand Down Expand Up @@ -302,7 +301,6 @@ export default function ProjectNameColumn(props: ProjectNameColumnProps) {
dispatchAssetEvent({
type: AssetEventType.openProject,
id: asset.id,
shouldAutomaticallySwitchPage: true,
runInBackground: false,
})
}
Expand Down
1 change: 0 additions & 1 deletion app/ide-desktop/lib/dashboard/src/events/assetEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ export interface AssetNewSecretEvent extends AssetBaseEvent<AssetEventType.newSe
/** A signal to open the specified project. */
export interface AssetOpenProjectEvent extends AssetBaseEvent<AssetEventType.openProject> {
readonly id: backend.ProjectId
readonly shouldAutomaticallySwitchPage: boolean
readonly runInBackground: boolean
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ export default function AssetContextMenu(props: AssetContextMenuProps) {
dispatchAssetEvent({
type: AssetEventType.openProject,
id: asset.id,
shouldAutomaticallySwitchPage: true,
runInBackground: false,
})
}}
Expand All @@ -174,7 +173,6 @@ export default function AssetContextMenu(props: AssetContextMenuProps) {
dispatchAssetEvent({
type: AssetEventType.openProject,
id: asset.id,
shouldAutomaticallySwitchPage: false,
runInBackground: true,
})
}}
Expand Down
3 changes: 0 additions & 3 deletions app/ide-desktop/lib/dashboard/src/layouts/AssetsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,6 @@ export default function AssetsTable(props: AssetsTableProps) {
dispatchAssetEvent({
type: AssetEventType.openProject,
id: projectToLoad.id,
shouldAutomaticallySwitchPage: true,
runInBackground: false,
})
})
Expand Down Expand Up @@ -964,7 +963,6 @@ export default function AssetsTable(props: AssetsTableProps) {
dispatchAssetEvent({
type: AssetEventType.openProject,
id: projectToLoad.id,
shouldAutomaticallySwitchPage: true,
runInBackground: false,
})
})
Expand Down Expand Up @@ -1219,7 +1217,6 @@ export default function AssetsTable(props: AssetsTableProps) {
type: AssetEventType.openProject,
id: item.item.id,
runInBackground: false,
shouldAutomaticallySwitchPage: true,
})
break
}
Expand Down
32 changes: 15 additions & 17 deletions app/ide-desktop/lib/dashboard/src/services/LocalBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,23 +315,21 @@ export default class LocalBackend extends Backend {
title: string | null
): Promise<void> {
const { id } = extractTypeAndId(projectId)
if (!this.projectManager.projects.has(id)) {
try {
await this.projectManager.openProject({
projectId: id,
missingComponentAction: projectManager.MissingComponentAction.install,
...(body?.parentId != null
? { projectsDirectory: extractTypeAndId(body.parentId).id }
: {}),
})
return
} catch (error) {
throw new Error(
`Could not open project ${title != null ? `'${title}'` : `with ID '${projectId}'`}: ${
errorModule.tryGetMessage(error) ?? 'unknown error'
}.`
)
}
try {
await this.projectManager.openProject({
projectId: id,
missingComponentAction: projectManager.MissingComponentAction.install,
...(body?.parentId != null
? { projectsDirectory: extractTypeAndId(body.parentId).id }
: {}),
})
return
} catch (error) {
throw new Error(
`Could not open project ${title != null ? `'${title}'` : `with ID '${projectId}'`}: ${
errorModule.tryGetMessage(error) ?? 'unknown error'
}.`
)
}
}

Expand Down

0 comments on commit 2638ba8

Please sign in to comment.