-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Refetch Button & tooltip & Last time updated info
- Loading branch information
muntean.gm
committed
Mar 22, 2024
1 parent
e2dd118
commit 701fec5
Showing
12 changed files
with
260 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use client'; | ||
|
||
import { | ||
ArrowPathIcon, | ||
InformationCircleIcon | ||
} from '@heroicons/react/24/outline'; | ||
import { useTranslations } from 'next-intl'; | ||
import { useState } from 'react'; | ||
|
||
import { cn } from '@utils/cn'; | ||
import useTimeElapsed from '@utils/hooks/useTimeElapsed'; | ||
|
||
import Tooltip from '@ui/tooltip/Tooltip'; | ||
|
||
type TUpdateData = { | ||
onRefetch: () => void; | ||
}; | ||
|
||
const UpdateData = ({ onRefetch }: TUpdateData) => { | ||
const [isAnimating, setIsAnimating] = useState(false); | ||
const [startTime, setStartTime] = useState(Date.now()); | ||
|
||
const timeElapsed = useTimeElapsed(new Date(startTime)); | ||
|
||
const t = useTranslations(); | ||
|
||
const animate = () => { | ||
setIsAnimating(true); | ||
setTimeout(() => { | ||
setIsAnimating(false); | ||
}, 1000); | ||
}; | ||
|
||
const handleRefetch = () => { | ||
onRefetch(); | ||
setStartTime(Date.now()); | ||
|
||
animate(); | ||
}; | ||
|
||
return ( | ||
<div className='flex items-center gap-4'> | ||
<div className='flex gap-2 text-sm text-slate-400'> | ||
{timeElapsed && ( | ||
<> | ||
<span>{t('global.updated_time_ago', { time: timeElapsed })}</span> | ||
<Tooltip content={<div>{t('global.updated_tooltip')}</div>}> | ||
<InformationCircleIcon className='h-4 w-4' /> | ||
</Tooltip> | ||
</> | ||
)} | ||
</div> | ||
<button | ||
type='button' | ||
className='flex cursor-pointer items-center gap-2 text-sm font-medium text-sky-950' | ||
onClick={handleRefetch} | ||
> | ||
<span>{t('global.update_data')}</span> | ||
<ArrowPathIcon | ||
className={cn('h-4 w-4', { | ||
'animate-spin-infinite': isAnimating | ||
})} | ||
/> | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
export default UpdateData; |
49 changes: 49 additions & 0 deletions
49
apps/frontend/components/ui/tooltip/Tooltip.components.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import * as TooltipPrimitive from '@radix-ui/react-tooltip'; | ||
import type { ComponentPropsWithoutRef, ElementRef } from 'react'; | ||
import { forwardRef } from 'react'; | ||
|
||
import { cn } from '@utils/cn'; | ||
|
||
const TooltipProvider = TooltipPrimitive.Provider; | ||
|
||
const Tooltip = TooltipPrimitive.Root; | ||
|
||
const TooltipTrigger = TooltipPrimitive.Trigger; | ||
|
||
const TooltipPortal = TooltipPrimitive.Portal; | ||
|
||
const TooltipArrow = forwardRef< | ||
ElementRef<typeof TooltipPrimitive.Arrow>, | ||
ComponentPropsWithoutRef<typeof TooltipPrimitive.Arrow> | ||
>((props, ref) => ( | ||
<TooltipPrimitive.Arrow ref={ref} className={cn('fill-sky-500')} {...props} /> | ||
)); | ||
|
||
TooltipArrow.displayName = 'TooltipArrow'; | ||
|
||
const TooltipContent = forwardRef< | ||
ElementRef<typeof TooltipPrimitive.Content>, | ||
ComponentPropsWithoutRef<typeof TooltipPrimitive.Content> | ||
>(({ className, sideOffset = 2, ...props }, ref) => ( | ||
<TooltipPrimitive.Content | ||
ref={ref} | ||
sideOffset={sideOffset} | ||
className={cn( | ||
'z-tooltip group max-w-72 overflow-hidden rounded-xl bg-sky-950 px-3 py-2.5 text-xs text-white', | ||
'fade-in', | ||
className | ||
)} | ||
{...props} | ||
/> | ||
)); | ||
|
||
TooltipContent.displayName = TooltipPrimitive.Content.displayName; | ||
|
||
export { | ||
Tooltip as TooltipRoot, | ||
TooltipTrigger, | ||
TooltipPortal, | ||
TooltipContent, | ||
TooltipProvider, | ||
TooltipArrow | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use client'; | ||
|
||
import type { FC, ReactNode } from 'react'; | ||
|
||
import * as Tooltip from '@components/ui/tooltip/Tooltip.components'; | ||
|
||
type TooltipProps = { | ||
content: ReactNode; | ||
children: ReactNode; | ||
}; | ||
|
||
const TooltipComponent: FC<TooltipProps> = ({ | ||
children, | ||
content, | ||
...triggerProps | ||
}) => ( | ||
<Tooltip.TooltipProvider delayDuration={100}> | ||
<Tooltip.TooltipRoot> | ||
<Tooltip.TooltipTrigger {...triggerProps}> | ||
{children} | ||
</Tooltip.TooltipTrigger> | ||
<Tooltip.TooltipContent side='top' align='center'> | ||
{content} | ||
<Tooltip.TooltipArrow /> | ||
</Tooltip.TooltipContent> | ||
</Tooltip.TooltipRoot> | ||
</Tooltip.TooltipProvider> | ||
); | ||
|
||
export default TooltipComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useTranslations } from 'next-intl'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
const ONE_SECOND = 1000; | ||
|
||
const useTimeElapsed = (startTime: Date): string => { | ||
const [timeElapsed, setTimeElapsed] = useState<string>(''); | ||
|
||
const t = useTranslations(); | ||
|
||
useEffect(() => { | ||
const interval = setInterval(() => { | ||
const currentTime = new Date(); | ||
const difference = Math.abs(currentTime.getTime() - startTime.getTime()); | ||
const secondsElapsed = Math.floor(difference / 1000); | ||
|
||
const m = t('global.minutes_unit'); | ||
const h = t('global.hours_unit'); | ||
|
||
if (secondsElapsed < 60) { | ||
setTimeElapsed(t('global.less_one_minute')); | ||
} else { | ||
const minutesElapsed = Math.floor(secondsElapsed / 60); | ||
if (minutesElapsed < 60) { | ||
setTimeElapsed(`${minutesElapsed}${m}`); | ||
} else { | ||
const hoursElapsed = Math.floor(minutesElapsed / 60); | ||
setTimeElapsed(`${hoursElapsed}${h}`); | ||
} | ||
} | ||
}, ONE_SECOND); | ||
|
||
return () => clearInterval(interval); | ||
}, [startTime, t]); | ||
|
||
return timeElapsed; | ||
}; | ||
|
||
export default useTimeElapsed; |
701fec5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deploy preview for ebsi-vector-frontend ready!
✅ Preview
https://ebsi-vector-frontend-mk4ox81mc-protokol.vercel.app
Built with commit 701fec5.
This pull request is being automatically deployed with vercel-action