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

fix: Change external portal cache strategy #2341

Merged
merged 1 commit into from
Oct 26, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import MoreVert from "@mui/icons-material/MoreVert";
import classNames from "classnames";
import gql from "graphql-tag";
import { useStore } from "pages/FlowEditor/lib/store";
import React from "react";
import React, { useState } from "react";
import { useDrag } from "react-dnd";
import { Link } from "react-navi";

Expand All @@ -12,84 +12,85 @@ import { getParentId } from "../lib/utils";
import Hanger from "./Hanger";
import Question from "./Question";

const ExternalPortal: React.FC<any> = React.memo(
(props) => {
const copyNode = useStore((state) => state.copyNode);

const { data } = useQuery(
gql`
query GetFlow($id: uuid!) {
flows_by_pk(id: $id) {
const ExternalPortal: React.FC<any> = (props) => {
Copy link
Contributor Author

@DafyddLlyr DafyddLlyr Oct 25, 2023

Choose a reason for hiding this comment

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

React.memo() was caching this component and causing it to update in an unexpected manner.

There's nothing particularly "expensive" going on here apart from the query. By adding an id field to the query response though Apollo will just cache this for us in the InMemoryCache and stop the query being repeated anyway.

The big diff here is a bit weird but it's pretty much just the indentation of React.memo() being removed.

const copyNode = useStore((state) => state.copyNode);
const [href, setHref] = useState("Loading...");

const { data, loading } = useQuery(
gql`
query GetExternalPortal($id: uuid!) {
flows_by_pk(id: $id) {
id
slug
team {
slug
team {
slug
}
}
}
`,
{ variables: { id: props.data.flowId } },
);
}
`,
{
variables: { id: props.data.flowId },
onCompleted: (data) =>
setHref([data.flows_by_pk.team.slug, data.flows_by_pk.slug].join("/")),
},
);

const parent = getParentId(props.parent);

const [{ isDragging }, drag] = useDrag({
item: {
id: props.id,
parent,
text: props.data.text,
},
type: "PORTAL",
collect: (monitor) => ({
isDragging: monitor.isDragging(),
}),
});

// If the flow referenced by an external portal has been deleted (eg !data),
// still show a "Corrupted" node so that editors have a visual cue to "delete".
// Until deleted, the flow schema will still contain a node reference to this portal causing dataMerged to fail
if (!data?.flows_by_pk) {
return (
<Question
hasFailed
type="Error"
id={props.id}
text="Corrupted external portal: flow no longer exists"
/>
);
}

const handleContext = (e: React.MouseEvent) => {
e.preventDefault();
e.stopPropagation();
copyNode(props.id);
};

const href = [data.flows_by_pk.team.slug, data.flows_by_pk.slug].join("/");

let editHref = `${window.location.pathname}/nodes/${props.id}/edit`;
if (parent) {
editHref = `${window.location.pathname}/nodes/${parent}/nodes/${props.id}/edit`;
}
const parent = getParentId(props.parent);

const [{ isDragging }, drag] = useDrag({
item: {
id: props.id,
parent,
text: props.data.text,
},
type: "PORTAL",
collect: (monitor) => ({
isDragging: monitor.isDragging(),
}),
});

// If the flow referenced by an external portal has been deleted (eg !data),
// still show a "Corrupted" node so that editors have a visual cue to "delete".
// Until deleted, the flow schema will still contain a node reference to this portal causing dataMerged to fail
if (!loading && !data?.flows_by_pk) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Checking this loading state stops the component flashing to "Corrupted" between updates.

return (
<>
<Hanger hidden={isDragging} before={props.id} parent={parent} />
<li
className={classNames("card", "portal", { isDragging })}
onContextMenu={handleContext}
>
<Link href={`/${href}`} prefetch={false} ref={drag}>
<span>{href}</span>
</Link>
<Link href={editHref} prefetch={false}>
<MoreVert titleAccess="Edit Portal" />
</Link>
</li>
</>
<Question
hasFailed
type="Error"
id={props.id}
text="Corrupted external portal: flow no longer exists"
/>
);
},
(a, b) => a.flowId === b.flowId,
);
}

const handleContext = (e: React.MouseEvent) => {
e.preventDefault();
e.stopPropagation();
copyNode(props.id);
};

let editHref = `${window.location.pathname}/nodes/${props.id}/edit`;
if (parent) {
editHref = `${window.location.pathname}/nodes/${parent}/nodes/${props.id}/edit`;
}

return (
<>
<Hanger hidden={isDragging} before={props.id} parent={parent} />
<li
className={classNames("card", "portal", { isDragging })}
onContextMenu={handleContext}
>
<Link href={`/${href}`} prefetch={false} ref={drag}>
<span>{href}</span>
</Link>
<Link href={editHref} prefetch={false}>
<MoreVert titleAccess="Edit Portal" />
</Link>
</li>
</>
);
};

const InternalPortal: React.FC<any> = (props) => {
const href = props.data.href || `${rootFlowPath(true)},${props.id}`;
Expand Down
Loading