8000 Fix react-compiler warnings: mutations, debounce, and delete patterns in admin UI by edewit · Pull Request #52904 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export const FlowRequirementDropdown = ({
<Select
=> setOpen(isOpen)}
value) => {
flow.requirement = value?.toString();
onChange(flow);
onChange({ ...flow, requirement: value?.toString() });
setOpen(false);
}}
selected={flow.requirement}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const ClusteringPanel = ({
clientId={id!}
isOpen={addNodeOpen}
=> {
nodes[node] = Date.now() / 1000;
setNodes((prev) => ({ ...prev, [node]: Date.now() / 1000 }));
refresh();
}}
=> setAddNodeOpen(false)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const OpenIdConnectCompatibilityModes = ({
"attributes.standard.token.exchange.enabled",
),
);
const useRefreshTokens = watch(
const refreshTokensEnabled = watch(
convertAttributeNameToForm<FormFields>("attributes.use.refresh.tokens"),
"true",
);
Expand Down Expand Up @@ -300,7 +300,7 @@ export const OpenIdConnectCompatibilityModes = ({
}}
isDisabled={
tokenExchangeEnabled?.toString() !== "true" ||
useRefreshTokens?.toString() !== "true"
refreshTokensEnabled?.toString() !== "true"
}
options={[
{ key: "", value: t("choose") },
Expand Down
19 changes: 6 additions & 13 deletions js/apps/admin-ui/src/components/group/GroupPickerDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,6 @@ export const GroupPickerDialog = ({
setCount(selectedGroup.subGroupCount!);
}

groups.forEach((group: SelectableGroup) => {
group.checked = !!selectedRows.find((r) => r.id === group.id);
});
setGroups(groups);
if (filter !== "" || !groupId) {
setCount(groups.length);
Expand Down Expand Up @@ -538,18 +535,14 @@ const GroupRow = ({
className="kc-join-group-modal-check"
data-testid={`${group.name}-check`}
aria-label={group.name}
checked={group.checked}
checked={!!selectedRows.find((r) => r.id === group.id)}
isDisabled={isRowDisabled(group)}
checked) => {
group.checked = checked;
let newSelectedRows: SelectableGroup[] = [];
if (!group.checked) {
newSelectedRows = selectedRows.filter((r) => r.id !== group.id);
} else {
newSelectedRows = [...selectedRows, group];
}

setSelectedRows(newSelectedRows);
setSelectedRows(
checked
? [...selectedRows, { ...group, checked: true }]
: selectedRows.filter((r) => r.id !== group.id),
);
}}
aria-labelledby={`select-${group.name}`}
/>
Expand Down
21 changes: 11 additions & 10 deletions js/apps/admin-ui/src/components/users/UserSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from "@patternfly/react-core";
import { TimesIcon } from "@patternfly/react-icons";
import { debounce } from "lodash-es";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useEffect, useMemo, useRef, useState } from "react";
import { Controller, useFormContext } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { useAdminClient } from "../../admin-client";
Expand Down Expand Up @@ -76,15 +76,16 @@ export const UserSelect = ({
const [search, setSearch] = useState("");
const textInputRef = useRef<HTMLInputElement>();

const debounceFn = useCallback(
debounce((value: string) => {
setFirst(0);
setHasMore(false);
setLoadingMore(false);
setPageUsers([]);
setExactMatch(undefined);
setSearch(value);
}, 500),
const debounceFn = useMemo(
() =>
debounce((value: string) => {
setFirst(0);
setHasMore(false);
setLoadingMore(false);
setPageUsers([]);
setExactMatch(undefined);
setSearch(value);
}, 500),
[],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from "@patternfly/react-core";
import { TimesIcon } from "@patternfly/react-icons";
import { debounce } from "lodash-es";
import { useCallback, useRef, useState } from "react";
import { useMemo, useRef, useState } from "react";
import { Controller, useFormContext } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { useAdminClient } from "../admin-client";
Expand Down Expand Up @@ -63,7 +63,10 @@ export const IdentityProviderSelect = ({
>([]);
const [search, setSearch] = useState("");

const debounceFn = useCallback(debounce(setSearch, 1000), []);
const debounceFn = useMemo(
() => debounce((value: string) => setSearch(value), 1000),
[],
);

useFetch(
async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
PageSection,
} from "@patternfly/react-core";
import { flatten } from "flat";
import { omit } from "lodash-es";
import { useState } from "react";
import { FormProvider, useForm, useFormContext } from "react-hook-form";
import { useTranslation } from "react-i18next";
Expand Down Expand Up @@ -200,8 +201,8 @@ export default function NewAttributeSettings() {
delete formFields.selector;
}

if (!hasRequiredScopes) {
delete formFields.required?.scopes;
if (!hasRequiredScopes && formFields.required) {
formFields.required = omit(formFields.required, "scopes");
}

const validations = formFields.validations.reduce(
Expand Down
9 changes: 7 additions & 2 deletions js/apps/admin-ui/src/realm-settings/PartialImport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
PartialImportResult,
} from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation";
import type RoleRepresentation from "@keycloak/keycloak-admin-client/lib/defs/roleRepresentation";
import { omit } from "lodash-es";
import { KeycloakSelect } from "@keycloak/keycloak-ui-shared";
import {
Alert,
Expand Down Expand Up @@ -250,8 +251,12 @@ export const PartialImportDialog = (props: PartialImportProps) => {
jsonToImport.clients = targetRealm.clients;
if (resourcesToImport["realmRoles"] || resourcesToImport["clientRoles"]) {
jsonToImport.roles = targetRealm.roles;
if (!resourcesToImport["realmRoles"]) delete jsonToImport.roles?.realm;
if (!resourcesToImport["clientRoles"]) delete jsonToImport.roles?.client;
if (!resourcesToImport["realmRoles"] && jsonToImport.roles) {
jsonToImport.roles = omit(jsonToImport.roles, "realm");
}
if (!resourcesToImport["clientRoles"] && jsonToImport.roles) {
jsonToImport.roles = omit(jsonToImport.roles, "client");
}
}
return jsonToImport;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
} from "@patternfly/react-table";
import { useRef, useState } from "react";
import { useForm } from "react-hook-form";
import type { i18n as I18n } from "i18next";
import { useTranslation } from "react-i18next";
import { useAdminClient } from "../../admin-client";
import { useConfirmDialog } from "../../components/confirm-dialog/ConfirmDialog";
Expand Down Expand Up @@ -76,6 +77,34 @@ function translationId(realm: string, locale: string, key: string) {
return JSON.stringify([realm, locale, key]);
}

async function reloadLocaleResourcesWithoutKeys(
i18n: I18n,
locale: string,
realm: string,
deletedKeys: Iterable<string>,
) {
// reloadResources merges into the existing bundle, so removed overrides would
// otherwise stay available to t() until a full page reload.
await i18n.reloadResources();
const existing = i18n.getResourceBundle(locale, realm) as
| Record<string, string>
| undefined;
if (!existing) {
return;
}
const next = { ...existing };
let changed = false;
for (const key of deletedKeys) {
if (key in next) {
delete next[key];
changed = true;
}
}
if (changed) {
i18n.addResourceBundle(locale, realm, next, false, true);
}
}

export type TranslationForm = {
key: string;
value: string;
Expand Down Expand Up @@ -269,14 +298,6 @@ export const RealmOverrides = ({
key,
});
deletedKeys.add(key);

const messages = i18n.getResourceBundle(
selectMenuLocale,
encodeURIComponent(realmName),
) as Record<string, string> | undefined;
if (messages) {
delete messages[key];
}
}
addAlert(t("deleteAllTranslationsSuccess"), AlertVariant.success);
} catch (error) {
Expand All @@ -295,6 +316,12 @@ export const RealmOverrides = ({
: previous,
);
setAreAllRowsSelected(false);
await reloadLocaleResourcesWithoutKeys(
i18n,
selectMenuLocale,
realmName,
deletedKeys,
);
}
setDeletingIds(new Set());
}
Expand Down
13 changes: 8 additions & 5 deletions js/apps/admin-ui/src/user/EditUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,16 @@ export default function EditUser() {
throw new Error(t("notFound"));
}

const { userProfileMetadata, ...user } = userData;
const { userProfileMetadata, ...userWithoutMetadata } = userData;
setUserProfileMetadata(userProfileMetadata);
user.unmanagedAttributes = unmanagedAttributes;
user.attributes = filterManagedAttributes(
user.attributes,
const user = {
...userWithoutMetadata,
unmanagedAttributes,
);
attributes: filterManagedAttributes(
userWithoutMetadata.attributes,
unmanagedAttributes,
),
};

if (upConfig.unmanagedAttributePolicy !== undefined) {
setUnmanagedAttributesEnabled(true);
Expand Down
Loading
0