mirror of
https://github.com/plebbit/seedit.git
synced 2026-02-15 08:21:19 -05:00
chore(time filter): improve comments
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user