mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
Restructure codebase to use src directory
This commit is contained in:
20
src/hooks/useTimeDistance.ts
Normal file
20
src/hooks/useTimeDistance.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user