1
0
Fork 0
docsearch/packages/docsearch-react/src/utils/storage.ts
Dylan Tientcheu 19c1ef9192
feat(v4): release v4 (#2555) (#2666)
* feat(v4): add new UI (#2555)

* feat: add new footer ui

* feat: add new search box ui

* feat: add searchbox actions

* feat: add start screen

* feat: add no results screen

* feat: add new card ui

* feat: add new icons

* feat: add new key press class

* fix: cypress tests

* fix: circleci

* fix: circleci

* fix: close aria label

* fix: circleci

* fix: remove unused classes

* fix: circleci

* feat: update version

* feat: add new dark mode ui

* fix: colors

* fix: design review

* fix: hit title length

* fix: improve accessibility

* fix: ci

* chore: increase bundle size threshold

* fix: css

* feat(v4): ask-ai foundations  (#2574)

* feat(v4): docsearch askAI context (#2587)

* fix(v4): ask ai updates (#2654)

* feat(v4): update beta documentation (#2659)

* chore: release v4.0.0-beta.0 (#2660)

* fix: make release run on branches like v4

* chore: release v4.0.0-beta.0

* hotfix: closing on askai error

* chore: release v4.0.0-beta.1 (#2661)

* fix: add a section on models

* fix: update to `<package>@beta`

* feat(docsearch-website): Updated docs (#2662)

* feat(v4): update the landing page (#2665)

* fix: beta in readme

* feat: added glow around the search bar and a little copy above the keyboard

* feat: add glow around the search bar and copy above the keyboard

* fix: Add more styling

* fix: polishing v4 (#2667)

* chore: release v4.0.0-beta.2 (#2668)

* fix: update docusaurus tarballs

* fix(website): update docusaurus tarballs

* fix: mobile search bar

---------

Co-authored-by: Vasco Bettencourt <32492444+vascobettencourt@users.noreply.github.com>
Co-authored-by: Natan Yagudayev <natanyagudayev@gmail.com>
2025-07-17 15:53:46 +02:00

89 lines
2.5 KiB
TypeScript

/**
* Checks if local storage is available and usable.
*/
export function isLocalStorageSupported(): boolean {
const key = '__TEST_KEY__';
try {
localStorage.setItem(key, '');
localStorage.removeItem(key);
return true;
} catch {
return false;
}
}
/**
* Creates a simple storage interface for arrays using localstorage.
* Provides basic getitem and setitem functionality.
* Falls back to a no-op implementation if localstorage is not supported..
*
* @template titem The type of items to store.
* @param key - The localstorage key to use.
* @returns An object with setitem and getitem methods.
*/
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function createStorage<TItem>(key: string) {
if (isLocalStorageSupported() === false) {
return {
setItem(): void {},
getItem(): TItem[] {
return [];
},
};
}
return {
setItem(item: TItem[]): void {
return window.localStorage.setItem(key, JSON.stringify(item));
},
getItem(): TItem[] {
const item = window.localStorage.getItem(key);
return item ? JSON.parse(item) : [];
},
};
}
/**
* Creates a simple storage interface for a single object using localstorage.
* Provides basic getitem, setitem, and removeitem functionality.
* Falls back to a no-op implementation if localstorage is not supported.
*
* @template titem The type of the object to store.
* @param key - The localstorage key to use.
* @returns An object with setitem, getitem, and removeitem methods.
*/
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function createObjectStorage<TItem>(key: string) {
if (isLocalStorageSupported() === false) {
return {
setItem(_item: TItem | null): void {},
getItem(): TItem | null {
return null;
},
removeItem(): void {},
};
}
return {
setItem(item: TItem | null): void {
if (item === null) {
window.localStorage.removeItem(key);
} else {
window.localStorage.setItem(key, JSON.stringify(item));
}
},
getItem(): TItem | null {
const item = window.localStorage.getItem(key);
try {
return item ? (JSON.parse(item) as TItem) : null;
} catch {
// handle potential JSON parsing errors, e.g., corrupted data
window.localStorage.removeItem(key); // clear corrupted data
return null;
}
},
removeItem(): void {
window.localStorage.removeItem(key);
},
};
}