Skip to content

fixed #348 #396

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions apps/web/components/email-list/EmailListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import { Button } from "@/components/ui/button";
import { findCtaLink } from "@/utils/parse/parseHtml.client";
import { ErrorBoundary } from "@/components/ErrorBoundary";
import { internalDateToDate } from "@/utils/date";
import { Badge } from "@/components/ui/badge";
import { isDefined } from "@/utils/types";
import { useLabels } from "@/hooks/useLabels";

export const EmailListItem = forwardRef(
(
Expand All @@ -45,13 +48,25 @@ export const EmailListItem = forwardRef(
ref: ForwardedRef<HTMLLIElement>,
) => {
const { thread, splitView, onSelected } = props;
const { userLabels } = useLabels();

const lastMessage = thread.messages?.[thread.messages.length - 1];

const isUnread = useMemo(() => {
return lastMessage?.labelIds?.includes("UNREAD");
}, [lastMessage?.labelIds]);

const labelsToDisplay = useMemo(() => {
const labelIds = lastMessage?.labelIds;
return labelIds
?.map((id) => {
const label = userLabels[Number(id)];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we convert to number?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise it will be of type any and typescript complains

if (!label) return null;
return { id, name: label.name };
})
.filter(isDefined);
}, [lastMessage?.labelIds, userLabels]);
Comment on lines +59 to +68
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix the label lookup logic.

The current implementation treats userLabels as an object with numeric keys, but according to the useLabels hook implementation, it returns an array. You should use array methods like find to locate labels by ID.

const labelsToDisplay = useMemo(() => {
  const labelIds = lastMessage?.labelIds;
  return labelIds
    ?.map((id) => {
-      const label = userLabels[Number(id)];
+      const label = userLabels.find(label => label.id === id);
      if (!label) return null;
      return { id, name: label.name };
    })
    .filter(isDefined);
}, [lastMessage?.labelIds, userLabels]);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const labelsToDisplay = useMemo(() => {
const labelIds = lastMessage?.labelIds;
return labelIds
?.map((id) => {
const label = userLabels[Number(id)];
if (!label) return null;
return { id, name: label.name };
})
.filter(isDefined);
}, [lastMessage?.labelIds, userLabels]);
const labelsToDisplay = useMemo(() => {
const labelIds = lastMessage?.labelIds;
return labelIds
?.map((id) => {
const label = userLabels.find(label => label.id === id);
if (!label) return null;
return { id, name: label.name };
})
.filter(isDefined);
}, [lastMessage?.labelIds, userLabels]);


const preventPropagation = useCallback(
(e: React.MouseEvent | React.KeyboardEvent) => e.stopPropagation(),
[],
Expand Down Expand Up @@ -138,6 +153,9 @@ export const EmailListItem = forwardRef(
)}
<div className="ml-2 min-w-0 overflow-hidden text-foreground">
{lastMessage.headers.subject}
{labelsToDisplay && labelsToDisplay.length > 0 && (
<LabelsDisplay labels={labelsToDisplay} />
)}
</div>
<div className="ml-4 mr-6 flex flex-1 items-center overflow-hidden truncate font-normal leading-5 text-muted-foreground">
{decodedSnippet}
Expand Down Expand Up @@ -195,6 +213,9 @@ export const EmailListItem = forwardRef(
<div className="mt-1.5 whitespace-nowrap text-sm leading-6">
<div className="min-w-0 overflow-hidden font-medium text-foreground">
{lastMessage.headers.subject}
{labelsToDisplay && labelsToDisplay.length > 0 && (
<LabelsDisplay labels={labelsToDisplay} />
)}
</div>
<div className="mr-6 mt-0.5 flex flex-1 items-center overflow-hidden truncate pl-1 font-normal leading-5 text-muted-foreground">
{decodedSnippet}
Expand All @@ -216,3 +237,25 @@ export const EmailListItem = forwardRef(
);

EmailListItem.displayName = "EmailListItem";

type Label = {
id: string;
name: string;
};

type LabelBadgesProps = {
labels: Label[] | undefined;
};

function LabelsDisplay({ labels }: LabelBadgesProps) {
if (!labels || labels.length === 0) return null;
return (
<span className="ml-2 inline-flex flex-wrap items-center gap-1">
{labels.map((label) => (
<Badge variant="secondary" key={label.id} className="text-xs">
{label.name}
</Badge>
))}
</span>
);
}