feat(app): Decouple colorbar UI from service and enable action menu programatic control (#5018)

This commit is contained in:
Alireza authored and GitHub committed 2025-05-08 12:51:30 -04:00
1 parent f14d9ebdd7
commit 1445e12124
41 files changed
+1872 -659

No files matched your search

@@ -1,11 +1,13 @@
import React from 'react';
import React, {
createContext,
useContext,
ReactNode,
useState,
useEffect,
useCallback,
} from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';
/**
* A small container that can render multiple "corner" items (like icons, status)
* in each corner of the viewport: top-left, top-right, bottom-left, bottom-right.
*/
export enum ViewportActionCornersLocations {
topLeft,
topRight,
@@ -33,35 +35,102 @@ const locationClasses = {
),
};
function ViewportActionCorners({ cornerComponents }) {
if (!cornerComponents) {
return null;
}
const ViewportActionCornersContext = createContext<{
registerCorner: (location: ViewportActionCornersLocations, children: ReactNode) => void;
} | null>(null);
function Container({ children }: { children: ReactNode }) {
const [corners, setCorners] = useState({
[ViewportActionCornersLocations.topLeft]: null,
[ViewportActionCornersLocations.topRight]: null,
[ViewportActionCornersLocations.bottomLeft]: null,
[ViewportActionCornersLocations.bottomRight]: null,
});
const registerCorner = useCallback(
(location: ViewportActionCornersLocations, children: ReactNode) => {
setCorners(prev => {
// Only update if the children are different to avoid unnecessary renders
if (prev[location] === children) {
return prev;
}
return {
...prev,
[location]: children,
};
});
},
[]
);
return (
<div
className="pointer-events-none absolute h-full w-full select-none"
onDoubleClick={event => {
event.preventDefault();
event.stopPropagation();
}}
>
{Object.entries(cornerComponents).map(([location, locationArray]) => (
<div
key={location}
className={locationClasses[location]}
>
{locationArray.map(componentInfo => (
<div key={componentInfo.id}>{componentInfo.component}</div>
))}
</div>
))}
</div>
<ViewportActionCornersContext.Provider value={{ registerCorner }}>
{children}
<div
className="pointer-events-none absolute h-full w-full select-none"
onDoubleClick={event => {
event.preventDefault();
event.stopPropagation();
}}
>
{Object.entries(corners).map(([location, children]) => {
if (!children) {
return null;
}
return (
<div
key={location}
className={locationClasses[location]}
>
{children}
</div>
);
})}
</div>
</ViewportActionCornersContext.Provider>
);
}
ViewportActionCorners.propTypes = {
cornerComponents: PropTypes.object.isRequired,
};
function Corner({
location,
children,
}: {
location: ViewportActionCornersLocations;
children: ReactNode;
}) {
const context = useContext(ViewportActionCornersContext);
export { ViewportActionCorners };
if (!context) {
throw new Error('Corner component must be used within a ViewportActionCorners.Container');
}
useEffect(() => {
context.registerCorner(location, children);
}, [context, location, children]);
return null;
}
function TopLeft({ children }: { children: ReactNode }) {
return <Corner location={ViewportActionCornersLocations.topLeft}>{children}</Corner>;
}
function TopRight({ children }: { children: ReactNode }) {
return <Corner location={ViewportActionCornersLocations.topRight}>{children}</Corner>;
}
function BottomLeft({ children }: { children: ReactNode }) {
return <Corner location={ViewportActionCornersLocations.bottomLeft}>{children}</Corner>;
}
function BottomRight({ children }: { children: ReactNode }) {
return <Corner location={ViewportActionCornersLocations.bottomRight}>{children}</Corner>;
}
export const ViewportActionCorners = {
Container,
TopLeft,
TopRight,
BottomLeft,
BottomRight,
};