use-check-latest-deploy.tsx 3.0 KB

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