Files
hnh-map/frontend-nuxt/composables/useRecentLocations.ts
Nikolay Tatarinov fd624c2357 Refactor frontend components for improved functionality and accessibility
- Consolidated global error handling in app.vue to redirect users to the login page on API authentication failure.
- Enhanced MapView component by reintroducing event listeners for selected map and marker updates, improving interactivity.
- Updated PasswordInput and various modal components to ensure proper input handling and accessibility compliance.
- Refactored MapControls and MapControlsContent to streamline prop management and enhance user experience.
- Improved error handling in local storage operations within useMapBookmarks and useRecentLocations composables.
- Standardized input elements across forms for consistency in user interaction.
2026-03-04 14:06:27 +03:00

64 lines
1.4 KiB
TypeScript

export interface RecentLocation {
mapId: number
x: number
y: number
zoom?: number
label?: string
at: number
}
const STORAGE_KEY = 'hnh-map-recent-locations'
const MAX_RECENT = 10
function loadRecent(): RecentLocation[] {
if (import.meta.server) return []
try {
const raw = localStorage.getItem(STORAGE_KEY)
if (!raw) return []
const parsed = JSON.parse(raw) as RecentLocation[]
return Array.isArray(parsed) ? parsed.slice(0, MAX_RECENT) : []
} catch {
return []
}
}
function saveRecent(list: RecentLocation[]) {
if (import.meta.server) return
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(list.slice(0, MAX_RECENT)))
} catch { /* ignore */ }
}
export function useRecentLocations() {
const recent = ref<RecentLocation[]>([])
function refresh() {
recent.value = loadRecent()
}
function push(entry: Omit<RecentLocation, 'at'>) {
const list = loadRecent()
const at = Date.now()
const filtered = list.filter(
(r) => !(r.mapId === entry.mapId && r.x === entry.x && r.y === entry.y)
)
filtered.unshift({ ...entry, at })
saveRecent(filtered)
refresh()
}
function clear() {
saveRecent([])
refresh()
}
onMounted(refresh)
return {
recent: readonly(recent),
refresh,
push,
clear,
}
}