Skip to content

Commit

Permalink
fix: fix Banner close action (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiazengp authored Apr 30, 2024
2 parents 0ef5487 + 37e9821 commit 87c1c88
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 @@ -13,21 +13,22 @@ const canBannerVisible = computed(
() => frontmatter.value.wip || typeof frontmatter.value.banner === 'string',
)
const isBannerVisible = ref(canBannerVisible.value)
const deal = () => (Date.now() + 8.64e7 * 1).toString() // current time + 1 day
const inExpiryDate = () => {
const dismissExpiryTime = computed(() => (Date.now() + 8.64e7 * 1).toString()) // current time + 1 day
const inExpiryDate = computed(() => {
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
}
})
const bannerText = computed(() => {
return frontmatter.value.wip
? theme.value.ui?.banner?.wip ?? ''
: frontmatter.value.banner
})
const bannerHash = computed(() => hash(bannerText.value))
watchEffect(() => {
if (height.value) {
Expand All @@ -40,23 +41,23 @@ watchEffect(() => {
const restore = (key, def = false) => {
const saved = localStorage.getItem(key)
const banner = JSON.parse(saved!)
const bannerData = JSON.parse(saved!)
if (!canBannerVisible.value) return hideBanner()
if (
saved
? hash(frontmatter.value.banner) == banner.hash && deal() > banner.time
? bannerHash.value === bannerData.hash && Date.now() < bannerData.time
: def
) {
hideBanner()
} else if (inExpiryDate()) hideBanner()
} else if (inExpiryDate.value) hideBanner()
}
onMounted(() => restore(storeKey))
const dismiss = () => {
const bannerData = {
time: deal(),
hash: hash(frontmatter.value.banner),
time: dismissExpiryTime.value,
hash: bannerHash.value,
}
localStorage.setItem(storeKey, JSON.stringify(bannerData))
hideBanner()
Expand Down

0 comments on commit 87c1c88

Please sign in to comment.