Skip to content

Commit

Permalink
fix: using toRefs to reference payload state nested objects to popula…
Browse files Browse the repository at this point in the history
…te useState reactively (#643)
  • Loading branch information
dahi.hichem committed Jun 30, 2024
1 parent 541c929 commit 723ff23
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
15 changes: 8 additions & 7 deletions packages/devtools/client/components/StateEditor.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<script setup lang="ts">
import JsonEditorVue from 'json-editor-vue'
import type { ToRef } from 'vue'
const props = defineProps<{
name?: string
open?: boolean
state?: any
state?: ToRef<any>
readonly?: boolean
}>()
Expand All @@ -17,16 +18,16 @@ const colorMode = useColorMode()
const proxy = ref()
const state = useState(props.name)
if (props.state)
proxy.value = JSON.parse(JSON.stringify(props.state))
else if (typeof props.state === 'number' || typeof props.state !== 'string')
proxy.value = props.state
if (props.state?.value)
proxy.value = JSON.parse(JSON.stringify(props.state.value))
else if (typeof props.state?.value === 'number' || typeof props.state?.value !== 'string')
proxy.value = props.state?.value
const watcher = watchPausable(
proxy,
(value) => {
if (typeof value !== 'number' && typeof value !== 'string')
deepSync(value, props.state)
deepSync(value, props.state?.value)
else
state.value = value
},
Expand All @@ -48,7 +49,7 @@ function deepSync(from: any, to: any) {
async function refresh() {
watcher.pause()
proxy.value = JSON.parse(JSON.stringify(props.state))
proxy.value = JSON.parse(JSON.stringify(props.state?.value))
await nextTick()
watcher.resume()
}
Expand Down
12 changes: 9 additions & 3 deletions packages/devtools/client/components/StateGroup.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<script setup lang="ts">
withDefaults(
import type { ToRefs } from 'vue'
const props = withDefaults(
defineProps<{
state?: Record<string, any>
prefix?: string
Expand All @@ -8,13 +10,17 @@ withDefaults(
prefix: '',
},
)
let stateRefs: ToRefs<typeof props.state>
if (props.state)
stateRefs = toRefs(props.state)
</script>

<template>
<div>
<div v-if="state && Object.keys(state).length > 0" flex="~ col gap-1">
<div v-if="stateRefs && state && Object.keys(state).length > 0" flex="~ col gap-1">
<StateEditor
v-for="value, key of state"
v-for="(value, key) of stateRefs"
:key="key"
:state="value"
:name="key.startsWith(prefix) ? key.slice(prefix.length) : key"
Expand Down
2 changes: 1 addition & 1 deletion packages/devtools/client/pages/modules/payload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async function refreshData(keys?: string[]) {
>
<StateEditor
ml--6
:state="payload.functions"
:state="toRef(payload.functions)"
/>
</NSectionBlock>
</div>
Expand Down

0 comments on commit 723ff23

Please sign in to comment.