-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuseMapState.ts
More file actions
77 lines (69 loc) · 2.45 KB
/
Copy pathuseMapState.ts
File metadata and controls
77 lines (69 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { useState } from '#imports'
import type { FilterState, MapBounds, MapFeature, MapFilterOptions, MapItem, UserLocation } from '../types/map'
const defaultFilters = (): FilterState => ({
type: 'badestelle',
category: '',
infrastructure: '',
})
const defaultFilterOptions = (): MapFilterOptions => ({
types: ['badestelle', 'poi'],
categories: [],
cities: [],
tags: [],
infrastructures: [],
})
export function useMapState() {
const selectedItem = useState<MapItem | null>('map:selected-item', () => null)
const selectedSlug = useState<string | null>('map:selected-slug', () => null)
const currentMarkers = useState<MapFeature[]>('map:current-markers', () => [])
const currentBounds = useState<MapBounds | null>('map:current-bounds', () => null)
const currentUserLocation = useState<UserLocation | null>('map:user-location', () => null)
const filters = useState<FilterState>('map:filters', defaultFilters)
const availableFilters = useState<MapFilterOptions>('map:available-filters', defaultFilterOptions)
const isSidebarOpen = useState<boolean>('map:sidebar-open', () => false)
const isBottomSheetOpen = useState<boolean>('map:bottomsheet-open', () => true)
const isLoading = useState<boolean>('map:is-loading', () => false)
const isSearching = useState<boolean>('map:is-searching', () => false)
const fetchError = useState<string | null>('map:fetch-error', () => null)
const queryMode = useState<'bounds' | 'radius'>('map:query-mode', () => 'bounds')
const searchQuery = useState<string>('map:search-query', () => '')
const searchResults = useState<MapItem[]>('map:search-results', () => [])
const searchTotal = useState<number>('map:search-total', () => 0)
function setSelectedItem(item: MapItem | null) {
selectedItem.value = item
selectedSlug.value = item?.slug ?? null
isSidebarOpen.value = !!item
isBottomSheetOpen.value = true
}
function clearSelection() {
setSelectedItem(null)
}
function openBottomSheet() {
isBottomSheetOpen.value = true
}
function closeBottomSheet() {
isBottomSheetOpen.value = false
}
return {
selectedItem,
selectedSlug,
currentMarkers,
currentBounds,
currentUserLocation,
filters,
availableFilters,
isSidebarOpen,
isBottomSheetOpen,
isLoading,
isSearching,
fetchError,
queryMode,
searchQuery,
searchResults,
searchTotal,
setSelectedItem,
clearSelection,
openBottomSheet,
closeBottomSheet,
}
}