From 50bac104fcf52a788eedcdaabdc142b5ec2244fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20J=C3=A4derberg?= Date: Fri, 26 Sep 2025 12:59:50 +0000 Subject: [PATCH] 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 --- packages/booking-flow/lib/hooks/useSearchHistory.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/booking-flow/lib/hooks/useSearchHistory.ts b/packages/booking-flow/lib/hooks/useSearchHistory.ts index 88de5b6f2..4d4971579 100644 --- a/packages/booking-flow/lib/hooks/useSearchHistory.ts +++ b/packages/booking-flow/lib/hooks/useSearchHistory.ts @@ -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([]) }