|
| 1 | +import React from 'react' |
| 2 | +import './Sidebar.css' |
| 3 | +import { VscChromeClose } from 'react-icons/vsc' |
| 4 | +import { TiDelete } from 'react-icons/ti' |
| 5 | +import { HiTicket } from 'react-icons/hi' |
| 6 | +import { SiGithub, SiReddit, SiProducthunt, SiYcombinator } from 'react-icons/si' |
| 7 | +import { |
| 8 | + ProSidebar, |
| 9 | + Menu, |
| 10 | + MenuItem, |
| 11 | + SubMenu, |
| 12 | + SidebarHeader, |
| 13 | + SidebarContent, |
| 14 | +} from 'react-pro-sidebar' |
| 15 | +import 'react-pro-sidebar/dist/css/styles.css' |
| 16 | +import CardLink from '../../../components/CardLink' |
| 17 | +import { trackLinkUnBookmark, Attributes } from 'src/lib/analytics' |
| 18 | +import { useUserPreferences } from 'src/stores/preferences' |
| 19 | + |
| 20 | +type BookmarkItemProps = { |
| 21 | + item: { |
| 22 | + url: string |
| 23 | + title: string |
| 24 | + source: string |
| 25 | + } |
| 26 | + appendRef?: boolean |
| 27 | +} |
| 28 | +const BookmarkItem = ({ item, appendRef = true }: BookmarkItemProps) => { |
| 29 | + const { unbookmarkPost } = useUserPreferences() |
| 30 | + const analyticsAttrs = { |
| 31 | + [Attributes.TRIGERED_FROM]: 'bookmarks', |
| 32 | + [Attributes.TITLE]: item.title, |
| 33 | + [Attributes.LINK]: item.url, |
| 34 | + [Attributes.SOURCE]: item.source, |
| 35 | + } |
| 36 | + const unBookmark = () => { |
| 37 | + unbookmarkPost(item) |
| 38 | + trackLinkUnBookmark(analyticsAttrs) |
| 39 | + } |
| 40 | + return ( |
| 41 | + <MenuItem |
| 42 | + suffix={ |
| 43 | + <span className="unbookmarkBtn" onClick={unBookmark}> |
| 44 | + <TiDelete /> |
| 45 | + </span> |
| 46 | + }> |
| 47 | + <CardLink |
| 48 | + link={item.url} |
| 49 | + appendRef={appendRef} |
| 50 | + analyticsAttributes={analyticsAttrs} |
| 51 | + className=""> |
| 52 | + {`${item.title}`} |
| 53 | + </CardLink> |
| 54 | + </MenuItem> |
| 55 | + ) |
| 56 | +} |
| 57 | + |
| 58 | +type BookmarksSidebarTtypes = { |
| 59 | + showSidebar: boolean |
| 60 | + onClose: () => void |
| 61 | +} |
| 62 | +export const BookmarksSidebar = ({ showSidebar, onClose }: BookmarksSidebarTtypes) => { |
| 63 | + const { userBookmarks } = useUserPreferences() |
| 64 | + const githubBookmarks = userBookmarks.filter((bm) => bm.source === 'github') |
| 65 | + const newsBookmarks = userBookmarks.filter( |
| 66 | + (bm) => |
| 67 | + [ |
| 68 | + 'hackernews', |
| 69 | + 'devto', |
| 70 | + 'hashnode', |
| 71 | + 'lobsters', |
| 72 | + 'freecodecamp', |
| 73 | + 'medium', |
| 74 | + 'indiehackers', |
| 75 | + ].indexOf(bm.source) !== -1 |
| 76 | + ) |
| 77 | + const conferencesBookmarks = userBookmarks.filter((bm) => bm.source === 'conferences') |
| 78 | + const productsBookmarks = userBookmarks.filter((bm) => bm.source === 'producthunt') |
| 79 | + const redditBookmarks = userBookmarks.filter((bm) => bm.source === 'reddit') |
| 80 | + |
| 81 | + return ( |
| 82 | + <ProSidebar className="sidebar" collapsed={!showSidebar} collapsedWidth={'0px'} width={'14%'}> |
| 83 | + <SidebarHeader> |
| 84 | + <div className="sidebarHeader"> |
| 85 | + <span className="title">Bookmarks</span> |
| 86 | + <span className="closeBtn" onClick={onClose}> |
| 87 | + <VscChromeClose /> |
| 88 | + </span> |
| 89 | + </div> |
| 90 | + </SidebarHeader> |
| 91 | + <SidebarContent> |
| 92 | + <Menu iconShape="circle"> |
| 93 | + <SubMenu |
| 94 | + title="Github Repos" |
| 95 | + icon={<SiGithub />} |
| 96 | + suffix={<span className="badge yellow">{githubBookmarks.length}</span>}> |
| 97 | + {githubBookmarks.map((bm, index) => ( |
| 98 | + <BookmarkItem item={bm} key={`gh-${index}`} /> |
| 99 | + ))} |
| 100 | + </SubMenu> |
| 101 | + |
| 102 | + <SubMenu |
| 103 | + title="News & Articles" |
| 104 | + icon={<SiYcombinator />} |
| 105 | + suffix={<span className="badge yellow">{newsBookmarks.length}</span>}> |
| 106 | + {newsBookmarks.map((bm, index) => ( |
| 107 | + <BookmarkItem item={bm} key={`ne-${index}`} /> |
| 108 | + ))} |
| 109 | + </SubMenu> |
| 110 | + |
| 111 | + <SubMenu |
| 112 | + title="Product Hunt" |
| 113 | + icon={<SiProducthunt />} |
| 114 | + suffix={<span className="badge yellow">{productsBookmarks.length}</span>}> |
| 115 | + {productsBookmarks.map((bm, index) => ( |
| 116 | + <BookmarkItem item={bm} key={`gh-${index}`} appendRef={false} /> |
| 117 | + ))} |
| 118 | + </SubMenu> |
| 119 | + |
| 120 | + <SubMenu |
| 121 | + title="Conferences" |
| 122 | + icon={<HiTicket />} |
| 123 | + suffix={<span className="badge yellow">{conferencesBookmarks.length}</span>}> |
| 124 | + {conferencesBookmarks.map((bm, index) => ( |
| 125 | + <BookmarkItem item={bm} key={`co-${index}`} /> |
| 126 | + ))} |
| 127 | + </SubMenu> |
| 128 | + <SubMenu |
| 129 | + title="Reddit" |
| 130 | + icon={<SiReddit />} |
| 131 | + suffix={<span className="badge yellow">{redditBookmarks.length}</span>}> |
| 132 | + {redditBookmarks.map((bm, index) => ( |
| 133 | + <BookmarkItem item={bm} key={`co-${index}`} /> |
| 134 | + ))} |
| 135 | + </SubMenu> |
| 136 | + </Menu> |
| 137 | + </SidebarContent> |
| 138 | + </ProSidebar> |
| 139 | + ) |
| 140 | +} |
| 141 | + |
| 142 | +export default BookmarksSidebar |
0 commit comments