Skip to content

Commit

Permalink
Review fixes 2 (#3)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ytkimirti authored Nov 21, 2024
1 parent 062543a commit b03e2f9
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 33 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@radix-ui/react-dropdown-menu": "^2.1.2",
"@radix-ui/react-icons": "1.3.0",
"@radix-ui/react-popover": "^1.0.7",
"@radix-ui/react-portal": "^1.1.2",
"@radix-ui/react-scroll-area": "^1.0.3",
"@radix-ui/react-select": "^2.0.0",
"@radix-ui/react-slot": "^1.0.2",
Expand Down
26 changes: 9 additions & 17 deletions src/components/databrowser/components/add-key-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
SelectValue,
} from "@/components/ui/select"
import { Spinner } from "@/components/ui/spinner"
import { toast } from "@/components/ui/use-toast"
import { TypeTag } from "@/components/databrowser/components/type-tag"
import { useAddKey } from "@/components/databrowser/hooks/use-add-key"

Expand All @@ -43,23 +42,16 @@ export function AddKeyModal() {
})

const onSubmit = handleSubmit(async ({ key, type }) => {
try {
await addKey({ key, type })
setSelectedKey(key)
setOpen(false)
setTimeout(() => {
window.document.querySelector(`[data-key="${key}"]`)?.scrollIntoView({
behavior: "smooth",
block: "start",
inline: "nearest",
})
}, 100)
} catch (error) {
toast({
description: error instanceof Error ? error.message : "An error occurred",
variant: "destructive",
await addKey({ key, type })
setSelectedKey(key)
setOpen(false)
setTimeout(() => {
window.document.querySelector(`[data-key="${key}"]`)?.scrollIntoView({
behavior: "smooth",
block: "start",
inline: "nearest",
})
}
}, 100)
})

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,24 @@ const ListEditForm = ({
<form onSubmit={onSubmit} className="flex flex-col gap-2">
<div className="flex grow flex-col gap-2">
{type !== "list" && (
<FormItem name="key" height={type === "set" ? 250 : 100} label={keyLabel} />
<FormItem
readOnly={type === "stream"}
name="key"
height={type === "set" ? 250 : 100}
label={keyLabel}
/>
)}

{type === "zset" ? (
<NumberFormItem name="value" label={valueLabel} />
) : (
type !== "set" && (
<FormItem name="value" height={type === "list" ? 250 : 100} label={valueLabel} />
<FormItem
readOnly={type === "stream"}
name="value"
height={type === "list" ? 250 : 100}
label={valueLabel}
/>
)
)}
</div>
Expand Down Expand Up @@ -131,18 +141,21 @@ const FormItem = ({
name,
label,
height,
readOnly,
}: {
name: string
label: string
isNumber?: boolean
height?: number
readOnly?: boolean
}) => {
const form = useFormContext()
const { editor, selector } = useField({
name,
form,
height: height,
showCopyButton: true,
readOnly,
})

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ export const CustomEditor = ({
onChange,
height,
showCopyButton,
readOnly,
}: {
language: string
value: string
onChange: (value: string) => void
height?: number
showCopyButton?: boolean
readOnly?: boolean
}) => {
const monaco = useMonaco()
const editorRef = useRef()
Expand Down Expand Up @@ -48,6 +50,7 @@ export const CustomEditor = ({
}}
defaultLanguage={language}
options={{
readOnly: readOnly,
wordWrap: "on",
overviewRulerBorder: false,
overviewRulerLanes: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ export const useField = ({
form,
height,
showCopyButton,
readOnly,
}: {
name: string
form: UseFormReturn<any>
height?: number
showCopyButton?: boolean
readOnly?: boolean
}) => {
const { field, fieldState } = useController<Record<string, string>>({
name,
Expand Down Expand Up @@ -60,6 +62,7 @@ export const useField = ({
onChange={field.onChange}
height={height}
showCopyButton={showCopyButton}
readOnly={readOnly}
/>
</>
),
Expand Down
2 changes: 2 additions & 0 deletions src/components/databrowser/hooks/use-add-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export const useAddKey = () => {

const mutation = useMutation({
mutationFn: async ({ key, type }: { key: string; type: DataType }) => {
if (await redis.exists(key)) throw new Error(`Key "${key}" already exists`)

switch (type) {
case "set": {
await redis.sadd(key, "value")
Expand Down
6 changes: 5 additions & 1 deletion src/components/databrowser/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "@/globals.css"

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

useEffect(() => {
queryClient.resetQueries()
}, [credentials.url])

return (
<QueryClientProvider client={queryClient}>
<TooltipProvider>
Expand Down
31 changes: 18 additions & 13 deletions src/components/ui/toaster.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { Portal } from "@radix-ui/react-portal"

import { portalRoot } from "@/lib/portal-root"
import {
Toast,
ToastClose,
Expand All @@ -12,18 +15,20 @@ export function Toaster() {
const { toasts } = useToast()

return (
<ToastProvider>
{toasts.map(({ id, title, description, action, ...props }) => (
<Toast key={id} {...props}>
<div className="grid gap-1">
{title && <ToastTitle>{title}</ToastTitle>}
{description && <ToastDescription>{description}</ToastDescription>}
</div>
{action}
<ToastClose />
</Toast>
))}
<ToastViewport />
</ToastProvider>
<Portal container={portalRoot}>
<ToastProvider>
{toasts.map(({ id, title, description, action, ...props }) => (
<Toast key={id} {...props}>
<div className="grid gap-1">
{title && <ToastTitle>{title}</ToastTitle>}
{description && <ToastDescription>{description}</ToastDescription>}
</div>
{action}
<ToastClose />
</Toast>
))}
<ToastViewport />
</ToastProvider>
</Portal>
)
}

0 comments on commit b03e2f9

Please sign in to comment.