Skip to content

Commit 50b59f7

Browse files
authored
Merge pull request #6 from upstash/DX-1802-fix-refresh
DX-1802: apply refresh to simple keys
2 parents f7e936b + b939817 commit 50b59f7

File tree

8 files changed

+65
-14
lines changed

8 files changed

+65
-14
lines changed

src/components/databrowser/components/display/display-header.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const DisplayHeader = ({
2020
const { setSelectedListItem } = useDatabrowserStore()
2121

2222
const handleAddItem = () => {
23-
setSelectedListItem({ key: type === "stream" ? "*" : "", value: "", isNew: true })
23+
setSelectedListItem({ key: type === "stream" ? "*" : "", isNew: true })
2424
}
2525

2626
return (

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useEffect } from "react"
12
import type { SelectedItem } from "@/store"
23
import { useDatabrowserStore } from "@/store"
34
import type { ListDataType } from "@/types"
@@ -7,6 +8,7 @@ import { Button } from "@/components/ui/button"
78
import { Spinner } from "@/components/ui/spinner"
89
import { SimpleTooltip } from "@/components/ui/tooltip"
910

11+
import { useFetchListItems } from "../../hooks"
1012
import { useEditListItem } from "../../hooks/use-edit-list-item"
1113
import { headerLabels } from "./display-list"
1214
import { useField } from "./input/use-field"
@@ -30,19 +32,41 @@ export const ListEditDisplay = ({
3032
const ListEditForm = ({
3133
type,
3234
dataKey,
33-
item: { key: itemKey, value: itemValue, isNew },
35+
item: { key: itemKey, isNew },
3436
}: {
3537
type: ListDataType
3638
dataKey: string
3739
item: SelectedItem
3840
}) => {
41+
const query = useFetchListItems({
42+
type,
43+
dataKey,
44+
})
45+
// Search in pages for item value
46+
const findValue = () => {
47+
for (const page of query.data?.pages ?? []) {
48+
const item = page.keys.find((item) => item.key === itemKey)
49+
// Check if item has a value property before returning it
50+
if (item && "value" in item) return item.value as string
51+
}
52+
return
53+
}
54+
const itemValue = findValue()
55+
3956
const form = useForm({
4057
defaultValues: {
4158
key: itemKey,
4259
value: itemValue,
4360
},
4461
})
4562

63+
useEffect(() => {
64+
form.reset({
65+
key: itemKey,
66+
value: itemValue,
67+
})
68+
}, [itemKey, itemValue])
69+
4670
const { mutateAsync: editItem, isPending } = useEditListItem()
4771
const { setSelectedListItem } = useDatabrowserStore()
4872

@@ -70,6 +94,7 @@ const ListEditForm = ({
7094
name="key"
7195
height={type === "set" ? 250 : 100}
7296
label={keyLabel}
97+
data={itemKey}
7398
/>
7499
)}
75100

@@ -82,6 +107,7 @@ const ListEditForm = ({
82107
name="value"
83108
height={type === "list" ? 250 : 100}
84109
label={valueLabel}
110+
data={itemValue ?? ""}
85111
/>
86112
)
87113
)}
@@ -142,12 +168,14 @@ const FormItem = ({
142168
label,
143169
height,
144170
readOnly,
171+
data,
145172
}: {
146173
name: string
147174
label: string
148175
isNumber?: boolean
149176
height?: number
150177
readOnly?: boolean
178+
data: string
151179
}) => {
152180
const form = useFormContext()
153181
const { editor, selector } = useField({
@@ -156,6 +184,7 @@ const FormItem = ({
156184
height: height,
157185
showCopyButton: true,
158186
readOnly,
187+
data,
159188
})
160189

161190
return (

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export const ListItems = ({
8282
data-item-key={key}
8383
data-item-value={value}
8484
onClick={() => {
85-
setSelectedListItem({ key, value })
85+
setSelectedListItem({ key })
8686
}}
8787
className="h-10 border-b border-b-zinc-100 hover:bg-zinc-50"
8888
>

src/components/databrowser/components/display/display-simple.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,21 @@ const EditorDisplayForm = ({
4545
const form = useForm({
4646
defaultValues: { value: data },
4747
})
48-
const { editor, selector } = useField({ name: "value", form })
49-
50-
const { mutateAsync: setKey, isPending: isSettingKey } = useSetSimpleKey(dataKey, type)
5148

5249
// Updates default values after submit
5350
useEffect(() => {
5451
form.reset(
5552
{ value: data },
5653
{
57-
keepValues: true,
54+
keepValues: false,
5855
}
5956
)
6057
}, [data])
6158

59+
const { editor, selector } = useField({ name: "value", form, data })
60+
61+
const { mutateAsync: setKey, isPending: isSettingKey } = useSetSimpleKey(dataKey, type)
62+
6263
const handleCancel = () => {
6364
form.reset()
6465
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ export const useField = ({
1010
height,
1111
showCopyButton,
1212
readOnly,
13+
data,
1314
}: {
1415
name: string
1516
form: UseFormReturn<any>
1617
height?: number
1718
showCopyButton?: boolean
1819
readOnly?: boolean
20+
data: string
1921
}) => {
2022
const { field, fieldState } = useController<Record<string, string>>({
2123
name,
@@ -26,16 +28,16 @@ export const useField = ({
2628
checkIsValidJSON(field.value) ? "JSON" : "Text"
2729
)
2830

29-
// Attempt to format JSON on initial load
31+
// Attempt to format JSON everytime the underlying data changes
3032
useEffect(() => {
31-
if (!checkIsValidJSON(field.value)) {
33+
if (!checkIsValidJSON(data)) {
3234
return
3335
}
3436

35-
form.setValue(name, formatJSON(field.value), {
37+
form.setValue(name, formatJSON(data), {
3638
shouldDirty: false,
3739
})
38-
}, [])
40+
}, [data])
3941

4042
const handleTypeChange = (type: ContentType) => {
4143
setContentType(type)

src/components/databrowser/components/sidebar/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { queryClient } from "@/lib/clients"
44
import { Button } from "@/components/ui/button"
55
import { Spinner } from "@/components/ui/spinner"
66

7-
import { FETCH_LIST_ITEMS_QUERY_KEY } from "../../hooks"
7+
import { FETCH_LIST_ITEMS_QUERY_KEY, FETCH_SIMPLE_KEY_QUERY_KEY } from "../../hooks"
88
import { useKeys } from "../../hooks/use-keys"
99
import { AddKeyModal } from "../add-key-modal"
1010
import { DisplayDbSize, FETCH_DB_SIZE_QUERY_KEY } from "./db-size"
@@ -32,6 +32,9 @@ export function Sidebar() {
3232
queryClient.invalidateQueries({
3333
queryKey: [FETCH_LIST_ITEMS_QUERY_KEY],
3434
})
35+
queryClient.invalidateQueries({
36+
queryKey: [FETCH_SIMPLE_KEY_QUERY_KEY],
37+
})
3538
queryClient.invalidateQueries({
3639
queryKey: [FETCH_DB_SIZE_QUERY_KEY],
3740
})

src/components/databrowser/hooks/use-fetch-simple-key.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,26 @@ export const useFetchSimpleKey = (dataKey: string, type: DataType) => {
1919
else if (type === "json") result = (await redis.json.get(dataKey)) as string | null
2020
else throw new Error(`Invalid type when fetching simple key: ${type}`)
2121

22+
if (type === "json" && result !== null)
23+
result = JSON.stringify(sortObject(JSON.parse(result)))
24+
2225
if (result === null) deleteKeyCache(dataKey)
2326

2427
return result
2528
},
2629
})
2730
}
31+
32+
// Add recursive key sorting to a JSON object
33+
const sortObject = (obj: unknown): unknown => {
34+
if (typeof obj !== "object" || obj === null) return obj
35+
return Object.fromEntries(
36+
Object.entries(obj)
37+
.sort((a, b) => a[0].localeCompare(b[0]))
38+
.map(([key, value]) =>
39+
typeof value === "object" && !Array.isArray(value) && value !== null
40+
? [key, sortObject(value)]
41+
: [key, value]
42+
)
43+
)
44+
}

src/store.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ export type SearchFilter = {
5959

6060
export type SelectedItem = {
6161
key: string
62-
value?: string
6362
isNew?: boolean
6463
}
6564

@@ -68,7 +67,7 @@ type DatabrowserStore = {
6867
setSelectedKey: (key: string | undefined) => void
6968

7069
selectedListItem?: SelectedItem
71-
setSelectedListItem: (item?: { key: string; value?: string; isNew?: boolean }) => void
70+
setSelectedListItem: (item?: { key: string; isNew?: boolean }) => void
7271

7372
search: SearchFilter
7473
setSearch: (search: SearchFilter) => void

0 commit comments

Comments
 (0)