Skip to content

Commit b03e2f9

Browse files
authored
Review fixes 2 (#3)
* fix: reset query cache based on db url * feat: make editors in stream types readonly * fix: mount toaster to portalRoot to show on top of modals * fix: add better error for useAddKey * fix: remove toaster logic from add key modal becuase of reactQuery
1 parent 062543a commit b03e2f9

File tree

9 files changed

+56
-33
lines changed

9 files changed

+56
-33
lines changed

bun.lockb

32 Bytes
Binary file not shown.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@radix-ui/react-dropdown-menu": "^2.1.2",
3737
"@radix-ui/react-icons": "1.3.0",
3838
"@radix-ui/react-popover": "^1.0.7",
39+
"@radix-ui/react-portal": "^1.1.2",
3940
"@radix-ui/react-scroll-area": "^1.0.3",
4041
"@radix-ui/react-select": "^2.0.0",
4142
"@radix-ui/react-slot": "^1.0.2",

src/components/databrowser/components/add-key-modal.tsx

+9-17
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
SelectValue,
2424
} from "@/components/ui/select"
2525
import { Spinner } from "@/components/ui/spinner"
26-
import { toast } from "@/components/ui/use-toast"
2726
import { TypeTag } from "@/components/databrowser/components/type-tag"
2827
import { useAddKey } from "@/components/databrowser/hooks/use-add-key"
2928

@@ -43,23 +42,16 @@ export function AddKeyModal() {
4342
})
4443

4544
const onSubmit = handleSubmit(async ({ key, type }) => {
46-
try {
47-
await addKey({ key, type })
48-
setSelectedKey(key)
49-
setOpen(false)
50-
setTimeout(() => {
51-
window.document.querySelector(`[data-key="${key}"]`)?.scrollIntoView({
52-
behavior: "smooth",
53-
block: "start",
54-
inline: "nearest",
55-
})
56-
}, 100)
57-
} catch (error) {
58-
toast({
59-
description: error instanceof Error ? error.message : "An error occurred",
60-
variant: "destructive",
45+
await addKey({ key, type })
46+
setSelectedKey(key)
47+
setOpen(false)
48+
setTimeout(() => {
49+
window.document.querySelector(`[data-key="${key}"]`)?.scrollIntoView({
50+
behavior: "smooth",
51+
block: "start",
52+
inline: "nearest",
6153
})
62-
}
54+
}, 100)
6355
})
6456

6557
return (

src/components/databrowser/components/display/display-list-edit.tsx

+15-2
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,24 @@ const ListEditForm = ({
6565
<form onSubmit={onSubmit} className="flex flex-col gap-2">
6666
<div className="flex grow flex-col gap-2">
6767
{type !== "list" && (
68-
<FormItem name="key" height={type === "set" ? 250 : 100} label={keyLabel} />
68+
<FormItem
69+
readOnly={type === "stream"}
70+
name="key"
71+
height={type === "set" ? 250 : 100}
72+
label={keyLabel}
73+
/>
6974
)}
7075

7176
{type === "zset" ? (
7277
<NumberFormItem name="value" label={valueLabel} />
7378
) : (
7479
type !== "set" && (
75-
<FormItem name="value" height={type === "list" ? 250 : 100} label={valueLabel} />
80+
<FormItem
81+
readOnly={type === "stream"}
82+
name="value"
83+
height={type === "list" ? 250 : 100}
84+
label={valueLabel}
85+
/>
7686
)
7787
)}
7888
</div>
@@ -131,18 +141,21 @@ const FormItem = ({
131141
name,
132142
label,
133143
height,
144+
readOnly,
134145
}: {
135146
name: string
136147
label: string
137148
isNumber?: boolean
138149
height?: number
150+
readOnly?: boolean
139151
}) => {
140152
const form = useFormContext()
141153
const { editor, selector } = useField({
142154
name,
143155
form,
144156
height: height,
145157
showCopyButton: true,
158+
readOnly,
146159
})
147160

148161
return (

src/components/databrowser/components/display/input/custom-editor.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ export const CustomEditor = ({
1010
onChange,
1111
height,
1212
showCopyButton,
13+
readOnly,
1314
}: {
1415
language: string
1516
value: string
1617
onChange: (value: string) => void
1718
height?: number
1819
showCopyButton?: boolean
20+
readOnly?: boolean
1921
}) => {
2022
const monaco = useMonaco()
2123
const editorRef = useRef()
@@ -48,6 +50,7 @@ export const CustomEditor = ({
4850
}}
4951
defaultLanguage={language}
5052
options={{
53+
readOnly: readOnly,
5154
wordWrap: "on",
5255
overviewRulerBorder: false,
5356
overviewRulerLanes: 0,

src/components/databrowser/components/display/input/use-field.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ export const useField = ({
99
form,
1010
height,
1111
showCopyButton,
12+
readOnly,
1213
}: {
1314
name: string
1415
form: UseFormReturn<any>
1516
height?: number
1617
showCopyButton?: boolean
18+
readOnly?: boolean
1719
}) => {
1820
const { field, fieldState } = useController<Record<string, string>>({
1921
name,
@@ -60,6 +62,7 @@ export const useField = ({
6062
onChange={field.onChange}
6163
height={height}
6264
showCopyButton={showCopyButton}
65+
readOnly={readOnly}
6366
/>
6467
</>
6568
),

src/components/databrowser/hooks/use-add-key.ts

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export const useAddKey = () => {
1313

1414
const mutation = useMutation({
1515
mutationFn: async ({ key, type }: { key: string; type: DataType }) => {
16+
if (await redis.exists(key)) throw new Error(`Key "${key}" already exists`)
17+
1618
switch (type) {
1719
case "set": {
1820
await redis.sadd(key, "value")

src/components/databrowser/index.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import "@/globals.css"
22

3-
import { useMemo } from "react"
3+
import { useEffect, useMemo } from "react"
44
import { DatabrowserProvider, type RedisCredentials } from "@/store"
55
import { TooltipProvider } from "@radix-ui/react-tooltip"
66
import { IconDotsVertical } from "@tabler/icons-react"
@@ -17,6 +17,10 @@ import { KeysProvider } from "./hooks/use-keys"
1717
export const RedisBrowser = ({ token, url }: RedisCredentials) => {
1818
const credentials = useMemo(() => ({ token, url }), [token, url])
1919

20+
useEffect(() => {
21+
queryClient.resetQueries()
22+
}, [credentials.url])
23+
2024
return (
2125
<QueryClientProvider client={queryClient}>
2226
<TooltipProvider>

src/components/ui/toaster.tsx

+18-13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { Portal } from "@radix-ui/react-portal"
2+
3+
import { portalRoot } from "@/lib/portal-root"
14
import {
25
Toast,
36
ToastClose,
@@ -12,18 +15,20 @@ export function Toaster() {
1215
const { toasts } = useToast()
1316

1417
return (
15-
<ToastProvider>
16-
{toasts.map(({ id, title, description, action, ...props }) => (
17-
<Toast key={id} {...props}>
18-
<div className="grid gap-1">
19-
{title && <ToastTitle>{title}</ToastTitle>}
20-
{description && <ToastDescription>{description}</ToastDescription>}
21-
</div>
22-
{action}
23-
<ToastClose />
24-
</Toast>
25-
))}
26-
<ToastViewport />
27-
</ToastProvider>
18+
<Portal container={portalRoot}>
19+
<ToastProvider>
20+
{toasts.map(({ id, title, description, action, ...props }) => (
21+
<Toast key={id} {...props}>
22+
<div className="grid gap-1">
23+
{title && <ToastTitle>{title}</ToastTitle>}
24+
{description && <ToastDescription>{description}</ToastDescription>}
25+
</div>
26+
{action}
27+
<ToastClose />
28+
</Toast>
29+
))}
30+
<ToastViewport />
31+
</ToastProvider>
32+
</Portal>
2833
)
2934
}

0 commit comments

Comments
 (0)