This repository was archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBlock.tsx
More file actions
173 lines (158 loc) · 4.72 KB
/
Copy pathBlock.tsx
File metadata and controls
173 lines (158 loc) · 4.72 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import type {
Block as BlockType,
FileBlockProps,
FolderBlockProps,
} from "@utils";
import loadable from "@loadable/component";
import * as PrimerReact from "@primer/react";
import { BaseStyles, ThemeProvider } from "@primer/react";
import React, { useCallback, useEffect, useState } from "react";
import * as ReactJSXRuntime from "react/jsx-runtime";
import ReactDOM from "react-dom";
import ReactDOMClient from "react-dom/client";
import {
callbackFunctions,
callbackFunctionsInternal,
useHandleCallbacks,
} from "../utils";
import { BlockComponentProps, BlockComponent } from "./BlockComponent";
const Bundle = ({ bundle }: { bundle: Asset[] }) => {
useEffect(() => {
const elements: HTMLElement[] = [];
bundle.forEach((asset) => {
if (asset.name.endsWith(".js")) {
const jsElement = document.createElement("script");
jsElement.textContent = `
var BlockBundle = ({ React, ReactJSXRuntime, ReactDOM, ReactDOMClient, PrimerReact }) => {
function require(name) {
switch (name) {
case "react":
return React;
case "react/jsx-runtime":
return ReactJSXRuntime;
case "react-dom":
return ReactDOM;
case "react-dom/client":
return ReactDOMClient;
case "@primer/react":
case "@primer/components":
return PrimerReact;
default:
console.log("no module '" + name + "'");
return null;
}
}
${asset.content}
return BlockBundle;
};`;
elements.push(jsElement);
} else if (asset.name.endsWith(".css")) {
const cssElement = document.createElement("style");
cssElement.textContent = asset.content;
elements.push(cssElement);
}
});
for (const el of elements) {
document.body.appendChild(el);
}
return () => {
for (const el of elements) {
document.body.removeChild(el);
}
};
}, [bundle]);
return null;
};
export const Block = ({
bundle,
props,
setProps,
}: {
bundle: Asset[];
props: FileBlockProps | FolderBlockProps;
setProps: (props: FileBlockProps | FolderBlockProps) => void;
}) => {
const [Block, setBlock] = useState<BlockType | undefined>(undefined);
useEffect(() => {
if (bundle.length === 0) {
const importPrefix = "../../../../../";
const imports = import.meta.glob("../../../../../blocks/**");
const importPath = importPrefix + props.block.entry;
const importContent = imports[importPath];
// @ts-ignore
const content = loadable(importContent);
// @ts-ignore
setBlock(content);
} else {
setBlock(
() =>
window.BlockBundle({
React,
ReactJSXRuntime,
ReactDOM,
ReactDOMClient,
PrimerReact,
}).default
);
}
}, []);
useHandleCallbacks("*");
const onUpdateContent = useCallback(
(content: string) => {
// the app does not send async content updates back to the block that
// originated them, to avoid overwriting subsequent changes; we update the
// content locally so controlled components work. this doesn't overwrite
// subsequent changes because it's synchronous.
setProps({ ...props, content });
callbackFunctions["onUpdateContent"](content);
},
[props, setProps]
);
const WrappedBlockComponent = useCallback(
(nestedProps: BlockComponentProps) => {
let context = {
...props.context,
...nestedProps.context,
};
// clear sha if viewing content from another repo
const parentRepo = [props.context.owner, props.context.repo].join("/");
const childRepo = [context.owner, context.repo].join("/");
const isSameRepo = parentRepo === childRepo;
if (!isSameRepo) {
context.sha = nestedProps.context.sha || "HEAD";
}
return <BlockComponent {...nestedProps} context={context} />;
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[JSON.stringify(props.context)]
);
const isInternal =
(props as unknown as { block: BlockType }).block.owner === "githubnext";
const filteredCallbackFunctions = isInternal
? callbackFunctionsInternal
: callbackFunctions;
return (
<>
{bundle.length > 0 && <Bundle bundle={bundle} />}
{Block && props && (
// @ts-ignore
<ThemeProvider>
<BaseStyles
style={{
width: "100%",
height: "100%",
}}
>
{/* @ts-ignore */}
<Block
{...props}
{...filteredCallbackFunctions}
onUpdateContent={onUpdateContent}
BlockComponent={WrappedBlockComponent}
/>
</BaseStyles>
</ThemeProvider>
)}
</>
);
};