Skip to content

Instantly share code, notes, and snippets.

@x8BitRain
Created August 16, 2023 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save x8BitRain/f791c431acb6096a341a25f7ddb7e4ce to your computer and use it in GitHub Desktop.
Save x8BitRain/f791c431acb6096a341a25f7ddb7e4ce to your computer and use it in GitHub Desktop.
import { acceptHMRUpdate, defineStore } from 'pinia'
import { computed, ref } from 'vue'
import type { Room } from '/@/interfaces'
import { hideFaviconNotification } from '/@/utils/toggleFaviconNotification'
interface Rooms {
[key: string]: Room
}
export const useRoomsStore = defineStore('rooms', () => {
const rooms = ref<Rooms>({})
const currentRoomId = ref('')
const roomsWithUnseenPastes = ref<string[]>([])
const showRoomSettings = ref(false)
const showCreateRoom = ref(false)
const currentRoom = computed(() => rooms.value[currentRoomId.value])
const currentRoomHasUnseenPastes = computed(() => {
return roomsWithUnseenPastes.value?.includes(currentRoomId.value)
})
const setActiveRoom = (roomId: string) => {
currentRoomId.value = roomId
localStorage.setItem('pasta:currentRoomId', roomId)
}
const addRoomToUnseenPastes = (roomId: string) => {
roomsWithUnseenPastes.value.push(roomId)
}
const removeRoomFromUnseenPaste = (roomId: string) => {
if (roomsWithUnseenPastes.value.includes(roomId)) {
roomsWithUnseenPastes.value = roomsWithUnseenPastes.value.filter(
(item) => item !== roomId
)
hideFaviconNotification()
}
}
return {
rooms,
currentRoomId,
currentRoom,
roomsWithUnseenPastes,
showRoomSettings,
showCreateRoom,
currentRoomHasUnseenPastes,
setActiveRoom,
addRoomToUnseenPastes,
removeRoomFromUnseenPaste,
}
})
if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useRoomsStore, import.meta.hot))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment