Skip to content

Commit b939817

Browse files
committed
fix: cache refresh when editing list item
1 parent a9a34df commit b939817

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
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/input/use-field.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const useField = ({
1717
height?: number
1818
showCopyButton?: boolean
1919
readOnly?: boolean
20-
data?: string
20+
data: string
2121
}) => {
2222
const { field, fieldState } = useController<Record<string, string>>({
2323
name,
@@ -30,11 +30,11 @@ export const useField = ({
3030

3131
// Attempt to format JSON everytime the underlying data changes
3232
useEffect(() => {
33-
if (!checkIsValidJSON(field.value)) {
33+
if (!checkIsValidJSON(data)) {
3434
return
3535
}
3636

37-
form.setValue(name, formatJSON(field.value), {
37+
form.setValue(name, formatJSON(data), {
3838
shouldDirty: false,
3939
})
4040
}, [data])

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)