forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.ts
More file actions
28 lines (24 loc) · 1.04 KB
/
context.ts
File metadata and controls
28 lines (24 loc) · 1.04 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
import { useProvide, useProvider, useRequest } from "@hooks"
import cateService from "@service/cate-service"
import { toMap } from '@util/array'
import { CATE_NOT_SET_ID } from '@util/site'
import { computed, type Ref } from "vue"
import { t } from './locale'
type AppContextValue = {
categories: Ref<timer.site.Cate[]>
refreshCategories: () => void
cateNameMap: Ref<Record<number, string>>
}
const NAMESPACE = '_'
export const initAppContext = () => {
const { data: categories, refresh: refreshCategories } = useRequest(() => cateService.listAll(), { defaultValue: [] })
const cateNameMap = computed(() => {
const map = toMap(categories.value, c => c.id, c => c.name)
map[CATE_NOT_SET_ID] = t(msg => msg.shared.cate.notSet)
return map
})
useProvide<AppContextValue>(NAMESPACE, { categories, refreshCategories, cateNameMap })
}
export const useCategories = () => useProvider<AppContextValue, "categories" | "refreshCategories" | "cateNameMap">(
NAMESPACE, "categories", "refreshCategories", "cateNameMap"
)