@@ -2,10 +2,11 @@ import { Button, HStack, Text, VStack, Wrap } from '@chakra-ui/react';
22import { ArticleCard , LoadingSpinner } from '@components' ;
33import { useGetArticlesData } from '@services' ;
44import { useLocation , useNavigate } from 'react-router-dom' ;
5- import { MarkdownViewer } from './components' ;
5+ import { MarkdownViewer , StreakStalker } from './components' ;
66import { useMemo , useState } from 'react' ;
7- import { groupBy } from 'lodash' ;
7+ import { groupBy , sortBy } from 'lodash' ;
88import { BASE_NAV_ROUTE } from '@router' ;
9+ import SortBy , { SortByType } from './components/SortBy' ;
910
1011const CategoryButton = ( {
1112 category,
@@ -47,6 +48,7 @@ const ArticlesScreen = () => {
4748 const location = useLocation ( ) ;
4849 const [ category , setCategory ] = useState ( 'All' ) ;
4950 const secondPath = location . pathname . split ( '/' ) [ 3 ] ;
51+ const [ sortByName , setSortBy ] = useState < SortByType > ( 'none' ) ;
5052
5153 const articles = useMemo (
5254 ( ) =>
@@ -58,10 +60,29 @@ const ArticlesScreen = () => {
5860
5961 const filteredArticles = groupBy ( articles , 'group_name' ) ;
6062
63+ const articleToShow =
64+ category && category !== 'All' ? filteredArticles [ category ] : articles ;
65+
66+ const sortByApplied = useMemo ( ( ) => {
67+ if ( sortByName === 'none' ) {
68+ return articleToShow ;
69+ } else if ( sortByName === 'publishedAt' ) {
70+ return sortBy ( articleToShow , 'last_updated' ) . reverse ( ) ;
71+ } else if ( sortByName === 'a-z' ) {
72+ return sortBy ( articleToShow , 'title' ) ;
73+ } else if ( sortByName === 'z-a' ) {
74+ return sortBy ( articleToShow , 'title' ) . reverse ( ) ;
75+ } else {
76+ return articleToShow ;
77+ }
78+ } , [ articleToShow , sortByName ] ) ;
79+
6180 if ( ! data ) {
6281 return < LoadingSpinner /> ;
6382 }
6483
84+ const date = articles . map ( ( article : any ) => article . last_updated as string ) ;
85+
6586 return (
6687 < VStack bg = { 'black' } w = { '100vw' } minH = { '100vh' } >
6788 < Text
@@ -95,6 +116,10 @@ const ArticlesScreen = () => {
95116 top = { '12vh' }
96117 marginTop = { '12vh' }
97118 >
119+ < HStack >
120+ < StreakStalker dates = { date } />
121+ < SortBy sortBy = { sortByName } setSortBy = { setSortBy } />
122+ </ HStack >
98123 < CategoryButton
99124 category = { 'All' }
100125 setCategory = { setCategory }
@@ -128,10 +153,7 @@ const ArticlesScreen = () => {
128153 justify = "start"
129154 justifyContent = { 'space-between' }
130155 >
131- { ( category && category !== 'All'
132- ? filteredArticles [ category ]
133- : articles
134- ) ?. map ( ( article : any ) => (
156+ { sortByApplied ?. map ( ( article : any ) => (
135157 < ArticleCard key = { article . id } { ...article } />
136158 ) ) }
137159 </ Wrap >
0 commit comments