From 3a513cd4c632233ee66712eb08bb077e88b8d7ba Mon Sep 17 00:00:00 2001 From: Rupin Khandelwal Date: Fri, 5 Aug 2022 16:28:03 +0200 Subject: [PATCH] Updating menu with new dynamic items like labels and subscriptions, also updating the architecture --- packages/web/components/templates/Menu.tsx | 156 +++++++++++++++------ 1 file changed, 114 insertions(+), 42 deletions(-) diff --git a/packages/web/components/templates/Menu.tsx b/packages/web/components/templates/Menu.tsx index 1179db927..eacae4eff 100644 --- a/packages/web/components/templates/Menu.tsx +++ b/packages/web/components/templates/Menu.tsx @@ -1,63 +1,135 @@ import Link from 'next/link' -import React from 'react' -import { styled } from '../tokens/stitches.config' +import React, { useEffect, useState } from 'react' + +import { useGetLabelsQuery } from '../../lib/networking/queries/useGetLabelsQuery' +import { useGetSubscriptionsQuery } from '../../lib/networking/queries/useGetSubscriptionsQuery' import { ProSidebar, Menu, MenuItem } from 'react-pro-sidebar' import 'react-pro-sidebar/dist/css/styles.css' -// Styles-------------------------------- +import { TagSimple } from 'phosphor-react' -const MenuStyle = styled(ProSidebar, { - display: 'inline-block', - '&:first-child': { - background: '$grayBgActive', - }, -}) +// styles +const proSideBarStyles = { + display: 'inline-block', +} -// Functions------------------------------ +// Types +type MenuItems = { + label: string + query: string + icon: string | JSX.Element | null + href: string +} + +// Functions const calculateTodayMenuItem = () => { const timeZoneHourDiff = -new Date().getTimezoneOffset() / 60 + const hrefStr = `?q=in%3Ainbox+saved%3A${ + new Date(new Date().getTime() - 24 * 3600000).toISOString().split('T')[0] + }Z%${timeZoneHourDiff}B2..*` // console.log(timeZoneHourDiff.toLocaleString('en-US', { // signDisplay: 'always', // })) - const hrefStr = `/home?q=in%3Ainbox+saved%3A${ - new Date(new Date().getTime() - 24 * 3600000).toISOString().split('T')[0] - }Z%${timeZoneHourDiff}B2..*` - return hrefStr } +const createDynamicMenuItems = ( + labels: Array, + subscriptions: Array +) => { + const labelsList: Array = [] + const subscriptionsList: Array = [] + // Create labels list + if (labels) { + labels.map((l) => + labelsList.push({ + label: l.name, + query: `label:"${l.name}"`, + icon: , + href: `?q=label:"${l.name}"`, + }) + ) + } + // create subscriptions list + if (subscriptions) { + subscriptions.map((s) => + subscriptionsList.push({ + label: s.name, + query: `subscription:"${s.subscription}"`, + icon: 'subscription', + href: `?q=subscription:"${s.subscription}"`, + }) + ) + } + return [...labelsList, ...subscriptionsList] +} + +// Component export const Menubar = () => { + const { labels } = useGetLabelsQuery() + const { subscriptions } = useGetSubscriptionsQuery() + + const [menuList, setMenuList] = useState>([]) + + useEffect(() => { + setMenuList([ + { + label: 'Home', + query: 'in:inbox', + icon: null, + href: '/home', + }, + { + label: 'Today', + query: 'in:inbox -label:Newsletter', + icon: null, + href: calculateTodayMenuItem(), + }, + { + label: 'Read Later', + query: 'in:inbox -label:Newsletter', + icon: null, + href: `?q=in inbox+-label Newsletter`, + }, + { + label: 'HighLights', + query: 'type:highlights', + icon: null, + href: `?q=type highlights`, + }, + { + label: 'Newsletters', + query: 'in:inbox label:Newsletter', + icon: null, + href: `?q=in inbox+label Newsletter`, + }, + ...createDynamicMenuItems(labels, subscriptions), + ]) + }, []) + // const menuItems = useMemo(() => { + // if (labels || subscriptions) { + // setMenuList( [...menuList, ...createDynamicMenuItems(labels, subscriptions)]) + // } + // }, [labels, subscriptions, menuList]) + + console.log('menuList', menuList) + return ( - - - - - Home - - - - - Today - - - - - Read Later - - - - - Highlights - - - - - Newsletters - - + + + {menuList.length > 0 && + menuList.map((item) => { + return ( + + + {item.label} + + + ) + })} - + ) }