Skip to content

Commit 95b5ba1

Browse files
committed
feat: ability to reset options
1 parent 18427b0 commit 95b5ba1

5 files changed

Lines changed: 39 additions & 7 deletions

File tree

packages/devtools-kit/src/_types/rpc.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface ServerFunctions {
2727
// Options
2828
getOptions<T extends keyof NuxtDevToolsOptions>(tab: T): Promise<NuxtDevToolsOptions[T]>
2929
updateOptions<T extends keyof NuxtDevToolsOptions>(tab: T, settings: Partial<NuxtDevToolsOptions[T]>): Promise<void>
30+
clearOptions(): Promise<void>
3031

3132
// Updates
3233
checkForUpdateFor(name: string): Promise<PackageUpdateInfo | undefined>

packages/devtools-ui-kit/src/components/NCheckbox.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useVModel } from '@vueuse/core'
33
44
const props = withDefaults(
55
defineProps<{
6-
modelValue?: boolean
6+
modelValue?: boolean | null
77
disabled?: boolean
88
}>(),
99
{

packages/devtools/client/pages/settings.vue

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ function pinMove(name: string, delta: number) {
7575
pinnedTabs.value = newPinnedTabs
7676
}
7777
78+
async function clearOptions() {
79+
// eslint-disable-next-line no-alert
80+
if (confirm('Are you sure you to reset all local settings & state? The app will reload.')) {
81+
Object.keys(localStorage).forEach((key) => {
82+
if (key.startsWith('nuxt-devtools-'))
83+
localStorage.removeItem(key)
84+
})
85+
await rpc.clearOptions()
86+
client.value?.app?.reload?.()
87+
window.location.reload()
88+
}
89+
}
90+
7891
// sync devtools options with frame state
7992
watchEffect(() => {
8093
if (client.value)
@@ -232,6 +245,16 @@ watchEffect(() => {
232245
</NButton>
233246
</div>
234247
</NCard>
248+
249+
<h3 mt2 text-lg>
250+
Debug
251+
</h3>
252+
<div flex="~ gap-2">
253+
<NButton n="orange" @click="clearOptions">
254+
<div i-carbon-breaking-change />
255+
Reset Local Settings & State
256+
</NButton>
257+
</div>
235258
</div>
236259
</div>
237260
</div>

packages/devtools/src/server-rpc/options.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { NuxtDevToolsOptions, NuxtDevtoolsServerContext, ServerFunctions } from '../types'
2-
import { readLocalOptions, writeLocalOptions } from '../utils/local-options'
2+
import { clearLocalOptions, readLocalOptions, writeLocalOptions } from '../utils/local-options'
33
import { defaultTabOptions } from '../constant'
44

55
let options: NuxtDevToolsOptions | undefined
@@ -28,6 +28,13 @@ export function setupOptionsRPC({ nuxt }: NuxtDevtoolsServerContext) {
2828

2929
getOptions('ui')
3030

31+
async function clearOptions() {
32+
options = undefined
33+
await clearLocalOptions({
34+
root: nuxt.options.rootDir,
35+
})
36+
}
37+
3138
return {
3239
async updateOptions(tab, _settings) {
3340
const settings = await getOptions(tab)
@@ -46,5 +53,6 @@ export function setupOptionsRPC({ nuxt }: NuxtDevtoolsServerContext) {
4653
})
4754
},
4855
getOptions,
56+
clearOptions,
4957
} satisfies Partial<ServerFunctions>
5058
}

packages/devtools/src/utils/local-options.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import { dirname } from 'node:path'
55
import { join } from 'pathe'
66
import { hash } from 'ohash'
77

8-
interface Options {
8+
interface LocalOptionSearchOptions {
99
root: string
1010
key?: string | boolean
1111
}
1212

13-
export async function readLocalOptions<T>(defaults: T, options: Options): Promise<T> {
13+
export async function readLocalOptions<T>(defaults: T, options: LocalOptionSearchOptions): Promise<T> {
1414
const { filePath } = getOptionsFilepath(options)
1515

1616
if (existsSync(filePath)) {
@@ -25,7 +25,7 @@ export async function readLocalOptions<T>(defaults: T, options: Options): Promis
2525
}
2626
}
2727

28-
function getOptionsFilepath(options: Options) {
28+
function getOptionsFilepath(options: LocalOptionSearchOptions) {
2929
let hashedKey
3030
if (options.key)
3131
hashedKey = hash(`${options.root}:${options.key}`)
@@ -38,13 +38,13 @@ function getOptionsFilepath(options: Options) {
3838
}
3939
}
4040

41-
export async function clearLocalOptions(options: Options) {
41+
export async function clearLocalOptions(options: LocalOptionSearchOptions) {
4242
const { filePath } = getOptionsFilepath(options)
4343
if (existsSync(filePath))
4444
await fs.unlink(filePath)
4545
}
4646

47-
export async function writeLocalOptions<T>(settings: T, options: Options) {
47+
export async function writeLocalOptions<T>(settings: T, options: LocalOptionSearchOptions) {
4848
const { filePath, hashedKey } = getOptionsFilepath(options)
4949

5050
await fs.mkdir(dirname(filePath), { recursive: true })

0 commit comments

Comments
 (0)