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

refactor: improve NodeOutputField and NodeInputField components #3235

Merged
merged 11 commits into from
Aug 13, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { scapedJSONStringfy } from "../../../../utils/reactflowUtils";
import useFetchDataOnMount from "../../../hooks/use-fetch-data-on-mount";
import useHandleOnNewValue from "../../../hooks/use-handle-new-value";
import NodeInputInfo from "../NodeInputInfo";
import HandleRenderComponent from "../handleRenderComponent";

export default function NodeInputField({
Expand All @@ -31,7 +32,6 @@ export default function NodeInputField({
showNode,
}: NodeInputFieldComponentType): JSX.Element {
const ref = useRef<HTMLDivElement>(null);
const infoHtml = useRef<HTMLDivElement & ReactNode>(null);
const nodes = useFlowStore((state) => state.nodes);
const edges = useFlowStore((state) => state.edges);
const myData = useTypesStore((state) => state.data);
Expand All @@ -57,44 +57,36 @@ export default function NodeInputField({

useFetchDataOnMount(data.node!, handleNodeClass, name, postTemplateValue);

useEffect(() => {
// @ts-ignore
infoHtml.current = (
<div className="h-full w-full break-words">
{info.split("\n").map((line, index) => (
<p key={index} className="block">
{line}
</p>
))}
</div>
);
}, [info]);

useEffect(() => {
if (optionalHandle && optionalHandle.length === 0) {
optionalHandle = null;
}
}, [optionalHandle]);

const displayHandle =
!LANGFLOW_SUPPORTED_TYPES.has(type ?? "") || optionalHandle;
!LANGFLOW_SUPPORTED_TYPES.has(type ?? "") ||
(optionalHandle && optionalHandle.length > 0);

const Handle = (
<HandleRenderComponent
left={true}
nodes={nodes}
tooltipTitle={tooltipTitle}
proxy={proxy}
id={id}
title={title}
edges={edges}
myData={myData}
colors={colors}
setFilterEdge={setFilterEdge}
showNode={showNode}
testIdComplement={`${data?.type?.toLowerCase()}-${showNode ? "shownode" : "noshownode"}`}
/>
);

return !showNode ? (
displayHandle ? (
<HandleRenderComponent
left={true}
nodes={nodes}
tooltipTitle={tooltipTitle}
proxy={proxy}
id={id}
title={title}
edges={edges}
myData={myData}
colors={colors}
setFilterEdge={setFilterEdge}
showNode={showNode}
testIdComplement={`${data?.type?.toLowerCase()}-noshownode`}
/>
Handle
) : (
<></>
)
Expand Down Expand Up @@ -125,7 +117,7 @@ export default function NodeInputField({
</span>
<div className="">
{info !== "" && (
<ShadTooltip content={infoHtml.current}>
<ShadTooltip content={<NodeInputInfo info={info} />}>
{/* put div to avoid bug that does not display tooltip */}
<div className="cursor-help">
<IconComponent
Expand All @@ -137,24 +129,8 @@ export default function NodeInputField({
)}
</div>
</div>
{(Array.isArray(displayHandle)
? displayHandle.length > 0
: displayHandle) && (
<HandleRenderComponent
left={true}
nodes={nodes}
tooltipTitle={tooltipTitle}
proxy={proxy}
id={id}
title={title}
edges={edges}
myData={myData}
colors={colors}
setFilterEdge={setFilterEdge}
showNode={showNode}
testIdComplement={`${data?.type?.toLowerCase()}-shownode`}
/>
)}

{displayHandle && Handle}
{data.node?.template[name] !== undefined && (
<div className="mt-2 w-full">
<ParameterRenderComponent
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function NodeInputInfo({ info }: { info: string }) {
return (
<div className="h-full w-full break-words">
{info.split("\n").map((line, index) => (
<p key={index} className="block">
{line}
</p>
))}
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,10 @@ export default function NodeOutputField({

function handleOutputWShortcut() {
if (!displayOutputPreview || unknownOutput) return;
if (isThereModal() && !openOutputModal) return;
if (selected) {
setOpenOutputModal((state) => !state);
}
}

const output = useShortcutsStore((state) => state.output);
useHotkeys(output, handleOutputWShortcut, { preventDefault });

Expand Down Expand Up @@ -123,7 +121,7 @@ export default function NodeOutputField({
}
}, [disabledOutput]);

return !showNode ? (
const Handle = (
<HandleRenderComponent
left={false}
nodes={nodes}
Expand All @@ -135,8 +133,12 @@ export default function NodeOutputField({
colors={colors}
setFilterEdge={setFilterEdge}
showNode={showNode}
testIdComplement={`${data?.type?.toLowerCase()}-noshownode`}
testIdComplement={`${data?.type?.toLowerCase()}-${showNode ? "shownode" : "noshownode"}`}
/>
);

return !showNode ? (
Handle
) : (
<div
ref={ref}
Expand All @@ -161,12 +163,12 @@ export default function NodeOutputField({
/>
</Button>
</div>
<Case condition={data.node?.frozen}>

{data.node?.frozen && (
<div className="pr-1">
<IconComponent className="h-5 w-5 text-ice" name={"Snowflake"} />
</div>
</Case>

)}
<div className="flex gap-2">
<span className={data.node?.frozen ? "text-ice" : ""}>
<OutputComponent
Expand All @@ -192,53 +194,40 @@ export default function NodeOutputField({
: "Please build the component first"
}
>
<Button
unstyled
<OutputModal
disabled={!displayOutputPreview || unknownOutput}
onClick={() => setOpenOutputModal(true)}
data-testid={`output-inspection-${title.toLowerCase()}`}
nodeId={flowPoolId}
outputName={internalOutputName}
>
{errorOutput ? (
<IconComponent
className={classNames("h-5 w-5 rounded-md text-status-red")}
name={"X"}
/>
) : (
<IconComponent
className={classNames(
"h-5 w-5 rounded-md",
displayOutputPreview && !unknownOutput
? "hover:text-medium-indigo"
: "cursor-not-allowed text-muted-foreground",
)}
name={"ScanEye"}
/>
)}
</Button>
<Button
unstyled
disabled={!displayOutputPreview || unknownOutput}
data-testid={`output-inspection-${title.toLowerCase()}`}
>
{errorOutput ? (
<IconComponent
className={classNames(
"h-5 w-5 rounded-md text-status-red",
)}
name={"X"}
/>
) : (
<IconComponent
className={classNames(
"h-5 w-5 rounded-md",
displayOutputPreview && !unknownOutput
? "hover:text-medium-indigo"
: "cursor-not-allowed text-muted-foreground",
)}
name={"ScanEye"}
/>
)}
</Button>
</OutputModal>
</ShadTooltip>
</div>
</div>
<HandleRenderComponent
left={false}
nodes={nodes}
tooltipTitle={tooltipTitle}
id={id}
title={title}
edges={edges}
myData={myData}
colors={colors}
setFilterEdge={setFilterEdge}
showNode={showNode}
testIdComplement={`${data?.type?.toLowerCase()}-shownode`}
/>
{openOutputModal && (
<OutputModal
open={openOutputModal}
nodeId={flowPoolId}
setOpen={setOpenOutputModal}
outputName={internalOutputName}
/>
)}
{Handle}
</>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import BaseModal from "../../../../modals/baseModal";
import SwitchOutputView from "./components/switchOutputView";

export default function OutputModal({
open,
setOpen,
nodeId,
outputName,
children,
disabled,
}): JSX.Element {
const [activeTab, setActiveTab] = useState<"Outputs" | "Logs">("Outputs");
return (
<BaseModal open={open} setOpen={setOpen} size="large">
<BaseModal disable={disabled} size="large">
<BaseModal.Header description="Inspect the output of the component below.">
<div className="flex items-center">
<span className="pr-2">Component Output</span>
Expand All @@ -37,13 +37,8 @@ export default function OutputModal({
type={activeTab}
/>
</BaseModal.Content>
<BaseModal.Footer>
<div className="flex w-full justify-end pt-2">
<Button className="flex gap-2 px-3" onClick={() => setOpen(false)}>
Close
</Button>
</div>
</BaseModal.Footer>
<BaseModal.Footer close></BaseModal.Footer>
<BaseModal.Trigger>{children}</BaseModal.Trigger>
</BaseModal>
);
}
8 changes: 7 additions & 1 deletion src/frontend/src/modals/baseModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ const Footer: React.FC<{
dataTestId?: string;
onClick?: () => void;
};
}> = ({ children, submit }) => {
close?: boolean;
}> = ({ children, submit, close }) => {
return (
<div className="flex flex-shrink-0 flex-row-reverse">
{submit ? (
Expand All @@ -115,6 +116,11 @@ const Footer: React.FC<{
) : (
<>{children && children}</>
)}
{close && (
<DialogClose asChild>
<Button type="button">Close</Button>
</DialogClose>
)}
</div>
);
};
Expand Down
Loading