Skip to content

Commit

Permalink
TypeScript Improvements (#181)
Browse files Browse the repository at this point in the history
Co-authored-by: Peter C <[email protected]>
  • Loading branch information
chippydip and somethingnew2-0 authored Nov 5, 2024
1 parent 5b0ed9f commit 60f5f91
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 54 deletions.
31 changes: 9 additions & 22 deletions src/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,16 @@ export function displayUserName(user: OktaUser | undefined) {
return user.display_name != null ? user.display_name : user.first_name + ' ' + user.last_name;
}

// https://stackoverflow.com/a/8817461
export function deepFind(obj: any, path: string) {
var paths = path.split('.'),
current = obj,
i;

for (i = 0; i < paths.length; ++i) {
if (current[paths[i]] == undefined) {
return undefined;
} else {
current = current[paths[i]];
}
}
return current;
}

// https://stackoverflow.com/a/34890276
export function groupBy(xs: Array<any>, key: string) {
return xs.reduce(function (rv, x) {
const newKey = deepFind(x, key);
(rv[newKey] = rv[newKey] || []).push(x);
return rv;
}, {});
export function groupBy<T>(xs: T[] | undefined, keyFn: (item: T) => string | undefined) {
return (xs ?? []).reduce(
(rv, x) => {
const newKey = keyFn(x) ?? '';
(rv[newKey] = rv[newKey] || []).push(x);
return rv;
},
{} as Record<string, T[]>,
);
}

export function getActiveTagsFromGroups(groups: PolymorphicGroup[]) {
Expand Down
6 changes: 2 additions & 4 deletions src/pages/apps/Read.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ function sortGroupMembers(
return aEmail.localeCompare(bEmail);
}

function groupMemberships(
memberships: Array<OktaUserGroupMember> | undefined,
): Map<string, Array<OktaUserGroupMember>> {
return groupBy(memberships ?? [], 'active_user.id');
function groupMemberships(memberships: Array<OktaUserGroupMember> | undefined) {
return groupBy(memberships, (m) => m.active_user?.id);
}

export default function ReadApp() {
Expand Down
19 changes: 8 additions & 11 deletions src/pages/groups/Read.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,20 @@ export default function ReadGroup() {
directRoleOwnerships.has(user.active_user!.id) ? null : allOwnerships.add(user);
});

let ownerships: Map<string, Array<OktaUserGroupMember>> = groupBy(Array.from(allOwnerships), 'active_user.id');
let ownerships = groupBy(Array.from(allOwnerships), (m) => m.active_user?.id);

const memberships: Map<string, Array<OktaUserGroupMember>> = groupBy(
group.active_user_memberships ?? [],
'active_user.id',
);
const memberships = groupBy(group.active_user_memberships, (m) => m.active_user?.id);

let role_associated_group_owners = new Map<String, Array<RoleGroupMap>>();
let role_associated_group_members = new Map<String, Array<RoleGroupMap>>();
let role_associated_group_owners: Record<string, RoleGroupMap[]> = {};
let role_associated_group_members: Record<string, RoleGroupMap[]> = {};
if (group.type == 'role_group') {
role_associated_group_owners = groupBy(
(group as RoleGroup).active_role_associated_group_owner_mappings ?? [],
'active_group.id',
(group as RoleGroup).active_role_associated_group_owner_mappings,
(g) => g.active_group?.id,
);
role_associated_group_members = groupBy(
(group as RoleGroup).active_role_associated_group_member_mappings ?? [],
'active_group.id',
(group as RoleGroup).active_role_associated_group_member_mappings,
(g) => g.active_group?.id,
);
}

Expand Down
11 changes: 4 additions & 7 deletions src/pages/requests/Read.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,7 @@ export default function ReadRequest() {
}));
}

const ownerships: Map<string, Array<OktaUserGroupMember>> = groupBy(
group.active_user_ownerships ?? [],
'active_user.id',
);
const ownerships = groupBy(group.active_user_ownerships, (m) => m.active_user?.id);

const {data: appData} = useGetAppById(
{
Expand All @@ -276,7 +273,7 @@ export default function ReadRequest() {
const appOwnershipsArray = (app.active_owner_app_groups ?? [])
.map((appGroup) => appGroup.active_user_ownerships ?? [])
.flat();
const appOwnerships: Map<string, Array<OktaUserGroupMember>> = groupBy(appOwnershipsArray, 'active_user.id');
const appOwnerships = groupBy(appOwnershipsArray, (m) => m.active_user?.id);

const {data: accessAppData} = useGetAppById(
{
Expand All @@ -295,9 +292,9 @@ export default function ReadRequest() {

const accessApp = accessAppData ?? ({} as App);

const accessAppOwnerships: Map<string, Array<OktaUserGroupMember>> = groupBy(
const accessAppOwnerships = groupBy(
(accessApp.active_owner_app_groups ?? []).map((appGroup) => appGroup.active_user_ownerships ?? []).flat(),
'active_user.id',
(m) => m.active_user?.id,
);

if (isError) {
Expand Down
14 changes: 4 additions & 10 deletions src/pages/users/Read.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function ReportingToCard({user}: ReportingToCardProps) {

interface OwnerTableProps {
user: OktaUser;
ownerships: Map<string, Array<OktaUserGroupMember>>;
ownerships: Record<string, OktaUserGroupMember[]>;
onClickRemoveGroupFromRole: (removeGroup: PolymorphicGroup, fromRole: RoleGroup, owner: boolean) => void;
onClickRemoveDirectAccess: (id: string, fromGroup: PolymorphicGroup, owner: boolean) => void;
}
Expand Down Expand Up @@ -260,7 +260,7 @@ function OwnerTable({user, ownerships, onClickRemoveGroupFromRole, onClickRemove

interface MemberTableProps {
user: OktaUser;
memberships: Map<string, Array<OktaUserGroupMember>>;
memberships: Record<string, OktaUserGroupMember[]>;
onClickRemoveGroupFromRole: (removeGroup: PolymorphicGroup, fromRole: RoleGroup, owner: boolean) => void;
onClickRemoveDirectAccess: (id: string, fromGroup: PolymorphicGroup, owner: boolean) => void;
}
Expand Down Expand Up @@ -434,14 +434,8 @@ export default function ReadUser() {

const user = data ?? ({} as OktaUser);

const ownerships: Map<string, Array<OktaUserGroupMember>> = groupBy(
user.active_group_ownerships ?? [],
'active_group.id',
);
const memberships: Map<string, Array<OktaUserGroupMember>> = groupBy(
user.active_group_memberships ?? [],
'active_group.id',
);
const ownerships = groupBy(user.active_group_ownerships, (m) => m.active_group?.id);
const memberships = groupBy(user.active_group_memberships, (m) => m.active_group?.id);

const showRemoveGroupFromRoleDialog = (removeGroup: PolymorphicGroup, fromRole: RoleGroup, owner: boolean) => {
setRemoveGroupsFromRoleDialogParameters({
Expand Down

0 comments on commit 60f5f91

Please sign in to comment.