chore(time filter): improve comments

This commit is contained in:
plebeius.eth
2024-01-27 13:49:22 +01:00
parent cba5090340
commit 4f18c8b786

View File

@@ -1,7 +1,6 @@
import assert from 'assert';
import { Comment } from '@plebbit/plebbit-react-hooks';
// Type Definitions
type TimeFilter = (comment: Comment) => boolean;
export type TimeFilterKey = '1h' | '24h' | '1w' | '1m' | '1y' | 'all';
export const timeFilterNames: TimeFilterKey[] = ['1h', '24h', '1w', '1m', '1y', 'all'];
@@ -10,7 +9,6 @@ interface TimeFilters {
[key: string]: TimeFilter | undefined;
}
// Time Filters
const timeFilters: TimeFilters = {
'1h': (comment) => comment.timestamp > Date.now() / 1000 - 60 * 60,
'24h': (comment) => comment.timestamp > Date.now() / 1000 - 60 * 60 * 24,
@@ -20,8 +18,9 @@ const timeFilters: TimeFilters = {
all: undefined,
};
// Last Visit Timestamp
// the timestamp the last time the user visited
const lastVisitTimestampString = localStorage.getItem('seeditLastVisitTimestamp');
let lastVisitTimeFilterName: TimeFilterKey;
if (lastVisitTimestampString) {
@@ -39,21 +38,19 @@ if (lastVisitTimestampString) {
lastVisitTimeFilterName = '24h';
}
} else {
// User has never visited before
lastVisitTimeFilterName = '1m';
}
// Update the last visited timestamp every n seconds
// update the last visited timestamp every n seconds
setInterval(() => {
localStorage.setItem('seeditLastVisitTimestamp', Date.now().toString());
}, 60 * 1000);
// useTimeFilter Function
const useTimeFilter = (
sortType?: string,
timeFilterName: TimeFilterKey = lastVisitTimeFilterName,
): { timeFilter: TimeFilter | undefined; timeFilterNames: TimeFilterKey[]; currentFilterName: TimeFilterKey } => {
// Default to the last visit time filter if no time filter name is provided
// default to the last visit time filter if no time filter name is provided
if (!timeFilterName) {
timeFilterName = lastVisitTimeFilterName;
}