mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
21 lines
784 B
TypeScript
21 lines
784 B
TypeScript
import formatDistanceStrict from 'date-fns/formatDistanceStrict';
|
|
import { useState, useEffect } from 'react';
|
|
|
|
/**
|
|
* Returns the time distance between the provided date and the current time, using the
|
|
* `date-fns` `formatDistanceStrict` function. This hook ensures that the same result is
|
|
* calculated on both the server and client, preventing hydration errors.
|
|
*
|
|
* @param createdAt The date to calculate the time distance from.
|
|
* @returns The time distance between the provided date and the current time.
|
|
*/
|
|
const useTimeDistance = (createdAt: Date) => {
|
|
const [timeDistance, setTimeDistance] = useState('');
|
|
useEffect(() => {
|
|
setTimeDistance(formatDistanceStrict(createdAt, new Date()));
|
|
}, [createdAt]);
|
|
return timeDistance;
|
|
};
|
|
|
|
export default useTimeDistance;
|