Skip to content

Commit 11cc9d3

Browse files
committed
fix: remove invalid plugins from active plugins on mount
2 parents 39d9683 + 1894b7e commit 11cc9d3

3 files changed

Lines changed: 49 additions & 43 deletions

File tree

‎packages/devtools/src/context/devtools-context.test.ts‎

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,56 @@ import { getStateFromLocalStorage } from "./devtools-context";
55

66
describe('getStateFromLocalStorage', () => {
77
beforeEach(() => {
8-
localStorage.clear();
9-
});
8+
localStorage.clear()
9+
})
1010
it('should return undefined when no data in localStorage', () => {
11-
const state = getStateFromLocalStorage();
12-
expect(state).toEqual(undefined);
13-
});
11+
const state = getStateFromLocalStorage()
12+
expect(state).toEqual(undefined)
13+
})
1414
it('should return parsed state from localStorage and not remove valid plugins', () => {
1515
const mockState = {
1616
activePlugins: ['plugin1'],
1717
settings: {
1818
theme: 'dark',
1919
},
20-
};
21-
localStorage.setItem(TANSTACK_DEVTOOLS_STATE, JSON.stringify(mockState));
22-
const state = getStateFromLocalStorage([{
23-
id: 'plugin1', render: () => { }, name: 'Plugin 1'
24-
}]);
25-
expect(state).toEqual(mockState);
26-
});
20+
}
21+
localStorage.setItem(TANSTACK_DEVTOOLS_STATE, JSON.stringify(mockState))
22+
const state = getStateFromLocalStorage([
23+
{
24+
id: 'plugin1',
25+
render: () => { },
26+
name: 'Plugin 1',
27+
},
28+
])
29+
expect(state).toEqual(mockState)
30+
})
2731
it('should filter out inactive plugins', () => {
2832
const mockState = {
2933
activePlugins: ['plugin1', 'plugin2'],
3034
settings: {
3135
theme: 'dark',
3236
},
33-
};
34-
localStorage.setItem(TANSTACK_DEVTOOLS_STATE, JSON.stringify(mockState));
35-
const plugins = [{ id: 'plugin1', render: () => { }, name: 'Plugin 1' }];
36-
const state = getStateFromLocalStorage(plugins);
37-
expect(state?.activePlugins).toEqual(['plugin1']);
38-
});
37+
}
38+
localStorage.setItem(TANSTACK_DEVTOOLS_STATE, JSON.stringify(mockState))
39+
const plugins = [{ id: 'plugin1', render: () => { }, name: 'Plugin 1' }]
40+
const state = getStateFromLocalStorage(plugins)
41+
expect(state?.activePlugins).toEqual(['plugin1'])
42+
})
3943
it('should return empty plugin state if all active plugins are invalid', () => {
4044
const mockState = {
4145
activePlugins: ['plugin1', 'plugin2'],
4246
settings: {
4347
theme: 'dark',
4448
},
45-
};
46-
localStorage.setItem(TANSTACK_DEVTOOLS_STATE, JSON.stringify(mockState));
47-
const plugins = [{ id: 'plugin3', render: () => { }, name: 'Plugin 3' }];
48-
const state = getStateFromLocalStorage(plugins);
49-
expect(state?.activePlugins).toEqual([]);
50-
});
49+
}
50+
localStorage.setItem(TANSTACK_DEVTOOLS_STATE, JSON.stringify(mockState))
51+
const plugins = [{ id: 'plugin3', render: () => { }, name: 'Plugin 3' }]
52+
const state = getStateFromLocalStorage(plugins)
53+
expect(state?.activePlugins).toEqual([])
54+
})
5155
it('should handle invalid JSON in localStorage gracefully', () => {
52-
localStorage.setItem(TANSTACK_DEVTOOLS_STATE, "invalid json");
53-
const state = getStateFromLocalStorage();
54-
expect(state).toEqual(undefined);
55-
});
56-
});
56+
localStorage.setItem(TANSTACK_DEVTOOLS_STATE, 'invalid json')
57+
const state = getStateFromLocalStorage()
58+
expect(state).toEqual(undefined)
59+
})
60+
})

‎packages/devtools/src/context/devtools-context.tsx‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ export interface TanStackDevtoolsPlugin {
3939
* ```
4040
*/
4141
name:
42-
| string
43-
| ((
44-
el: HTMLHeadingElement,
45-
theme: DevtoolsStore['settings']['theme'],
46-
) => void)
42+
| string
43+
| ((
44+
el: HTMLHeadingElement,
45+
theme: DevtoolsStore['settings']['theme'],
46+
) => void)
4747
/**
4848
* Unique identifier for the plugin.
4949
* If not provided, it will be generated based on the name.
@@ -99,10 +99,14 @@ const generatePluginId = (plugin: TanStackDevtoolsPlugin, index: number) => {
9999
return index.toString()
100100
}
101101

102-
export function getStateFromLocalStorage(plugins?: Array<TanStackDevtoolsPlugin>) {
102+
export function getStateFromLocalStorage(
103+
plugins?: Array<TanStackDevtoolsPlugin>,
104+
) {
103105
const existingStateString = getStorageItem(TANSTACK_DEVTOOLS_STATE)
104-
const existingState = tryParseJson<DevtoolsStore["state"]>(existingStateString)
105-
const pluginIds = plugins?.map((plugin, i) => generatePluginId(plugin, i)) || []
106+
const existingState =
107+
tryParseJson<DevtoolsStore['state']>(existingStateString)
108+
const pluginIds =
109+
plugins?.map((plugin, i) => generatePluginId(plugin, i)) || []
106110
if (existingState?.activePlugins) {
107111
const originalLength = existingState.activePlugins.length
108112
// Filter out any active plugins that are no longer available
@@ -112,10 +116,7 @@ export function getStateFromLocalStorage(plugins?: Array<TanStackDevtoolsPlugin>
112116

113117
if (existingState.activePlugins.length !== originalLength) {
114118
// If any active plugins were removed, update local storage
115-
setStorageItem(
116-
TANSTACK_DEVTOOLS_STATE,
117-
JSON.stringify(existingState),
118-
)
119+
setStorageItem(TANSTACK_DEVTOOLS_STATE, JSON.stringify(existingState))
119120
}
120121
}
121122

‎packages/devtools/src/context/devtools-store.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ export const initialState: DevtoolsStore = {
8181
requireUrlFlag: false,
8282
urlFlag: 'tanstack-devtools',
8383
theme:
84-
typeof window !== 'undefined' && typeof window.matchMedia !== "undefined" &&
85-
window.matchMedia('(prefers-color-scheme: dark)').matches
84+
typeof window !== 'undefined' &&
85+
typeof window.matchMedia !== 'undefined' &&
86+
window.matchMedia('(prefers-color-scheme: dark)').matches
8687
? 'dark'
8788
: 'light',
8889
},

0 commit comments

Comments
 (0)