-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathcategory.ts
125 lines (105 loc) · 2.92 KB
/
category.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
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
import {
changeSyncMapById,
createFilter,
createSyncMap,
deleteSyncMapById,
type FilterStore,
loadValue,
syncMapTemplate
} from '@logux/client'
import { nanoid } from 'nanoid'
import { getClient } from './client.ts'
import type { FeedValue } from './feed.ts'
import { commonMessages as common } from './messages/index.ts'
export type CategoryValue = {
id: string
title: string
}
export type FeedsByCategory = [CategoryValue, FeedValue[]][]
export const Category = syncMapTemplate<CategoryValue>('categories', {
offline: true,
remote: false
})
export function getCategories(): FilterStore<CategoryValue> {
return createFilter(getClient(), Category)
}
export async function loadCategories(): Promise<CategoryValue[]> {
let value = await loadValue(getCategories())
return value.list
}
export async function addCategory(
fields: Omit<CategoryValue, 'id'>
): Promise<string> {
let id = nanoid()
await createSyncMap(getClient(), Category, { id, ...fields })
return id
}
export async function changeCategory(
categoryId: string,
changes: Partial<CategoryValue>
): Promise<void> {
return changeSyncMapById(getClient(), Category, categoryId, changes)
}
export async function deleteCategory(categoryId: string): Promise<void> {
return deleteSyncMapById(getClient(), Category, categoryId)
}
export async function loadCategory(
categoryId: string
): Promise<CategoryValue | undefined> {
return loadValue(Category(categoryId, getClient()))
}
export const GENERAL_CATEGORY: CategoryValue = {
id: 'general',
title: ''
}
export const BROKEN_CATEGORY: CategoryValue = {
id: 'broken',
title: ''
}
export function feedsByCategory(
categories: CategoryValue[],
feeds: FeedValue[]
): FeedsByCategory {
let allCategories = categories.sort((a, b) => {
return a.title.localeCompare(b.title)
})
let general: FeedValue[] = []
let broken: FeedValue[] = []
let byId: Record<string, FeedValue[]> = {
general: []
}
for (let category of allCategories) {
byId[category.id] = []
}
for (let feed of feeds) {
if (feed.categoryId === 'general') {
general.push(feed)
} else if (categories.some(i => i.id === feed.categoryId)) {
byId[feed.categoryId]!.push(feed)
} else {
broken.push(feed)
}
}
let result: [CategoryValue, FeedValue[]][] = allCategories.map(category => {
return [category, byId[category.id]!]
})
if (general.length > 0) {
result.unshift([GENERAL_CATEGORY, general])
}
if (broken.length > 0) {
result.push([BROKEN_CATEGORY, broken])
}
for (let i of result) {
i[1] = i[1].sort((a, b) => a.title.localeCompare(b.title))
}
return result
}
export function getCategoryTitle(category: CategoryValue): string {
if (category.id === 'general') {
return common.value?.generalCategory || category.id
} else if (category.id === 'broken') {
return common.value?.brokenCategory || category.id
} else {
return category.title
}
}