Skip to content

Commit 6987960

Browse files
feat(desktop): remember current project
1 parent fdbb03c commit 6987960

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

desktop/src/contexts/DevPodContext/Pro/ProProvider.tsx

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Text } from "@chakra-ui/react"
77
import { ManagementV1Project } from "@loft-enterprise/client/gen/models/managementV1Project"
88
import { useQuery } from "@tanstack/react-query"
99
import { ReactNode, useEffect, useMemo, useState } from "react"
10-
import { Navigate, useNavigate } from "react-router-dom"
10+
import { Navigate, useNavigate, useSearchParams } from "react-router-dom"
1111
import { useProInstances } from "../proInstances"
1212
import { ProWorkspaceStore, useWorkspaceStore } from "../workspaceStore"
1313
import { ContextSwitcher } from "./ContextSwitcher"
@@ -19,6 +19,7 @@ export function ProProvider({ host, children }: { host: string; children: ReactN
1919
const [isLoadingWorkspaces, setIsLoadingWorkspaces] = useState(false)
2020
const [ownerFilter, setOwnerFilter] = useState<TWorkspaceOwnerFilterState>("self")
2121
const navigate = useNavigate()
22+
const [searchParams, setSearchParams] = useSearchParams()
2223
const currentProInstance = useMemo(() => {
2324
return proInstances?.find((instance) => instance.host == host)
2425
}, [host, proInstances])
@@ -30,7 +31,6 @@ export function ProProvider({ host, children }: { host: string; children: ReactN
3031

3132
return globalClient.getProClient(currentProInstance)
3233
}, [currentProInstance])
33-
const [selectedProject, setSelectedProject] = useState<ManagementV1Project | null>(null)
3434
const managementSelfQuery = useQuery({
3535
queryKey: ["managementSelf", client],
3636
queryFn: async () => {
@@ -47,12 +47,23 @@ export function ProProvider({ host, children }: { host: string; children: ReactN
4747
})
4848

4949
const currentProject = useMemo<ManagementV1Project | undefined>(() => {
50-
if (selectedProject) {
51-
return selectedProject
50+
if (projectsQuery.data == null) {
51+
return undefined
5252
}
5353

54-
return projectsQuery.data?.[0]
55-
}, [projectsQuery, selectedProject])
54+
const projectName = searchParams.get("project") ?? undefined
55+
if (!projectName) {
56+
return projectsQuery.data[0] ?? undefined
57+
}
58+
59+
const maybeProject =
60+
projectsQuery.data.find((project) => project.metadata?.name === projectName) ?? undefined
61+
if (!maybeProject) {
62+
return projectsQuery.data[0] ?? undefined
63+
}
64+
65+
return maybeProject
66+
}, [searchParams, projectsQuery.data])
5667

5768
const [cancelWatch, setCancelWatch] = useState<
5869
{ fn: () => Promise<Result<undefined>> } | undefined
@@ -119,15 +130,21 @@ export function ProProvider({ host, children }: { host: string; children: ReactN
119130
return toCancel().finally(() => setWaitingForCancel(false))
120131
}
121132
setCancelWatch({ fn: canceler })
133+
122134
return () => {
123135
canceler()
124136
}
125137
}
126138
}, [client, store, currentProject, ownerFilter])
127139

128140
const handleProjectChanged = (newProject: ManagementV1Project) => {
129-
setSelectedProject(newProject)
130-
navigate(Routes.toProInstance(host))
141+
setSearchParams((prev) => {
142+
prev.set("project", newProject.metadata?.name ?? "")
143+
144+
return prev
145+
})
146+
147+
navigate(Routes.toProInstance(host) + "?" + searchParams.toString())
131148
}
132149

133150
const handleHostChanged = (newHost: string) => {
@@ -137,7 +154,11 @@ export function ProProvider({ host, children }: { host: string; children: ReactN
137154
return
138155
}
139156

140-
setSelectedProject(null) // reset selected project
157+
setSearchParams((prev) => {
158+
prev.delete("project")
159+
160+
return prev
161+
})
141162
navigate(Routes.toProInstance(newHost))
142163
}
143164

desktop/src/main.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const l = localStorage.getItem("devpod-location-current") // check usePreserveLo
3434
if (l) {
3535
const loc = JSON.parse(l) as Location
3636
if (window.location.pathname !== loc.pathname) {
37-
window.location.pathname = loc.pathname
37+
window.location.href = loc.pathname + loc.search
3838
render = false
3939
}
4040
}

0 commit comments

Comments
 (0)