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