-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84b45bc
commit 1edc0e1
Showing
7 changed files
with
96 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,4 @@ | ||
{ | ||
"presets": [ | ||
"@babel/preset-env", | ||
"@babel/preset-typescript", | ||
"@babel/react" | ||
], | ||
"plugins": [ | ||
"@babel/plugin-proposal-class-properties", | ||
[ | ||
"@babel/plugin-transform-typescript", | ||
{ | ||
"allowNamespaces": true | ||
} | ||
] | ||
] | ||
} | ||
"presets": ["@babel/preset-env", "@babel/preset-typescript", "@babel/react"], | ||
"plugins": ["@babel/plugin-proposal-class-properties"] | ||
} |
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import * as React from "react"; | ||
import { render } from "react-dom"; | ||
import * as ReactDOM from "react-dom"; | ||
import App from "./App"; | ||
//@ts-ignore | ||
import { initializeIcons } from "@fluentui/react"; | ||
|
||
initializeIcons(); | ||
|
||
render(<App />, document.getElementById("web-os-root")); | ||
ReactDOM.render(<App />, document.getElementById("web-os-root")); |
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,63 @@ | ||
import * as React from "react"; | ||
|
||
function useScript(src: string) { | ||
// Keep track of script status ("idle", "loading", "ready", "error") | ||
const [status, setStatus] = React.useState(src ? "loading" : "idle"); | ||
React.useEffect( | ||
() => { | ||
// Allow falsy src value if waiting on other data needed for | ||
// constructing the script URL passed to this hook. | ||
if (!src) { | ||
setStatus("idle"); | ||
return; | ||
} | ||
// Fetch existing script element by src | ||
// It may have been added by another intance of this hook | ||
let script = document.querySelector( | ||
`script[src="${src}"]` | ||
) as HTMLScriptElement; | ||
if (!script) { | ||
// Create script | ||
script = document.createElement("script") as HTMLScriptElement; | ||
script.src = src; | ||
script.async = true; | ||
script.setAttribute("data-status", "loading"); | ||
// Add script to document body | ||
document.body.appendChild(script); | ||
// Store status in attribute on script | ||
// This can be read by other instances of this hook | ||
const setAttributeFromEvent = (event: any) => { | ||
script!.setAttribute( | ||
"data-status", | ||
event.type === "load" ? "ready" : "error" | ||
); | ||
}; | ||
script.addEventListener("load", setAttributeFromEvent); | ||
script.addEventListener("error", setAttributeFromEvent); | ||
} else { | ||
// Grab existing script status from attribute and set to state. | ||
setStatus(script!.getAttribute("data-status")!); | ||
} | ||
// Script event handler to update status in state | ||
// Note: Even if the script already exists we still need to add | ||
// event handlers to update the state for *this* hook instance. | ||
const setStateFromEvent = (event: any) => { | ||
setStatus(event.type === "load" ? "ready" : "error"); | ||
}; | ||
// Add event listeners | ||
script.addEventListener("load", setStateFromEvent); | ||
script.addEventListener("error", setStateFromEvent); | ||
// Remove event listeners on cleanup | ||
return () => { | ||
if (script) { | ||
script.removeEventListener("load", setStateFromEvent); | ||
script.removeEventListener("error", setStateFromEvent); | ||
} | ||
}; | ||
}, | ||
[src] // Only re-run effect if script src changes | ||
); | ||
return status; | ||
} | ||
|
||
export default useScript; |
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