moss / src /hooks /useChatTip /useChatTip.ts
jjjjjjj1's picture
update
186b709
raw
history blame
702 Bytes
import { onMounted, ref } from 'vue'
import { TIP_THRESHOLD, strNum2Num } from './common.js'
const countCacheKey = '__chat_tip_count__'
export function useChatTip() {
const tipCount = ref(0)
onMounted(() => {
forceSyncCount()
})
/** 从localStorage中同步count */
function forceSyncCount() {
const cacheCount = strNum2Num(localStorage.getItem(countCacheKey) || '0')
if (cacheCount !== tipCount.value)
tipCount.value = cacheCount
}
function increase() {
tipCount.value++
if (tipCount.value > TIP_THRESHOLD)
tipCount.value = 0
localStorage.setItem(countCacheKey, String(tipCount.value))
}
return {
increase,
count: tipCount,
}
}