|
| 1 | +import dayjs from 'dayjs' |
| 2 | +import { useRouter } from 'next/router' |
| 3 | +import { useEffect, useState } from 'react' |
| 4 | +import { toast } from 'sonner' |
| 5 | + |
| 6 | +import { IS_PLATFORM } from 'common' |
| 7 | +import { useDeploymentCommitQuery } from 'data/utils/deployment-commit-query' |
| 8 | +import { Button, StatusIcon } from 'ui' |
| 9 | +import { useFlag } from './ui/useFlag' |
| 10 | + |
| 11 | +const DeployCheckToast = ({ id }: { id: string | number }) => { |
| 12 | + const router = useRouter() |
| 13 | + |
| 14 | + return ( |
| 15 | + <div className="flex gap-3 flex-col w-full"> |
| 16 | + <div className="flex gap-3 flex-row"> |
| 17 | + <StatusIcon variant="default" className="mt-0.5" /> |
| 18 | + <div className="flex w-full justify-between flex-col text-sm"> |
| 19 | + <p className="text-foreground">A new version of this page is available</p> |
| 20 | + <p className="text-foreground-light">Refresh to see the latest changes.</p> |
| 21 | + </div> |
| 22 | + </div> |
| 23 | + |
| 24 | + <div className="flex gap-5 justify-end"> |
| 25 | + <Button type="outline" onClick={() => toast.dismiss(id)}> |
| 26 | + Not now |
| 27 | + </Button> |
| 28 | + <Button onClick={() => router.reload()}>Refresh</Button> |
| 29 | + </div> |
| 30 | + </div> |
| 31 | + ) |
| 32 | +} |
| 33 | + |
| 34 | +// This hook checks if the user is using old Studio pages and shows a toast to refresh the page. It's only triggered if |
| 35 | +// there's a new version of Studio is available, and the user has been on the old dashboard (based on commit) for more than 24 hours. |
| 36 | +// [Joshen] K-Dog has a suggestion here to bring down the time period here by checking commits |
| 37 | +export function useCheckLatestDeploy() { |
| 38 | + const showRefreshToast = useFlag('showRefreshToast') |
| 39 | + |
| 40 | + const [currentCommitTime, setCurrentCommitTime] = useState('') |
| 41 | + const [isToastShown, setIsToastShown] = useState(false) |
| 42 | + |
| 43 | + const { data: commit } = useDeploymentCommitQuery({ |
| 44 | + enabled: IS_PLATFORM && showRefreshToast, |
| 45 | + staleTime: 10000, // 10 seconds |
| 46 | + }) |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + // if the fetched commit is undefined is undefined |
| 50 | + if (!commit || commit.commitTime === 'unknown') { |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + // set the current commit on first load |
| 55 | + if (!currentCommitTime) { |
| 56 | + setCurrentCommitTime(commit.commitTime) |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + // if the current commit is the same as the fetched commit, do nothing |
| 61 | + if (currentCommitTime === commit.commitTime) { |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + // prevent showing the toast again if user has already seen and dismissed it |
| 66 | + if (isToastShown) { |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + // check if the time difference between commits is more than 24 hours |
| 71 | + const hourDiff = dayjs(commit.commitTime).diff(dayjs(currentCommitTime), 'hour') |
| 72 | + if (hourDiff < 24) { |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + // show the toast |
| 77 | + toast.custom((id) => <DeployCheckToast id={id} />, { |
| 78 | + duration: Infinity, |
| 79 | + position: 'bottom-right', |
| 80 | + }) |
| 81 | + setIsToastShown(true) |
| 82 | + }, [commit, isToastShown, currentCommitTime]) |
| 83 | +} |
0 commit comments