Skip to content

Commit a792751

Browse files
authored
feat(admin): show org slug in detail URLs instead of UUID (#1763)
1 parent 68774c3 commit a792751

15 files changed

Lines changed: 232 additions & 47 deletions

File tree

‎web/apps/admin/src/pages/admins/AdminsPage.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ export default function AdminsPage() {
88

99
return (
1010
<AdminsView
11-
onNavigateToOrg={(orgId: string) => navigate(`/${paths.organizations}/${orgId}`)}
11+
onNavigateToOrg={(slug: string, orgId: string) =>
12+
navigate(`/${paths.organizations}/${slug}`, { state: { orgId } })
13+
}
1214
icon={<AdminsIcon />}
1315
/>
1416
);

‎web/apps/admin/src/pages/audit-logs/AuditLogsPage.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ export default function AuditLogsPage() {
1616
"audit-logs.csv",
1717
);
1818
}, []);
19-
const onNavigate = useCallback((path: string) => navigate(path), [navigate]);
19+
const onNavigate = useCallback(
20+
// Forward router state (e.g. the org id) to the destination page.
21+
(path: string, state?: { orgId?: string }) =>
22+
navigate(path, state ? { state } : undefined),
23+
[navigate],
24+
);
2025

2126
return (
2227
<AuditLogsView onExportCsv={onExportCsv} onNavigate={onNavigate} />

‎web/apps/admin/src/pages/organizations/details/index.tsx‎

Lines changed: 116 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,160 @@
1-
import { OrganizationDetailsView } from '@raystack/frontier/admin';
1+
import { OrganizationDetailsView, useAdminPaths } from '@raystack/frontier/admin';
22
import { 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';
46
import { AppContext } from '~/contexts/App';
57
import { clients } from '~/connect/clients';
68
import { exportCsvFromStream } from '~/utils/helper';
9+
import LoadingState from '~/components/states/Loading';
710

811
const 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-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
16+
1017
async 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

1522
export 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>

‎web/apps/admin/src/pages/organizations/list/index.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ export default function OrganizationListPage() {
2626
}, []);
2727

2828
const onNavigateToOrg = useCallback(
29-
(id: string) => navigate(`/${paths.organizations}/${id}`),
29+
// Slug in the URL, id in router state: id is the stable key; the slug is display-only.
30+
(slug: string, orgId: string) =>
31+
navigate(`/${paths.organizations}/${slug}`, { state: { orgId } }),
3032
[navigate, paths.organizations],
3133
);
3234

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { useQuery } from "@connectrpc/connect-query";
2+
import { FrontierServiceQueries } from "@raystack/proton/frontier";
3+
4+
/**
5+
* Fetches an organization by id — for display (its title) and to build a slug
6+
* link. Call it with the id: a disabled org's slug no longer resolves
7+
* server-side, so the id is the reliable key. react-query dedupes/caches repeat
8+
* lookups of the same id across callers.
9+
*
10+
* Pass `undefined`/empty to disable the query (e.g. when there's no org yet).
11+
*/
12+
export const useOrganizationLookup = (orgId?: string) =>
13+
useQuery(
14+
FrontierServiceQueries.getOrganization,
15+
{ id: orgId || "" },
16+
{
17+
enabled: !!orgId,
18+
staleTime: 5 * 60 * 1000,
19+
select: (data) => data?.organization,
20+
},
21+
);

‎web/sdk/admin/views/admins/columns.tsx‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
11
import { Button, type DataTableColumnDef } from "@raystack/apsara";
22
import type { ServiceUser, User } from "@raystack/proton/frontier";
33
import { TerminologyEntity } from "../../hooks/useTerminology";
4+
import { useOrganizationLookup } from "../../hooks/useOrganizationLookup";
45
import styles from "./admins.module.css";
56

7+
// The cell only has the org id — fetch the org to show its title and slug link.
8+
const OrgCell = ({
9+
orgId,
10+
onNavigateToOrg,
11+
}: {
12+
orgId: string;
13+
onNavigateToOrg?: (slug: string, orgId: string) => void;
14+
}) => {
15+
const { data: org } = useOrganizationLookup(orgId);
16+
17+
return (
18+
<Button
19+
variant="text"
20+
disabled={!org}
21+
onClick={() => org && onNavigateToOrg?.(org.name || orgId, orgId)}
22+
data-test-id="frontier-admin-navigate-to-org-btn"
23+
>
24+
{org?.title || org?.name || orgId}
25+
</Button>
26+
);
27+
};
28+
629
export const getColumns: (options?: {
7-
onNavigateToOrg?: (orgId: string) => void;
30+
onNavigateToOrg?: (slug: string, orgId: string) => void;
831
t?: {
932
organization: TerminologyEntity;
1033
};
@@ -52,13 +75,7 @@ export const getColumns: (options?: {
5275
cell: (info) => {
5376
const org_id = info.getValue() as string;
5477
return org_id ? (
55-
<Button
56-
variant="text"
57-
onClick={() => onNavigateToOrg?.(org_id)}
58-
data-test-id="frontier-admin-navigate-to-org-btn"
59-
>
60-
{org_id}
61-
</Button>
78+
<OrgCell orgId={org_id} onNavigateToOrg={onNavigateToOrg} />
6279
) : (
6380
"-"
6481
);

‎web/sdk/admin/views/admins/index.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const NoAdmins = () => {
2424
};
2525

2626
export type AdminsViewProps = {
27-
onNavigateToOrg?: (orgId: string) => void;
27+
onNavigateToOrg?: (slug: string, orgId: string) => void;
2828
/** Icon rendered in the page header next to the title. */
2929
icon?: ReactNode;
3030
};

‎web/sdk/admin/views/audit-logs/index.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ export type AuditLogsViewProps = {
6666
appName?: string;
6767
/** Callback to export audit logs as CSV with the current query filters applied. */
6868
onExportCsv?: (query: RQLRequest) => Promise<void>;
69-
/** Navigation callback for links within audit log entries (e.g. to org/user pages). */
70-
onNavigate?: (path: string) => void;
69+
/** Navigate to a link in an audit entry (e.g. org/user page). `state` carries the org id. */
70+
onNavigate?: (path: string, state?: { orgId?: string }) => void;
7171
};
7272

7373
export default function AuditLogsView({ appName, onExportCsv, onNavigate }: AuditLogsViewProps = {}) {

‎web/sdk/admin/views/audit-logs/sidepanel-details.tsx‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ import { isZeroUUID } from "../../utils/helper";
1818
import SidepanelListId from "./sidepanel-list-id";
1919
import { useTerminology } from "../../hooks/useTerminology";
2020
import { useAdminPaths } from "../../hooks/useAdminPaths";
21+
import { useOrganizationLookup } from "../../hooks/useOrganizationLookup";
2122

2223
type SidePanelDetailsProps = Partial<AuditRecord> & {
2324
onClose: () => void;
24-
onNavigate?: (path: string) => void;
25+
onNavigate?: (path: string, state?: { orgId?: string }) => void;
2526
};
2627

2728
type AuditSessionContext = {
@@ -47,6 +48,11 @@ export default function SidePanelDetails({
4748
const location =
4849
(session && `${session.Location.City}, ${session.Location.Country}`) || "-";
4950

51+
// The record only has the org title, not the slug — look the org up (by id)
52+
// to get its `name` for the link; fall back to the id while loading.
53+
const isOrgLink = !!orgId && !isZeroUUID(orgId);
54+
const { data: org } = useOrganizationLookup(isOrgLink ? orgId : undefined);
55+
5056
return (
5157
<SidePanel
5258
data-test-id="admin-user-details-sidepanel"
@@ -76,8 +82,9 @@ export default function SidePanelDetails({
7682
<ActorCell value={actor!} size="small" maxLength={12} />
7783
</SidepanelListItemLink>
7884
<SidepanelListItemLink
79-
isLink={!!orgId && !isZeroUUID(orgId)}
80-
href={`/${paths.organizations}/${orgId}`}
85+
isLink={isOrgLink && !!org}
86+
href={`/${paths.organizations}/${org?.name || orgId}`}
87+
state={orgId ? { orgId } : undefined}
8188
label={t.organization({ case: "capital" })}
8289
onNavigate={onNavigate}
8390
data-test-id="actor-link">

‎web/sdk/admin/views/audit-logs/sidepanel-list-link.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ type SidepanelListItemLinkProps = {
77
children: ReactNode;
88
href: string;
99
label: string;
10-
onNavigate?: (path: string) => void;
10+
onNavigate?: (path: string, state?: { orgId?: string }) => void;
11+
/** Router state to carry along (e.g. the org id). */
12+
state?: { orgId?: string };
1113
"data-test-id"?: string;
1214
};
1315

@@ -17,6 +19,7 @@ export default function SidepanelListItemLink({
1719
href,
1820
label,
1921
onNavigate,
22+
state,
2023
"data-test-id": dataTestId,
2124
}: SidepanelListItemLinkProps) {
2225
if (isLink && onNavigate) {
@@ -29,7 +32,7 @@ export default function SidepanelListItemLink({
2932
color="neutral"
3033
data-test-id={dataTestId}
3134
className={styles["sidepanel-link-trigger"]}
32-
onClick={() => onNavigate(href)}>
35+
onClick={() => onNavigate(href, state)}>
3336
{children}
3437
</Button>
3538
</List.Value>

0 commit comments

Comments
 (0)