Skip to content

Commit ba4401e

Browse files
committed
Refactor VersionSelectorCache to use a default cache item and improve type definitions
1 parent 23d8668 commit ba4401e

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

xp-archive/client/versionSelector/VersionSelectorCache.tsx

+16-17
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import React from 'react';
22
import { VersionReference } from 'shared/types';
33

4-
const contentCache: Record<
5-
string,
6-
{
7-
component: React.ReactNode | null;
8-
versions: VersionReference[];
9-
isOpen: boolean;
10-
}
11-
> = {};
4+
type VersionSelectorCacheItem = {
5+
component: React.ReactNode | null;
6+
versions: VersionReference[];
7+
isOpen: boolean;
8+
};
9+
10+
const DEFAULT_CACHE: VersionSelectorCacheItem = {
11+
component: null,
12+
versions: [],
13+
isOpen: false,
14+
};
15+
16+
const contentCache: Record<string, VersionSelectorCacheItem> = {};
1217

1318
export const setCachedVersionSelector = (
1419
contentId: string,
@@ -29,17 +34,11 @@ export const setCachedVersionSelector = (
2934
};
3035
};
3136

32-
export const getCachedVersionSelector = (contentId: string) => {
33-
return (
34-
contentCache[contentId] || {
35-
component: null,
36-
versions: [],
37-
isOpen: false,
38-
}
39-
);
37+
export const getCachedVersionSelector = (contentId: string): VersionSelectorCacheItem => {
38+
return contentCache[contentId] || DEFAULT_CACHE;
4039
};
4140

42-
export const clearCachedVersionSelector = (contentId: string) => {
41+
export const clearCachedVersionSelector = (contentId: string): void => {
4342
if (contentId in contentCache) {
4443
delete contentCache[contentId];
4544
}

0 commit comments

Comments
 (0)