4
4
* See License.AGPL.txt in the project root for license information.
5
5
*/
6
6
7
- import { FunctionComponent } from "react" ;
7
+ import { FunctionComponent , useCallback } from "react" ;
8
8
import ContextMenu , { ContextMenuEntry } from "../components/ContextMenu" ;
9
9
import { OrgIcon , OrgIconProps } from "../components/org-icon/OrgIcon" ;
10
10
import { useCurrentUser } from "../user-context" ;
@@ -13,6 +13,7 @@ import { useUserBillingMode } from "../data/billing-mode/user-billing-mode-query
13
13
import { useFeatureFlags } from "../contexts/FeatureFlagContext" ;
14
14
import { useCurrentOrg , useOrganizations } from "../data/organizations/orgs-query" ;
15
15
import { useOrgBillingMode } from "../data/billing-mode/org-billing-mode-query" ;
16
+ import { useLocation } from "react-router" ;
16
17
17
18
export interface OrganizationSelectorProps { }
18
19
@@ -23,6 +24,7 @@ export default function OrganizationSelector(p: OrganizationSelectorProps) {
23
24
const { data : userBillingMode } = useUserBillingMode ( ) ;
24
25
const { data : orgBillingMode } = useOrgBillingMode ( ) ;
25
26
const { showUsageView } = useFeatureFlags ( ) ;
27
+ const getOrgURL = useGetOrgURL ( ) ;
26
28
27
29
const userFullName = user ?. fullName || user ?. name || "..." ;
28
30
@@ -117,7 +119,7 @@ export default function OrganizationSelector(p: OrganizationSelectorProps) {
117
119
// marking as active for styles
118
120
active : true ,
119
121
separator : true ,
120
- link : `/? org= ${ org . id } ` ,
122
+ link : getOrgURL ( org . id ) ,
121
123
} ) ) ;
122
124
123
125
const userMigrated = user ?. additionalData ?. isMigratedToTeamOnlyAttribution ?? false ;
@@ -137,7 +139,7 @@ export default function OrganizationSelector(p: OrganizationSelectorProps) {
137
139
// marking as active for styles
138
140
active : true ,
139
141
separator : true ,
140
- link : `/?org=0` ,
142
+ link : getOrgURL ( "0" ) ,
141
143
} ,
142
144
]
143
145
: [ ] ) ,
@@ -264,3 +266,26 @@ const CurrentOrgEntry: FunctionComponent<CurrentOrgEntryProps> = ({ title, subti
264
266
</ div >
265
267
) ;
266
268
} ;
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 ( / ^ \/ n e w ( \/ $ ) ? $ / . 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
+ } ;
0 commit comments