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

overridable panel not found component #4522

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions app/packages/plugins/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ export function useActivePlugins(type: PluginComponentType, ctx: any) {
});
}

/**
* A react hook that returns a component plugin by name if exist.
* @param name The name of the plugin
* @param ctx Argument passed to the plugin's activator function
* @returns The plugin component or `undefined`
*/
export function usePluginComponent(name: string, ctx?: unknown) {
const plugins = useActivePlugins(PluginComponentType.Component, ctx);
return plugins.find((p) => p.name === name);
}
Comment on lines +233 to +242
Copy link
Contributor

Choose a reason for hiding this comment

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

New Hook Implementation: usePluginComponent

The usePluginComponent hook provides a mechanism to retrieve a component by name from the active plugins, which aligns with the PR's objectives to enhance the plugin system's flexibility. However, consider using optional chaining to handle potential null or undefined values gracefully when accessing properties.

-  return plugins.find((p) => p.name === name);
+  return plugins.find((p) => p.name === name)?.component;
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
/**
* A react hook that returns a component plugin by name if exist.
* @param name The name of the plugin
* @param ctx Argument passed to the plugin's activator function
* @returns The plugin component or `undefined`
*/
export function usePluginComponent(name: string, ctx?: unknown) {
const plugins = useActivePlugins(PluginComponentType.Component, ctx);
return plugins.find((p) => p.name === name);
}
/**
* A react hook that returns a component plugin by name if exist.
* @param name The name of the plugin
* @param ctx Argument passed to the plugin's activator function
* @returns The plugin component or `undefined`
*/
export function usePluginComponent(name: string, ctx?: unknown) {
const plugins = useActivePlugins(PluginComponentType.Component, ctx);
return plugins.find((p) => p.name === name)?.component;
}


/**
* The type of plugin component.
*
Expand Down
9 changes: 4 additions & 5 deletions app/packages/spaces/src/components/Panel.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { CenteredStack } from "@fiftyone/components";
import * as fos from "@fiftyone/state";
import { Typography } from "@mui/material";
import React from "react";
import { PANEL_LOADING_TIMEOUT } from "../constants";
import { PanelContext } from "../contexts";
import { useReactivePanel } from "../hooks";
import { PanelProps } from "../types";
import PanelNotFound from "./PanelNotFound";
import PanelSkeleton from "./PanelSkeleton";
import { StyledPanel } from "./StyledElements";

function Panel({ node }: PanelProps) {
function Panel(props: PanelProps) {
const { node } = props;
const panelName = node.type as string;
const panel = useReactivePanel(panelName);
const dimensions = fos.useDimensions();
Expand All @@ -22,9 +23,7 @@ function Panel({ node }: PanelProps) {
{pending ? (
<PanelSkeleton />
) : (
<Typography>
Panel &quot;{panelName}&quot; no longer exists!
</Typography>
<PanelNotFound panelName={panelName} {...props} />
)}
</CenteredStack>
</StyledPanel>
Expand Down
18 changes: 18 additions & 0 deletions app/packages/spaces/src/components/PanelNotFound.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { usePluginComponent } from "@fiftyone/plugins";
import { Typography } from "@mui/material";
import { PanelProps } from "../types";

export default function PanelNotFound(props: PanelNotFoundPropsType) {
const { panelName } = props;
const CustomPanelNotFound = usePluginComponent("PanelNotFound")?.component;

if (CustomPanelNotFound) {
return <CustomPanelNotFound {...props} />;
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems reasonable from a design POV to allow this to be over-ridden...but I'm not sure on the use case. What is the actual use case for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The main use case atm is to enable displaying a more appropriate message in teams environment

}

return (
<Typography>Panel &quot;{panelName}&quot; no longer exists!</Typography>
);
}

type PanelNotFoundPropsType = PanelProps & { panelName: string };
Loading