This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from githubnext/aw/block-builds
update production block to use iframe on `blocks` repo
- Loading branch information
Showing
10 changed files
with
781 additions
and
595 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { useEffect, useRef, useState } from "react"; | ||
// @ts-ignore | ||
import loadable from "@loadable/component"; | ||
import { FileContext, FolderContext, RepoFiles } from "@githubnext/utils"; | ||
|
||
interface ProductionBlockProps { | ||
block: { | ||
id: string; | ||
type: string; | ||
title: string; | ||
description: string; | ||
entry: string; | ||
extensions?: string[]; | ||
}; | ||
contents?: string; | ||
tree?: RepoFiles; | ||
metadata?: any; | ||
context: FileContext | FolderContext | ||
} | ||
export const ProductionBlock = (props: ProductionBlockProps) => { | ||
const { | ||
block, | ||
contents, | ||
tree, | ||
metadata = {}, | ||
context, | ||
} = props; | ||
|
||
const [bundleCode, setBundleCode] = useState(""); | ||
const [iframeIsLoaded, setIframeIsLoaded] = useState(false); | ||
const iframeElement = useRef<HTMLIFrameElement>(null); | ||
|
||
const getContents = async () => { | ||
const content = await import( | ||
`../../../dist/${block.id}/index.js?raw` | ||
).then((m) => m.default); | ||
setBundleCode(content) | ||
} | ||
useEffect(() => { getContents() }, [block.entry]) | ||
|
||
const onUpdateMetadata = (newMetadata: any) => { | ||
window.postMessage({ | ||
type: "update-metadata", | ||
context, | ||
metadata: newMetadata, | ||
}, "*") | ||
} | ||
|
||
useEffect(() => { | ||
if (!bundleCode || !iframeIsLoaded) return | ||
const iframeWindow = iframeElement.current?.contentWindow | ||
if (!iframeWindow) return | ||
|
||
iframeWindow.postMessage({ | ||
type: "block-props", | ||
block, contents, tree, metadata, context, bundleCode | ||
}, "*") | ||
}, [bundleCode, contents, tree, metadata, context, iframeIsLoaded]) | ||
|
||
if (!bundleCode) return null | ||
const url = "http://localhost:3000/block-testing" | ||
|
||
return ( | ||
<iframe className="w-full h-full border-none" src={url} title="Block testing" onLoad={() => setIframeIsLoaded(true)} ref={iframeElement} | ||
|
||
/> | ||
) | ||
|
||
} |
Oops, something went wrong.