|
| 1 | +<script setup lang="ts"> |
| 2 | +import { computed, ref, watch } from 'vue' |
| 3 | +import { Pane, Splitpanes } from 'splitpanes' |
| 4 | +import { callInspectorNodeAction, getInspectorNodeActions, getInspectorState, getInspectorTree, onInspectorStateUpdated, onInspectorTreeUpdated } from '@vue/devtools-core' |
| 5 | +import { parse } from '@vue/devtools-kit' |
| 6 | +import type { InspectorNodeTag, InspectorState } from '@vue/devtools-kit' |
| 7 | +import { vTooltip } from '@vue/devtools-ui' |
| 8 | +import Navbar from '~/components/basic/Navbar.vue' |
| 9 | +import SelectiveList from '~/components/basic/SelectiveList.vue' |
| 10 | +import DevToolsHeader from '~/components/basic/DevToolsHeader.vue' |
| 11 | +import Empty from '~/components/basic/Empty.vue' |
| 12 | +import RootStateViewer from '~/components/state/RootStateViewer.vue' |
| 13 | +import { createExpandedContext } from '~/composables/toggle-expanded' |
| 14 | +
|
| 15 | +const { expanded: expandedStateNodes } = createExpandedContext('vue-query-state') |
| 16 | +
|
| 17 | +interface NodeAction { |
| 18 | + icon: string |
| 19 | + tooltip: string |
| 20 | + actions?: (payload: unknown) => void |
| 21 | +} |
| 22 | +const inspectorId = 'vue-query' |
| 23 | +const nodeActions = ref<NodeAction[]>([]) |
| 24 | +
|
| 25 | +const selected = ref('') |
| 26 | +const tree = ref<{ id: string, label: string, tags: InspectorNodeTag[] }[]>([]) |
| 27 | +const state = ref<Record<string, InspectorState[]>>({}) |
| 28 | +const emptyState = computed(() => !Object.keys(state.value).length) |
| 29 | +
|
| 30 | +function getNodeActions() { |
| 31 | + getInspectorNodeActions(inspectorId).then((actions) => { |
| 32 | + nodeActions.value = actions as NodeAction[] |
| 33 | + }) |
| 34 | +} |
| 35 | +
|
| 36 | +getNodeActions() |
| 37 | +
|
| 38 | +function callNodeAction(index: number) { |
| 39 | + callInspectorNodeAction(inspectorId, index, selected.value) |
| 40 | +} |
| 41 | +
|
| 42 | +function filterEmptyState(data: Record<string, InspectorState[]>) { |
| 43 | + for (const key in data) { |
| 44 | + if (!data[key]?.length) |
| 45 | + delete data[key] |
| 46 | + } |
| 47 | + return data |
| 48 | +} |
| 49 | +
|
| 50 | +function getVueQueryState(nodeId: string) { |
| 51 | + getInspectorState({ inspectorId, nodeId }).then((data) => { |
| 52 | + state.value = filterEmptyState(parse(data!)) |
| 53 | + expandedStateNodes.value = Array.from({ length: Object.keys(state.value).length }, (_, i) => `${i}`) |
| 54 | + }) |
| 55 | +} |
| 56 | +
|
| 57 | +function clearVueQueryState() { |
| 58 | + state.value = {} |
| 59 | +} |
| 60 | +
|
| 61 | +watch(selected, () => { |
| 62 | + clearVueQueryState() |
| 63 | + getVueQueryState(selected.value) |
| 64 | +}) |
| 65 | +
|
| 66 | +const getVueQueryInspectorTree = () => { |
| 67 | + getInspectorTree({ inspectorId, filter: '' }).then((_data) => { |
| 68 | + const data = parse(_data!) |
| 69 | + tree.value = data |
| 70 | + if (!selected.value && data.length) { |
| 71 | + selected.value = data[0].id |
| 72 | + getVueQueryState(data[0].id) |
| 73 | + } |
| 74 | + }) |
| 75 | +} |
| 76 | +getVueQueryInspectorTree() |
| 77 | +
|
| 78 | +onInspectorTreeUpdated((data) => { |
| 79 | + if (!data?.data.length || data.inspectorId !== inspectorId) |
| 80 | + return |
| 81 | + tree.value = data.data as unknown as { id: string, label: string, tags: InspectorNodeTag[] }[] |
| 82 | + if ((!selected.value && data.data.length) || (selected.value && !data.data.find(node => node.id === selected.value))) { |
| 83 | + selected.value = data.data[0].id |
| 84 | + getVueQueryState(data.data[0].id) |
| 85 | + } |
| 86 | +}) |
| 87 | +
|
| 88 | +onInspectorStateUpdated((data) => { |
| 89 | + if (!data || data.inspectorId !== inspectorId) |
| 90 | + return |
| 91 | +
|
| 92 | + state.value = filterEmptyState(data as any) |
| 93 | + expandedStateNodes.value = Array.from({ length: Object.keys(state.value).length }, (_, i) => `${i}`) |
| 94 | +}) |
| 95 | +</script> |
| 96 | + |
| 97 | +<template> |
| 98 | + <div class="h-full flex flex-col"> |
| 99 | + <DevToolsHeader doc-link="https://tanstack.com/query/latest/docs/framework/vue/overview/" github-repo-link="https://github.com/TanStack/query/tree/main/packages/vue-query/"> |
| 100 | + <Navbar /> |
| 101 | + </DevToolsHeader> |
| 102 | + <template v-if="tree.length"> |
| 103 | + <Splitpanes class="flex-1 overflow-auto"> |
| 104 | + <Pane border="r base" size="40" h-full> |
| 105 | + <div h-full select-none overflow-scroll class="no-scrollbar"> |
| 106 | + <SelectiveList v-model="selected" :data="tree" /> |
| 107 | + </div> |
| 108 | + </Pane> |
| 109 | + <Pane size="60"> |
| 110 | + <div class="h-full flex flex-col p2"> |
| 111 | + <div class="flex justify-end pb-1" border="b dashed base"> |
| 112 | + <div class="flex items-center gap-2 px-1"> |
| 113 | + <div v-for="(action, index) in nodeActions" :key="index" v-tooltip.bottom-end="{ content: action.tooltip }" class="flex items-center gap1" @click="callNodeAction(index)"> |
| 114 | + <i :class="`i-ic-baseline-${action.icon.replace(/\_/g, '-')}`" cursor-pointer op70 text-base hover:op100 /> |
| 115 | + </div> |
| 116 | + </div> |
| 117 | + </div> |
| 118 | + <RootStateViewer v-if="selected && !emptyState" :data="state" :node-id="selected" :inspector-id="inspectorId" expanded-state-id="vue-query-state" class="no-scrollbar flex-1 select-none overflow-scroll" /> |
| 119 | + <Empty v-else> |
| 120 | + No Data |
| 121 | + </Empty> |
| 122 | + </div> |
| 123 | + </Pane> |
| 124 | + </Splitpanes> |
| 125 | + </template> |
| 126 | + <Empty v-else> |
| 127 | + No Data |
| 128 | + </Empty> |
| 129 | + </div> |
| 130 | +</template> |
0 commit comments