Skip to content
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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Add mail labels #355

wants to merge 5 commits into from

Conversation

RicSala
Copy link
Contributor

@RicSala RicSala commented Feb 23, 2025

Add coloured labels to the EmailListItem component in the apps/web directory.:

image

New component for label rendering:

Enhancements to user labels:

Summary by CodeRabbit

  • New Features
    • Enhanced email thread display by showcasing user labels as colored badges.
    • Introduced a hoverable interface to reveal additional labels when there are more than can be immediately displayed.
    • Updated user label type to include optional color properties for improved visual representation.

Copy link

vercel bot commented Feb 23, 2025

@RicSala is attempting to deploy a commit to the Inbox Zero Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Feb 23, 2025

Walkthrough

The changes enhance the email thread display by integrating user label functionality. In the email list component, a constant limits the number of thread labels, and the useLabels hook is used to retrieve labels which are then processed via useMemo. Conditional rendering displays the labels using a new EmailLabels component. Additionally, the UserLabel type in the labels hook has been updated to include color properties.

Changes

File(s) Change Summary
apps/web/components/email-list/EmailListItem.tsx Added MAX_THREAD_LABELS constant; integrated the useLabels hook and useMemo to process thread labels; implemented conditional rendering with EmailLabels.
apps/web/hooks/useLabels.ts Updated the UserLabel type to include a color property with textColor and backgroundColor fields.

Sequence Diagram(s)

sequenceDiagram
    participant EL as EmailListItem
    participant UL as useLabels Hook
    participant UM as useMemo Processing
    participant LG as EmailLabels Component
    participant UI as UI Renderer

    EL->>UL: Retrieve userLabels
    EL->>UM: Process thread messages for labels (useMemo)
    EL->>LG: Render EmailLabels if labels exist
    alt More labels than allowed
      EL->>UI: Wrap EmailLabels in a HoverCard
    end
    LG->>UI: Render each label as a Badge with color styling
Loading

Poem

I'm a coding rabbit, hopping with glee,
New labels prance in our email spree.
Colorful badges shine on screen,
Hovering cards reveal what's seen.
In this code garden, I happily play—
A bright bunny's dance in the digital day! 🐰

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

apps/web/components/email-list/EmailListItem.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

Error: Failed to load parser '@typescript-eslint/parser' declared in 'apps/web/.eslintrc.json': Cannot find module '@typescript-eslint/parser'
Require stack:

  • /apps/web/.eslintrc.json
    at Module._resolveFilename (node:internal/modules/cjs/loader:1248:15)
    at Function.resolve (node:internal/modules/helpers:145:19)
    at Object.resolve (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2346:46)
    at ConfigArrayFactory._loadParser (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3325:39)
    at ConfigArrayFactory._normalizeObjectConfigDataBody (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3099:43)
    at _normalizeObjectConfigDataBody.next ()
    at ConfigArrayFactory._normalizeObjectConfigData (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3040:20)
    at _normalizeObjectConfigData.next ()
    at ConfigArrayFactory.loadInDirectory (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2886:28)
    at CascadingConfigArrayFactory._loadConfigInAncestors (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3871:46)
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
apps/web/components/email-list/EmailListItem.tsx (3)

24-24: Use import type for type-only imports.

The UserLabel import is only used as a type and should be imported using the type keyword.

-import { useLabels, UserLabel } from "@/hooks/useLabels";
+import { useLabels } from "@/hooks/useLabels";
+import type { UserLabel } from "@/hooks/useLabels";
🧰 Tools
🪛 Biome (1.9.4)

[error] 24-24: Some named imports are only used as types.

This import is only used as a type.

Importing the types with import type ensures that they are removed by the compilers and avoids loading unnecessary modules.
Safe fix: Add inline type keywords.

(lint/style/useImportType)


83-92: Optimize array operations using flatMap.

The chain of .map().flat() can be simplified using .flatMap() for better readability and performance.

    const threadLabels = useMemo(() => {
-      return thread.messages
-        .map((message) =>
+      return thread.messages.flatMap((message) =>
           message.labelIds
             ?.map((id) => userLabels?.find((label) => label.id === id))
-            .filter(Boolean),
-        )
-        .flat()
+            .filter(Boolean) ?? []
+        )
         .filter(isDefined);
    }, [thread.messages, userLabels]);
🧰 Tools
🪛 Biome (1.9.4)

[error] 84-90: The call chain .map().flat() can be replaced with a single .flatMap() call.

Safe fix: Replace the chain with .flatMap().

(lint/complexity/useFlatMap)


270-295: Consider performance optimization for the LabelsGroup component.

The component looks good, but consider memoizing it since it's used within a HoverCard that might trigger frequent re-renders.

-const LabelsGroup = ({
+const LabelsGroup = memo(({
   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>
   );
-};
+});

+LabelsGroup.displayName = "LabelsGroup";
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fa52f9c and 32f1db4.

📒 Files selected for processing (2)
  • apps/web/components/email-list/EmailListItem.tsx (4 hunks)
  • apps/web/hooks/useLabels.ts (1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
apps/web/components/email-list/EmailListItem.tsx

[error] 24-24: Some named imports are only used as types.

This import is only used as a type.

Importing the types with import type ensures that they are removed by the compilers and avoids loading unnecessary modules.
Safe fix: Add inline type keywords.

(lint/style/useImportType)


[error] 84-90: The call chain .map().flat() can be replaced with a single .flatMap() call.

Safe fix: Replace the chain with .flatMap().

(lint/complexity/useFlatMap)

🔇 Additional comments (2)
apps/web/hooks/useLabels.ts (1)

6-14: LGTM! Well-structured type definition for label colors.

The UserLabel type is well-defined with clear color properties that enable complete customization of label appearance.

apps/web/components/email-list/EmailListItem.tsx (1)

163-188: LGTM! Well-implemented label display with hover functionality.

The implementation of label display with a "show more" hover card is clean and user-friendly. The use of stopPropagation in the hover button prevents unintended email selection.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
apps/web/components/email-list/EmailListItem.tsx (3)

24-24: Use type-only import for UserLabel.

Since UserLabel is only used as a type, it should be imported using the type keyword to ensure proper tree-shaking.

-import { useLabels, UserLabel } from "@/hooks/useLabels";
+import { useLabels, type UserLabel } from "@/hooks/useLabels";
🧰 Tools
🪛 Biome (1.9.4)

[error] 24-24: Some named imports are only used as types.

This import is only used as a type.

Importing the types with import type ensures that they are removed by the compilers and avoids loading unnecessary modules.
Safe fix: Add inline type keywords.

(lint/style/useImportType)


83-92: Optimize array operations using flatMap.

The chain of .map().flat() can be simplified using .flatMap() for better readability and performance.

    const threadLabels = useMemo(() => {
-     return thread.messages
-       .map((message) =>
-         message.labelIds
-           ?.map((id) => userLabels?.find((label) => label.id === id))
-           .filter(Boolean),
-       )
-       .flat()
-       .filter(isDefined);
+     return thread.messages.flatMap((message) =>
+       (message.labelIds ?? [])
+         .map((id) => userLabels?.find((label) => label.id === id))
+         .filter(isDefined)
+     );
    }, [thread.messages, userLabels]);
🧰 Tools
🪛 Biome (1.9.4)

[error] 84-90: The call chain .map().flat() can be replaced with a single .flatMap() call.

Safe fix: Replace the chain with .flatMap().

(lint/complexity/useFlatMap)


270-295: Remove empty className prop from Badge component.

The Badge component has an empty className prop which can be removed.

        <Badge
-         className=""
          key={label.id}
          style={{
            color: label?.color?.textColor,
            backgroundColor: label?.color?.backgroundColor,
          }}
        >
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 32f1db4 and 8b128ee.

📒 Files selected for processing (2)
  • apps/web/components/email-list/EmailListItem.tsx (4 hunks)
  • apps/web/hooks/useLabels.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/web/hooks/useLabels.ts
🧰 Additional context used
🪛 Biome (1.9.4)
apps/web/components/email-list/EmailListItem.tsx

[error] 24-24: Some named imports are only used as types.

This import is only used as a type.

Importing the types with import type ensures that they are removed by the compilers and avoids loading unnecessary modules.
Safe fix: Add inline type keywords.

(lint/style/useImportType)


[error] 84-90: The call chain .map().flat() can be replaced with a single .flatMap() call.

Safe fix: Replace the chain with .flatMap().

(lint/complexity/useFlatMap)

🔇 Additional comments (1)
apps/web/components/email-list/EmailListItem.tsx (1)

163-188: LGTM! Good implementation of label display with hover functionality.

The implementation effectively handles:

  • Conditional rendering of labels
  • "Show more" functionality using hover card
  • Event propagation prevention

@elie222
Copy link
Owner

elie222 commented Feb 23, 2025

Thanks. Take a look at the nitpick comments from CodeRabbit. They'd be good to use here. @RicSala

Comment on lines 162 to 187
{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>
)}
Copy link
Owner

Choose a reason for hiding this comment

The 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

Copy link
Owner

Choose a reason for hiding this comment

The 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

Copy link
Owner

Choose a reason for hiding this comment

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

possible we reuse this in other places in the app

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
2- make /components/badge more flexible (just a bit concerned about giving it "too much flexibility"), for example adding a style attr
3- Creating a "emailLabel" component, that uses the shadcn badge (ui/badge), and consistently use that one for email labels
4- Add the rest of the colors to badgeVariants

In my opinion, 1 and 3 are the cleanest, but lmk if you think otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@elie222 ☝🏻

@RicSala
Copy link
Contributor Author

RicSala commented Feb 23, 2025

Thanks. Take a look at the nitpick comments from CodeRabbit. They'd be good to use here. @RicSala

I was on it! They are fixed and gone now!

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
apps/web/components/email-list/EmailListItem.tsx (2)

24-28: Consider moving MAX_THREAD_LABELS to a shared constants file.

Since this constant might be reused in other components (e.g., in email details view), consider moving it to a shared constants file.


79-92: Extract label processing logic into a custom hook.

Consider moving the label processing logic into a custom hook (e.g., useThreadLabels) to improve reusability and reduce component complexity. This would align with the component extraction suggestions from previous reviews.

Example implementation:

// hooks/useThreadLabels.ts
export function useThreadLabels(thread: Thread) {
  const { userLabels } = useLabels();
  
  const threadLabels = useMemo(() => {
    return thread.messages
      .flatMap((message) =>
        message.labelIds
          ?.map((id) => userLabels?.find((label) => label.id === id))
          .filter(Boolean),
      )
      .filter(isDefined);
  }, [thread.messages, userLabels]);

  return {
    threadLabels,
    hasLabels: threadLabels.length > 0,
    hasMoreLabels: threadLabels.length > MAX_THREAD_LABELS,
  };
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8b128ee and 76fcc06.

📒 Files selected for processing (1)
  • apps/web/components/email-list/EmailListItem.tsx (3 hunks)
🔇 Additional comments (1)
apps/web/components/email-list/EmailListItem.tsx (1)

160-165: LGTM! Clean implementation of label display.

The implementation correctly follows the component extraction pattern and handles the display of labels in a clean, maintainable way.

@@ -71,6 +76,21 @@ export const EmailListItem = forwardRef(

const cta = findCtaLink(lastMessage.textHtml);

const { userLabels } = useLabels();
Copy link
Owner

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

Copy link
Contributor Author

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!

Copy link
Owner

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 in EmailListItem:

Both perspectives have valid points, but I would side with @elie222's suggestion for these reasons:

  1. Component Responsibility

    • Even though SWR will deduplicate the actual API requests, the hook execution itself still runs for every row item
    • The EmailListItem component should ideally be focused on presentation, not data fetching
    • Moving data fetching up reduces the cognitive load when reading the item component
  2. Performance Optimization

    • While SWR handles API deduplication, the hook still:
      • Creates new state instances per row
      • Runs its internal logic for each row
      • Triggers re-renders on each row when data changes
    • With many emails in the list, this overhead adds up
  3. Props vs. Hooks

    • Props make dependencies explicit and easier to test
    • Props allow better control over when re-renders happen
    • The parent component can optimize when labels need to be refetched

Here's how I would refactor it:

// ... existing code ...
const { userLabels } = useLabels();

return (
  <div>
    {threads.map(thread => (
      <EmailListItem 
        key={thread.id}
        userLabels={userLabels}
        thread={thread}
        // ... other props ...
      />
    ))}
  </div>
);
// ... existing code ...
export const EmailListItem = forwardRef(
  (
    props: {
      userLabels: Label[];  // Add this prop
      userEmailAddress: string;
      thread: Thread;
      // ... other props ...
    },
    ref: ForwardedRef<HTMLLIElement>,
  ) => {
    // Remove useLabels() hook
    // ... rest of component ...
  }
);

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.

Copy link
Owner

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(

const cta = findCtaLink(lastMessage.textHtml);

const { userLabels } = useLabels();

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!

@elie222
Copy link
Owner

elie222 commented Mar 2, 2025

We have a Gmail provider that I used for other pages just now:
#367

Can probably do the same thing for Mail page. The gmail provider just stores the state once for us so a little more efficient although probably minor difference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants