Skip to content

Commit

Permalink
Extract theme to a composable
Browse files Browse the repository at this point in the history
  • Loading branch information
kadiryazici committed Feb 28, 2024
1 parent 7966f6b commit 6127b65
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 31 deletions.
5 changes: 4 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { AppStorage } from './storage'
import { Page, useRoute } from './stores/route'
import ContextMenu from './components/ContextMenu.vue'
import { useContextmenu } from './stores/contextmenu'
import { useTheme } from './composables/useTheme'
const store = useStore()
const route = useRoute()
Expand All @@ -22,8 +23,10 @@ useInterval(() => {
store.fetchNotifications()
}, FETCH_INTERVAL_DURATION)
const { theme } = useTheme()
watchEffect(() => {
if (store.theme === ColorPreference.Dark)
if (theme.value === ColorPreference.Dark)
document.documentElement.classList.remove('light-theme')
else
document.documentElement.classList.add('light-theme')
Expand Down
6 changes: 4 additions & 2 deletions src/components/AppScroller.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import type { Option } from '../types'
import { batchFn } from '../utils/batch'
import { ColorPreference } from '../constants'
import { useRoute } from '../stores/route'
import { useTheme } from '../composables/useTheme'
const store = useStore()
const route = useRoute()
const scrollView = ref<Option<OverlayScrollbarsComponentRef>>(null)
Expand All @@ -28,10 +28,12 @@ watch(() => route.currentPage, () => {
element.scrollTop = 0
}, { flush: 'post' })
const { theme } = useTheme()
const options = computedEager<OverlayScrollbarsOptions>(() => ({
scrollbars: {
autoHide: 'scroll',
theme: store.theme === ColorPreference.Dark ? 'os-theme-light' : 'os-theme-dark',
theme: theme.value === ColorPreference.Dark ? 'os-theme-light' : 'os-theme-dark',
},
}))
</script>
Expand Down
26 changes: 26 additions & 0 deletions src/composables/useTheme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { computed } from 'vue'
import { useMediaQuery } from '@vueuse/core'
import { singleton } from '../utils/common'
import { AppStorage } from '../storage'
import { ColorPreference } from '../constants'

export const useTheme = singleton(() => {
const prefersDark = useMediaQuery('(prefers-color-scheme: dark)')

const theme = computed(() => {
const preference = AppStorage.get('colorPreference')

let theme: ColorPreference.Dark | ColorPreference.Light

if (preference === ColorPreference.Dark)
theme = ColorPreference.Dark
else if (preference === ColorPreference.Light)
theme = ColorPreference.Light
else
theme = prefersDark.value ? ColorPreference.Dark : ColorPreference.Light

return theme
})

return { theme, prefersDark }
})
3 changes: 0 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Mutex } from 'async-mutex'
import { useMediaQuery } from '@vueuse/core'
import { Icons } from './components/Icons'

export const prefersDark = useMediaQuery('(prefers-color-scheme: dark)')

export enum CheckedNotificationProcess {
Unsubscribe,
MarkAsRead,
Expand Down
19 changes: 1 addition & 18 deletions src/stores/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import { ref, shallowRef, triggerRef, watchEffect } from 'vue'
import pAll from 'p-all'
import { type UpdateManifest, installUpdate } from '@tauri-apps/api/updater'
import { relaunch } from '@tauri-apps/api/process'
import { computedEager } from '@vueuse/core'
import { type MinimalRepository, type Thread, getNotifications, markNotificationAsRead, unsubscribeNotification } from '../api/notifications'
import { CheckedNotificationProcess, ColorPreference, InvokeCommand, notificationApiMutex, prefersDark } from '../constants'
import { CheckedNotificationProcess, InvokeCommand, notificationApiMutex } from '../constants'
import { AppStorage } from '../storage'
import type { NotificationList, Option } from '../types'
import { filterNewThreads, isRepository, isThread, toNotificationList } from '../utils/notification'
Expand Down Expand Up @@ -195,21 +194,6 @@ export const useStore = defineStore('store', () => {
}
}

const theme = computedEager(() => {
const preference = AppStorage.get('colorPreference')

let theme: ColorPreference.Dark | ColorPreference.Light

if (preference === ColorPreference.Dark)
theme = ColorPreference.Dark
else if (preference === ColorPreference.Light)
theme = ColorPreference.Light
else
theme = prefersDark.value ? ColorPreference.Dark : ColorPreference.Light

return theme
})

function isChecked(item: MinimalRepository | Thread | null) {
if (item == null || !isCheckable(item))
return false
Expand Down Expand Up @@ -301,7 +285,6 @@ export const useStore = defineStore('store', () => {
failedLoadingNotifications,
checkedItems,
installingUpate,
theme,
mutateNotifications,
getThreadsOfRepository,
isChecked,
Expand Down
10 changes: 3 additions & 7 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import type { Ref, UnwrapRef } from 'vue'

/**
* Just returns value of passed ref, used for reactivity tracking.
*/
export function trackRef<T extends Ref<any>>(ref: T): UnwrapRef<T> {
return ref.value
export function singleton<T>(fn: () => T): () => T {
const instance = fn()
return () => instance
}

0 comments on commit 6127b65

Please sign in to comment.