/
use-search-type.ts
96 lines (79 loc) · 2.19 KB
/
use-search-type.ts
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
import { computed, ref } from "vue"
import {
ALL_MEDIA,
AUDIO,
IMAGE,
MODEL_3D,
VIDEO,
additionalSearchTypes,
supportedSearchTypes,
SearchType,
} from "~/constants/media"
import { useSearchStore } from "~/stores/search"
import { useFeatureFlagStore } from "~/stores/feature-flag"
import { useI18n } from "~/composables/use-i18n"
import { useAnalytics } from "~/composables/use-analytics"
import { useComponentName } from "./use-component-name"
const icons = {
[ALL_MEDIA]: "all",
[AUDIO]: "audio",
[IMAGE]: "image",
[VIDEO]: "video",
[MODEL_3D]: "model-3d",
} as const
const labels = {
[ALL_MEDIA]: "searchType.all",
[IMAGE]: "searchType.image",
[AUDIO]: "searchType.audio",
[VIDEO]: "searchType.video",
[MODEL_3D]: "searchType.model3d",
} as const
export default function useSearchType() {
const i18n = useI18n()
const componentName = useComponentName()
const analytics = useAnalytics()
const activeType = computed(() => useSearchStore().searchType)
const previousSearchType = ref(activeType.value)
const featureFlagStore = useFeatureFlagStore()
const additionalTypes = computed(() =>
featureFlagStore.isOn("additional_search_types")
? additionalSearchTypes
: []
)
const searchTypes = [...supportedSearchTypes]
const setActiveType = (searchType: SearchType) => {
if (previousSearchType.value === searchType) {
return
}
analytics.sendCustomEvent("CHANGE_CONTENT_TYPE", {
previous: previousSearchType.value,
next: searchType,
component: componentName,
})
// `setActiveType` is called after the search middleware
// ran and updated the search store state
// TODO: Figure out why
if (activeType.value === searchType) {
return
}
useSearchStore().setSearchType(searchType)
previousSearchType.value = searchType
}
const getSearchTypeProps = (searchType?: SearchType) => {
const type = searchType ?? activeType.value
return {
label: i18n.t(labels[type]).toString(),
icon: icons[type],
searchType: type,
}
}
return {
setActiveType,
activeType,
getSearchTypeProps,
types: searchTypes,
icons,
labels,
additionalTypes,
}
}