diff --git a/src/sh2.less b/src/sh2.less index dd25c5ed..a268ebbb 100644 --- a/src/sh2.less +++ b/src/sh2.less @@ -3172,7 +3172,47 @@ input[type=checkbox] { animation-duration: 0.3s; } -body.prompt-webshare { +body.prompt-webshare #main { + display: flex; + flex-direction: column; + + .logo-header { + display: flex; + flex-direction: row; + padding: 10px; + border-bottom: 1px solid #777; + align-items: center; + + .logo-text { + .mono-font(32px); + color: @prompt-green; + } + + .download-button { + margin-right: 20px; + } + } + + .prompt-content { + flex-grow: 1; + padding: 10px; + } + + .prompt-footer { + display: flex; + flex-direction: row; + align-items: center; + color: #aaa; + border-top: 1px solid #777; + height: 50px; + padding-left: 20px; + padding-right: 20px; + + a { + display: block; + } + } + color: white; #app { diff --git a/src/util.ts b/src/util.ts index 2e7807c2..628618ef 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,6 +1,26 @@ import * as mobx from "mobx"; import {sprintf} from "sprintf-js"; +function handleNotOkResp(resp : any, url : URL) : Promise { + let errMsg = sprintf("Bad status code response from fetch '%s': code=%d %s", url.toString(), resp.status, resp.statusText); + return resp.text().then((textData) => { + if (textData == null || textData == "") { + throw new Error(errMsg); + } + let rtnData : any = null; + try { + rtnData = JSON.parse(textData); + } + catch (err) { + // nothing (rtnData will be null) + } + if (rtnData != null && typeof(rtnData) == "object" && rtnData["error"] != null) { + throw new Error(rtnData["error"]); + } + throw new Error(errMsg + "\n" + textData); + }); +} + function fetchJsonData(resp : any, ctErr : boolean) : Promise { let contentType = resp.headers.get("Content-Type"); if (contentType != null && contentType.startsWith("application/json")) { @@ -27,13 +47,7 @@ function fetchJsonData(resp : any, ctErr : boolean) : Promise { function handleJsonFetchResponse(url : URL, resp : any) : Promise { if (!resp.ok) { - let errData = fetchJsonData(resp, false); - if (errData && errData["error"]) { - throw new Error(errData["error"]) - } - let errMsg = sprintf("Bad status code response from fetch '%s': %d %s", url.toString(), resp.status, resp.statusText); - let rtnErr = new Error(errMsg); - throw rtnErr; + return handleNotOkResp(resp, url); } let rtnData = fetchJsonData(resp, true); return rtnData; diff --git a/src/webshare-elems.tsx b/src/webshare-elems.tsx index 880ec7e4..8f7018dd 100644 --- a/src/webshare-elems.tsx +++ b/src/webshare-elems.tsx @@ -5,11 +5,42 @@ import {sprintf} from "sprintf-js"; import {boundMethod} from "autobind-decorator"; import {If, For, When, Otherwise, Choose} from "tsx-control-statements/components"; import cn from "classnames"; +import {WebShareModel} from "./webshare-model"; +@mobxReact.observer class WebShareMain extends React.Component<{}, {}> { + renderCopy() { + return (
© 2023 Dashborg Inc
); + } + render() { return ( -

hello from webshare

+
+
+
[prompt]
+ +
+
screenid={WebShareModel.screenId}, viewkey={WebShareModel.viewKey}
+
{WebShareModel.errMessage.get()}
+
+
+ {this.renderCopy()} + +
); } } diff --git a/src/webshare-model.ts b/src/webshare-model.ts new file mode 100644 index 00000000..ce9a2f27 --- /dev/null +++ b/src/webshare-model.ts @@ -0,0 +1,63 @@ +import * as mobx from "mobx"; +import {boundMethod} from "autobind-decorator"; +import {handleJsonFetchResponse} from "./util"; + +type OV = mobx.IObservableValue; +type OArr = mobx.IObservableArray; +type OMap = mobx.ObservableMap; +type CV = mobx.IComputedValue; + +function isBlank(s : string) { + return (s == null || s == ""); +} + +function getBaseUrl() { + return "https://ot2e112zx5.execute-api.us-west-2.amazonaws.com/dev"; +} + +class WebShareModelClass { + viewKey : string; + screenId : string; + errMessage : OV = mobx.observable.box(null, {name: "errMessage"}); + + constructor() { + let urlParams = new URLSearchParams(window.location.search); + this.viewKey = urlParams.get("viewkey"); + this.screenId = urlParams.get("screenid"); + setTimeout(() => this.loadFullScreenData(), 10); + + } + + setErrMessage(msg : string) : void { + mobx.action(() => { + this.errMessage.set(msg); + })(); + } + + loadFullScreenData() : void { + if (isBlank(this.screenId)) { + this.setErrMessage("No ScreenId Specified, Cannot Load."); + return; + } + if (isBlank(this.viewKey)) { + this.setErrMessage("No ViewKey Specified, Cannot Load."); + return; + } + let usp = new URLSearchParams({screenid: this.screenId, viewkey: this.viewKey}); + let url = new URL(getBaseUrl() + "/webshare/screen?" + usp.toString()); + fetch(url, {method: "GET", mode: "cors", cache: "no-cache"}).then((resp) => handleJsonFetchResponse(url, resp)).then((data) => { + console.log("got data", data); + }).catch((err) => { + this.errMessage.set("Cannot get screen: " + err.message); + }); + + } +} + +let WebShareModel : WebShareModelClass = null; +if ((window as any).WebShareModel == null) { + WebShareModel = new WebShareModelClass(); + (window as any).WebShareModel = WebShareModel; +} + +export {WebShareModel}; diff --git a/webpack.share.dev.js b/webpack.share.dev.js index 06024d89..d95b5761 100644 --- a/webpack.share.dev.js +++ b/webpack.share.dev.js @@ -20,6 +20,9 @@ var merged = merge.merge(common, { headers: { 'Cache-Control': 'no-store', }, + allowedHosts: "all", + hot: false, + liveReload: false, }, watchOptions: { aggregateTimeout: 200,