neko/client/src/store/remote.ts

138 lines
2.8 KiB
TypeScript
Raw Normal View History

2020-01-21 03:36:18 +13:00
import { getterTree, mutationTree, actionTree } from 'typed-vuex'
2020-01-24 04:23:26 +13:00
import { Member } from '~/neko/types'
import { EVENT } from '~/neko/events'
2020-01-23 06:16:40 +13:00
import { accessor } from '~/store'
2020-01-21 03:36:18 +13:00
export const namespaced = true
export const state = () => ({
id: '',
2020-01-26 03:29:52 +13:00
clipboard: '',
2020-02-12 12:51:57 +13:00
locked: false,
2020-01-21 03:36:18 +13:00
})
export const getters = getterTree(state, {
hosting: (state, getters, root) => {
return root.user.id === state.id
},
2020-01-23 06:16:40 +13:00
hosted: (state, getters, root) => {
return state.id !== ''
},
2020-01-21 03:36:18 +13:00
host: (state, getters, root) => {
return root.user.member[state.id] || null
},
})
export const mutations = mutationTree(state, {
setHost(state, host: string | Member) {
if (typeof host === 'string') {
state.id = host
} else {
state.id = host.id
}
},
2020-01-24 04:23:26 +13:00
2020-01-26 03:29:52 +13:00
setClipboard(state, clipboard: string) {
state.clipboard = clipboard
},
2020-02-12 12:51:57 +13:00
setLocked(state, locked: boolean) {
state.locked = locked
},
reset(state) {
2020-01-24 04:23:26 +13:00
state.id = ''
2020-01-26 03:29:52 +13:00
state.clipboard = ''
2020-02-12 12:51:57 +13:00
state.locked = false
2020-01-24 04:23:26 +13:00
},
2020-01-21 03:36:18 +13:00
})
export const actions = actionTree(
{ state, getters, mutations },
{
2020-01-26 03:29:52 +13:00
sendClipboard({ getters }, clipboard: string) {
if (!accessor.connected || !getters.hosting) {
return
}
$client.sendMessage(EVENT.CONTROL.CLIPBOARD, { text: clipboard })
},
2020-01-23 06:16:40 +13:00
toggle({ getters }) {
if (!accessor.connected) {
return
}
if (!getters.hosting) {
2020-06-16 08:26:47 +12:00
$client.sendMessage(EVENT.CONTROL.REQUEST, { keyboard_layout: accessor.settings.keyboard_layout })
2020-01-23 06:16:40 +13:00
} else {
$client.sendMessage(EVENT.CONTROL.RELEASE)
}
},
request({ getters }) {
if (!accessor.connected || !getters.hosting) {
return
}
$client.sendMessage(EVENT.CONTROL.REQUEST)
},
release({ getters }) {
if (!accessor.connected || getters.hosting) {
return
}
$client.sendMessage(EVENT.CONTROL.RELEASE)
},
2020-01-24 04:23:26 +13:00
give({ getters }, member: string | Member) {
if (!accessor.connected || !getters.hosting) {
return
}
if (typeof member === 'string') {
member = accessor.user.members[member]
}
if (!member) {
return
}
$client.sendMessage(EVENT.CONTROL.GIVE, { id: member.id })
},
2020-01-23 06:16:40 +13:00
adminControl() {
if (!accessor.connected || !accessor.user.admin) {
return
}
$client.sendMessage(EVENT.ADMIN.CONTROL)
},
adminRelease() {
if (!accessor.connected || !accessor.user.admin) {
return
}
$client.sendMessage(EVENT.ADMIN.RELEASE)
},
2020-01-24 04:23:26 +13:00
adminGive({ getters }, member: string | Member) {
if (!accessor.connected) {
return
}
if (typeof member === 'string') {
member = accessor.user.members[member]
}
if (!member) {
return
}
$client.sendMessage(EVENT.ADMIN.GIVE, { id: member.id })
},
2020-01-21 03:36:18 +13:00
},
)