Files
hnh-map/frontend-nuxt/composables/useRecentLocations.ts
Nikolay Tatarinov 7f990c0c11 Enhance MapView component with new features and icons
- Added fullscreen and measurement mode buttons for improved map interaction.
- Introduced a bookmarks section to save and navigate to locations easily.
- Implemented a search feature for quick access to coordinates and markers.
- Added keyboard shortcuts overlay for enhanced usability.
- Refactored MapControls and MapControlsContent to support new functionalities.
- Introduced new icon components for better visual representation in the UI.
2026-03-01 16:00:25 +03:00

64 lines
1.3 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 (_) {}
}
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,
}
}