11<script setup lang="ts">
22import { storeToRefs } from ' pinia'
3- import { computed , ref , onMounted , onUnmounted } from ' vue'
3+ import { computed , ref , onMounted , onUnmounted , watch } from ' vue'
44import { useFacetStore } from ' ../stores/facets'
55import { useSessionStore } from ' ../stores/session'
66import { useSearchStore } from ' ../stores/search'
@@ -16,21 +16,41 @@ const { facets } = storeToRefs(useFacetStore())
1616const { isAdmin } = storeToRefs (useSessionStore ())
1717
1818const search = useSearchStore ()
19- const facetsOpen = ref ( false )
19+
2020const isMobile = ref (window .innerWidth <= 700 )
21+ const savedStatus = loadFacetsOpen ()
22+ const facetsOpen = ref (
23+ savedStatus !== null ? savedStatus : ! isMobile .value // first time or localStorage not availabe: mobile close, desktop open
24+ )
25+
26+ watch (facetsOpen , (val ) => {
27+ saveFacetsOpen (val )
28+ })
29+
30+ function saveFacetsOpen(value : boolean ) {
31+ try {
32+ localStorage .setItem (' facetsOpen' , String (value ))
33+ } catch (e ) {
34+ console .warn (' Could not save facetsOpen to localStorage.' , e )
35+ }
36+ }
37+
38+ function loadFacetsOpen(): boolean | null {
39+ try {
40+ const status = localStorage .getItem (' facetsOpen' )
41+ return status !== null ? status === ' true' : null
42+ } catch (e ) {
43+ console .warn (' Could not read facetsOpen from localStorage' , e )
44+ return null
45+ }
46+ }
2147
2248function toggleFacets() {
2349 facetsOpen .value = ! facetsOpen .value
2450}
2551
2652function handleWindowResize() {
2753 isMobile .value = window .innerWidth <= 700
28- if (isMobile .value ) {
29- facetsOpen .value = false
30- }
31- else {
32- facetsOpen .value = true
33- }
3454}
3555
3656onMounted (() => {
0 commit comments