11import { h } from 'preact' ;
22import { Link } from 'preact-router/match' ;
33import CategoryCard from './category-card.js' ;
4+ import { sub } from 'date-fns' ;
45
56function Paper ( { paper } ) {
67 return h ( 'div' , { class : 'paper' } , [
@@ -15,19 +16,73 @@ function Paper ({ paper }) {
1516 ] ) ;
1617}
1718
19+ function findRelativeDate ( papers , range ) {
20+ const now = new Date ( ) ;
21+ const today = now . toISOString ( ) . split ( 'T' ) [ 0 ] ;
22+ const daysAgo = sub ( now , { days : 4 } ) . toISOString ( ) ;
23+ const weekAgo = sub ( now , { weeks : 1 } ) . toISOString ( ) ;
24+ const monthAgo = sub ( now , { months : 1 } ) . toISOString ( ) ;
25+ const displayPapers = [ ] ;
26+
27+ for ( const paper of papers ) {
28+ const paperDate = paper . date . toISOString ( ) . split ( 'T' ) [ 0 ] ;
29+
30+ if ( range === 'today' && paperDate === today ) { // Today's papers
31+ displayPapers . push ( paper ) ;
32+ } else if ( range === 'days' && paperDate >= daysAgo && paperDate < today ) { // Papers from 4 days ago
33+ displayPapers . push ( paper ) ;
34+ } else if ( range === 'week' && paperDate >= weekAgo && paperDate < daysAgo ) { // Papers from past week
35+ displayPapers . push ( paper ) ;
36+ } else if ( range === 'month' && paperDate >= monthAgo && paperDate < weekAgo ) { // Rest of the papers from the past month
37+ displayPapers . push ( paper ) ;
38+ }
39+ }
40+ return displayPapers ;
41+ }
42+
1843export default function CategoryResultsScreen ( { store } ) {
1944 const { selectedPapers, selectedCategory } = store ;
45+ const todayPapers = findRelativeDate ( selectedPapers , 'today' ) ;
46+ const fewDaysPapers = findRelativeDate ( selectedPapers , 'days' ) ;
47+ const weekPapers = findRelativeDate ( selectedPapers , 'week' ) ;
48+ const monthPapers = findRelativeDate ( selectedPapers , 'month' ) ;
2049
21- return h ( 'div' , { class : 'category-results-screen' } , [
22- h ( Link , { href : '/' , class : 'category-results-reset link' } , '< Select a different topic' ) , // TODO: < should be a nice SVG icon
50+ const resultsScreenElements = [
51+ h ( Link , { href : '/' , class : 'category-results-reset link' } , '< Select a different topic' ) ,
2352 h ( 'div' , { class : 'category-results-info' } , [ // TODO: better css for mobile
2453 CategoryCard ( { category : selectedCategory , store, selectable : false , includeName : false } ) ,
2554 h ( 'div' , { class : 'category-results-details' } , [
2655 h ( 'div' , { class : 'category-results-title' } , `${ selectedCategory . name } ` ) ,
2756 h ( 'div' , { class : 'category-results-date' } , 'Updated yesterday' ) ,
2857 h ( 'div' , { class : 'category-results-description' } , selectedCategory . description )
2958 ] )
30- ] ) ,
31- h ( 'div' , { class : 'papers' } , selectedPapers . map ( paper => h ( Paper , { paper } ) ) )
32- ] ) ;
59+ ] )
60+ ] ;
61+
62+ if ( todayPapers . length !== 0 ) {
63+ resultsScreenElements . push ( h ( 'div' , { class : 'papers-today' } , [
64+ h ( 'h2' , { class : 'papers-today-title' } , 'New papers today' ) ,
65+ h ( 'div' , { class : 'papers' } , todayPapers . map ( paper => h ( Paper , { paper } ) ) )
66+ ] ) ) ;
67+ }
68+ if ( fewDaysPapers . length !== 0 ) {
69+ resultsScreenElements . push ( h ( 'div' , { class : 'papers-last-few-days' } , [
70+ h ( 'h2' , { class : 'papers-last-few-days-title' } , 'Papers in the last few days' ) ,
71+ h ( 'div' , { class : 'papers' } , fewDaysPapers . map ( paper => h ( Paper , { paper } ) ) )
72+ ] ) ) ;
73+ }
74+ if ( weekPapers . length !== 0 ) {
75+ resultsScreenElements . push ( h ( 'div' , { class : 'papers-last-week' } , [
76+ h ( 'h2' , { class : 'papers-last-week-title' } , 'Papers in the last week' ) ,
77+ h ( 'div' , { class : 'papers' } , weekPapers . map ( paper => h ( Paper , { paper } ) ) )
78+ ] ) ) ;
79+ }
80+ if ( monthPapers . length !== 0 ) {
81+ resultsScreenElements . push ( h ( 'div' , { class : 'category-results-papers-this-month' } , [
82+ h ( 'h2' , { class : 'papers-this-month-title' } , 'Papers in the past month' ) ,
83+ h ( 'div' , { class : 'papers' } , monthPapers . map ( paper => h ( Paper , { paper } ) ) )
84+ ] ) ) ;
85+ }
86+
87+ return h ( 'div' , { class : 'category-results-screen' } , resultsScreenElements ) ;
3388}
0 commit comments