mirror of
https://github.com/aljazceru/rabbit.git
synced 2026-01-06 07:24:24 +01:00
use localStorage for cache
This commit is contained in:
@@ -1,13 +1,20 @@
|
||||
import { createSignal, type Accessor } from 'solid-js';
|
||||
import { createSignal, createEffect, onCleanup, type Accessor } from 'solid-js';
|
||||
|
||||
const [currentDate, setCurrentDate] = createSignal(new Date());
|
||||
type DatePulserProps = {
|
||||
interval: number;
|
||||
};
|
||||
|
||||
// 7 seconds is used for the interval so that the last digit of relative time is changed.
|
||||
setInterval(() => {
|
||||
setCurrentDate(new Date());
|
||||
}, 7000);
|
||||
const useDatePulser = (propsProvider: () => DatePulserProps): Accessor<Date> => {
|
||||
const [currentDate, setCurrentDate] = createSignal(new Date());
|
||||
|
||||
createEffect(() => {
|
||||
const id = setInterval(() => {
|
||||
setCurrentDate(new Date());
|
||||
}, propsProvider().interval);
|
||||
|
||||
onCleanup(() => clearInterval(id));
|
||||
});
|
||||
|
||||
const useDatePulser = (): Accessor<Date> => {
|
||||
return currentDate;
|
||||
};
|
||||
|
||||
|
||||
@@ -4,20 +4,23 @@ import useDatePulser from '@/hooks/useDatePulser';
|
||||
|
||||
import { formatRelative, formatAbsoluteLong, formatAbsoluteShort } from '@/utils/formatDate';
|
||||
|
||||
// 7 seconds is used here so that the last digit of relative time is changed.
|
||||
const currentDateHigh = useDatePulser(() => ({ interval: 7000 }));
|
||||
const currentDateLow = useDatePulser(() => ({ interval: 60 * 1000 }));
|
||||
|
||||
const useFormatDate = () => {
|
||||
const { config } = useConfig();
|
||||
const currentDate = useDatePulser();
|
||||
|
||||
return (date: Date) => {
|
||||
switch (config().dateFormat) {
|
||||
case 'absolute-long':
|
||||
return formatAbsoluteLong(date, currentDate());
|
||||
return formatAbsoluteLong(date, currentDateLow());
|
||||
case 'absolute-short':
|
||||
return formatAbsoluteShort(date, currentDate());
|
||||
return formatAbsoluteShort(date, currentDateLow());
|
||||
case 'relative':
|
||||
return formatRelative(date, currentDate());
|
||||
return formatRelative(date, currentDateHigh());
|
||||
default:
|
||||
return formatRelative(date, currentDate());
|
||||
return formatRelative(date, currentDateHigh());
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user