Skip to content

Commit fc3da50

Browse files
authored
Add fallbacks for setup key name & setup key group names (#370)
* Add try catch block for global search * Add fallback for group name * Add fallback for setup key name * Do not load setup key modal if it's not open * Check if auto_groups actually exists for the setup keys * Add fallback for group names in setup keys table * Add fallback for group names in peers table
1 parent 6d4716c commit fc3da50

File tree

9 files changed

+43
-30
lines changed

9 files changed

+43
-30
lines changed

src/app/(dashboard)/setup-keys/page.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import SkeletonTable from "@components/skeletons/SkeletonTable";
77
import { RestrictedAccess } from "@components/ui/RestrictedAccess";
88
import useFetchApi from "@utils/api";
99
import { ExternalLinkIcon } from "lucide-react";
10-
import React, { lazy, Suspense } from "react";
10+
import React, { lazy, Suspense, useMemo } from "react";
1111
import SetupKeysIcon from "@/assets/icons/SetupKeysIcon";
1212
import { useGroups } from "@/contexts/GroupsProvider";
1313
import { Group } from "@/interfaces/Group";
@@ -22,16 +22,21 @@ export default function SetupKeys() {
2222
const { data: setupKeys, isLoading } = useFetchApi<SetupKey[]>("/setup-keys");
2323
const { groups } = useGroups();
2424

25-
const setupKeysWithGroups = setupKeys?.map((setupKey) => {
26-
if (!setupKey.auto_groups) return setupKey;
27-
if (!groups) return setupKey;
28-
return {
29-
...setupKey,
30-
groups: setupKey.auto_groups.map((group) => {
31-
return groups.find((g) => g.id === group) || undefined;
32-
}) as Group[] | undefined,
33-
};
34-
});
25+
const setupKeysWithGroups = useMemo(() => {
26+
if (!setupKeys) return [];
27+
return setupKeys?.map((setupKey) => {
28+
if (!setupKey.auto_groups) return setupKey;
29+
if (!groups) return setupKey;
30+
return {
31+
...setupKey,
32+
groups: setupKey.auto_groups
33+
?.map((group) => {
34+
return groups.find((g) => g.id === group) || undefined;
35+
})
36+
.filter((group) => group !== undefined) as Group[],
37+
};
38+
});
39+
}, [setupKeys, groups]);
3540

3641
return (
3742
<PageContainer>

src/components/table/DataTable.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ declare module "@tanstack/table-core" {
5555
}
5656

5757
const fuzzyFilter: FilterFn<any> = (row, columnId, value, addMeta) => {
58-
const val = row.getValue(columnId);
59-
if (!val) return false;
60-
if (typeof val !== "string") return false;
61-
const lowerCaseValue = removeAllSpaces(trim(value.toLowerCase()));
62-
return val.toLowerCase().includes(lowerCaseValue);
58+
try {
59+
const val = row.getValue(columnId);
60+
if (!val) return false;
61+
if (typeof val !== "string") return false;
62+
const lowerCaseValue = removeAllSpaces(trim(value.toLowerCase()));
63+
return val.toLowerCase().includes(lowerCaseValue);
64+
} catch (e) {
65+
return false;
66+
}
6367
};
6468

6569
const exactMatch: FilterFn<any> = (row, columnId, value) => {

src/components/ui/GroupBadge.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ export default function GroupBadge({
2121
}: Props) {
2222
return (
2323
<Badge
24-
key={group.name}
24+
key={group.id}
2525
useHover={true}
2626
variant={"gray-ghost"}
2727
className={cn("transition-all group whitespace-nowrap", className)}
2828
onClick={onClick}
2929
>
3030
<FolderGit2 size={12} className={"shrink-0"} />
31-
<TextWithTooltip text={group.name} maxChars={20} />
31+
<TextWithTooltip text={group?.name || ""} maxChars={20} />
3232
{children}
3333
{showX && (
3434
<XIcon

src/modules/groups/GroupSelector.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ export function GroupSelector({
142142
<div className={""}>
143143
<div className={"grid grid-cols-1 gap-1"}>
144144
{orderBy(groups, "name")?.map((item) => {
145-
const value = item.name;
145+
const value = item?.name || "";
146+
if (value === "") return null;
146147
const isSelected =
147148
values.find((c) => c == value) != undefined;
148149

src/modules/peers/PeersTable.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ const PeersTableColumns: ColumnDef<Peer>[] = [
7272
},
7373
{
7474
accessorKey: "group_name_strings",
75-
accessorFn: (peer) => peer.groups?.map((g) => g.name).join(", "),
75+
accessorFn: (peer) => peer.groups?.map((g) => g?.name || "").join(", "),
7676
sortingFn: "text",
7777
},
7878
{
7979
accessorKey: "group_names",
80-
accessorFn: (peer) => peer.groups?.map((g) => g.name),
80+
accessorFn: (peer) => peer.groups?.map((g) => g?.name || ""),
8181
sortingFn: "text",
8282
filterFn: "arrIncludesSome",
8383
},

src/modules/setup-keys/SetupKeyActionCell.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ export default function SetupKeyActionCell({ setupKey }: Props) {
1717

1818
const handleRevoke = async () => {
1919
notify({
20-
title: "Setup Key: " + setupKey.name,
20+
title: setupKey?.name || "Setup Key",
2121
description: "Setup key was successfully revoked",
2222
promise: deleteRequest
2323
.put({
24-
name: setupKey.name,
24+
name: setupKey?.name || "Setup Key",
2525
type: setupKey.type,
2626
expires_in: setupKey.expires_in,
2727
revoked: true,
@@ -39,7 +39,7 @@ export default function SetupKeyActionCell({ setupKey }: Props) {
3939

4040
const handleConfirm = async () => {
4141
const choice = await confirm({
42-
title: `Revoke '${setupKey.name}'?`,
42+
title: `Revoke '${setupKey?.name || "Setup Key"}'?`,
4343
description:
4444
"Are you sure you want to revoke the setup key? This action cannot be undone.",
4545
confirmText: "Revoke",

src/modules/setup-keys/SetupKeyGroupsCell.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ export default function SetupKeyGroupsCell({ setupKey }: Props) {
1717
const groups = await Promise.all(promises);
1818

1919
notify({
20-
title: setupKey.name,
20+
title: setupKey?.name || "Setup Key",
2121
description: "Groups of the setup key were successfully saved",
2222
promise: request
2323
.put({
24-
name: setupKey.name,
24+
name: setupKey?.name || "Setup Key",
2525
type: setupKey.type,
2626
expires_in: setupKey.expires_in,
2727
revoked: setupKey.revoked,
28-
auto_groups: groups.map((group) => group.id),
28+
auto_groups: groups?.map((group) => group.id) || [],
2929
usage_limit: setupKey.usage_limit,
3030
ephemeral: setupKey.ephemeral,
3131
})

src/modules/setup-keys/SetupKeysTable.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default function SetupKeysTable({ setupKeys, isLoading }: Props) {
5252

5353
return (
5454
<>
55-
<SetupKeyModal open={open} setOpen={setOpen} />
55+
{open && <SetupKeyModal open={open} setOpen={setOpen} />}
5656
<DataTable
5757
isLoading={isLoading}
5858
text={"Setup Keys"}

src/modules/setup-keys/SetupKeysTableColumns.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ export const SetupKeysTableColumns: ColumnDef<SetupKey>[] = [
3939
},
4040
sortingFn: "text",
4141
cell: ({ row }) => (
42-
<SetupKeyNameCell valid={row.original.valid} name={row.original.name} />
42+
<SetupKeyNameCell
43+
valid={row.original.valid}
44+
name={row.original?.name || ""}
45+
/>
4346
),
4447
},
4548
{
@@ -66,7 +69,7 @@ export const SetupKeysTableColumns: ColumnDef<SetupKey>[] = [
6669
{
6770
id: "group_strings",
6871
accessorKey: "group_strings",
69-
accessorFn: (s) => s.groups?.map((g) => g.name).join(", "),
72+
accessorFn: (s) => s.groups?.map((g) => g?.name || "").join(", "),
7073
},
7174
{
7275
accessorKey: "last_used",

0 commit comments

Comments
 (0)