Skip to content

Commit

Permalink
fix: populate payload state using it keys (#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
dahi.hichem committed Jun 28, 2024
1 parent d88b003 commit 5ff0777
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
34 changes: 23 additions & 11 deletions packages/devtools/client/components/StateEditor.vue
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
<script setup lang="ts">
import JsonEditorVue from 'json-editor-vue'
import type { NuxtPayload } from '#app'
const props = defineProps<{
name?: string
open?: boolean
state?: any
stateKey?: string
payloadKey?: keyof NuxtPayload
readonly?: boolean
}>()
const emit = defineEmits<{
(e: 'update:open', value: boolean): void
}>()
const client = useClient()
const isOpen = useVModel(props, 'open', emit, { passive: true })
const colorMode = useColorMode()
const proxy = ref()
let clientState = ref<Record<string, any>>()
if (props.payloadKey)
clientState = ref(client.value.nuxt.payload[props.payloadKey] as Record<string, any>)
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.stateKey) {
const stateValue = clientState.value?.[props.stateKey]
if (stateValue)
proxy.value = JSON.parse(JSON.stringify(stateValue))
else if (typeof stateValue === 'number' || typeof stateValue !== 'string')
proxy.value = stateValue
}
const watcher = watchPausable(
proxy,
(value) => {
if (typeof value !== 'number' && typeof value !== 'string')
deepSync(value, props.state)
if (typeof value !== 'number' && typeof value !== 'string' && props.stateKey)
deepSync(value, clientState.value?.[props.stateKey])
else
state.value = value
},
Expand All @@ -47,10 +57,12 @@ function deepSync(from: any, to: any) {
}
async function refresh() {
watcher.pause()
proxy.value = JSON.parse(JSON.stringify(props.state))
await nextTick()
watcher.resume()
if (props.stateKey) {
watcher.pause()
proxy.value = JSON.parse(JSON.stringify(clientState.value?.[props.stateKey]))
await nextTick()
watcher.resume()
}
}
</script>

Expand Down
8 changes: 6 additions & 2 deletions packages/devtools/client/components/StateGroup.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<script setup lang="ts">
import type { NuxtPayload } from '#app'
withDefaults(
defineProps<{
state?: Record<string, any>
payloadKey?: keyof NuxtPayload
prefix?: string
}>(),
{
Expand All @@ -14,9 +17,10 @@ withDefaults(
<div>
<div v-if="state && Object.keys(state).length > 0" flex="~ col gap-1">
<StateEditor
v-for="value, key of state"
v-for="key of Object.keys(state)"
:key="key"
:state="value"
:state-key="key"
:payload-key="payloadKey"
:name="key.startsWith(prefix) ? key.slice(prefix.length) : key"
>
<template #actions="props">
Expand Down
6 changes: 3 additions & 3 deletions packages/devtools/client/pages/modules/payload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function refreshData(keys?: string[]) {
:padding="false"
>
<StateGroup
:state="payload.state" prefix="$s"
:state="payload.state" payload-key="state" prefix="$s"
/>
</NSectionBlock>
<NSectionBlock
Expand All @@ -45,7 +45,7 @@ async function refreshData(keys?: string[]) {
Re-fetch all data
</NButton>
</template>
<StateGroup :state="payload.data">
<StateGroup :state="payload.data" payload-key="data">
<template #actions="{ isOpen, name }">
<NButton
v-if="isOpen && name"
Expand All @@ -66,7 +66,7 @@ async function refreshData(keys?: string[]) {
>
<StateEditor
ml--6
:state="payload.functions"
:state="payload.functions" payload-key="functions"
/>
</NSectionBlock>
</div>
Expand Down

0 comments on commit 5ff0777

Please sign in to comment.