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: fix props handling when switching Map <-> DV plugins #2272

Merged
merged 2 commits into from
Mar 24, 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
79 changes: 38 additions & 41 deletions src/components/Item/VisualizationItem/Visualization/IframePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ const IframePlugin = ({

// When this mounts, check if the dashboard is recording
const { isCached, recordingState } = useCacheableSection(dashboardId)
const [communicationReceived, setCommunicationReceived] = useState(false)
const [recordOnNextLoad, setRecordOnNextLoad] = useState(
recordingState === 'recording'
)

const prevPluginRef = useRef()

const onError = () => setError('plugin')

const pluginProps = useMemo(
Expand All @@ -56,7 +57,7 @@ const IframePlugin = ({
// TODO: May also want user ID too for multi-user situations
cacheId: `${dashboardId}-${itemId}`,
isParentCached: isCached,
recordOnNextLoad: recordOnNextLoad,
recordOnNextLoad,
}),
[
userSettings,
Expand All @@ -68,6 +69,31 @@ const IframePlugin = ({
]
)

const getIframeSrc = useCallback(() => {
const pluginType = [CHART, REPORT_TABLE].includes(activeType)
? VISUALIZATION
: activeType

// 1. check if there is an override for the plugin
const pluginOverrides = getPluginOverrides()

if (pluginOverrides && pluginOverrides[pluginType]) {
return pluginOverrides[pluginType]
}

// 2. check if there is an installed app for the pluginType
// and use its plugin launch URL
const pluginLaunchUrl = getPluginLaunchUrl(pluginType, d2, baseUrl)

if (pluginLaunchUrl) {
return pluginLaunchUrl
}

setError('missing-plugin')
}, [activeType, d2, baseUrl])

const iframeSrc = getIframeSrc()

useEffect(() => {
// Tell plugin to remove cached data if this dashboard has been removed
// from offline storage
Expand All @@ -88,14 +114,14 @@ const IframePlugin = ({
useEffect(() => {
if (iframeRef?.current) {
// if iframe has not sent initial request, set up a listener
if (!communicationReceived) {
if (iframeSrc !== prevPluginRef.current) {
prevPluginRef.current = iframeSrc

const listener = postRobot.on(
'getProps',
// listen for messages coming only from the iframe rendered by this component
{ window: iframeRef.current.contentWindow },
() => {
setCommunicationReceived(true)

if (recordOnNextLoad) {
// Avoid recording unnecessarily,
// e.g. if plugin re-requests props for some reason
Expand All @@ -107,47 +133,20 @@ const IframePlugin = ({
)

return () => listener.cancel()
} else {
postRobot.send(
iframeRef.current.contentWindow,
'newProps',
pluginProps
)
}
}

if (communicationReceived && iframeRef.current?.contentWindow) {
postRobot.send(
iframeRef.current.contentWindow,
'newProps',
pluginProps
)
}
}, [communicationReceived, recordOnNextLoad, pluginProps])
}, [recordOnNextLoad, pluginProps, iframeSrc])

useEffect(() => {
setError(null)
}, [filterVersion, visualization.type])

const getIframeSrc = useCallback(() => {
const pluginType = [CHART, REPORT_TABLE].includes(activeType)
? VISUALIZATION
: activeType

// 1. check if there is an override for the plugin
const pluginOverrides = getPluginOverrides()

if (pluginOverrides && pluginOverrides[pluginType]) {
return pluginOverrides[pluginType]
}

// 2. check if there is an installed app for the pluginType
// and use its plugin launch URL
const pluginLaunchUrl = getPluginLaunchUrl(pluginType, d2, baseUrl)

if (pluginLaunchUrl) {
return pluginLaunchUrl
}

setError('missing-plugin')

return
}, [activeType, d2, baseUrl])

if (error) {
return error === 'missing-plugin' ? (
<div style={style}>
Expand All @@ -167,8 +166,6 @@ const IframePlugin = ({
)
}

const iframeSrc = getIframeSrc()

return (
<div className={classes.wrapper}>
{iframeSrc ? (
Expand Down