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([]) }