1- import { OrganizationDetailsView } from '@raystack/frontier/admin' ;
1+ import { OrganizationDetailsView , useAdminPaths } from '@raystack/frontier/admin' ;
22import { useCallback , useContext , useEffect , useState } from 'react' ;
3- import { useLocation , useNavigate , useParams , Outlet } from 'react-router-dom' ;
3+ import { useLocation , useNavigate , useParams , Outlet , Navigate } from 'react-router-dom' ;
4+ import { useQuery } from '@connectrpc/connect-query' ;
5+ import { FrontierServiceQueries } from '@raystack/proton/frontier' ;
46import { AppContext } from '~/contexts/App' ;
57import { clients } from '~/connect/clients' ;
68import { exportCsvFromStream } from '~/utils/helper' ;
9+ import LoadingState from '~/components/states/Loading' ;
710
811const adminClient = clients . admin ( { useBinary : true } ) ;
912
13+ /* Old bookmarks use the id; new URLs use the slug — tell them apart. */
14+ const UUID_RE =
15+ / ^ [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 12 } $ / i;
16+
1017async function loadCountries ( ) : Promise < string [ ] > {
1118 const data = await import ( "~/assets/data/countries.json" ) ;
1219 return ( data . default as { name : string } [ ] ) . map ( ( c ) => c . name ) ;
1320}
1421
1522export default function OrganizationDetailsPage ( ) {
16- const { organizationId } = useParams < { organizationId : string } > ( ) ;
23+ /*
24+ * View runs by org id:
25+ * - in-app nav: id via router state (fast path)
26+ * - cold load: resolve the URL id/slug via getOrganization (accepts either)
27+ * - old UUID URL: rewrite to the canonical slug once resolved
28+ * - unresolvable URL: redirect to list
29+ */
30+ const { organizationId : urlParam } = useParams < { organizationId : string } > ( ) ;
1731 const location = useLocation ( ) ;
1832 const navigate = useNavigate ( ) ;
33+ const paths = useAdminPaths ( ) ;
1934 const { config } = useContext ( AppContext ) ;
2035 const [ countries , setCountries ] = useState < string [ ] > ( [ ] ) ;
2136
37+ const incomingOrgId = ( location . state as { orgId ?: string } | null ) ?. orgId ;
38+
39+ /*
40+ * Hold the id keyed to its URL, recomputed during render:
41+ * - a urlParam change invalidates a stale id, re-arming the resolve
42+ * - setState-during-render keeps children from seeing the old id (no flash,
43+ * no stale org under a new URL)
44+ */
45+ const [ held , setHeld ] = useState < { param ?: string ; orgId ?: string } > ( {
46+ param : urlParam ,
47+ orgId : incomingOrgId ,
48+ } ) ;
49+ if ( held . param !== urlParam || ( incomingOrgId && incomingOrgId !== held . orgId ) ) {
50+ setHeld ( { param : urlParam , orgId : incomingOrgId } ) ;
51+ }
52+ const stateOrgId = held . param === urlParam ? held . orgId : undefined ;
53+
54+ /*
55+ * Cold-load resolve (only when state carries no id):
56+ * - getOrganization takes an id OR a slug and returns disabled orgs too,
57+ * so a single call covers every URL form (server GetRaw branches on UUID)
58+ * - a UUID param is already the id, but we still resolve to read the slug +
59+ * state for the canonical-URL rewrite below
60+ */
61+ const paramIsId = ! ! urlParam && UUID_RE . test ( urlParam ) ;
62+ const needsResolve = ! stateOrgId && ! ! urlParam ;
63+ const {
64+ data : org ,
65+ error : resolveError ,
66+ isSuccess,
67+ } = useQuery (
68+ FrontierServiceQueries . getOrganization ,
69+ { id : urlParam ?? '' } ,
70+ {
71+ enabled : needsResolve ,
72+ staleTime : 5 * 60 * 1000 ,
73+ select : ( d ) => d ?. organization ,
74+ } ,
75+ ) ;
76+
77+ const orgId = stateOrgId || ( paramIsId ? urlParam : org ?. id ) ;
78+ const notFound = needsResolve && isSuccess && ! org ?. id ;
79+
80+ /*
81+ * Old UUID bookmark → canonical slug URL:
82+ * - one live URL per org; replace keeps the back-button sane
83+ * - preserve the sub-tab (rest of the path) across the swap
84+ * - skip disabled orgs — keep them on the stable id URL
85+ */
86+ useEffect ( ( ) => {
87+ if ( paramIsId && org ?. name && org . state !== 'disabled' ) {
88+ const rest = location . pathname . slice (
89+ location . pathname . indexOf ( urlParam ! ) + urlParam ! . length ,
90+ ) ;
91+ navigate ( `/${ paths . organizations } /${ org . name } ${ rest } ` , {
92+ replace : true ,
93+ state : { orgId } ,
94+ } ) ;
95+ }
96+ } , [
97+ paramIsId ,
98+ org ?. name ,
99+ org ?. state ,
100+ orgId ,
101+ urlParam ,
102+ location . pathname ,
103+ navigate ,
104+ paths . organizations ,
105+ ] ) ;
106+
107+ /* Carry the org id in router state so breadcrumb/tab navs skip the resolve. */
108+ const onNavigate = useCallback (
109+ ( path : string , state ?: { orgId ?: string } ) =>
110+ navigate ( path , state ? { state } : undefined ) ,
111+ [ navigate ] ,
112+ ) ;
113+
22114 useEffect ( ( ) => {
23115 loadCountries ( ) . then ( setCountries ) ;
24116 } , [ ] ) ;
25117
26118 const onExportMembers = useCallback ( async ( ) => {
27- if ( ! organizationId ) return ;
119+ if ( ! orgId ) return ;
28120 await exportCsvFromStream (
29121 adminClient . exportOrganizationUsers ,
30- { id : organizationId } ,
122+ { id : orgId } ,
31123 "organization-members.csv" ,
32124 ) ;
33- } , [ organizationId ] ) ;
125+ } , [ orgId ] ) ;
34126
35127 const onExportProjects = useCallback ( async ( ) => {
36- if ( ! organizationId ) return ;
128+ if ( ! orgId ) return ;
37129 await exportCsvFromStream (
38130 adminClient . exportOrganizationProjects ,
39- { id : organizationId } ,
131+ { id : orgId } ,
40132 "organization-projects.csv" ,
41133 ) ;
42- } , [ organizationId ] ) ;
134+ } , [ orgId ] ) ;
43135
44136 const onExportTokens = useCallback ( async ( ) => {
45- if ( ! organizationId ) return ;
137+ if ( ! orgId ) return ;
46138 await exportCsvFromStream (
47139 adminClient . exportOrganizationTokens ,
48- { id : organizationId } ,
140+ { id : orgId } ,
49141 "organization-tokens.csv" ,
50142 ) ;
51- } , [ organizationId ] ) ;
143+ } , [ orgId ] ) ;
144+
145+ /* No id, error, or empty result (also guards the infinite loader) → list. */
146+ if ( ! urlParam || resolveError || notFound ) {
147+ return < Navigate to = { `/${ paths . organizations } ` } replace /> ;
148+ }
149+
150+ /* Still resolving — loader, not a redirect flash. */
151+ if ( ! orgId ) {
152+ return < LoadingState /> ;
153+ }
52154
53155 return (
54156 < OrganizationDetailsView
55- organizationId = { organizationId }
157+ organizationId = { orgId }
56158 appUrl = { config ?. app_url }
57159 tokenProductId = { config ?. token_product_id }
58160 countries = { countries }
@@ -61,7 +163,7 @@ export default function OrganizationDetailsPage() {
61163 onExportProjects = { onExportProjects }
62164 onExportTokens = { onExportTokens }
63165 currentPath = { location . pathname }
64- onNavigate = { navigate }
166+ onNavigate = { onNavigate }
65167 >
66168 < Outlet />
67169 </ OrganizationDetailsView >
0 commit comments