Merged in fix/nullcheck-localStorage (pull request #2875)

fix(BOOK-397): localStorage could be undefined

* fix(BOOK-397): localStorage could be undefined


Approved-by: Linus Flood
This commit is contained in:
Joakim Jäderberg
2025-09-26 12:59:50 +00:00
parent 7bdcfc7681
commit 50bac104fc

View File

@@ -13,7 +13,7 @@ export function useSearchHistory() {
const KEY = useSearchHistoryKey()
const getHistoryFromLocalStorage = useCallback((): AutoCompleteLocation[] => {
const stringifiedHistory = localStorage.getItem(KEY)
const stringifiedHistory = window.localStorage?.getItem(KEY)
try {
const parsedHistory = JSON.parse(stringifiedHistory ?? "[]")
@@ -27,7 +27,7 @@ export function useSearchHistory() {
return existingHistory
} catch (error) {
logger.error("Failed to parse search history:", error)
localStorage.removeItem(KEY)
window.localStorage?.removeItem(KEY)
return []
}
@@ -47,7 +47,8 @@ export function useSearchHistory() {
newItem,
...oldSearchHistoryWithoutTheNew.slice(0, MAX_HISTORY_LENGTH - 1),
]
localStorage.setItem(KEY, JSON.stringify(updatedSearchHistory))
window.localStorage?.setItem(KEY, JSON.stringify(updatedSearchHistory))
return updatedSearchHistory
}
@@ -59,7 +60,8 @@ export function useSearchHistory() {
}, [KEY, getHistoryFromLocalStorage])
function clearHistory() {
localStorage.removeItem(KEY)
window.localStorage?.removeItem(KEY)
setSearchHistory([])
}