-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
|
||
|
@@ -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) => { | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checking this |
||
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}`; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 theInMemoryCache
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.