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

embeddings cta polish #5001

Merged
merged 1 commit into from
Oct 30, 2024
Merged

embeddings cta polish #5001

merged 1 commit into from
Oct 30, 2024

Conversation

ritch
Copy link
Contributor

@ritch ritch commented Oct 30, 2024

What changes are proposed in this pull request?

Refactors embeddings CTA logic into hook. Fixes issue where compute visualization button was displayed imporperly. Cleans up style of compute viz button in menu (see below).

image

How is this patch tested? If it is not, please explain why.

(Details)

Release Notes

Is this a user-facing change that should be mentioned in the release notes?

  • No. You can skip the rest of this section.
  • Yes. Give a description of this change to be included in the release
    notes for FiftyOne users.

(Details in 1-2 sentences. You can just refer to another PR with a description
if this PR is part of a larger change.)

What areas of FiftyOne does this PR affect?

  • App: FiftyOne application changes
  • Build: Build and test infrastructure changes
  • Core: Core fiftyone Python library changes
  • Documentation: FiftyOne documentation changes
  • Other

Summary by CodeRabbit

  • New Features

    • Introduced a new ComputeVisualizationButton with improved styling and functionality based on the variant prop.
    • Added a new visualization option in the Embeddings component that prompts users to compute a visualization.
    • Launched a custom hook, useComputeVisualization, to manage compute visualization operations.
  • Bug Fixes

    • Streamlined the logic in the EmptyEmbeddings component for better handling of compute visualization availability.
  • Refactor

    • Removed dependencies on previous URI management and panel events to simplify component interactions.

Copy link
Contributor

coderabbitai bot commented Oct 30, 2024

Walkthrough

The changes involve significant modifications to the ComputeVisualizationButton, Embeddings, and EmptyEmbeddings components, along with the introduction of a new custom hook useComputeVisualization. The ComputeVisualizationButton now accepts variant and onClick props, altering its rendering logic. The Embeddings component integrates the new hook to manage visualization computations, while the EmptyEmbeddings component shifts its logic to utilize this hook, removing dependencies on URI management. The new hook encapsulates the logic for determining the availability of compute visualization and provides a prompt function.

Changes

File Path Change Summary
app/packages/embeddings/src/ComputeVisualizationButton.tsx - Updated function signature to accept variant and onClick props.
- Removed dependencies on usePanelEvent and usePanelId.
- Conditional rendering based on variant prop.
app/packages/embeddings/src/Embeddings.tsx - Added import for useComputeVisualization.
- Introduced PlotOption component for visualization computation.
- Removed ComputeVisualizationButton from JSX.
app/packages/embeddings/src/EmptyEmbeddings.tsx - Removed useFirstExistingUri hook.
- Utilized useComputeVisualization for managing visualization availability.
- Updated ComputeVisContent to accept computeViz prop.
app/packages/embeddings/src/useComputeVisualization.ts - Introduced useComputeVisualization hook for managing compute visualization operations.
- Added useFirstExistingUri function.

Possibly related PRs

  • New Embeddings CTA #4997: The changes in the ComputeVisualizationButton component in the main PR directly relate to the introduction of the ComputeVisualizationButton in the retrieved PR, indicating a significant overlap in functionality and purpose.

Suggested reviewers

  • imanjra

🐰 In the meadow, a button so bright,
With variants dancing in morning light.
No more URIs, just a click and a cheer,
Visualizations now, oh so near!
Hopping along, we embrace the new,
A simpler path for all to pursue. 🌼


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 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.

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

@minhtuev minhtuev left a comment

Choose a reason for hiding this comment

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

Looks good 👍

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: 4

🧹 Outside diff range and nitpick comments (7)
app/packages/embeddings/src/ComputeVisualizationButton.tsx (2)

Line range hint 23-36: Improve default button styling and accessibility.

Similar to the box variant, the default button needs accessibility improvements and consistent styling.

Apply these changes:

 <Button
   variant="contained"
+  aria-label="Compute visualization"
   sx={{
     backgroundColor: theme.primary.main,
     color: theme.text.primary,
+    '&:hover': {
+      backgroundColor: theme.primary.dark,
+    },
   }}
   startIcon={<AddIcon />}
   onClick={onClick}
 >
-  Compute Visualization
+  {BUTTON_TEXT}
 </Button>

Line range hint 1-36: Consider extracting button styles to a theme constant.

The button styles are duplicated and could be better managed through theme constants.

Consider creating a dedicated theme constant for the button styles:

// In your theme file
export const computeVisualizationButton = {
  box: {
    borderColor: theme.primary.main,
    color: theme.primary.main,
    '&:hover': {
      borderColor: theme.primary.dark,
      color: theme.primary.dark,
    },
  },
  default: {
    backgroundColor: theme.primary.main,
    color: theme.text.primary,
    '&:hover': {
      backgroundColor: theme.primary.dark,
    },
  },
};

// Then in the component
sx={variant === "box" ? theme.computeVisualizationButton.box : theme.computeVisualizationButton.default}
app/packages/embeddings/src/useComputeVisualization.ts (1)

17-39: Consider adding error tracking integration

While the hook effectively encapsulates the compute visualization logic, consider integrating with an error tracking system to monitor failed computations or invalid panel states in production.

This would help in:

  1. Monitoring computation failures
  2. Tracking invalid panel states
  3. Improving user experience by handling edge cases gracefully
app/packages/embeddings/src/EmptyEmbeddings.tsx (2)

6-12: Add TypeScript types for the compute visualization hook.

The computeViz object lacks explicit TypeScript types. Consider adding proper type definitions to improve type safety and code maintainability.

+interface ComputeVisualization {
+  isAvailable: boolean;
+  prompt: () => void;
+}

-const computeViz = useComputeVisualization();
+const computeViz: ComputeVisualization = useComputeVisualization();

110-119: Add prop types and simplify the component structure.

The component could benefit from TypeScript prop types and a simpler structure.

+interface ComputeVisContentProps {
+  computeViz: {
+    prompt: () => void;
+  };
+}

-function ComputeVisContent({ computeViz }) {
+function ComputeVisContent({ computeViz }: ComputeVisContentProps) {
   const theme = useTheme();
   const secondaryBodyColor = theme.text.secondary;
   return (
-    <>
-      <Grid item>
-        <ComputeVisualizationButton onClick={() => computeViz.prompt()} />
-      </Grid>
-    </>
+    <Grid item>
+      <ComputeVisualizationButton onClick={() => computeViz.prompt()} />
+    </Grid>
   );
 }
app/packages/embeddings/src/Embeddings.tsx (2)

Line range hint 1-30: Remove unused import ComputeVisualizationButton

Since the component has been refactored to use the new useComputeVisualization hook, the ComputeVisualizationButton import is no longer needed.

- import ComputeVisualizationButton from "./ComputeVisualizationButton";

55-55: Consider adding explicit type annotation for computeViz

To improve type safety and code readability, consider adding an explicit type annotation for the computeViz object.

- const computeViz = useComputeVisualization();
+ const computeViz: ComputeVisualizationResult = useComputeVisualization();
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between c717875 and abf4525.

📒 Files selected for processing (4)
  • app/packages/embeddings/src/ComputeVisualizationButton.tsx (2 hunks)
  • app/packages/embeddings/src/Embeddings.tsx (4 hunks)
  • app/packages/embeddings/src/EmptyEmbeddings.tsx (3 hunks)
  • app/packages/embeddings/src/useComputeVisualization.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (4)
app/packages/embeddings/src/ComputeVisualizationButton.tsx (1)

Pattern **/*.{ts,tsx}: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.

app/packages/embeddings/src/Embeddings.tsx (1)

Pattern **/*.{ts,tsx}: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.

app/packages/embeddings/src/EmptyEmbeddings.tsx (1)

Pattern **/*.{ts,tsx}: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.

app/packages/embeddings/src/useComputeVisualization.ts (1)

Pattern **/*.{ts,tsx}: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.

🔇 Additional comments (4)
app/packages/embeddings/src/useComputeVisualization.ts (1)

1-4: LGTM: Imports are well-organized and properly typed

The imports are clean, necessary, and properly scoped to their respective packages.

app/packages/embeddings/src/EmptyEmbeddings.tsx (1)

56-58: LGTM! Clean conditional rendering implementation.

The refactored code improves maintainability by centralizing the compute visualization logic in the hook and using clear conditional rendering.

app/packages/embeddings/src/Embeddings.tsx (2)

95-102: LGTM! Clean implementation of compute visualization option

The new compute visualization option is well-integrated into the existing UI pattern using PlotOption. The conditional rendering and event handling are implemented correctly.


Line range hint 1-102: Verify complete removal of old button styles

Let's ensure all old button styles have been cleaned up as mentioned in the PR objectives.

✅ Verification successful

Let me check the actual button style implementation to verify if there are any old styles remaining.


Button styles have been properly cleaned up

The button implementation now uses MUI's Button component with inline styles through the sx prop, and there are no old custom button styles remaining. The styling is consistent with the PR's cleanup objectives.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any remaining button-related styles in the codebase
rg -g '*.{ts,tsx,css,scss}' -i '(computevisualization.*button|button.*style)' app/packages/embeddings/

Length of output: 594


Script:

#!/bin/bash
# Check the implementation of ComputeVisualizationButton component
cat app/packages/embeddings/src/ComputeVisualizationButton.tsx

# Check for any styled-components related to buttons
rg -g '*.{ts,tsx}' 'styled.*button' app/packages/embeddings/src/

Length of output: 941

Comment on lines +10 to +21
<Button
variant="outlined"
sx={{
borderColor: theme.primary.main,
color: theme.primary.main,
}}
startIcon={<AddIcon />}
onClick={onClick}
>
Compute Visualization
</Button>
);
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance button accessibility and extract common styles.

The button lacks proper ARIA attributes and the styles could be better organized.

Consider these improvements:

 <Button
   variant="outlined"
+  aria-label="Compute visualization"
   sx={{
     borderColor: theme.primary.main,
     color: theme.primary.main,
+    '&:hover': {
+      borderColor: theme.primary.dark,
+      color: theme.primary.dark,
+    },
   }}
   startIcon={<AddIcon />}
   onClick={onClick}
 >
-  Compute Visualization
+  {BUTTON_TEXT}
 </Button>

And at the top of the file:

const BUTTON_TEXT = 'Compute Visualization';

import AddIcon from "@mui/icons-material/Add";
import { useTheme } from "@fiftyone/components";

export default function ComputeVisualizationButton({ uri }) {
const triggerEvent = usePanelEvent();
export default function ComputeVisualizationButton({ variant, onClick }) {
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

Add TypeScript type definitions for props.

The component props should be properly typed for better type safety and documentation.

Apply this diff:

-export default function ComputeVisualizationButton({ variant, onClick }) {
+interface ComputeVisualizationButtonProps {
+  variant?: 'box';
+  onClick: () => void;
+}
+
+export default function ComputeVisualizationButton({ 
+  variant, 
+  onClick 
+}: ComputeVisualizationButtonProps) {
📝 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
export default function ComputeVisualizationButton({ variant, onClick }) {
interface ComputeVisualizationButtonProps {
variant?: 'box';
onClick: () => void;
}
export default function ComputeVisualizationButton({
variant,
onClick
}: ComputeVisualizationButtonProps) {

Comment on lines +6 to +15
const useFirstExistingUri = (uris: string[]) => {
const availableOperators = useMemo(() => listLocalAndRemoteOperators(), []);
return useMemo(() => {
const existingUri = uris.find((uri) =>
availableOperators.allOperators.some((op) => op.uri === uri)
);
const exists = Boolean(existingUri);
return { firstExistingUri: existingUri, exists };
}, [availableOperators, uris]);
};
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add TypeScript types and documentation to improve maintainability

The hook logic is sound, but could benefit from better typing and documentation.

Consider applying these improvements:

+/**
+ * Hook to find the first existing URI from a list of URIs
+ * @param uris - Array of URIs to check
+ * @returns Object containing the first existing URI and whether it exists
+ */
-const useFirstExistingUri = (uris: string[]) => {
+interface UseFirstExistingUriResult {
+  firstExistingUri: string | undefined;
+  exists: boolean;
+}
+const useFirstExistingUri = (uris: string[]): UseFirstExistingUriResult => {
   const availableOperators = useMemo(() => listLocalAndRemoteOperators(), []);
   return useMemo(() => {
     const existingUri = uris.find((uri) =>
       availableOperators.allOperators.some((op) => op.uri === uri)
     );
     const exists = Boolean(existingUri);
     return { firstExistingUri: existingUri, exists };
   }, [availableOperators, uris]);
 };
📝 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 useFirstExistingUri = (uris: string[]) => {
const availableOperators = useMemo(() => listLocalAndRemoteOperators(), []);
return useMemo(() => {
const existingUri = uris.find((uri) =>
availableOperators.allOperators.some((op) => op.uri === uri)
);
const exists = Boolean(existingUri);
return { firstExistingUri: existingUri, exists };
}, [availableOperators, uris]);
};
/**
* Hook to find the first existing URI from a list of URIs
* @param uris - Array of URIs to check
* @returns Object containing the first existing URI and whether it exists
*/
interface UseFirstExistingUriResult {
firstExistingUri: string | undefined;
exists: boolean;
}
const useFirstExistingUri = (uris: string[]): UseFirstExistingUriResult => {
const availableOperators = useMemo(() => listLocalAndRemoteOperators(), []);
return useMemo(() => {
const existingUri = uris.find((uri) =>
availableOperators.allOperators.some((op) => op.uri === uri)
);
const exists = Boolean(existingUri);
return { firstExistingUri: existingUri, exists };
}, [availableOperators, uris]);
};

@ritch ritch merged commit de79980 into develop Oct 30, 2024
10 of 11 checks passed
@ritch ritch deleted the embeddings_cta_polish branch October 30, 2024 03:05
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