From bd7ca9a7fa4df46cd3f04a1cc675223ec2121cc7 Mon Sep 17 00:00:00 2001 From: Jon Koops Date: Fri, 8 Oct 2021 20:21:09 +0200 Subject: [PATCH] Add utility function to pretty print JSON (#1310) --- src/clients/scopes/EvaluateScopes.tsx | 3 ++- .../download-dialog/DownloadDialog.tsx | 5 +++-- src/events/AdminEvents.tsx | 17 ++++------------- src/realm-settings/PoliciesTab.tsx | 4 ++-- src/realm-settings/ProfilesTab.tsx | 3 ++- src/util.ts | 4 +++- 6 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/clients/scopes/EvaluateScopes.tsx b/src/clients/scopes/EvaluateScopes.tsx index 0054e44cf3..bb07dbb23f 100644 --- a/src/clients/scopes/EvaluateScopes.tsx +++ b/src/clients/scopes/EvaluateScopes.tsx @@ -35,6 +35,7 @@ import { KeycloakDataTable } from "../../components/table-toolbar/KeycloakDataTa import { useAdminClient, useFetch } from "../../context/auth/AdminClient"; import { useRealm } from "../../context/realm-context/RealmContext"; import { useServerInfo } from "../../context/server-info/ServerInfoProvider"; +import { prettyPrintJSON } from "../../util"; import "./evaluate.css"; export type EvaluateScopesProps = { @@ -232,7 +233,7 @@ export const EvaluateScopes = ({ clientId, protocol }: EvaluateScopesProps) => { } }, (accessToken) => { - setAccessToken(JSON.stringify(accessToken, undefined, 3)); + setAccessToken(prettyPrintJSON(accessToken)); }, [user, selected] ); diff --git a/src/components/download-dialog/DownloadDialog.tsx b/src/components/download-dialog/DownloadDialog.tsx index 2d6e4b5c97..61f87e0b17 100644 --- a/src/components/download-dialog/DownloadDialog.tsx +++ b/src/components/download-dialog/DownloadDialog.tsx @@ -16,6 +16,7 @@ import React, { useEffect, useMemo, useState } from "react"; import { useTranslation } from "react-i18next"; import { useAdminClient, useFetch } from "../../context/auth/AdminClient"; import { useServerInfo } from "../../context/server-info/ServerInfoProvider"; +import { prettyPrintJSON } from "../../util"; import { ConfirmDialogModal } from "../confirm-dialog/ConfirmDialog"; import { useHelp } from "../help-enabler/HelpHeader"; import { HelpItem } from "../help-enabler/HelpItem"; @@ -46,7 +47,7 @@ export const DownloadDialog = ({ const [openType, setOpenType] = useState(false); const selectedConfig = useMemo( - () => configFormats?.find((config) => config.id === selected) ?? null, + () => configFormats.find((config) => config.id === selected) ?? null, [selected] ); @@ -59,7 +60,7 @@ export const DownloadDialog = ({ if (typeof snippet === "string") { return snippet; } else { - return JSON.stringify(snippet, undefined, 3); + return prettyPrintJSON(snippet); } }, (snippet) => setSnippet(snippet), diff --git a/src/events/AdminEvents.tsx b/src/events/AdminEvents.tsx index e394429bea..6b00bf8fb1 100644 --- a/src/events/AdminEvents.tsx +++ b/src/events/AdminEvents.tsx @@ -37,6 +37,7 @@ import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable import { useAdminClient } from "../context/auth/AdminClient"; import { useRealm } from "../context/realm-context/RealmContext"; import { useServerInfo } from "../context/server-info/ServerInfoProvider"; +import { prettyPrintJSON } from "../util"; import "./events.css"; type DisplayDialogProps = { @@ -541,21 +542,11 @@ export const AdminEvents = () => { [t("ipAddress"), authEvent?.authDetails?.ipAddress], ]; - function prettyPrintJSON(json: string) { - try { - return JSON.stringify(JSON.parse(json), null, 2); - } catch (error) { - return json; - } - } - const code = useMemo( () => - prettyPrintJSON( - representationEvent?.representation - ? prettyPrintJSON(representationEvent.representation) - : "" - ), + representationEvent?.representation + ? prettyPrintJSON(JSON.parse(representationEvent.representation)) + : "", [representationEvent?.representation] ); diff --git a/src/realm-settings/PoliciesTab.tsx b/src/realm-settings/PoliciesTab.tsx index b3b11c031f..c4d47306bd 100644 --- a/src/realm-settings/PoliciesTab.tsx +++ b/src/realm-settings/PoliciesTab.tsx @@ -17,7 +17,7 @@ import { ListEmptyState } from "../components/list-empty-state/ListEmptyState"; import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable"; import { useTranslation } from "react-i18next"; import { useAdminClient, useFetch } from "../context/auth/AdminClient"; -import { upperCaseFormatter } from "../util"; +import { prettyPrintJSON, upperCaseFormatter } from "../util"; import { CodeEditor, Language } from "@patternfly/react-code-editor"; import { Link } from "react-router-dom"; import type ClientPolicyRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientPolicyRepresentation"; @@ -44,7 +44,7 @@ export const PoliciesTab = () => { const loader = async () => policies ?? []; - const code = useMemo(() => JSON.stringify(policies, null, 2), [policies]); + const code = useMemo(() => prettyPrintJSON(policies), [policies]); const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({ titleKey: t("deleteClientPolicyConfirmTitle"), diff --git a/src/realm-settings/ProfilesTab.tsx b/src/realm-settings/ProfilesTab.tsx index a2ca5ce45b..47d7bd680c 100644 --- a/src/realm-settings/ProfilesTab.tsx +++ b/src/realm-settings/ProfilesTab.tsx @@ -20,6 +20,7 @@ import { useAdminClient, useFetch } from "../context/auth/AdminClient"; import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog"; import { useRealm } from "../context/realm-context/RealmContext"; import { useAlerts } from "../components/alert/Alerts"; +import { prettyPrintJSON } from "../util"; import { Link } from "react-router-dom"; import { toNewClientProfile } from "./routes/NewClientProfile"; import type ClientProfileRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientProfileRepresentation"; @@ -260,7 +261,7 @@ export const ProfilesTab = () => {