Skip to content

Commit

Permalink
Realized event system is trash, commonCalls is good
Browse files Browse the repository at this point in the history
  • Loading branch information
kadiryazici committed Mar 19, 2024
1 parent 1599a23 commit cc5b8e4
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 189 deletions.
3 changes: 1 addition & 2 deletions .vscode/settings.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
"markdown",
"json",
"jsonc",
"yaml",
"toml"
"yaml"
],
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer"
Expand Down
146 changes: 3 additions & 143 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
<script setup lang="ts">
import { onMounted, watchEffect } from 'vue'
import { open } from '@tauri-apps/api/shell'
import { invoke } from '@tauri-apps/api'
import { sendNotification } from '@tauri-apps/api/notification'
import AppScroller from './components/AppScroller.vue'
import AppSidebar from './components/AppSidebar.vue'
import HomePage from './pages/HomePage.vue'
import SettingsPage from './pages/SettingsPage.vue'
import { ColorPreference, FETCH_INTERVAL_DURATION, InvokeCommand } from './constants'
import { useStore } from './stores/store'
import LandingPage from './pages/LandingPage.vue'
import { useInterval } from './composables/useInterval'
import { AppStorage } from './storage'
import ContextMenu from './components/ContextMenu.vue'
import { useTheme } from './composables/useTheme'
import { Page, useRoute } from './composables/useRoute'
import { useContextMenu } from './composables/useContextMenu'
import { useAppHooks } from './composables/useAppHooks'
import { filterNewNotifications, isRepository, isThread, toNotificationList } from './utils/notification'
import { createGithubWebURL } from './utils/github'
import type { MinimalRepository, Thread } from './api/notifications'
import { getNotifications, markNotificationAsRead, unsubscribeNotification } from './api/notifications'
import { useCommonCalls } from './composables/useCommonCalls'
const store = useStore()
const { currentPage } = useRoute()
const contextmenu = useContextMenu()
const { onOpen, onUnsubscribe, onMarkAsRead, emitMarkAsRead, onRefetch, emitRefetch } = useAppHooks()
const commonCalls = useCommonCalls()
useInterval(() => {
if (AppStorage.get('accessToken') && AppStorage.get('user')) {
emitRefetch(false)
commonCalls.fetchThreads(false)
}
}, FETCH_INTERVAL_DURATION)
Expand All @@ -43,137 +34,6 @@ watchEffect(() => {
document.documentElement.classList.add('light-theme')
}
})
function getThreadsToProcess(target: Thread | MinimalRepository) {
let threads = [] as Thread[]
if (isRepository(target)) {
threads = store.getThreadsOfRepository(target)
}
else if (store.isChecked(target)) {
threads = [...store.checkedItems]
store.checkedItems = []
}
else {
threads = [target]
}
return threads
}
onMarkAsRead((target) => {
let threads = [] as Thread[]
if (isRepository(target) || isThread(target)) {
threads = getThreadsToProcess(target)
}
else {
threads = target
}
for (const thread of threads) {
if (!thread.unread) {
continue
}
if (AppStorage.get('showReadNotifications')) {
thread.unread = false
}
else {
store.removeNotificationById(thread.id)
}
markNotificationAsRead(thread.id, AppStorage.get('accessToken')!)
}
})
onOpen((target) => {
const threads = getThreadsToProcess(target)
for (const thread of threads) {
const url = createGithubWebURL({ notification: thread, userId: AppStorage.get('user')!.id })
open(url)
}
if (AppStorage.get('markAsReadOnOpen')) {
emitMarkAsRead(threads)
}
})
onUnsubscribe((target) => {
const threads = getThreadsToProcess(target)
for (const thread of threads) {
unsubscribeNotification(thread.id, AppStorage.get('accessToken')!)
}
emitMarkAsRead(threads)
})
onRefetch(async (withSkeletons) => {
if (store.loadingNotifications) {
return
}
const accessToken = AppStorage.get('accessToken')
if (accessToken == null) {
return
}
const previousThreads = store.notifications.filter(isThread)
if (withSkeletons) {
store.skeletonVisible = true
store.notifications = []
}
store.loadingNotifications = true
store.failedLoadingNotifications = false
try {
const { data } = await getNotifications({
accessToken,
showOnlyParticipating: AppStorage.get('showOnlyParticipating'),
showReadNotifications: AppStorage.get('showReadNotifications'),
})
const threadSet = new Set(data.map(thread => thread.id))
store.checkedItems = store.checkedItems.filter(thread => threadSet.has(thread.id))
store.notifications = toNotificationList(data)
}
catch (error) {
store.notifications = []
store.failedLoadingNotifications = true
store.checkedItems = []
}
store.loadingNotifications = false
store.skeletonVisible = false
const newNotifications = filterNewNotifications(previousThreads, store.notifications.filter(isThread))
if (newNotifications.length > 0) {
if (AppStorage.get('soundsEnabled')) {
invoke(InvokeCommand.PlayNotificationSound)
}
if (AppStorage.get('showSystemNotifications')) {
sendNotification({
title: newNotifications[0].repository.full_name,
body: newNotifications[0].subject.title,
})
}
}
})
const token = AppStorage.get('accessToken')
const user = AppStorage.get('user')
if (token && user) {
emitRefetch(true)
}
</script>

<template>
Expand Down
8 changes: 4 additions & 4 deletions src/components/AppSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { AppStorage } from '../storage'
import { useKey } from '../composables/useKey'
import { useI18n } from '../composables/useI18n'
import { Page, useRoute } from '../composables/useRoute'
import { useAppHooks } from '../composables/useAppHooks'
import { useCommonCalls } from '../composables/useCommonCalls'
import { Icons } from './Icons'
import SidebarButton from './SidebarButton.vue'
import Popover from './Popover.vue'
Expand Down Expand Up @@ -48,10 +48,10 @@ const moreItems = computed(() => [
}),
])
const { emitRefetch } = useAppHooks()
const commonCalls = useCommonCalls()
useKey('r', () => {
emitRefetch(true)
commonCalls.fetchThreads(true)
}, { source: () => route.currentPage.value === Page.Home })
const morePopover = ref<InstanceType<typeof Popover> | null>(null)
Expand Down Expand Up @@ -119,7 +119,7 @@ useKey('.', () => {
<template #default>
<SidebarButton
:disabled="route.currentPage.value !== Page.Home"
@click="emitRefetch(true)"
@click="commonCalls.fetchThreads(true)"
>
<Icons.Sync16 />
</SidebarButton>
Expand Down
21 changes: 0 additions & 21 deletions src/composables/useAppHooks.ts

This file was deleted.

145 changes: 145 additions & 0 deletions src/composables/useCommonCalls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import { createSharedComposable } from '@vueuse/core'
import { invoke } from '@tauri-apps/api'
import { sendNotification } from '@tauri-apps/api/notification'
import { open as shellOpen } from '@tauri-apps/api/shell'
import { type MinimalRepository, type Thread, getNotifications, markNotificationAsRead, unsubscribeNotification } from '../api/notifications'
import { useStore } from '../stores/store'
import { filterNewNotifications, isRepository, isThread, toNotificationList } from '../utils/notification'
import { AppStorage } from '../storage'
import { InvokeCommand } from '../constants'
import { createGithubWebURL } from '../utils/github'

export const useCommonCalls = createSharedComposable(() => {
const store = useStore()

function getThreadsToProcess(target: Thread | MinimalRepository) {
let threads = [] as Thread[]

if (isRepository(target)) {
threads = store.getThreadsOfRepository(target)
}
else if (store.isChecked(target)) {
threads = [...store.checkedItems]
store.checkedItems = []
}
else {
threads = [target]
}

return threads
}

function markAsRead(target: MinimalRepository | Thread | Thread[]) {
let threads = [] as Thread[]

if (isRepository(target) || isThread(target)) {
threads = getThreadsToProcess(target)
}
else {
threads = target
}

for (const thread of threads) {
if (!thread.unread) {
continue
}

if (AppStorage.get('showReadNotifications')) {
thread.unread = false
}
else {
store.removeNotificationById(thread.id)
}

markNotificationAsRead(thread.id, AppStorage.get('accessToken')!)
}
}

function open(target: Thread | MinimalRepository) {
const threads = getThreadsToProcess(target)

for (const thread of threads) {
const url = createGithubWebURL({ notification: thread, userId: AppStorage.get('user')!.id })
shellOpen(url)
}

if (AppStorage.get('markAsReadOnOpen')) {
markAsRead(threads)
}
}

function unsubscribeThreadOrRepo(target: Thread | MinimalRepository) {
const threads = getThreadsToProcess(target)

for (const thread of threads) {
unsubscribeNotification(thread.id, AppStorage.get('accessToken')!)
}

markAsRead(threads)
}

async function fetchThreads(withSkeletons = false) {
if (store.loadingNotifications) {
return
}

const accessToken = AppStorage.get('accessToken')

if (accessToken == null) {
return
}

const previousThreads = store.notifications.filter(isThread)

if (withSkeletons) {
store.skeletonVisible = true
store.notifications = []
}

store.loadingNotifications = true
store.failedLoadingNotifications = false

try {
const { data } = await getNotifications({
accessToken,
showOnlyParticipating: AppStorage.get('showOnlyParticipating'),
showReadNotifications: AppStorage.get('showReadNotifications'),
})

const threadSet = new Set(data.map(thread => thread.id))

store.checkedItems = store.checkedItems.filter(thread => threadSet.has(thread.id))
store.notifications = toNotificationList(data)
}
catch (error) {
store.notifications = []
store.failedLoadingNotifications = true
store.checkedItems = []
}

store.loadingNotifications = false
store.skeletonVisible = false

const newNotifications = filterNewNotifications(previousThreads, store.notifications.filter(isThread))

if (newNotifications.length > 0) {
if (AppStorage.get('soundsEnabled')) {
invoke(InvokeCommand.PlayNotificationSound)
}

if (AppStorage.get('showSystemNotifications')) {
sendNotification({
title: newNotifications[0].repository.full_name,
body: newNotifications[0].subject.title,
})
}
}
}

return {
markAsRead,
open,
unsubscribeThreadOrRepo,
fetchThreads,
}
})
Loading

0 comments on commit cc5b8e4

Please sign in to comment.