|
1 | | -import { useEffect, useRef, useState } from "react"; |
| 1 | +import React from "react" |
2 | 2 |
|
3 | | -const Retool = ({ data, url, height, width }) => { |
4 | | - const embeddedIframe = useRef(null); |
5 | | - const [elementWatchers, setElementWatchers] = useState({}); |
| 3 | +class Retool extends React.Component { |
| 4 | + constructor(props) { |
| 5 | + super(props) |
6 | 6 |
|
7 | | - /* Retool passes up the list of elements to watch on page load */ |
8 | | - useEffect(() => { |
9 | | - for (const key in elementWatchers) { |
10 | | - const watcher = elementWatchers[key]; |
11 | | - watcher.iframe?.contentWindow.postMessage( |
12 | | - { |
13 | | - type: "PARENT_WINDOW_RESULT", |
14 | | - result: data[watcher.selector], |
15 | | - id: watcher.queryId, |
16 | | - pageName: watcher.pageName, |
17 | | - }, |
18 | | - "*" |
19 | | - ); |
| 7 | + if (!this.props.url) |
| 8 | + throw new Error("Please pass a url into the Retool component.") |
| 9 | + |
| 10 | + this.state = { |
| 11 | + url: props.url, |
| 12 | + elementWatchers: {}, |
| 13 | + parentData: this.props.data || {}, |
20 | 14 | } |
21 | | - }, [data, elementWatchers]); |
22 | | - |
23 | | - /* On page load, add event listener to listen for events from Retool */ |
24 | | - useEffect(() => { |
25 | | - /* Handle events - if PWQ then create/replace watchers -> return result */ |
26 | | - const handler = (event) => { |
27 | | - if (!embeddedIframe?.current?.contentWindow) return; |
28 | | - if (event.data.type === "PARENT_WINDOW_QUERY") { |
29 | | - createOrReplaceWatcher( |
30 | | - event.data.selector, |
31 | | - event.data.pageName, |
32 | | - event.data.id |
33 | | - ); |
34 | | - postMessageForSelector("PARENT_WINDOW_RESULT", event.data); |
35 | | - } |
36 | | - }; |
| 15 | + } |
37 | 16 |
|
38 | | - window.addEventListener("message", handler); |
| 17 | + componentDidMount() { |
| 18 | + this.startListening() |
| 19 | + this.startWatchers() |
| 20 | + } |
39 | 21 |
|
40 | | - return () => window.removeEventListener("message", handler); |
41 | | - }, []); |
| 22 | + startListening = () => { |
| 23 | + if (this.iframe) { |
| 24 | + window.addEventListener("message", (e) => this.handle(e)) |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + startWatchers = () => { |
| 29 | + var watcherKeys = Object.keys(this.state.elementWatchers) |
| 30 | + |
| 31 | + for (var i = 0; i < watcherKeys.length; i++) { |
| 32 | + var key = watcherKeys[i] |
| 33 | + var watcher = this.state.elementWatchers[key] |
| 34 | + var selector = watcher.selector |
| 35 | + const value = this.dataFromSelector(selector) |
| 36 | + if (value !== watcher.prevValue) { |
| 37 | + watcher.prevValue = value |
| 38 | + watcher.iframe.contentWindow.postMessage( |
| 39 | + { |
| 40 | + type: "PARENT_WINDOW_RESULT", |
| 41 | + result: value, |
| 42 | + id: watcher.queryId, |
| 43 | + pageName: watcher.pageName, |
| 44 | + }, |
| 45 | + "*" |
| 46 | + ) |
| 47 | + } |
| 48 | + } |
42 | 49 |
|
43 | | - /* Creates or updates the list of values for us to watch for changes */ |
44 | | - const createOrReplaceWatcher = (selector, pageName, queryId) => { |
45 | | - const watcherId = pageName + "-" + queryId; |
46 | | - const updatedState = elementWatchers; |
| 50 | + setTimeout(this.startWatchers, 100) |
| 51 | + } |
47 | 52 |
|
48 | | - updatedState[watcherId] = { |
49 | | - iframe: embeddedIframe.current, |
| 53 | + createOrReplaceWatcher = (selector, pageName, queryId) => { |
| 54 | + var watcherId = pageName + "-" + queryId |
| 55 | + var watchers = { ...this.state.elementWatchers } |
| 56 | + |
| 57 | + watchers[watcherId] = { |
| 58 | + iframe: this.iframe, |
50 | 59 | selector: selector, |
51 | 60 | pageName: pageName, |
52 | 61 | queryId: queryId, |
53 | | - }; |
| 62 | + prevValue: null, |
| 63 | + } |
| 64 | + |
| 65 | + this.setState({ elementWatchers: watchers }) |
| 66 | + } |
54 | 67 |
|
55 | | - setElementWatchers(updatedState); |
56 | | - }; |
| 68 | + dataFromSelector = (selector) => { |
| 69 | + // Two places the app might be asking for data: |
| 70 | + // 1. The textContent of an HTML element. |
| 71 | + // 2. From data passed into this component |
| 72 | + const matchingInjectedData = this.state.parentData[selector] |
| 73 | + const nodeData = document.querySelector(selector)?.textContent |
| 74 | + return matchingInjectedData || nodeData || null |
| 75 | + } |
57 | 76 |
|
58 | | - /* Checks for selectors for data and posts message for Retool to read */ |
59 | | - const postMessageForSelector = (messageType, eventData) => { |
60 | | - const maybeData = data[eventData.selector]; |
| 77 | + postMessageForSelector = (messageType, eventData) => { |
| 78 | + const maybeData = this.dataFromSelector(eventData.selector) |
61 | 79 |
|
62 | 80 | if (maybeData) { |
63 | | - embeddedIframe.current.contentWindow.postMessage( |
| 81 | + this.iframe.contentWindow.postMessage( |
64 | 82 | { |
65 | 83 | type: messageType, |
66 | 84 | result: maybeData, |
67 | 85 | id: eventData.id, |
68 | 86 | pageName: eventData.pageName, |
69 | 87 | }, |
70 | 88 | "*" |
71 | | - ); |
| 89 | + ) |
72 | 90 | } else { |
73 | 91 | console.log( |
74 | 92 | `Not sending data back to Retool, nothing found for selector: ${eventData.selector}` |
75 | | - ); |
| 93 | + ) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + handle = (event) => { |
| 98 | + if (!this.iframe?.contentWindow) return |
| 99 | + if (event.data.type === "PARENT_WINDOW_QUERY") { |
| 100 | + this.createOrReplaceWatcher( |
| 101 | + event.data.selector, |
| 102 | + event.data.pageName, |
| 103 | + event.data.id |
| 104 | + ) |
| 105 | + this.postMessageForSelector("PARENT_WINDOW_RESULT", event.data) |
76 | 106 | } |
77 | | - }; |
78 | | - |
79 | | - return ( |
80 | | - <iframe |
81 | | - height={height ?? "100%"} |
82 | | - width={width ?? "100%"} |
83 | | - frameBorder="none" |
84 | | - src={url} |
85 | | - ref={embeddedIframe} |
86 | | - title="retool" |
87 | | - ></iframe> |
88 | | - ); |
89 | | -}; |
90 | | - |
91 | | -export default Retool; |
| 107 | + |
| 108 | + if (event.data.type === "PARENT_WINDOW_PREVIEW_QUERY") { |
| 109 | + this.postMessageForSelector("PARENT_WINDOW_PREVIEW_RESULT", event.data) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + render() { |
| 114 | + return ( |
| 115 | + <iframe |
| 116 | + height="100%" |
| 117 | + width="100%" |
| 118 | + frameBorder="none" |
| 119 | + src={this.state.url} |
| 120 | + ref={(e) => { |
| 121 | + this.iframe = e |
| 122 | + }} |
| 123 | + title="retool" |
| 124 | + ></iframe> |
| 125 | + ) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +export default Retool |
0 commit comments