diff --git a/jam-ui/public/index.html b/jam-ui/public/index.html index 3c65bdaa4..6729cfd40 100644 --- a/jam-ui/public/index.html +++ b/jam-ui/public/index.html @@ -5,11 +5,12 @@ + + /> JamKazam diff --git a/jam-ui/src/App.js b/jam-ui/src/App.js index bb66a59cb..0e4a11df1 100644 --- a/jam-ui/src/App.js +++ b/jam-ui/src/App.js @@ -6,13 +6,48 @@ import 'react-toastify/dist/ReactToastify.min.css'; import 'react-datetime/css/react-datetime.css'; import 'react-image-lightbox/style.css'; -import useImportScript from "./hooks/useImportScript"; +import useScript from './hooks/useScript'; const App = () => { - useImportScript(`${process.env.REACT_APP_LEGACY_BASE_URL}/client_scripts`) + function initJKScripts() { + const app = window.JK.JamKazam(); + const jamServer = new window.JK.JamServer(app, function(event_type) { + console.log('---EVENT_TYPE---', event_type); + //return app.activeElementEvent(event_type) + }); + jamServer.initialize(); + window.JK.initJamClient(app); - return ( + const clientInit = new window.JK.ClientInit(); + clientInit.init(); + + window.JK.JamServer.connect() // singleton here defined in JamServer.js + .done(function() { + console.log('Jamserver connected'); + //_initAfterConnect(true); + }) + .fail(function() { + console.log('Jamserver connection error'); + //_initAfterConnect(false); + }); + + registerTextMessageCallback(); + + } + + function registerTextMessageCallback(){ + window.JK.JamServer.registerMessageCallback(window.JK.MessageType.TEXT_MESSAGE, function(header, payload) { + console.log('Handling CHAT_MESSAGE msg ' + JSON.stringify(payload)); + // chatMessageReceived(payload); + // context.ChatActions.msgReceived(payload); + // handledNotification(payload); + }); + } + + useScript(`${process.env.REACT_APP_LEGACY_BASE_URL}/client_scripts`, initJKScripts); + + return ( diff --git a/jam-ui/src/components/profile/JKMessageModal.js b/jam-ui/src/components/profile/JKMessageModal.js index 7a22acb26..1e896d0dc 100644 --- a/jam-ui/src/components/profile/JKMessageModal.js +++ b/jam-ui/src/components/profile/JKMessageModal.js @@ -47,7 +47,6 @@ const JKMessageModal = props => { }, [show]); - return ( <> diff --git a/jam-ui/src/helpers/scriptCache.js b/jam-ui/src/helpers/scriptCache.js new file mode 100644 index 000000000..4a72fcdc7 --- /dev/null +++ b/jam-ui/src/helpers/scriptCache.js @@ -0,0 +1,106 @@ +// Inspiration from: https://www.fullstackreact.com/articles/Declaratively_loading_JS_libraries/index.html +// ================================================================================ +// Summary: a handy class for dynamically loading and using async JS libs in a ReactJS app +// ================================================================================ +// +// Usage: +// 1. create a `ScriptCache` instance: +// const scriptCache = new ScriptCache(["http://remote.cdn.com/myLibrary.min.js", "http://..."]); +// 2. pass any functions that depend on window globals (from your script) into `scriptCache.onLoad` +// ================================================================================ + +export default class ScriptCache { + + static SCRIPT_STATUS = { + COMPLETE: "complete", + ERROR: "error" + } + + constructor(scripts) { + this.loaded = []; + this.failed = []; + this.pending = []; + this.load(scripts); + } + + /** + * Use this block to run any additional setup + * when the scripts have loaded (or failed) + */ + onLoad(onSuccess, onReject) { + if (onReject) onReject(this.failed); + if (onSuccess) onSuccess(this.loaded); + } + + /** + * This will loop through and load any scripts + * passed into the class constructor + */ + load(scripts = []) { + if (!scripts.length) return; + const scriptPromises = []; + for (let script of scripts) { + scriptPromises.push(this.loadScript(script)) + } + + return Promise.all(scriptPromises); + } + + /** + * This loads a single script from its source. + * The 'loading' action is wrapped in a promise, + * which should fail if the script cannot be fetched + */ + loadScript(script) { + if (this.loaded.indexOf(script) > -1) return Promise.resolve(script); + this.pending.push(script); + return this.createScriptTag(script) + .then((script) => { + this.loaded.push(script); + this.pending.splice(this.pending.indexOf(script), 1); + return script; + }) + .catch((e) => { + this.failed.push(script); + this.pending.splice(this.pending.indexOf(script), 1); + }) + } + + /** + * This creates a