mirror of https://github.com/buster-so/buster.git
bust storage update
This commit is contained in:
parent
7f5e1c80c4
commit
170380fe09
|
@ -37,7 +37,7 @@ interface IAppSplitterProps {
|
||||||
hideSplitter?: boolean;
|
hideSplitter?: boolean;
|
||||||
leftPanelClassName?: string;
|
leftPanelClassName?: string;
|
||||||
rightPanelClassName?: string;
|
rightPanelClassName?: string;
|
||||||
bustStorageOnInit?: boolean;
|
bustStorageOnInit?: boolean | ((preservedSideValue: number | null) => boolean);
|
||||||
renderLeftPanel?: boolean;
|
renderLeftPanel?: boolean;
|
||||||
renderRightPanel?: boolean;
|
renderRightPanel?: boolean;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ interface Options<T> {
|
||||||
serializer?: (value: T) => string;
|
serializer?: (value: T) => string;
|
||||||
deserializer?: (value: string) => T;
|
deserializer?: (value: string) => T;
|
||||||
onError?: (error: unknown) => void;
|
onError?: (error: unknown) => void;
|
||||||
bustStorageOnInit?: boolean;
|
bustStorageOnInit?: boolean | ((layout: T) => boolean);
|
||||||
expirationTime?: number;
|
expirationTime?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,15 +38,20 @@ export function useLocalStorageState<T>(
|
||||||
|
|
||||||
// Get initial value from localStorage or use default
|
// Get initial value from localStorage or use default
|
||||||
const getInitialValue = useMemoizedFn((): T | undefined => {
|
const getInitialValue = useMemoizedFn((): T | undefined => {
|
||||||
// If bustStorageOnInit is true, ignore localStorage and use default value
|
const letsBusterTheStorageBaby = () => {
|
||||||
if (bustStorageOnInit) {
|
window.localStorage.removeItem(key);
|
||||||
return typeof defaultValue === 'function' ? (defaultValue as () => T)() : defaultValue;
|
return typeof defaultValue === 'function' ? (defaultValue as () => T)() : defaultValue;
|
||||||
|
};
|
||||||
|
|
||||||
|
// If bustStorageOnInit is true, ignore localStorage and use default value
|
||||||
|
if (bustStorageOnInit === true) {
|
||||||
|
return letsBusterTheStorageBaby();
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const item = window.localStorage.getItem(key);
|
const item = window.localStorage.getItem(key);
|
||||||
if (item === null) {
|
if (item === null) {
|
||||||
return typeof defaultValue === 'function' ? (defaultValue as () => T)() : defaultValue;
|
return letsBusterTheStorageBaby();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the stored data which includes value and timestamp
|
// Parse the stored data which includes value and timestamp
|
||||||
|
@ -60,8 +65,7 @@ export function useLocalStorageState<T>(
|
||||||
!('timestamp' in storageData)
|
!('timestamp' in storageData)
|
||||||
) {
|
) {
|
||||||
// If the data doesn't have the expected structure (legacy data), treat as expired
|
// If the data doesn't have the expected structure (legacy data), treat as expired
|
||||||
window.localStorage.removeItem(key);
|
return letsBusterTheStorageBaby();
|
||||||
return typeof defaultValue === 'function' ? (defaultValue as () => T)() : defaultValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the data has expired
|
// Check if the data has expired
|
||||||
|
@ -70,21 +74,20 @@ export function useLocalStorageState<T>(
|
||||||
|
|
||||||
if (timeDifference > expirationTime) {
|
if (timeDifference > expirationTime) {
|
||||||
// Data has expired, remove it and return default value
|
// Data has expired, remove it and return default value
|
||||||
window.localStorage.removeItem(key);
|
return letsBusterTheStorageBaby();
|
||||||
return typeof defaultValue === 'function' ? (defaultValue as () => T)() : defaultValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Data is still valid, deserialize and return the value
|
// Data is still valid, deserialize and return the value
|
||||||
return deserializer(JSON.stringify(storageData.value));
|
const deserializedValue = deserializer(JSON.stringify(storageData.value));
|
||||||
|
|
||||||
|
if (typeof bustStorageOnInit === 'function' && bustStorageOnInit(deserializedValue)) {
|
||||||
|
return letsBusterTheStorageBaby();
|
||||||
|
}
|
||||||
|
|
||||||
|
return deserializedValue;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
onError?.(error);
|
onError?.(error);
|
||||||
// If there's an error, clean up the invalid data and return default
|
return letsBusterTheStorageBaby();
|
||||||
try {
|
|
||||||
window.localStorage.removeItem(key);
|
|
||||||
} catch (cleanupError) {
|
|
||||||
onError?.(cleanupError);
|
|
||||||
}
|
|
||||||
return typeof defaultValue === 'function' ? (defaultValue as () => T)() : defaultValue;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,9 @@ export const ChatLayout: React.FC<ChatSplitterProps> = ({ children }) => {
|
||||||
const rightPanelMaxSize = selectedLayout === 'chat-only' ? '0px' : undefined;
|
const rightPanelMaxSize = selectedLayout === 'chat-only' ? '0px' : undefined;
|
||||||
const renderLeftPanel = selectedLayout !== 'file-only';
|
const renderLeftPanel = selectedLayout !== 'file-only';
|
||||||
const renderRightPanel = selectedLayout !== 'chat-only';
|
const renderRightPanel = selectedLayout !== 'chat-only';
|
||||||
const bustStorageOnInit = selectedLayout === 'chat-only' || selectedLayout === 'file-only';
|
const secondaryFileView = chatLayoutProps.secondaryView;
|
||||||
|
const bustStorageOnInit =
|
||||||
|
selectedLayout === 'chat-only' || selectedLayout === 'file-only' || !!secondaryFileView;
|
||||||
|
|
||||||
useMount(() => {
|
useMount(() => {
|
||||||
setMounted(true); //we need to wait for the app splitter to be mounted because this is nested in the app splitter
|
setMounted(true); //we need to wait for the app splitter to be mounted because this is nested in the app splitter
|
||||||
|
|
|
@ -89,6 +89,13 @@ export const FileContainer: React.FC<FileContainerProps> = ({ children }) => {
|
||||||
}, 20);
|
}, 20);
|
||||||
}, [isOpenSecondary]);
|
}, [isOpenSecondary]);
|
||||||
|
|
||||||
|
const bustStorageOnInit = useMemoizedFn((layout: number | null) => {
|
||||||
|
if (selectedFileViewRenderSecondary && !layout) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return !debouncedSelectedFileViewSecondary;
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AppPageLayout
|
<AppPageLayout
|
||||||
headerSizeVariant="default"
|
headerSizeVariant="default"
|
||||||
|
@ -114,7 +121,7 @@ export const FileContainer: React.FC<FileContainerProps> = ({ children }) => {
|
||||||
preserveSide={'right'}
|
preserveSide={'right'}
|
||||||
rightPanelMinSize={250}
|
rightPanelMinSize={250}
|
||||||
rightPanelMaxSize={385}
|
rightPanelMaxSize={385}
|
||||||
bustStorageOnInit={!debouncedSelectedFileViewSecondary}
|
bustStorageOnInit={bustStorageOnInit}
|
||||||
/>
|
/>
|
||||||
</AppPageLayout>
|
</AppPageLayout>
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue