-
Notifications
You must be signed in to change notification settings - Fork 384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add mail labels #355
base: main
Are you sure you want to change the base?
Add mail labels #355
Changes from 2 commits
743c3d9
32f1db4
8b128ee
14f36f6
76fcc06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,13 @@ import { Button } from "@/components/ui/button"; | |
import { findCtaLink } from "@/utils/parse/parseHtml.client"; | ||
import { ErrorBoundary } from "@/components/ErrorBoundary"; | ||
import { internalDateToDate } from "@/utils/date"; | ||
import { useLabels, UserLabel } from "@/hooks/useLabels"; | ||
import { Badge } from "@/components/ui/badge"; | ||
import { MoreVertical } from "lucide-react"; | ||
import { HoverCard } from "@/components/HoverCard"; | ||
import { isDefined } from "@/utils/types"; | ||
|
||
const MAX_THREAD_LABELS = 3; | ||
|
||
export const EmailListItem = forwardRef( | ||
( | ||
|
@@ -71,6 +78,22 @@ export const EmailListItem = forwardRef( | |
|
||
const cta = findCtaLink(lastMessage.textHtml); | ||
|
||
const { userLabels } = useLabels(); | ||
|
||
const threadLabels = useMemo(() => { | ||
RicSala marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return thread.messages | ||
.map((message) => | ||
message.labelIds | ||
?.map((id) => userLabels?.find((label) => label.id === id)) | ||
.filter(Boolean), | ||
) | ||
.flat() | ||
.filter(isDefined); | ||
}, [thread.messages, userLabels]); | ||
|
||
const hasLabels = threadLabels.length > 0; | ||
const hasMoreLabels = threadLabels.length > MAX_THREAD_LABELS; | ||
|
||
return ( | ||
<ErrorBoundary extra={{ props, cta, decodedSnippet }}> | ||
<li | ||
|
@@ -137,6 +160,32 @@ export const EmailListItem = forwardRef( | |
</Link> | ||
</Button> | ||
)} | ||
{hasLabels && ( | ||
<LabelsGroup | ||
labels={threadLabels} | ||
maxShown={MAX_THREAD_LABELS} | ||
/> | ||
)} | ||
{hasMoreLabels && ( | ||
<HoverCard | ||
content={<LabelsGroup labels={threadLabels} wraps />} | ||
> | ||
<Button | ||
variant="ghost" | ||
size="icon" | ||
className="w-fit px-2 hover:bg-transparent" | ||
onClick={(e) => { | ||
e.stopPropagation(); | ||
e.preventDefault(); | ||
}} | ||
> | ||
<MoreVertical | ||
size={16} | ||
className="text-muted-foreground" | ||
/> | ||
</Button> | ||
</HoverCard> | ||
)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make this whole thing a component. i think all the new code you added can be a component There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can email move it into its own file in the email-list folder There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. possible we reuse this in other places in the app There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done btw, I used the badge from /ui folder because the other one (the one in components) was a bit rigid for the gmail color schemas. I could either: 1- leave it as it is (using /ui/badge) In my opinion, 1 and 3 are the cleanest, but lmk if you think otherwise. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @elie222 ☝🏻 |
||
<div className="ml-2 min-w-0 overflow-hidden text-foreground"> | ||
{lastMessage.headers.subject} | ||
</div> | ||
|
@@ -217,3 +266,30 @@ export const EmailListItem = forwardRef( | |
); | ||
|
||
EmailListItem.displayName = "EmailListItem"; | ||
|
||
const LabelsGroup = ({ | ||
labels, | ||
maxShown, | ||
wraps = false, | ||
}: { | ||
labels: UserLabel[]; | ||
maxShown?: number; | ||
wraps?: boolean; | ||
}) => { | ||
return ( | ||
<div className={clsx("ml-2 flex gap-2", { "flex-wrap": wraps })}> | ||
{labels.slice(0, maxShown).map((label) => ( | ||
<Badge | ||
className="" | ||
key={label.id} | ||
style={{ | ||
color: label?.color.textColor, | ||
backgroundColor: label?.color.backgroundColor, | ||
}} | ||
> | ||
{label.name} | ||
</Badge> | ||
))} | ||
</div> | ||
); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so to optimise here, don't do uselabels on every row item. but do it a level above, and pass in the labels. basically any logic here is being run on every single row. and you could just have it run once for the table
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we are using SWR, actually I think only one request will be made! Others will be dedup (afaik).
I can move it to the parent, just wanted to avoid cluttering more the attributes of EmailListItem comp!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just asked Cursor. What it had to say. I considered your approach too, but I agree with the AI 😂
Cursor:
Right.Let me analyze this PR discussion about the
useLabels
hook inEmailListItem
:Both perspectives have valid points, but I would side with @elie222's suggestion for these reasons:
Component Responsibility
EmailListItem
component should ideally be focused on presentation, not data fetchingPerformance Optimization
Props vs. Hooks
Here's how I would refactor it:
While the extra prop might seem like clutter, it's a worthwhile tradeoff for better performance and clearer component responsibilities. The parent component is a more appropriate place for data fetching that affects multiple child components.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I asked it btw:
what do you think about this discussion on a pr?
apps/web/components/email-list/EmailListItem.tsx
@@ -71,6 +76,21 @@ export const EmailListItem = forwardRef(
Owner
@elie222 elie222 yesterday
so to optimise here, don't do uselabels on every row item. but do it a level above, and pass in the labels. basically any logic here is being run on every single row. and you could just have it run once for the table
Contributor
Author
@RicSala RicSala yesterday
As we are using SWR, actually I think only one request will be made! Others will be dedup (afaik).
I can move it to the parent, just wanted to avoid cluttering more the attributes of EmailListItem comp!