Skip to content

Commit 8fe91c7

Browse files
Persist new workspace contextURL on org change (#17209)
1 parent 541f6f6 commit 8fe91c7

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

components/dashboard/src/menu/OrganizationSelector.tsx

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* See License.AGPL.txt in the project root for license information.
55
*/
66

7-
import { FunctionComponent } from "react";
7+
import { FunctionComponent, useCallback } from "react";
88
import ContextMenu, { ContextMenuEntry } from "../components/ContextMenu";
99
import { OrgIcon, OrgIconProps } from "../components/org-icon/OrgIcon";
1010
import { useCurrentUser } from "../user-context";
@@ -13,6 +13,7 @@ import { useUserBillingMode } from "../data/billing-mode/user-billing-mode-query
1313
import { useFeatureFlags } from "../contexts/FeatureFlagContext";
1414
import { useCurrentOrg, useOrganizations } from "../data/organizations/orgs-query";
1515
import { useOrgBillingMode } from "../data/billing-mode/org-billing-mode-query";
16+
import { useLocation } from "react-router";
1617

1718
export interface OrganizationSelectorProps {}
1819

@@ -23,6 +24,7 @@ export default function OrganizationSelector(p: OrganizationSelectorProps) {
2324
const { data: userBillingMode } = useUserBillingMode();
2425
const { data: orgBillingMode } = useOrgBillingMode();
2526
const { showUsageView } = useFeatureFlags();
27+
const getOrgURL = useGetOrgURL();
2628

2729
const userFullName = user?.fullName || user?.name || "...";
2830

@@ -117,7 +119,7 @@ export default function OrganizationSelector(p: OrganizationSelectorProps) {
117119
// marking as active for styles
118120
active: true,
119121
separator: true,
120-
link: `/?org=${org.id}`,
122+
link: getOrgURL(org.id),
121123
}));
122124

123125
const userMigrated = user?.additionalData?.isMigratedToTeamOnlyAttribution ?? false;
@@ -137,7 +139,7 @@ export default function OrganizationSelector(p: OrganizationSelectorProps) {
137139
// marking as active for styles
138140
active: true,
139141
separator: true,
140-
link: `/?org=0`,
142+
link: getOrgURL("0"),
141143
},
142144
]
143145
: []),
@@ -264,3 +266,26 @@ const CurrentOrgEntry: FunctionComponent<CurrentOrgEntryProps> = ({ title, subti
264266
</div>
265267
);
266268
};
269+
270+
// Determine url to use when switching orgs
271+
// Maintains the current location & context url (hash) when on the new workspace page
272+
const useGetOrgURL = () => {
273+
const location = useLocation();
274+
275+
return useCallback(
276+
(orgID: string) => {
277+
// Default to root path when switching orgs
278+
let path = "/";
279+
let hash = "";
280+
281+
// If we're on the new workspace page, try to maintain the location and context url
282+
if (/^\/new(\/$)?$/.test(location.pathname)) {
283+
path = `/new`;
284+
hash = location.hash;
285+
}
286+
287+
return `${path}?org=${encodeURIComponent(orgID)}${hash}`;
288+
},
289+
[location.hash, location.pathname],
290+
);
291+
};

components/dashboard/src/workspaces/CreateWorkspacePage.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ export function CreateWorkspacePage() {
9696
}
9797
}, [project, props.workspaceClass]);
9898

99+
// In addition to updating state, we want to update the url hash as well
100+
// This allows the contextURL to persist if user changes orgs, or copies/shares url
101+
const handleContextURLChange = useCallback(
102+
(newContextURL: string) => {
103+
setContextURL(newContextURL);
104+
history.replace(`#${newContextURL}`);
105+
},
106+
[history],
107+
);
108+
99109
const onSelectEditorChange = useCallback(
100110
(ide: string, useLatest: boolean) => {
101111
setSelectedIde(ide);
@@ -219,7 +229,7 @@ export function CreateWorkspacePage() {
219229
</div>
220230
<div className="-mx-6 px-6 mt-6 w-full">
221231
<div className="pt-3">
222-
<RepositoryFinder setSelection={setContextURL} initialValue={contextURL} />
232+
<RepositoryFinder setSelection={handleContextURLChange} initialValue={contextURL} />
223233
<ErrorMessage
224234
error={
225235
(createWorkspaceMutation.error as StartWorkspaceError) ||

0 commit comments

Comments
 (0)