add generic date pivot helper

This commit is contained in:
keyan 2023-08-10 17:35:11 -05:00
parent 46274fba4f
commit 26762efcea
4 changed files with 18 additions and 9 deletions

View File

@ -4,7 +4,7 @@ import { msatsToSats } from '../../lib/format'
import { bioSchema, emailSchema, settingsSchema, ssValidate, userSchema } from '../../lib/validate' import { bioSchema, emailSchema, settingsSchema, ssValidate, userSchema } from '../../lib/validate'
import { createMentions, getItem, SELECT, updateItem, filterClause } from './item' import { createMentions, getItem, SELECT, updateItem, filterClause } from './item'
import serialize from './serial' import serialize from './serial'
import { dayPivot } from '../../lib/time' import { datePivot } from '../../lib/time'
export function within (table, within) { export function within (table, within) {
let interval = ' AND "' + table + '".created_at >= $1 - INTERVAL ' let interval = ' AND "' + table + '".created_at >= $1 - INTERVAL '
@ -54,13 +54,13 @@ export function viewWithin (table, within) {
export function withinDate (within) { export function withinDate (within) {
switch (within) { switch (within) {
case 'day': case 'day':
return dayPivot(new Date(), -1) return datePivot(new Date(), { days: -1 })
case 'week': case 'week':
return dayPivot(new Date(), -7) return datePivot(new Date(), { days: -7 })
case 'month': case 'month':
return dayPivot(new Date(), -30) return datePivot(new Date(), { days: -30 })
case 'year': case 'year':
return dayPivot(new Date(), -365) return datePivot(new Date(), { days: -365 })
default: default:
return new Date(0) return new Date(0)
} }

View File

@ -12,7 +12,7 @@ export default function Snl ({ ignorePreference }) {
useEffect(() => { useEffect(() => {
const dismissed = window.localStorage.getItem('snl') const dismissed = window.localStorage.getItem('snl')
if (!ignorePreference && dismissed && dismissed > new Date(dismissed) < dayPivot(new Date(), -6)) { if (!ignorePreference && dismissed && dismissed > new Date(dismissed) < datePivot(new Date(), { days: -6 })) {
return return
} }

View File

@ -5,7 +5,7 @@ export const defaultCommentSort = (pinned, bio, createdAt) => {
// pins sort by recent // pins sort by recent
if (pinned) return 'recent' if (pinned) return 'recent'
// old items (that aren't bios) sort by top // old items (that aren't bios) sort by top
if (!bio && new Date(createdAt) < dayPivot(new Date(), -OLD_ITEM_DAYS)) return 'top' if (!bio && new Date(createdAt) < datePivot(new Date(), { days: -OLD_ITEM_DAYS })) return 'top'
// everything else sorts by hot // everything else sorts by hot
return 'hot' return 'hot'
} }

View File

@ -20,8 +20,17 @@ export function timeSince (timeStamp) {
return 'now' return 'now'
} }
export function dayPivot (date, days) { export function datePivot (date,
return new Date(date.getTime() + days * 24 * 60 * 60 * 1000) { years = 0, months = 0, days = 0, hours = 0, minutes = 0, seconds = 0, milliseconds = 0 }) {
return new Date(
date.getFullYear() + years,
date.getMonth() + months,
date.getDate() + days,
date.getHours() + hours,
date.getMinutes() + minutes,
date.getSeconds() + seconds,
date.getMilliseconds() + milliseconds
)
} }
export function timeLeft (timeStamp) { export function timeLeft (timeStamp) {