umami/src/components/hooks/useTimezone.ts

24 lines
663 B
TypeScript
Raw Normal View History

import { setItem } from 'next-basics';
2020-09-30 23:27:27 +00:00
import { TIMEZONE_CONFIG } from 'lib/constants';
import { formatInTimeZone } from 'date-fns-tz';
import useStore, { setTimezone } from 'store/app';
const selector = (state: { timezone: string }) => state.timezone;
2023-05-18 06:20:06 +00:00
export function useTimezone() {
const timezone = useStore(selector);
const saveTimezone = (value: string) => {
setItem(TIMEZONE_CONFIG, value);
setTimezone(value);
};
const formatDate = (date: string, pattern: string) => {
return formatInTimeZone(date.split(' ').join('T') + 'Z', timezone, pattern);
};
return { timezone, saveTimezone, formatDate };
}
2023-05-18 06:20:06 +00:00
export default useTimezone;