Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix Banner close action #249

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading