Use React Router v6 for the routable tabs of the client page (#4132)

This commit is contained in:
Jon Koops 2023-01-06 13:05:12 +01:00 committed by GitHub
parent 3e82a57383
commit bf18af49e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -20,7 +20,6 @@ import {
useWatch, useWatch,
} from "react-hook-form-v7"; } from "react-hook-form-v7";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { useHistory } from "react-router-dom";
import { useNavigate } from "react-router-dom-v5-compat"; import { useNavigate } from "react-router-dom-v5-compat";
import { useAlerts } from "../components/alert/Alerts"; import { useAlerts } from "../components/alert/Alerts";
@ -33,8 +32,8 @@ import type { KeyValueType } from "../components/key-value-form/key-value-conver
import { KeycloakSpinner } from "../components/keycloak-spinner/KeycloakSpinner"; import { KeycloakSpinner } from "../components/keycloak-spinner/KeycloakSpinner";
import { PermissionsTab } from "../components/permission-tab/PermissionTab"; import { PermissionsTab } from "../components/permission-tab/PermissionTab";
import { import {
routableTab,
RoutableTabs, RoutableTabs,
useRoutableTab,
} from "../components/routable-tabs/RoutableTabs"; } from "../components/routable-tabs/RoutableTabs";
import { import {
ViewHeader, ViewHeader,
@ -72,7 +71,7 @@ import {
} from "./routes/AuthenticationTab"; } from "./routes/AuthenticationTab";
import { ClientParams, ClientTab, toClient } from "./routes/Client"; import { ClientParams, ClientTab, toClient } from "./routes/Client";
import { toClients } from "./routes/Clients"; import { toClients } from "./routes/Clients";
import { toClientScopesTab } from "./routes/ClientScopeTab"; import { ClientScopesTab, toClientScopesTab } from "./routes/ClientScopeTab";
import { ClientScopes } from "./scopes/ClientScopes"; import { ClientScopes } from "./scopes/ClientScopes";
import { EvaluateScopes } from "./scopes/EvaluateScopes"; import { EvaluateScopes } from "./scopes/EvaluateScopes";
import { ServiceAccount } from "./service-account/ServiceAccount"; import { ServiceAccount } from "./service-account/ServiceAccount";
@ -204,7 +203,6 @@ export default function ClientDetails() {
const hasManageClients = hasAccess("manage-clients"); const hasManageClients = hasAccess("manage-clients");
const hasViewUsers = hasAccess("view-users"); const hasViewUsers = hasAccess("view-users");
const history = useHistory();
const navigate = useNavigate(); const navigate = useNavigate();
const [downloadDialogOpen, toggleDownloadDialogOpen] = useToggle(); const [downloadDialogOpen, toggleDownloadDialogOpen] = useToggle();
@ -227,6 +225,55 @@ export default function ClientDetails() {
return sortBy(roles, (role) => role.name?.toUpperCase()); return sortBy(roles, (role) => role.name?.toUpperCase());
}; };
const useTab = (tab: ClientTab) =>
useRoutableTab(
toClient({
realm,
clientId,
tab,
})
);
const settingsTab = useTab("settings");
const keysTab = useTab("keys");
const credentialsTab = useTab("credentials");
const rolesTab = useTab("roles");
const clientScopesTab = useTab("clientScopes");
const authorizationTab = useTab("authorization");
const serviceAccountTab = useTab("serviceAccount");
const sessionsTab = useTab("sessions");
const permissionsTab = useTab("permissions");
const advancedTab = useTab("advanced");
const useClientScopesTab = (tab: ClientScopesTab) =>
useRoutableTab(
toClientScopesTab({
realm,
clientId,
tab,
})
);
const clientScopesSetupTab = useClientScopesTab("setup");
const clientScopesEvaluateTab = useClientScopesTab("evaluate");
const useAuthorizationTab = (tab: AuthorizationTab) =>
useRoutableTab(
toAuthorizationTab({
realm,
clientId,
tab,
})
);
const authorizationSettingsTab = useAuthorizationTab("settings");
const authorizationResourcesTab = useAuthorizationTab("resources");
const authorizationScopesTab = useAuthorizationTab("scopes");
const authorizationPoliciesTab = useAuthorizationTab("policies");
const authorizationPermissionsTab = useAuthorizationTab("permissions");
const authorizationEvaluateTab = useAuthorizationTab("evaluate");
const authorizationExportTab = useAuthorizationTab("export");
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({ const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
titleKey: "clients:clientDeleteConfirmTitle", titleKey: "clients:clientDeleteConfirmTitle",
messageKey: "clients:clientDeleteConfirm", messageKey: "clients:clientDeleteConfirm",
@ -324,26 +371,6 @@ export default function ClientDetails() {
return <KeycloakSpinner />; return <KeycloakSpinner />;
} }
const route = (tab: ClientTab) =>
routableTab({
to: toClient({
realm,
clientId,
tab,
}),
history,
});
const authenticationRoute = (tab: AuthorizationTab) =>
routableTab({
to: toAuthorizationTab({
realm,
clientId,
tab,
}),
history,
});
return ( return (
<> <>
<ConfirmDialogModal <ConfirmDialogModal
@ -393,7 +420,7 @@ export default function ClientDetails() {
id="settings" id="settings"
data-testid="clientSettingsTab" data-testid="clientSettingsTab"
title={<TabTitleText>{t("common:settings")}</TabTitleText>} title={<TabTitleText>{t("common:settings")}</TabTitleText>}
{...route("settings")} {...settingsTab}
> >
<ClientSettings <ClientSettings
client={client} client={client}
@ -407,7 +434,7 @@ export default function ClientDetails() {
id="keys" id="keys"
data-testid="keysTab" data-testid="keysTab"
title={<TabTitleText>{t("keys")}</TabTitleText>} title={<TabTitleText>{t("keys")}</TabTitleText>}
{...route("keys")} {...keysTab}
> >
{client.protocol === "openid-connect" && ( {client.protocol === "openid-connect" && (
<Keys <Keys
@ -427,7 +454,7 @@ export default function ClientDetails() {
<Tab <Tab
id="credentials" id="credentials"
title={<TabTitleText>{t("credentials")}</TabTitleText>} title={<TabTitleText>{t("credentials")}</TabTitleText>}
{...route("credentials")} {...credentialsTab}
> >
<Credentials <Credentials
key={key} key={key}
@ -441,7 +468,7 @@ export default function ClientDetails() {
id="roles" id="roles"
data-testid="rolesTab" data-testid="rolesTab"
title={<TabTitleText>{t("roles")}</TabTitleText>} title={<TabTitleText>{t("roles")}</TabTitleText>}
{...route("roles")} {...rolesTab}
> >
<RolesList <RolesList
loader={loader} loader={loader}
@ -455,7 +482,7 @@ export default function ClientDetails() {
id="clientScopes" id="clientScopes"
data-testid="clientScopesTab" data-testid="clientScopesTab"
title={<TabTitleText>{t("clientScopes")}</TabTitleText>} title={<TabTitleText>{t("clientScopes")}</TabTitleText>}
{...route("clientScopes")} {...clientScopesTab}
> >
<RoutableTabs <RoutableTabs
defaultLocation={toClientScopesTab({ defaultLocation={toClientScopesTab({
@ -467,14 +494,7 @@ export default function ClientDetails() {
<Tab <Tab
id="setup" id="setup"
title={<TabTitleText>{t("setup")}</TabTitleText>} title={<TabTitleText>{t("setup")}</TabTitleText>}
{...routableTab({ {...clientScopesSetupTab}
to: toClientScopesTab({
realm,
clientId,
tab: "setup",
}),
history,
})}
> >
<ClientScopes <ClientScopes
clientName={client.clientId!} clientName={client.clientId!}
@ -486,14 +506,7 @@ export default function ClientDetails() {
<Tab <Tab
id="evaluate" id="evaluate"
title={<TabTitleText>{t("evaluate")}</TabTitleText>} title={<TabTitleText>{t("evaluate")}</TabTitleText>}
{...routableTab({ {...clientScopesEvaluateTab}
to: toClientScopesTab({
realm,
clientId,
tab: "evaluate",
}),
history,
})}
> >
<EvaluateScopes <EvaluateScopes
clientId={clientId} clientId={clientId}
@ -508,7 +521,7 @@ export default function ClientDetails() {
id="authorization" id="authorization"
data-testid="authorizationTab" data-testid="authorizationTab"
title={<TabTitleText>{t("authorization")}</TabTitleText>} title={<TabTitleText>{t("authorization")}</TabTitleText>}
{...route("authorization")} {...authorizationTab}
> >
<RoutableTabs <RoutableTabs
mountOnEnter mountOnEnter
@ -523,7 +536,7 @@ export default function ClientDetails() {
id="settings" id="settings"
data-testid="authorizationSettings" data-testid="authorizationSettings"
title={<TabTitleText>{t("settings")}</TabTitleText>} title={<TabTitleText>{t("settings")}</TabTitleText>}
{...authenticationRoute("settings")} {...authorizationSettingsTab}
> >
<AuthorizationSettings clientId={clientId} /> <AuthorizationSettings clientId={clientId} />
</Tab> </Tab>
@ -531,7 +544,7 @@ export default function ClientDetails() {
id="resources" id="resources"
data-testid="authorizationResources" data-testid="authorizationResources"
title={<TabTitleText>{t("resources")}</TabTitleText>} title={<TabTitleText>{t("resources")}</TabTitleText>}
{...authenticationRoute("resources")} {...authorizationResourcesTab}
> >
<AuthorizationResources clientId={clientId} /> <AuthorizationResources clientId={clientId} />
</Tab> </Tab>
@ -539,7 +552,7 @@ export default function ClientDetails() {
id="scopes" id="scopes"
data-testid="authorizationScopes" data-testid="authorizationScopes"
title={<TabTitleText>{t("scopes")}</TabTitleText>} title={<TabTitleText>{t("scopes")}</TabTitleText>}
{...authenticationRoute("scopes")} {...authorizationScopesTab}
> >
<AuthorizationScopes clientId={clientId} /> <AuthorizationScopes clientId={clientId} />
</Tab> </Tab>
@ -547,7 +560,7 @@ export default function ClientDetails() {
id="policies" id="policies"
data-testid="authorizationPolicies" data-testid="authorizationPolicies"
title={<TabTitleText>{t("policies")}</TabTitleText>} title={<TabTitleText>{t("policies")}</TabTitleText>}
{...authenticationRoute("policies")} {...authorizationPoliciesTab}
> >
<AuthorizationPolicies clientId={clientId} /> <AuthorizationPolicies clientId={clientId} />
</Tab> </Tab>
@ -557,7 +570,7 @@ export default function ClientDetails() {
title={ title={
<TabTitleText>{t("common:permissions")}</TabTitleText> <TabTitleText>{t("common:permissions")}</TabTitleText>
} }
{...authenticationRoute("permissions")} {...authorizationPermissionsTab}
> >
<AuthorizationPermissions clientId={clientId} /> <AuthorizationPermissions clientId={clientId} />
</Tab> </Tab>
@ -565,7 +578,7 @@ export default function ClientDetails() {
id="evaluate" id="evaluate"
data-testid="authorizationEvaluate" data-testid="authorizationEvaluate"
title={<TabTitleText>{t("evaluate")}</TabTitleText>} title={<TabTitleText>{t("evaluate")}</TabTitleText>}
{...authenticationRoute("evaluate")} {...authorizationEvaluateTab}
> >
<AuthorizationEvaluate client={client} save={save} /> <AuthorizationEvaluate client={client} save={save} />
</Tab> </Tab>
@ -573,7 +586,7 @@ export default function ClientDetails() {
id="export" id="export"
data-testid="authorizationExport" data-testid="authorizationExport"
title={<TabTitleText>{t("common:export")}</TabTitleText>} title={<TabTitleText>{t("common:export")}</TabTitleText>}
{...authenticationRoute("export")} {...authorizationExportTab}
> >
<AuthorizationExport /> <AuthorizationExport />
</Tab> </Tab>
@ -585,7 +598,7 @@ export default function ClientDetails() {
id="serviceAccount" id="serviceAccount"
data-testid="serviceAccountTab" data-testid="serviceAccountTab"
title={<TabTitleText>{t("serviceAccount")}</TabTitleText>} title={<TabTitleText>{t("serviceAccount")}</TabTitleText>}
{...route("serviceAccount")} {...serviceAccountTab}
> >
<ServiceAccount client={client} /> <ServiceAccount client={client} />
</Tab> </Tab>
@ -594,7 +607,7 @@ export default function ClientDetails() {
id="sessions" id="sessions"
data-testid="sessionsTab" data-testid="sessionsTab"
title={<TabTitleText>{t("sessions")}</TabTitleText>} title={<TabTitleText>{t("sessions")}</TabTitleText>}
{...route("sessions")} {...sessionsTab}
> >
<ClientSessions client={client} /> <ClientSessions client={client} />
</Tab> </Tab>
@ -604,7 +617,7 @@ export default function ClientDetails() {
id="permissions" id="permissions"
data-testid="permissionsTab" data-testid="permissionsTab"
title={<TabTitleText>{t("common:permissions")}</TabTitleText>} title={<TabTitleText>{t("common:permissions")}</TabTitleText>}
{...route("permissions")} {...permissionsTab}
> >
<PermissionsTab id={client.id!} type="clients" /> <PermissionsTab id={client.id!} type="clients" />
</Tab> </Tab>
@ -613,7 +626,7 @@ export default function ClientDetails() {
id="advanced" id="advanced"
data-testid="advancedTab" data-testid="advancedTab"
title={<TabTitleText>{t("advanced")}</TabTitleText>} title={<TabTitleText>{t("advanced")}</TabTitleText>}
{...route("advanced")} {...advancedTab}
> >
<AdvancedTab save={save} client={client} /> <AdvancedTab save={save} client={client} />
</Tab> </Tab>