-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNSLVWidget.jsx
96 lines (86 loc) · 2.58 KB
/
NSLVWidget.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* This is a widget created to be used with "NEAR Social Local Viewer" CLI.
* A tool that enables you to develop your widgets locally.
* Learn more here: https://github.com/wpdas/near-social-local-viewer
*
* This Widget lives here: https://near.social/#/wendersonpires.near/widget/NSLVWidget
*
* How it works:
*
* 1 - This widget connects to both local and remote widgets;
* 2 - Example of use:
*
* <Widget
* src="wendersonpires.near/widget/NSLVWidget"
* props={{ src: "wendersonpires.near/widget/Profile" }}
* />
*
* Where "NSLVWidget" Loads the Near Social Local Viewer logic to render local or remote widget
* and "props={src: "wendersonpires.near/widget/Profile"}" is the widget path
*
* It will always try to find a local Widget first, if it fails, the remote Widget will be used.
*/
const src = props.src;
State.init({
code: null,
ready: false,
props: props.srcProps || {},
});
if (!src) {
return (
<div>
<p
style={{ fontWeight: 600, color: "#AB2E28", fontFamily: "Courier new" }}
>
This is a widget created to be used with
<a href="https://github.com/wpdas/near-social-local-viewer">
"NEAR Social Local Viewer"
</a>{" "}
CLI. A tool that enables you to develop your widgets locally.
</p>
<p
style={{ fontWeight: 600, color: "#AB2E28", fontFamily: "Courier new" }}
>
Learn more here:{" "}
<a href="https://github.com/wpdas/near-social-local-viewer">
https://github.com/wpdas/near-social-local-viewer
</a>
</p>
</div>
);
}
const localWidgetPath = src.split("/");
const localWidgetName = localWidgetPath[localWidgetPath.length - 1];
const fetchCode = () => {
asyncFetch(`http://localhost:9000/widget/get/${localWidgetName}`)
.then((localWidgetSrc) => {
if (
localWidgetSrc &&
localWidgetSrc?.status === 200 &&
localWidgetSrc?.body?.code
) {
// Set local widget code
State.update({ code: localWidgetSrc.body.code });
// If it's local (because its using local code server), refresh every 1 sec (to get most updated local widget)
setTimeout(() => {
fetchCode();
}, 1000);
}
})
.finally(() => {
State.update({ ready: true });
});
};
fetchCode();
// Wait till it loads
if (!state.ready) {
return <div />;
}
if (state.ready && state.code) {
// Render local widget
return <Widget code={state.code} props={state.props} />;
}
// Render remote widget
if (state.ready && !state.code) {
return <Widget src={src} props={state.props} />;
}