Skip to content

Commit 33c8d39

Browse files
authored
feat: handle unknown key gracefully (#43)
1 parent 868b29e commit 33c8d39

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

bun.lockb

-22.3 KB
Binary file not shown.

src/components/databrowser/components/display/index.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable unicorn/no-negated-condition */
22

3+
import { DATA_TYPES, SIMPLE_DATA_TYPES, type SimpleDataType } from "@/types"
34
import { useKeys, useKeyType } from "../../hooks/use-keys"
45
import { ListDisplay } from "./display-list"
56
import { EditorDisplay } from "./display-simple"
@@ -23,12 +24,16 @@ export const DataDisplay = () => {
2324
) : (
2425
<div />
2526
)
27+
) : !DATA_TYPES.includes(type as any) ? (
28+
<div className="flex h-full items-center justify-center">
29+
<span className="text-zinc-500">Unrecognized key type: {type}</span>
30+
</div>
2631
) : (
2732
<>
28-
{type === "string" || type === "json" ? (
29-
<EditorDisplay dataKey={selectedKey} type={type} />
33+
{SIMPLE_DATA_TYPES.includes(type as SimpleDataType) ? (
34+
<EditorDisplay dataKey={selectedKey} type={type as SimpleDataType} />
3035
) : (
31-
<ListDisplay dataKey={selectedKey} type={type} />
36+
<ListDisplay dataKey={selectedKey} type={type as Exclude<typeof type, SimpleDataType>} />
3237
)}
3338
</>
3439
)}

src/components/databrowser/components/sidebar/keys-list.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Fragment, useRef } from "react"
22
import { useTab } from "@/tab-provider"
3-
import type { DataType, RedisKey } from "@/types"
3+
import type { RedisKey } from "@/types"
44

55
import { cn } from "@/lib/utils"
66
import { Button } from "@/components/ui/button"
@@ -36,15 +36,17 @@ export const KeysList = () => {
3636
)
3737
}
3838

39-
const keyStyles = {
39+
const keyStyles: Record<string, string> = {
4040
string: "border-sky-400 !bg-sky-50 text-sky-900",
4141
hash: "border-amber-400 !bg-amber-50 text-amber-900",
4242
set: "border-indigo-400 !bg-indigo-50 text-indigo-900",
4343
zset: "border-pink-400 !bg-pink-50 text-pink-900",
4444
json: "border-purple-400 !bg-purple-50 text-purple-900",
4545
list: "border-orange-400 !bg-orange-50 text-orange-900",
4646
stream: "border-green-400 !bg-green-50 text-green-900",
47-
} as Record<DataType, string>
47+
}
48+
49+
const defaultKeyStyle = "border-gray-400 !bg-gray-50 text-gray-900"
4850

4951
const KeyItem = ({
5052
data,
@@ -92,7 +94,7 @@ const KeyItem = ({
9294
"relative flex h-10 w-full items-center justify-start gap-2 px-3 py-0 !ring-0 focus-visible:bg-zinc-50",
9395
"-my-px select-none border border-transparent text-left",
9496
isKeySelected && "shadow-sm",
95-
isKeySelected && keyStyles[dataType]
97+
isKeySelected && (keyStyles[dataType] ?? defaultKeyStyle)
9698
)}
9799
onClick={handleClick}
98100
>

src/components/databrowser/components/type-tag.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@ import {
77
IconLayersIntersect,
88
IconList,
99
IconQuote,
10+
IconQuestionMark,
1011
} from "@tabler/icons-react"
1112
import { cva, type VariantProps } from "class-variance-authority"
1213

1314
import { cn } from "@/lib/utils"
1415

15-
const iconsMap = {
16+
const iconsMap: Record<string, React.ReactNode> = {
1617
string: <IconQuote size={15} stroke={1.2} />,
1718
set: <IconLayersIntersect size={15} stroke={1.2} />,
1819
hash: <IconHash size={15} stroke={1.2} />,
1920
json: <IconCodeDots size={15} stroke={1.2} />,
2021
zset: <IconArrowsSort size={15} stroke={1.2} />,
2122
list: <IconList size={15} stroke={1.2} />,
2223
stream: <IconList size={15} stroke={1.2} />,
23-
} as const
24+
}
2425

2526
const tagVariants = cva("inline-flex shrink-0 items-center rounded-md justify-center", {
2627
variants: {
@@ -32,14 +33,15 @@ const tagVariants = cva("inline-flex shrink-0 items-center rounded-md justify-ce
3233
json: "bg-purple-200 text-purple-800",
3334
list: "bg-orange-200 text-orange-800",
3435
stream: "bg-green-200 text-green-800",
36+
default: "bg-gray-200 text-gray-800",
3537
},
3638
type: {
3739
icon: "size-5",
3840
badge: "h-6 px-2 uppercase whitespace-nowrap text-xs font-medium leading-none tracking-wide",
3941
},
4042
},
4143
defaultVariants: {
42-
variant: "string",
44+
variant: "default",
4345
type: "icon",
4446
},
4547
})
@@ -49,9 +51,14 @@ export interface TypeTagProps
4951
VariantProps<typeof tagVariants> {}
5052

5153
export function TypeTag({ className, variant, type }: TypeTagProps) {
54+
const defaultIcon = <IconQuestionMark size={15} stroke={1.2} />
55+
const variantKey = variant && variant in iconsMap ? variant : "default"
56+
5257
return (
53-
<span className={cn(tagVariants({ variant, type, className }))}>
54-
{type === "icon" ? iconsMap[variant as DataType] : DATA_TYPE_NAMES[variant as DataType]}
58+
<span className={cn(tagVariants({ variant: variantKey, type, className }))}>
59+
{type === "icon"
60+
? (iconsMap[variant as string] ?? defaultIcon)
61+
: (DATA_TYPE_NAMES[variant as DataType] ?? variant ?? "Unknown")}
5562
</span>
5663
)
5764
}

0 commit comments

Comments
 (0)