neko/client/src/store/emoji.ts

75 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-02-02 09:35:48 +13:00
import { getterTree, mutationTree, actionTree } from 'typed-vuex'
import { get, set } from '~/utils/localstorage'
import { accessor } from '~/store'
export const namespaced = true
interface Group {
name: string
id: string
list: string[]
}
interface Keywords {
[name: string]: string[]
}
interface Emojis {
groups: Group[]
keywords: Keywords
list: string[]
}
export const state = () => ({
groups: [
{
id: 'recent',
name: 'Recent',
list: JSON.parse(get('emoji_recent', '[]')) as string[],
},
] as Group[],
keywords: {} as Keywords,
list: [] as string[],
})
export const getters = getterTree(state, {})
export const mutations = mutationTree(state, {
setRecent(state, emoji: string) {
if (!state.groups[0].list.includes(emoji)) {
if (state.groups[0].list.length > 30) {
state.groups[0].list.shift()
}
state.groups[0].list.push(emoji)
set('emoji_recent', JSON.stringify(state.groups[0].list))
}
},
addGroup(state, group: Group) {
state.groups.push(group)
},
setKeywords(state, keywords: Keywords) {
state.keywords = keywords
},
setList(state, list: string[]) {
state.list = list
},
})
export const actions = actionTree(
{ state, getters, mutations },
{
2021-09-01 03:58:32 +12:00
async initialise() {
try {
const req = await $http.get<Emojis>('emoji.json')
for (const group of req.data.groups) {
accessor.emoji.addGroup(group)
}
accessor.emoji.setList(req.data.list)
accessor.emoji.setKeywords(req.data.keywords)
2021-09-01 04:25:06 +12:00
} catch (err: any) {
2021-09-01 03:58:32 +12:00
console.error(err)
}
2020-02-02 09:35:48 +13:00
},
},
)