|
| 1 | +import React, { useContext } from 'react' |
| 2 | +import { VscTriangleUp } from 'react-icons/vsc' |
| 3 | +import CardComponent from '../components/CardComponent' |
| 4 | +import ListComponent from '../components/ListComponent' |
| 5 | +import indiehackersApi from '../services/indiehackers' |
| 6 | +import { BiCommentDetail } from 'react-icons/bi' |
| 7 | +import { MdAccessTime } from 'react-icons/md' |
| 8 | +import CardLink from '../components/CardLink' |
| 9 | +import CardItemWithActions from '../components/CardItemWithActions' |
| 10 | +import PreferencesContext from '../preferences/PreferencesContext' |
| 11 | +import { FaChevronUp } from 'react-icons/fa' |
| 12 | +import { format } from 'timeago.js' |
| 13 | + |
| 14 | +const StoryItem = ({ item, index, analyticsTag }) => { |
| 15 | + const { listingMode } = useContext(PreferencesContext) |
| 16 | + |
| 17 | + return ( |
| 18 | + <CardItemWithActions |
| 19 | + source={'indiehackers'} |
| 20 | + index={index} |
| 21 | + item={item} |
| 22 | + key={index} |
| 23 | + cardItem={ |
| 24 | + <> |
| 25 | + <p className="rowTitle"> |
| 26 | + <CardLink link={item.url} analyticsSource={analyticsTag}> |
| 27 | + {listingMode === 'compact' && ( |
| 28 | + <span className="counterWrapper"> |
| 29 | + <VscTriangleUp /> |
| 30 | + <span className="value">{item.score}</span> |
| 31 | + </span> |
| 32 | + )} |
| 33 | + |
| 34 | + <span className="subTitle">{item.title}</span> |
| 35 | + </CardLink> |
| 36 | + </p> |
| 37 | + {listingMode === 'normal' && ( |
| 38 | + <div className="rowDetails"> |
| 39 | + <span className="rowItem inRowItem"> |
| 40 | + <FaChevronUp className="rowItemIcon" /> {item.likes} points |
| 41 | + </span> |
| 42 | + <span className="rowItem"> |
| 43 | + <MdAccessTime className="rowItemIcon" /> {format(new Date(item.published_at))} |
| 44 | + </span> |
| 45 | + <span className="rowItem"> |
| 46 | + <BiCommentDetail className="rowItemIcon" /> {item.comments} comments |
| 47 | + </span> |
| 48 | + </div> |
| 49 | + )} |
| 50 | + </> |
| 51 | + } |
| 52 | + /> |
| 53 | + ) |
| 54 | +} |
| 55 | + |
| 56 | +function IndieHackersCard({ analyticsTag, label, icon, withAds }) { |
| 57 | + const fetchStories = async () => { |
| 58 | + return await indiehackersApi.getTopStories() |
| 59 | + } |
| 60 | + |
| 61 | + const renderItem = (item, index) => ( |
| 62 | + <StoryItem item={item} key={`st-${index}`} index={index} analyticsTag={analyticsTag} /> |
| 63 | + ) |
| 64 | + |
| 65 | + return ( |
| 66 | + <CardComponent |
| 67 | + icon={<span className="blockHeaderIcon">{icon}</span>} |
| 68 | + link="https://indiehackers.com/" |
| 69 | + title={label}> |
| 70 | + <ListComponent fetchData={fetchStories} renderItem={renderItem} withAds={withAds} /> |
| 71 | + </CardComponent> |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +export default IndieHackersCard |
0 commit comments