Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
jiazengp committed Feb 12, 2024
1 parent f24d9c1 commit c39d4d2
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions .vitepress/theme/components/Banner.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ import dayjs from 'dayjs'
const el = ref<HTMLElement>()
const { height } = useElementSize(el)
const { frontmatter, page } = useData()
const storeKey = `banner-${page.value.relativePath}`
const isShow = ref(typeof frontmatter.value.banner === 'string')
const deal = () => (Date.now() + 8.64e7 * 1).toString() // current time + 1 day
const inExpiryDate = () => {
if (!frontmatter.value.bannerExpiryDate) return false
if (!dayjs(frontmatter.value.bannerExpiryDate).isValid())
console.error(
`The ${page.value.relativePath} of ${frontmatter.value.bannerExpiryDate} is an invalid date`,
)
if (dayjs().isBefore(dayjs(frontmatter.value.bannerExpiryDate))) return false
return true
}
watchEffect(() => {
if (height.value) {
document.documentElement.style.setProperty(
Expand All @@ -27,28 +30,26 @@ watchEffect(() => {
})
const restore = (key, def = false) => {
const saved = JSON.parse(localStorage.getItem(key) || '{}')
if (typeof frontmatter.value.banner !== 'string') hideBanner()
const saved = localStorage.getItem(key)
const banner = JSON.parse(saved!)
if (typeof frontmatter.value.banner !== 'string') return hideBanner()
if (
saved
? hash(frontmatter.value.banner) == saved.hash && deal() > saved.time
? hash(frontmatter.value.banner) == banner.hash && deal() > banner.time
: def
) {
hideBanner()
} else if (inExpiryDate()) hideBanner()
}
onMounted(() => restore(`banner-${page.value.relativePath}`))
onMounted(() => restore(storeKey))
const dismiss = () => {
const bannerData = {
time: deal(),
hash: hash(frontmatter.value.banner),
}
localStorage.setItem(
`banner-${page.value.relativePath}`,
JSON.stringify(bannerData),
)
localStorage.setItem(storeKey, JSON.stringify(bannerData))
hideBanner()
}
Expand Down

0 comments on commit c39d4d2

Please sign in to comment.