|
1 | | -import React from "react" |
| 1 | +import React, { useEffect, useRef, useState } from "react"; |
2 | 2 |
|
3 | | -class Retool extends React.Component { |
4 | | - constructor(props) { |
5 | | - super(props) |
| 3 | +const Retool = ({ data, url }) => { |
| 4 | + const embeddedIframe = useRef(null); |
| 5 | + const [elementWatchers, setElementWatchers] = useState({}); |
6 | 6 |
|
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 || {}, |
14 | | - } |
15 | | - } |
16 | | - |
17 | | - componentDidMount() { |
18 | | - this.startListening() |
19 | | - this.startWatchers() |
20 | | - } |
21 | | - |
22 | | - startListening = () => { |
23 | | - if (this.iframe) { |
24 | | - window.addEventListener("message", (e) => this.handle(e)) |
| 7 | + useEffect(() => { |
| 8 | + for (const key in elementWatchers) { |
| 9 | + const watcher = elementWatchers[key]; |
| 10 | + watcher.iframe?.contentWindow.postMessage( |
| 11 | + { |
| 12 | + type: "PARENT_WINDOW_RESULT", |
| 13 | + result: data[watcher.selector], |
| 14 | + id: watcher.queryId, |
| 15 | + pageName: watcher.pageName, |
| 16 | + }, |
| 17 | + "*" |
| 18 | + ); |
25 | 19 | } |
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 | | - ) |
| 20 | + }, [data, elementWatchers]); |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + const handler = (event) => { |
| 24 | + if (!embeddedIframe?.current?.contentWindow) return; |
| 25 | + if (event.data.type === "PARENT_WINDOW_QUERY") { |
| 26 | + createOrReplaceWatcher( |
| 27 | + event.data.selector, |
| 28 | + event.data.pageName, |
| 29 | + event.data.id |
| 30 | + ); |
| 31 | + postMessageForSelector("PARENT_WINDOW_RESULT", event.data); |
47 | 32 | } |
48 | | - } |
| 33 | + }; |
49 | 34 |
|
50 | | - setTimeout(this.startWatchers, 100) |
51 | | - } |
| 35 | + window.addEventListener("message", handler); |
52 | 36 |
|
53 | | - createOrReplaceWatcher = (selector, pageName, queryId) => { |
54 | | - var watcherId = pageName + "-" + queryId |
55 | | - var watchers = { ...this.state.elementWatchers } |
| 37 | + // clean up |
| 38 | + return () => window.removeEventListener("message", handler); |
| 39 | + }, []); |
56 | 40 |
|
57 | | - watchers[watcherId] = { |
58 | | - iframe: this.iframe, |
| 41 | + const createOrReplaceWatcher = (selector, pageName, queryId) => { |
| 42 | + const watcherId = pageName + "-" + queryId; |
| 43 | + const updatedState = elementWatchers; |
| 44 | + |
| 45 | + updatedState[watcherId] = { |
| 46 | + iframe: embeddedIframe.current, |
59 | 47 | selector: selector, |
60 | 48 | pageName: pageName, |
61 | 49 | queryId: queryId, |
62 | | - prevValue: null, |
63 | | - } |
64 | | - |
65 | | - this.setState({ elementWatchers: watchers }) |
66 | | - } |
| 50 | + }; |
67 | 51 |
|
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 | | - } |
| 52 | + setElementWatchers(updatedState); |
| 53 | + }; |
76 | 54 |
|
77 | | - postMessageForSelector = (messageType, eventData) => { |
78 | | - const maybeData = this.dataFromSelector(eventData.selector) |
| 55 | + const postMessageForSelector = (messageType, eventData) => { |
| 56 | + const maybeData = data[eventData.selector]; |
79 | 57 |
|
80 | 58 | if (maybeData) { |
81 | | - this.iframe.contentWindow.postMessage( |
| 59 | + embeddedIframe.current.contentWindow.postMessage( |
82 | 60 | { |
83 | 61 | type: messageType, |
84 | 62 | result: maybeData, |
85 | 63 | id: eventData.id, |
86 | 64 | pageName: eventData.pageName, |
87 | 65 | }, |
88 | 66 | "*" |
89 | | - ) |
| 67 | + ); |
90 | 68 | } else { |
91 | 69 | console.log( |
92 | 70 | `Not sending data back to Retool, nothing found for selector: ${eventData.selector}` |
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) |
| 71 | + ); |
106 | 72 | } |
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 |
| 73 | + }; |
| 74 | + |
| 75 | + return ( |
| 76 | + <iframe |
| 77 | + height="100%" |
| 78 | + width="100%" |
| 79 | + frameBorder="none" |
| 80 | + src={url} |
| 81 | + ref={embeddedIframe} |
| 82 | + title="retool" |
| 83 | + ></iframe> |
| 84 | + ); |
| 85 | +}; |
| 86 | + |
| 87 | +export default Retool; |
0 commit comments