Initial working

This commit is contained in:
Jack Hadrill
2022-03-19 21:47:54 +00:00
commit b1977447a2
28 changed files with 2217 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
import { defineStore } from 'pinia'
class Key {
constructor(base64) {
this.base64 = base64
this.raw = Uint8Array.from(window.atob(base64), c => c.charCodeAt(0))
}
}
export const useChannelStore = defineStore({
id: 'channelStore',
state: () => ({
channels: [
{
id: 'a798423c-3fbf-434d-9a00-57d7642bff65',
name: 'Default',
key: new Key('')
},
{
id: '47198ee7-8bf4-41fe-8b42-bb001bd5f140',
name: 'HackerNews',
key: new Key('HACKERNEWQAAAAAAAAAAAA==')
}
]
}),
getters: {
getChannelById: (state) => {
return (channelId) => state.channels.find((channel) => channel.id === channelId)
}
},
actions: {
addChannel(channel) {
this.channels.push(channel)
}
}
})

View File

@@ -0,0 +1,14 @@
import { defineStore } from 'pinia'
export const useMercuryStore = defineStore({
id: 'mercuryStore',
state: () => ({
connected: false
}),
getters: {},
actions: {
setConnectionState(state) {
this.connected = state
}
}
})

View File

@@ -0,0 +1,25 @@
import { v4 as uuidv4 } from 'uuid'
import { defineStore } from 'pinia'
export const useMessageStore = defineStore({
id: 'messageStore',
state: () => ({
messages: []
}),
getters: {
getMessagesByChannelId: (state) => {
return (channelId) => state.messages.filter((message) => message.channelId === channelId)
}
},
actions: {
addMessage(channelId, senderId, content) {
this.messages.push({
id: uuidv4(),
channelId: channelId,
userId: senderId,
content: content,
received: new Date()
})
}
}
})

57
src/stores/userStore.js Normal file
View File

@@ -0,0 +1,57 @@
import Color from 'color'
import { defineStore } from 'pinia'
export const useUserStore = defineStore({
id: 'userStore',
state: () => ({
users: [
{
id: -1,
name: 'Mercury',
color: Color('#ff4000'),
channels: [],
self: true
}
]
}),
getters: {
getSelf: (state) => {
return (self) => state.users.find(user => user.self === true)
},
getUserById: (state) => {
return (id) => state.users.find(user => user.id === id)
},
getUsersByChannelId: (state) => {
return (channelId) => state.users.filter(user => { return user.channels.includes(channelId) })
}
},
actions: {
updateUser(newUser) {
var user = this.users.find(user => user.id == newUser.id)
if (!user) {
user = {
id: newUser.id,
name: String(newUser.id),
color: Color('#000000'),
channels: []
}
this.users.push(user)
}
user.lastSeen = new Date()
},
setName(id, name) {
var user = this.users.find(user => user.id == id)
user.name = name
},
setColor(id, color) {
var user = this.users.find(user => user.id == id)
user.color = color
},
addChannel(id, channelId) {
var user = this.users.find(user => user.id == id)
if (!user.channels.includes(channelId)) {
user.channels.push(channelId)
}
}
}
})