/
App.vue
163 lines (143 loc) · 3.97 KB
/
App.vue
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<script lang="ts">
export default {
name: 'HistoireApp',
}
</script>
<script lang="ts" setup>
// @ts-expect-error virtual module
import { files as rawFiles, tree as rawTree, onUpdate } from 'virtual:$histoire-stories'
import StoryList from './components/tree/StoryList.vue'
import BaseSplitPane from './components/base/BaseSplitPane.vue'
import { computed, onMounted, ref, watch } from 'vue'
import AppHeader from './components/app/AppHeader.vue'
import type { StoryFile, Tree } from './types'
import { useStoryStore } from './stores/story'
import { mapFile } from './util/mapping'
import { useTitle } from '@vueuse/core'
import { histoireConfig } from './util/config'
import { onKeyboardShortcut } from './util/keyboard'
import { isMobile } from './util/responsive'
import Breadcrumb from './components/app/Breadcrumb.vue'
import SearchModal from './components/search/SearchModal.vue'
import InitialLoading from './components/app/InitialLoading.vue'
import GenericMountStory from './components/story/GenericMountStory.vue'
const files = ref<StoryFile[]>(rawFiles.map(file => mapFile(file)))
const tree = ref<Tree>(rawTree)
onUpdate((newFiles: StoryFile[], newTree: Tree) => {
loading.value = false
files.value = newFiles.map(file => {
const existingFile = files.value.find(f => f.id === file.id)
return mapFile(file, existingFile)
})
tree.value = newTree
})
const stories = computed(() => files.value.reduce((acc, file) => {
acc.push(file.story)
return acc
}, []))
// Store
const storyStore = useStoryStore()
watch(stories, value => {
storyStore.setStories(value)
}, {
immediate: true,
})
useTitle(computed(() => {
if (storyStore.currentStory) {
let title = storyStore.currentStory.title
if (storyStore.currentVariant) {
title += ` › ${storyStore.currentVariant.title}`
}
return `${title} | ${histoireConfig.theme.title}`
}
return histoireConfig.theme.title
}))
// Search
const loadSearch = ref(false)
const isSearchOpen = ref(false)
watch(isSearchOpen, value => {
if (value) {
loadSearch.value = true
}
})
onKeyboardShortcut(['ctrl+k', 'meta+k'], (event) => {
isSearchOpen.value = true
event.preventDefault()
})
const loading = ref(false)
if (import.meta.hot && !rawFiles.length) {
loading.value = true
import.meta.hot.on('histoire:all-stories-loaded', () => {
loading.value = false
})
}
const mounted = ref(false)
onMounted(() => {
mounted.value = true
})
</script>
<template>
<div
v-if="storyStore.currentStory"
class="histoire-app htw-hidden"
>
<GenericMountStory
:key="storyStore.currentStory.id"
:story="storyStore.currentStory"
/>
</div>
<div
class="htw-h-screen htw-bg-white dark:htw-bg-gray-700 dark:htw-text-gray-100"
:style="{
// Prevent flash of content
opacity: mounted && !loading ? 1 : 0,
}"
>
<div
v-if="isMobile"
class="htw-h-full htw-flex htw-flex-col htw-divide-y htw-divide-gray-100 dark:htw-divide-gray-800"
>
<AppHeader @search="isSearchOpen = true" />
<Breadcrumb
:tree="tree"
:stories="stories"
/>
<RouterView class="htw-grow" />
</div>
<BaseSplitPane
v-else
save-id="main-horiz"
:min="5"
:max="50"
:default-split="15"
class="htw-h-full"
>
<template #first>
<div class="htw-flex htw-flex-col htw-h-full htw-bg-gray-100 dark:htw-bg-gray-750 __histoire-pane-shadow-from-right">
<AppHeader
class="htw-flex-none"
@search="isSearchOpen = true"
/>
<StoryList
:tree="tree"
:stories="stories"
class="htw-flex-1"
/>
</div>
</template>
<template #last>
<RouterView />
</template>
</BaseSplitPane>
<SearchModal
v-if="loadSearch"
:shown="isSearchOpen"
@close="isSearchOpen = false"
/>
</div>
<transition name="__histoire-fade">
<InitialLoading
v-if="loading"
/>
</transition>
</template>