From 660ad41d4e05f798b903b1eed0dc9bdef61e94dd Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Wed, 25 Jan 2023 22:19:18 -0800 Subject: [PATCH] Show/hide toast logic --- components/Layout/index.tsx | 51 +++++++++++++++++++++++++++----- components/UpdateToast/index.tsx | 51 +++++++++++++++++++++++++++++--- 2 files changed, 91 insertions(+), 11 deletions(-) diff --git a/components/Layout/index.tsx b/components/Layout/index.tsx index 3ed4b1f3..224eb7b0 100644 --- a/components/Layout/index.tsx +++ b/components/Layout/index.tsx @@ -1,18 +1,55 @@ -import type { ReactElement } from 'react' +import { PropsWithChildren, useEffect, useState } from 'react' +import { add, format } from 'date-fns' +import { getCookie } from 'cookies-next' + +import { appState } from '~utils/appState' + import TopHeader from '~components/Header' -import Toast from '~components/Toast' import UpdateToast from '~components/UpdateToast' import './index.scss' -interface Props { - children: ReactElement -} -const Layout = ({ children }: Props) => { +interface Props {} + +const Layout = ({ children }: PropsWithChildren) => { + const [updateToastOpen, setUpdateToastOpen] = useState(false) + + useEffect(() => { + const cookie = getToastCookie() + const now = new Date() + const updatedAt = new Date(appState.version.updated_at) + const validUntil = add(updatedAt, { days: 7 }) + + if (now < validUntil && !cookie.seen) setUpdateToastOpen(true) + }, []) + + function getToastCookie() { + const updatedAt = new Date(appState.version.updated_at) + const cookieValues = getCookie(`update-${format(updatedAt, 'yyyy-MM-dd')}`) + return cookieValues + ? (JSON.parse(cookieValues as string) as { seen: true }) + : { seen: false } + } + + function handleToastActionClicked() { + setUpdateToastOpen(false) + } + + function handleToastOpenChanged(open: boolean) { + setUpdateToastOpen(false) + } + return ( <> - + {/* TODO: Don't show toast on about pages */} +
{children}
) diff --git a/components/UpdateToast/index.tsx b/components/UpdateToast/index.tsx index 7d8d6bd5..79ea4caf 100644 --- a/components/UpdateToast/index.tsx +++ b/components/UpdateToast/index.tsx @@ -1,4 +1,7 @@ import React from 'react' +import { useRouter } from 'next/router' +import { setCookie } from 'cookies-next' +import { add, format } from 'date-fns' import classNames from 'classnames' import Button from '~components/Button' @@ -10,24 +13,64 @@ import { useTranslation } from 'next-i18next' interface Props { open: boolean updateType: 'feature' | 'content' + lastUpdated: string + onActionClicked: () => void + onOpenChange: (open: boolean) => void } -const UpdateToast = (props: Props) => { +const UpdateToast = ({ + open, + updateType, + lastUpdated, + onActionClicked, + onOpenChange, +}: Props) => { const { t } = useTranslation('roadmap') const classes = classNames({ Update: true, }) + function setToastCookie() { + const updatedAt = new Date(lastUpdated) + const expiresAt = add(updatedAt, { days: 7 }) + setCookie( + `update-${format(updatedAt, 'yyyy-MM-dd')}`, + { seen: true }, + { path: '/', expires: expiresAt } + ) + } + + function handleButtonClicked() { + // TODO: Set a timestamped cookie to not show + console.log('Primary button clicked') + window.open('/about', '_blank') + setToastCookie() + onActionClicked() + } + + function handleOpenChanged(open: boolean) { + if (!open) { + setToastCookie() + onOpenChange(false) + } + } + return ( -