Skip to content

Commit

Permalink
feat(server-routes): implement persisting input values in localStorage (
Browse files Browse the repository at this point in the history
  • Loading branch information
arashsheyda committed Dec 27, 2023
1 parent d5908ad commit 67dbf65
Showing 1 changed file with 55 additions and 7 deletions.
62 changes: 55 additions & 7 deletions packages/devtools/client/components/ServerRouteDetails.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import JsonEditorVue from 'json-editor-vue'
import { createReusableTemplate } from '@vueuse/core'
import { createReusableTemplate, watchDebounced } from '@vueuse/core'
import type { $Fetch } from 'ofetch'
import type { CodeSnippet, ServerRouteInfo, ServerRouteInput } from '~/../src/types'
Expand Down Expand Up @@ -85,7 +85,7 @@ const methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD']
const bodyPayloadMethods = ['PATCH', 'POST', 'PUT', 'DELETE']
const hasBody = computed(() => bodyPayloadMethods.includes(routeMethod.value.toUpperCase()))
const activeTab = ref(paramNames.value.length ? 'params' : 'query')
const activeTab = ref()
const tabInputs = ['input', 'json']
const selectedTabInput = ref(tabInputs[0])
Expand Down Expand Up @@ -308,6 +308,50 @@ watchEffect(() => {
}
})
const savedRouteInputs = useLocalStorage<{ path: string, tab: string, inputs: any }[]>('nuxt-devtools:server-routes:inputs', () => [], {
window: window.parent,
})
watchDebounced([routeInputs, activeTab], () => {
const savedEntry = savedRouteInputs.value?.find((entry: any) => entry.path === props.route.filepath)
if (!savedEntry) {
const newEntry = {
path: props.route.filepath,
tab: paramNames.value.length ? 'params' : 'query',
inputs: {
...routeInputs,
...(paramNames.value.length ? { params: routeParams.value } : {}),
},
}
savedRouteInputs.value.push(newEntry)
if (!activeTab.value)
activeTab.value = newEntry.tab
}
else {
if (!activeTab.value)
activeTab.value = savedEntry.tab
if (savedEntry.tab !== activeTab.value)
savedEntry.tab = activeTab.value
// update routeInputs with local storage
const { body, query, headers, params } = savedEntry.inputs
Object.assign(routeInputs, { body, query, headers })
routeParams.value = params
}
}, { immediate: true, deep: true, debounce: 500 })
function clearSavedCache() {
savedRouteInputs.value = []
routeInputs.body = []
routeInputs.query = []
routeInputs.headers = []
routeParams.value = {}
activeTab.value = paramNames.value.length ? 'params' : 'query'
}
const copy = useCopy()
</script>

Expand Down Expand Up @@ -369,15 +413,18 @@ const copy = useCopy()
<NButton
v-for="tab of tabs"
:key="tab.slug"
v-tooltip="tab.name"
:class="activeTab === tab.slug ? 'text-primary n-primary' : 'border-transparent shadow-none'"
@click="activeTab = tab.slug"
>
<NIcon :icon="ServerRouteTabIcons[tab.slug]" />
{{ tab.name }}
{{ tab?.length ? `(${tab.length})` : '' }}
<span>
{{ inputDefaults[tab.slug]?.length ? `(${inputDefaults[tab.slug].length})` : '' }}
</span>
<div class="hidden md:block">
{{ tab.name }}
{{ tab?.length ? `(${tab.length})` : '' }}
<span>
{{ inputDefaults[tab.slug]?.length ? `(${inputDefaults[tab.slug].length})` : '' }}
</span>
</div>
</NButton>
<div flex-auto />
<div text-xs op50>
Expand All @@ -395,6 +442,7 @@ const copy = useCopy()
DevTools
</option>
</NSelect>
<NButton v-tooltip="'Clear Inputs Saved Cache'" n="orange" class="p-3" icon="i-carbon-clean" @click="clearSavedCache" />
</div>
<div
v-if="activeTab === 'params'"
Expand Down

0 comments on commit 67dbf65

Please sign in to comment.