-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hooks): implement time filter for all views
- Loading branch information
1 parent
4cd0c5e
commit 91ffb79
Showing
9 changed files
with
44 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,60 @@ | ||
import assert from 'assert' | ||
import {useParams} from 'react-router-dom' | ||
|
||
// the timestamp the last time the user visited | ||
const lastVisitTimestamp = localStorage.getItem('plebonesLastVisitTimestamp') | ||
|
||
// TODO: remove debug, test setting some older date in console | ||
// localStorage.setItem('plebonesLastVisitTimestamp', Date.now() - 20 * 24 * 60 * 60 * 1000) | ||
console.log('lastVisitTimestamp', lastVisitTimestamp, ((Date.now() - lastVisitTimestamp) / (60 * 60 * 1000)).toFixed(2) + ' hours ago') | ||
|
||
// update the last visited timestamp every n seconds | ||
setInterval(() => { | ||
localStorage.setItem('plebonesLastVisitTimestamp', Date.now()) | ||
}, 60 * 1000) | ||
|
||
const timeFilters = { | ||
'1h': (comment) => comment.timestamp > Date.now() / 1000 - 60 * 60, | ||
'12h': (comment) => comment.timestamp > Date.now() / 1000 - 60 * 60 * 12, | ||
'24h': (comment) => comment.timestamp > Date.now() / 1000 - 60 * 60 * 24, | ||
'48h': (comment) => comment.timestamp > Date.now() / 1000 - 60 * 60 * 24 * 2, | ||
week: (comment) => comment.timestamp > Date.now() / 1000 - 60 * 60 * 24 * 7, | ||
month: (comment) => comment.timestamp > Date.now() / 1000 - 60 * 60 * 24 * 30, | ||
year: (comment) => comment.timestamp > Date.now() / 1000 - 60 * 60 * 24 * 365, | ||
'1h-active': (comment) => (comment.lastReplyTimestamp || comment.timestamp) > Date.now() / 1000 - 60 * 60, | ||
'12h-active': (comment) => (comment.lastReplyTimestamp || comment.timestamp) > Date.now() / 1000 - 60 * 60 * 12, | ||
'24h-active': (comment) => (comment.lastReplyTimestamp || comment.timestamp) > Date.now() / 1000 - 60 * 60 * 24, | ||
'48h-active': (comment) => (comment.lastReplyTimestamp || comment.timestamp) > Date.now() / 1000 - 60 * 60 * 24 * 2, | ||
'week-active': (comment) => (comment.lastReplyTimestamp || comment.timestamp) > Date.now() / 1000 - 60 * 60 * 24 * 7, | ||
'month-active': (comment) => (comment.lastReplyTimestamp || comment.timestamp) > Date.now() / 1000 - 60 * 60 * 24 * 30, | ||
'year-active': (comment) => (comment.lastReplyTimestamp || comment.timestamp) > Date.now() / 1000 - 60 * 60 * 24 * 365, | ||
const timeFilterNamesToSeconds = { | ||
'1h': 60 * 60, | ||
'12h': 60 * 60 * 12, | ||
'24h': 60 * 60 * 24, | ||
'48h': 60 * 60 * 24 * 2, | ||
week: 60 * 60 * 24 * 7, | ||
month: 60 * 60 * 24 * 30, | ||
year: 60 * 60 * 24 * 365, | ||
all: undefined, | ||
} | ||
|
||
// calculate the last visit filter | ||
// calculate the last visit timeFilterNamesToSeconds | ||
const secondsSinceLastVisit = lastVisitTimestamp ? (Date.now() - lastVisitTimestamp) / 1000 : Infinity | ||
const day = 24 * 60 * 60 | ||
let lastVisitTimeFilterName | ||
if (secondsSinceLastVisit > 30 * day) { | ||
lastVisitTimeFilterName = 'month' | ||
timeFilters[lastVisitTimeFilterName] = timeFilters['month'] | ||
timeFilters[`${lastVisitTimeFilterName}-active`] = timeFilters['month-active'] | ||
timeFilterNamesToSeconds[lastVisitTimeFilterName] = timeFilterNamesToSeconds['month'] | ||
} else if (secondsSinceLastVisit > 7 * day) { | ||
const weeks = Math.ceil(secondsSinceLastVisit / day / 7) | ||
lastVisitTimeFilterName = `${weeks}w` | ||
timeFilters[lastVisitTimeFilterName] = (comment) => comment.timestamp > Date.now() / 1000 - 60 * 60 * 24 * 7 * weeks | ||
timeFilters[`${lastVisitTimeFilterName}-active`] = (comment) => (comment.lastReplyTimestamp || comment.timestamp) > Date.now() / 1000 - 60 * 60 * 24 * 7 * weeks | ||
timeFilterNamesToSeconds[lastVisitTimeFilterName] = 60 * 60 * 24 * 7 * weeks | ||
} else if (secondsSinceLastVisit > day) { | ||
const days = Math.ceil(secondsSinceLastVisit / day) | ||
lastVisitTimeFilterName = `${days}d` | ||
timeFilters[lastVisitTimeFilterName] = (comment) => comment.timestamp > Date.now() / 1000 - 60 * 60 * 24 * days | ||
timeFilters[`${lastVisitTimeFilterName}-active`] = (comment) => (comment.lastReplyTimestamp || comment.timestamp) > Date.now() / 1000 - 60 * 60 * 24 * days | ||
timeFilterNamesToSeconds[lastVisitTimeFilterName] = 60 * 60 * 24 * days | ||
} else { | ||
lastVisitTimeFilterName = '24h' | ||
timeFilters[lastVisitTimeFilterName] = timeFilters['24h'] | ||
timeFilters[`${lastVisitTimeFilterName}-active`] = timeFilters['24h-active'] | ||
timeFilterNamesToSeconds[lastVisitTimeFilterName] = timeFilterNamesToSeconds['24h'] | ||
} | ||
|
||
const timeFilterNames = [lastVisitTimeFilterName, '1h', '12h', '24h', '48h', 'week', 'month', 'year', 'all'] | ||
|
||
const useTimeFilter = (sortType, timeFilterName) => { | ||
const useTimeFilter = () => { | ||
const params = useParams() | ||
let timeFilterName = params.timeFilterName | ||
|
||
// the default time filter is the last visit time filter | ||
if (!timeFilterName) { | ||
timeFilterName = lastVisitTimeFilterName | ||
} | ||
|
||
assert(!sortType || typeof sortType === 'string', `useTimeFilter sortType argument '${sortType}' not a string`) | ||
assert(!timeFilterName || typeof timeFilterName === 'string', `useTimeFilter timeFilterName argument '${timeFilterName}' not a string`) | ||
const timeFilter = sortType === 'active' ? timeFilters[timeFilterName + '-active'] : timeFilters[timeFilterName] | ||
assert(!timeFilterName || timeFilterName === 'all' || timeFilter !== undefined, `useTimeFilter no filter for timeFilterName '${timeFilterName}'`) | ||
return {timeFilter, timeFilterNames} | ||
const timeFilterSeconds = timeFilterNamesToSeconds[timeFilterName] | ||
assert(!timeFilterName || timeFilterName === 'all' || timeFilterSeconds !== undefined, `useTimeFilter no filter for timeFilterName '${timeFilterName}'`) | ||
return {timeFilterSeconds, timeFilterNames} | ||
} | ||
|
||
export default useTimeFilter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters