Merged in fix/SW-2668-tracking-room-details-missing (pull request #2015)
fix(SW-2668): added search term and room details to tracking * fix(SW-2668): added search term and room details to tracking * fix: change to optional type Approved-by: Christian Andolf Approved-by: Matilda Landström
This commit is contained in:
86
apps/scandic-web/hooks/useSearchHistory.ts
Normal file
86
apps/scandic-web/hooks/useSearchHistory.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import { useCallback, useEffect, useState } from "react"
|
||||
|
||||
import {
|
||||
type AutoCompleteLocation,
|
||||
autoCompleteLocationSchema,
|
||||
} from "@/server/routers/autocomplete/schema"
|
||||
|
||||
import useLang from "@/hooks/useLang"
|
||||
|
||||
export function useSearchHistory() {
|
||||
const MAX_HISTORY_LENGTH = 5
|
||||
const KEY = useSearchHistoryKey()
|
||||
|
||||
const getHistoryFromLocalStorage = useCallback((): AutoCompleteLocation[] => {
|
||||
const stringifiedHistory = localStorage.getItem(KEY)
|
||||
|
||||
try {
|
||||
const parsedHistory = JSON.parse(stringifiedHistory ?? "[]")
|
||||
if (!Array.isArray(parsedHistory)) {
|
||||
throw new Error("Invalid search history format")
|
||||
}
|
||||
const existingHistory = parsedHistory.map((item) =>
|
||||
autoCompleteLocationSchema.parse(item)
|
||||
)
|
||||
|
||||
return existingHistory
|
||||
} catch (error) {
|
||||
console.error("Failed to parse search history:", error)
|
||||
localStorage.removeItem(KEY)
|
||||
|
||||
return []
|
||||
}
|
||||
}, [KEY])
|
||||
|
||||
function updateSearchHistory(newItem: AutoCompleteLocation) {
|
||||
const existingHistory = getHistoryFromLocalStorage()
|
||||
if (!autoCompleteLocationSchema.safeParse(newItem).success) {
|
||||
return existingHistory
|
||||
}
|
||||
|
||||
const oldSearchHistoryWithoutTheNew = existingHistory.filter(
|
||||
(h) => h.type !== newItem.type || h.id !== newItem.id
|
||||
)
|
||||
|
||||
const updatedSearchHistory = [
|
||||
newItem,
|
||||
...oldSearchHistoryWithoutTheNew.slice(0, MAX_HISTORY_LENGTH - 1),
|
||||
]
|
||||
localStorage.setItem(KEY, JSON.stringify(updatedSearchHistory))
|
||||
|
||||
return updatedSearchHistory
|
||||
}
|
||||
|
||||
const [searchHistory, setSearchHistory] = useState<AutoCompleteLocation[]>([])
|
||||
|
||||
useEffect(() => {
|
||||
setSearchHistory(getHistoryFromLocalStorage())
|
||||
}, [KEY, getHistoryFromLocalStorage])
|
||||
|
||||
function clearHistory() {
|
||||
localStorage.removeItem(KEY)
|
||||
setSearchHistory([])
|
||||
}
|
||||
|
||||
function insertSearchHistoryItem(
|
||||
newItem: AutoCompleteLocation
|
||||
): AutoCompleteLocation[] {
|
||||
const updatedHistory = updateSearchHistory(newItem)
|
||||
setSearchHistory(updatedHistory)
|
||||
|
||||
return updatedHistory
|
||||
}
|
||||
|
||||
return {
|
||||
searchHistory,
|
||||
insertSearchHistoryItem,
|
||||
clearHistory,
|
||||
}
|
||||
}
|
||||
|
||||
function useSearchHistoryKey() {
|
||||
const SEARCH_HISTORY_LOCALSTORAGE_KEY = "searchHistory"
|
||||
const lang = useLang()
|
||||
|
||||
return `${SEARCH_HISTORY_LOCALSTORAGE_KEY}-${lang}`.toLowerCase()
|
||||
}
|
||||
Reference in New Issue
Block a user