Add user clock with time format presets above login avatar
parent
21d6f7630b
commit
f61b490127
@ -0,0 +1,20 @@
|
||||
import { defineComponent, computed } from 'vue'
|
||||
import { useAppStore } from '@/store/app'
|
||||
import { usePageStore } from '@/store/page'
|
||||
import timeRef, { timePresets, type TimePreset } from '@/utils/time'
|
||||
|
||||
export default defineComponent({
|
||||
name: 'UserClock',
|
||||
setup() {
|
||||
const appStore = useAppStore()
|
||||
const pageStore = usePageStore()
|
||||
|
||||
const formatted = computed(() => {
|
||||
const preset = (appStore.timeFormat as TimePreset) || 'HH:mm'
|
||||
const options = timePresets[preset] ?? timePresets['HH:mm']
|
||||
return new Intl.DateTimeFormat(pageStore.locale, options).format(timeRef.time)
|
||||
})
|
||||
|
||||
return () => <div class="user-clock">{formatted.value}</div>
|
||||
}
|
||||
})
|
||||
@ -1,38 +1,19 @@
|
||||
import { reactive } from 'vue'
|
||||
import { usePageStore } from '@/store/page'
|
||||
|
||||
const timeRef = reactive({
|
||||
time: new Date(),
|
||||
shortTime: '',
|
||||
longTime: ''
|
||||
})
|
||||
export const timePresets = {
|
||||
'HH:mm': { hour: 'numeric', minute: 'numeric', hour12: false },
|
||||
'HH:mm:ss': { hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: false },
|
||||
'short': { weekday: 'short', day: 'numeric', month: 'short', hour: 'numeric', minute: 'numeric', hour12: false },
|
||||
'long': { weekday: 'long', day: 'numeric', month: 'long', hour: 'numeric', minute: 'numeric', hour12: false },
|
||||
} as const satisfies Record<string, Intl.DateTimeFormatOptions>
|
||||
|
||||
const formatTime = (type: 'long' | 'short' = 'short') => {
|
||||
const options: Intl.DateTimeFormatOptions = {
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
hour: 'numeric',
|
||||
minute: 'numeric',
|
||||
hour12: false
|
||||
}
|
||||
export type TimePreset = keyof typeof timePresets
|
||||
|
||||
if (type === 'long') {
|
||||
options.month = 'long'
|
||||
options.weekday = 'long'
|
||||
}
|
||||
|
||||
return new Intl.DateTimeFormat(usePageStore().locale, options).format(new Date())
|
||||
}
|
||||
|
||||
const updateTime = () => {
|
||||
timeRef.time = new Date()
|
||||
timeRef.shortTime = formatTime('short')
|
||||
timeRef.longTime = formatTime('long')
|
||||
}
|
||||
const timeRef = reactive({ time: new Date() })
|
||||
|
||||
export const initTimer = () => {
|
||||
updateTime()
|
||||
setInterval(updateTime, 1000)
|
||||
timeRef.time = new Date()
|
||||
setInterval(() => { timeRef.time = new Date() }, 1000)
|
||||
}
|
||||
|
||||
export default timeRef
|
||||
|
||||
Loading…
Reference in New Issue