use localStorage for cache

This commit is contained in:
Shusui MOYATANI
2023-03-20 20:51:14 +09:00
parent 9a764ba086
commit 44a512c64b
16 changed files with 172 additions and 319 deletions

View File

@@ -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;
};

View File

@@ -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());
}
};
};