Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
m1k1o
GitHub Repository: m1k1o/neko
Path: blob/master/client/src/store/emoji.ts
1301 views
1
import { getterTree, mutationTree, actionTree } from 'typed-vuex'
2
import { get, set } from '~/utils/localstorage'
3
import { accessor } from '~/store'
4
5
export const namespaced = true
6
7
interface Group {
8
name: string
9
id: string
10
list: string[]
11
}
12
13
interface Keywords {
14
[name: string]: string[]
15
}
16
17
interface Emojis {
18
groups: Group[]
19
keywords: Keywords
20
list: string[]
21
}
22
23
export const state = () => ({
24
groups: [
25
{
26
id: 'recent',
27
name: 'Recent',
28
list: JSON.parse(get('emoji_recent', '[]')) as string[],
29
},
30
] as Group[],
31
keywords: {} as Keywords,
32
list: [] as string[],
33
})
34
35
export const getters = getterTree(state, {})
36
37
export const mutations = mutationTree(state, {
38
setRecent(state, emoji: string) {
39
if (!state.groups[0].list.includes(emoji)) {
40
if (state.groups[0].list.length > 30) {
41
state.groups[0].list.shift()
42
}
43
state.groups[0].list.push(emoji)
44
set('emoji_recent', JSON.stringify(state.groups[0].list))
45
}
46
},
47
addGroup(state, group: Group) {
48
state.groups.push(group)
49
},
50
setKeywords(state, keywords: Keywords) {
51
state.keywords = keywords
52
},
53
setList(state, list: string[]) {
54
state.list = list
55
},
56
})
57
58
export const actions = actionTree(
59
{ state, getters, mutations },
60
{
61
async initialise() {
62
try {
63
const req = await $http.get<Emojis>('emoji.json')
64
for (const group of req.data.groups) {
65
accessor.emoji.addGroup(group)
66
}
67
accessor.emoji.setList(req.data.list)
68
accessor.emoji.setKeywords(req.data.keywords)
69
} catch (err: any) {
70
console.error(err)
71
}
72
},
73
},
74
)
75
76