Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: populate useState reactively in devtools #663

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions packages/devtools/client/pages/modules/runtime-configs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ const privateConfig = computed(() => {
text="App Config"
:padding="false"
>
<StateEditor :state="client.app.appConfig" />
<StateEditor :state="toRef(client.app.appConfig)" />
</NSectionBlock>

<NSectionBlock
icon="carbon-settings"
text="Public Runtime Config"
:padding="false"
>
<StateEditor :state="payload.config?.public" />
<StateEditor :state="toRef(payload.config?.public)" />
</NSectionBlock>

<NSectionBlock
Expand All @@ -49,7 +49,7 @@ const privateConfig = computed(() => {
:padding="false"
description="These values are not exposed to the client. Readonly in the DevTools."
>
<StateEditor :state="privateConfig" readonly />
<StateEditor :state="toRef(privateConfig)" readonly />
</NSectionBlock>
</div>

Expand Down