From d8ddd3392b1204ed9609ca56954aa97949a4c09b Mon Sep 17 00:00:00 2001 From: niyazm524 Date: Thu, 26 Nov 2020 18:30:24 +0300 Subject: [PATCH 1/9] feat(temp-stands): awful fix for temp stands --- .env | 2 ++ package-lock.json | 5 +++++ package.json | 3 ++- public/dev/temp-stands.html | 11 +++++++++++ public/dev/xdLocalStorage.min.js | 1 + src/index.tsx | 17 +++++++++++------ src/shared/helpers/index.ts | 3 ++- src/shared/helpers/temp-stand.ts | 23 +++++++++++++++++++++++ 8 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 public/dev/temp-stands.html create mode 100644 public/dev/xdLocalStorage.min.js create mode 100644 src/shared/helpers/temp-stand.ts diff --git a/.env b/.env index 95c1d50..df7533f 100644 --- a/.env +++ b/.env @@ -13,3 +13,5 @@ REACT_APP_FIREBASE_projectId=github-client-47c49 REACT_APP_FIREBASE_storageBucket=github-client-47c49.appspot.com REACT_APP_FIREBASE_messagingSenderId=14406286286 REACT_APP_FIREBASE_appId=1:14406286286:web:58c7c11c2762d36a55c99f + +REACT_APP_DEV_STORAGE_URL=https://dev.github-client.gq/dev/temp-stands.html diff --git a/package-lock.json b/package-lock.json index a8120e4..72f03f8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21816,6 +21816,11 @@ "async-limiter": "~1.0.0" } }, + "xdlocalstorage": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/xdlocalstorage/-/xdlocalstorage-2.0.5.tgz", + "integrity": "sha1-B894LAfveYO5HRhXVZJcQgS8hdI=" + }, "xml-name-validator": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/xml-name-validator/-/xml-name-validator-3.0.0.tgz", diff --git a/package.json b/package.json index 628fdc0..45c144e 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,8 @@ "react-scripts": "3.4.3", "tailwindcss": "^1.9.5", "typescript": "^3.7.5", - "use-query-params": "^1.1.9" + "use-query-params": "^1.1.9", + "xdlocalstorage": "^2.0.5" }, "scripts": { "start": "react-scripts start", diff --git a/public/dev/temp-stands.html b/public/dev/temp-stands.html new file mode 100644 index 0000000..a3647b6 --- /dev/null +++ b/public/dev/temp-stands.html @@ -0,0 +1,11 @@ + + + + + Only for development + + + + + + diff --git a/public/dev/xdLocalStorage.min.js b/public/dev/xdLocalStorage.min.js new file mode 100644 index 0000000..27b6b00 --- /dev/null +++ b/public/dev/xdLocalStorage.min.js @@ -0,0 +1 @@ +"use strict";window.XdUtils=window.XdUtils||function(){function a(a,b){var c,d=b||{};for(c in a)a.hasOwnProperty(c)&&(d[c]=a[c]);return d}return{extend:a}}(),window.xdLocalStorage=window.xdLocalStorage||function(){function a(a){k[a.id]&&(k[a.id](a),delete k[a.id])}function b(b){var c;try{c=JSON.parse(b.data)}catch(a){}c&&c.namespace===h&&("iframe-ready"===c.id?(m=!0,i.initCallback()):a(c))}function c(a,b,c,d){j++,k[j]=d;var e={namespace:h,id:j,action:a,key:b,value:c};g.contentWindow.postMessage(JSON.stringify(e),"*")}function d(a){i=XdUtils.extend(a,i);var c=document.createElement("div");window.addEventListener?window.addEventListener("message",b,!1):window.attachEvent("onmessage",b),c.innerHTML='',document.body.appendChild(c),g=document.getElementById(i.iframeId)}function e(){return l?!!m||(console.log("You must wait for iframe ready message before using the api."),!1):(console.log("You must call xdLocalStorage.init() before using it."),!1)}function f(){return"complete"===document.readyState}var g,h="cross-domain-local-message",i={iframeId:"cross-domain-iframe",iframeUrl:void 0,initCallback:function(){}},j=-1,k={},l=!1,m=!0;return{init:function(a){if(!a.iframeUrl)throw"You must specify iframeUrl";if(l)return void console.log("xdLocalStorage was already initialized!");l=!0,f()?d(a):document.addEventListener?document.addEventListener("readystatechange",function(){f()&&d(a)}):document.attachEvent("readystatechange",function(){f()&&d(a)})},setItem:function(a,b,d){e()&&c("set",a,b,d)},getItem:function(a,b){e()&&c("get",a,null,b)},removeItem:function(a,b){e()&&c("remove",a,null,b)},key:function(a,b){e()&&c("key",a,null,b)},getSize:function(a){e()&&c("size",null,null,a)},getLength:function(a){e()&&c("length",null,null,a)},clear:function(a){e()&&c("clear",null,null,a)},wasInit:function(){return l}}}(); \ No newline at end of file diff --git a/src/index.tsx b/src/index.tsx index 6022887..2ce9b01 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -4,13 +4,18 @@ import App from "./app"; import * as serviceWorker from "./serviceWorker"; import "normalize.css"; import "antd/dist/antd.css"; +import { loadLocalStorageFromDevIfNeeded } from "./shared/helpers/temp-stand"; -ReactDOM.render( - - - , - document.getElementById("root"), -); +function init() { + ReactDOM.render( + + + , + document.getElementById("root"), + ); +} + +loadLocalStorageFromDevIfNeeded().then(init); // If you want your app to work offline and load faster, you can change // unregister() to register() below. Note this comes with some pitfalls. diff --git a/src/shared/helpers/index.ts b/src/shared/helpers/index.ts index e8b17db..8363b8a 100644 --- a/src/shared/helpers/index.ts +++ b/src/shared/helpers/index.ts @@ -1,4 +1,5 @@ import * as str from "./string"; import * as dom from "./dom"; +import * as tempStand from "./temp-stand"; -export { str, dom }; +export { str, dom, tempStand }; diff --git a/src/shared/helpers/temp-stand.ts b/src/shared/helpers/temp-stand.ts new file mode 100644 index 0000000..e70e97e --- /dev/null +++ b/src/shared/helpers/temp-stand.ts @@ -0,0 +1,23 @@ +import { CREDENTIAL_KEY } from "features/auth"; + +const tempStandRegex = /^(github-client-47c49|dev-github-client)--pr\d+.+\.web\.app$/; + +const isTempStand = () => tempStandRegex.test(window.location.host); + +interface xdLocalStorageAPI { + getItem: (key: string, callback: (value: string) => void) => void; +} + +export const loadLocalStorageFromDevIfNeeded = async () => { + if (!isTempStand()) { + return; + } + // Сделал бы через DefinePlugin, но ради этого делать eject не хочется + // @ts-ignore + await import("xdlocalstorage/dist/scripts/xdLocalStoragePostMessageApi.min"); + const xdLocalStorage = (window as any).xdLocalStorage as xdLocalStorageAPI; + const userCredentialRaw = await new Promise((resolve) => + xdLocalStorage.getItem(CREDENTIAL_KEY, resolve), + ); + localStorage.setItem(CREDENTIAL_KEY, userCredentialRaw); +}; From d421d3efcc557790482f9625c12e0ab376c48a20 Mon Sep 17 00:00:00 2001 From: niyazm524 Date: Thu, 26 Nov 2020 19:01:26 +0300 Subject: [PATCH 2/9] feat(temp-stands): es6 modules rewrite (sorry) --- .eslintignore | 1 + src/shared/helpers/temp-stand.ts | 8 +- src/shared/helpers/xd-local-storage/index.ts | 110 ++++++++++++++++++ .../xd-local-storage/services/xd-utils.ts | 15 +++ 4 files changed, 127 insertions(+), 7 deletions(-) create mode 100644 src/shared/helpers/xd-local-storage/index.ts create mode 100644 src/shared/helpers/xd-local-storage/services/xd-utils.ts diff --git a/.eslintignore b/.eslintignore index c39cafd..e697fe7 100644 --- a/.eslintignore +++ b/.eslintignore @@ -3,3 +3,4 @@ dist/** build/** *.gen.ts .github +src/shared/helpers/xd-local-storage diff --git a/src/shared/helpers/temp-stand.ts b/src/shared/helpers/temp-stand.ts index e70e97e..4beebdc 100644 --- a/src/shared/helpers/temp-stand.ts +++ b/src/shared/helpers/temp-stand.ts @@ -4,18 +4,12 @@ const tempStandRegex = /^(github-client-47c49|dev-github-client)--pr\d+.+\.web\. const isTempStand = () => tempStandRegex.test(window.location.host); -interface xdLocalStorageAPI { - getItem: (key: string, callback: (value: string) => void) => void; -} - export const loadLocalStorageFromDevIfNeeded = async () => { if (!isTempStand()) { return; } // Сделал бы через DefinePlugin, но ради этого делать eject не хочется - // @ts-ignore - await import("xdlocalstorage/dist/scripts/xdLocalStoragePostMessageApi.min"); - const xdLocalStorage = (window as any).xdLocalStorage as xdLocalStorageAPI; + const { default: xdLocalStorage } = await import("./xd-local-storage"); const userCredentialRaw = await new Promise((resolve) => xdLocalStorage.getItem(CREDENTIAL_KEY, resolve), ); diff --git a/src/shared/helpers/xd-local-storage/index.ts b/src/shared/helpers/xd-local-storage/index.ts new file mode 100644 index 0000000..4bde058 --- /dev/null +++ b/src/shared/helpers/xd-local-storage/index.ts @@ -0,0 +1,110 @@ +import XdUtils from "./services/xd-utils" + +/** + * Created by dagan on 07/04/2014. + */ + +interface xdLocalStorageAPI { + getItem: (key: string, callback: (value: string) => void) => void; +} + +/* global XdUtils */ +export default (function () { + var MESSAGE_NAMESPACE = "cross-domain-local-message"; + + var defaultData = { + namespace: MESSAGE_NAMESPACE, + }; + + function postData(id: string, data: any) { + var mergedData = XdUtils.extend(data, defaultData); + mergedData.id = id; + parent.postMessage(JSON.stringify(mergedData), "*"); + } + + function getData(id: string, key: string) { + var value = localStorage.getItem(key); + var data = { + key: key, + value: value, + }; + postData(id, data); + } + + function setData(id: string, key: string, value: string) { + localStorage.setItem(key, value); + var checkGet = localStorage.getItem(key); + var data = { + success: checkGet === value, + }; + postData(id, data); + } + + function removeData(id: string, key: string) { + localStorage.removeItem(key); + postData(id, {}); + } + + function getKey(id: string, index: number) { + var key = localStorage.key(index); + postData(id, { key: key }); + } + + function getSize(id: string) { + var size = JSON.stringify(localStorage).length; + postData(id, { size: size }); + } + + function getLength(id: string) { + var length = localStorage.length; + postData(id, { length: length }); + } + + function clear(id: string) { + localStorage.clear(); + postData(id, {}); + } + + function receiveMessage(event: any) { + var data; + try { + data = JSON.parse(event.data); + } catch (err) { + //not our message, can ignore + } + + if (data && data.namespace === MESSAGE_NAMESPACE) { + if (data.action === "set") { + setData(data.id, data.key, data.value); + } else if (data.action === "get") { + getData(data.id, data.key); + } else if (data.action === "remove") { + removeData(data.id, data.key); + } else if (data.action === "key") { + getKey(data.id, data.key); + } else if (data.action === "size") { + getSize(data.id); + } else if (data.action === "length") { + getLength(data.id); + } else if (data.action === "clear") { + clear(data.id); + } + } + } + + if (window.addEventListener) { + window.addEventListener("message", receiveMessage, false); + } else { + window.onmessage = receiveMessage; + } + + function sendOnLoad() { + var data = { + namespace: MESSAGE_NAMESPACE, + id: "iframe-ready", + }; + parent.postMessage(JSON.stringify(data), "*"); + } + //on creation + sendOnLoad(); +})() as any as xdLocalStorageAPI; diff --git a/src/shared/helpers/xd-local-storage/services/xd-utils.ts b/src/shared/helpers/xd-local-storage/services/xd-utils.ts new file mode 100644 index 0000000..0432863 --- /dev/null +++ b/src/shared/helpers/xd-local-storage/services/xd-utils.ts @@ -0,0 +1,15 @@ +/** + * Created by Ofir_Dagan on 4/17/14. + */ + +export default { + extend(object: any, defaultObject: any) { + const result = defaultObject || {}; + for (let key in object) { + if (Object.prototype.hasOwnProperty.call(object, key)) { + result[key] = object[key]; + } + } + return result; + } +} From 64c439c1639aef829ee4188e4b8ee98bea033aed Mon Sep 17 00:00:00 2001 From: niyazm524 Date: Thu, 26 Nov 2020 19:06:37 +0300 Subject: [PATCH 3/9] feat(temp-stands): fixed eslintignore glob --- .eslintignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.eslintignore b/.eslintignore index e697fe7..e1ff32a 100644 --- a/.eslintignore +++ b/.eslintignore @@ -3,4 +3,4 @@ dist/** build/** *.gen.ts .github -src/shared/helpers/xd-local-storage +src/shared/helpers/xd-local-storage/**/* From ccdce10610ecfb05e91f1770996ac1f7b15d42b7 Mon Sep 17 00:00:00 2001 From: niyazm524 Date: Thu, 26 Nov 2020 19:08:12 +0300 Subject: [PATCH 4/9] feat(temp-stands): fixed restricted reference to window's property --- src/shared/helpers/xd-local-storage/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/shared/helpers/xd-local-storage/index.ts b/src/shared/helpers/xd-local-storage/index.ts index 4bde058..35cba0d 100644 --- a/src/shared/helpers/xd-local-storage/index.ts +++ b/src/shared/helpers/xd-local-storage/index.ts @@ -19,7 +19,7 @@ export default (function () { function postData(id: string, data: any) { var mergedData = XdUtils.extend(data, defaultData); mergedData.id = id; - parent.postMessage(JSON.stringify(mergedData), "*"); + window.parent.postMessage(JSON.stringify(mergedData), "*"); } function getData(id: string, key: string) { @@ -103,7 +103,7 @@ export default (function () { namespace: MESSAGE_NAMESPACE, id: "iframe-ready", }; - parent.postMessage(JSON.stringify(data), "*"); + window.parent.postMessage(JSON.stringify(data), "*"); } //on creation sendOnLoad(); From 614ea55db39212348dd9e1859029b8123512c3f4 Mon Sep 17 00:00:00 2001 From: niyazm524 Date: Thu, 26 Nov 2020 19:18:40 +0300 Subject: [PATCH 5/9] feat(temp-stands): exclude lib from tslint --- src/shared/helpers/xd-local-storage/index.ts | 3 +-- tsconfig.json | 3 +++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/shared/helpers/xd-local-storage/index.ts b/src/shared/helpers/xd-local-storage/index.ts index 35cba0d..f06f909 100644 --- a/src/shared/helpers/xd-local-storage/index.ts +++ b/src/shared/helpers/xd-local-storage/index.ts @@ -8,9 +8,8 @@ interface xdLocalStorageAPI { getItem: (key: string, callback: (value: string) => void) => void; } -/* global XdUtils */ export default (function () { - var MESSAGE_NAMESPACE = "cross-domain-local-message"; + const MESSAGE_NAMESPACE = "cross-domain-local-message"; var defaultData = { namespace: MESSAGE_NAMESPACE, diff --git a/tsconfig.json b/tsconfig.json index 989911d..7bd7d27 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -22,5 +22,8 @@ }, "include": [ "src" + ], + "exclude": [ + "src/shared/helpers/xd-local-storage/index.ts" ] } From aeb438acf0b72ba472da491cab9edadfdd68da41 Mon Sep 17 00:00:00 2001 From: niyazm524 Date: Fri, 27 Nov 2020 17:41:40 +0300 Subject: [PATCH 6/9] feat(temp-stands): using another library to achieve effect --- package-lock.json | 10 +++++----- package.json | 4 ++-- public/dev/main.306cbe88.js | 10 ++++++++++ public/dev/temp-stands.html | 12 +----------- public/dev/xdLocalStorage.min.js | 1 - src/shared/helpers/temp-stand.ts | 18 ++++++++++++++---- 6 files changed, 32 insertions(+), 23 deletions(-) create mode 100644 public/dev/main.306cbe88.js delete mode 100644 public/dev/xdLocalStorage.min.js diff --git a/package-lock.json b/package-lock.json index 72f03f8..72bac80 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6889,6 +6889,11 @@ "sha.js": "^2.4.8" } }, + "cross-domain-storage": { + "version": "2.0.7", + "resolved": "https://registry.npmjs.org/cross-domain-storage/-/cross-domain-storage-2.0.7.tgz", + "integrity": "sha512-IWaLY2NbAxX5yYxgNxQVx4HzWM2+LZi8LQPS4ILXX3JFF2TQ2rpph0Rh1kRWN8AZ01W0YuLY1FidqVWTyD5mBQ==" + }, "cross-fetch": { "version": "3.0.6", "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.0.6.tgz", @@ -21816,11 +21821,6 @@ "async-limiter": "~1.0.0" } }, - "xdlocalstorage": { - "version": "2.0.5", - "resolved": "https://registry.npmjs.org/xdlocalstorage/-/xdlocalstorage-2.0.5.tgz", - "integrity": "sha1-B894LAfveYO5HRhXVZJcQgS8hdI=" - }, "xml-name-validator": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/xml-name-validator/-/xml-name-validator-3.0.0.tgz", diff --git a/package.json b/package.json index 45c144e..73ef8c0 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "@types/react-dom": "^16.9.8", "antd": "^4.8.5", "classnames": "^2.2.6", + "cross-domain-storage": "^2.0.7", "dayjs": "^1.9.6", "firebase": "^8.0.1", "graphql": "^15.3.0", @@ -27,8 +28,7 @@ "react-scripts": "3.4.3", "tailwindcss": "^1.9.5", "typescript": "^3.7.5", - "use-query-params": "^1.1.9", - "xdlocalstorage": "^2.0.5" + "use-query-params": "^1.1.9" }, "scripts": { "start": "react-scripts start", diff --git a/public/dev/main.306cbe88.js b/public/dev/main.306cbe88.js new file mode 100644 index 0000000..8046aba --- /dev/null +++ b/public/dev/main.306cbe88.js @@ -0,0 +1,10 @@ +parcelRequire=function(e,r,t,n){var i,o="function"==typeof parcelRequire&&parcelRequire,u="function"==typeof require&&require;function f(t,n){if(!r[t]){if(!e[t]){var i="function"==typeof parcelRequire&&parcelRequire;if(!n&&i)return i(t,!0);if(o)return o(t,!0);if(u&&"string"==typeof t)return u(t);var c=new Error("Cannot find module '"+t+"'");throw c.code="MODULE_NOT_FOUND",c}p.resolve=function(r){return e[t][1][r]||r},p.cache={};var l=r[t]=new f.Module(t);e[t][0].call(l.exports,p,l,l.exports,this)}return r[t].exports;function p(e){return f(p.resolve(e))}}f.isParcelRequire=!0,f.Module=function(e){this.id=e,this.bundle=f,this.exports={}},f.modules=e,f.cache=r,f.parent=o,f.register=function(r,t){e[r]=[function(e,r){r.exports=t},{}]};for(var c=0;c - - - - Only for development - - - - - - +Dev stands helper diff --git a/public/dev/xdLocalStorage.min.js b/public/dev/xdLocalStorage.min.js deleted file mode 100644 index 27b6b00..0000000 --- a/public/dev/xdLocalStorage.min.js +++ /dev/null @@ -1 +0,0 @@ -"use strict";window.XdUtils=window.XdUtils||function(){function a(a,b){var c,d=b||{};for(c in a)a.hasOwnProperty(c)&&(d[c]=a[c]);return d}return{extend:a}}(),window.xdLocalStorage=window.xdLocalStorage||function(){function a(a){k[a.id]&&(k[a.id](a),delete k[a.id])}function b(b){var c;try{c=JSON.parse(b.data)}catch(a){}c&&c.namespace===h&&("iframe-ready"===c.id?(m=!0,i.initCallback()):a(c))}function c(a,b,c,d){j++,k[j]=d;var e={namespace:h,id:j,action:a,key:b,value:c};g.contentWindow.postMessage(JSON.stringify(e),"*")}function d(a){i=XdUtils.extend(a,i);var c=document.createElement("div");window.addEventListener?window.addEventListener("message",b,!1):window.attachEvent("onmessage",b),c.innerHTML='',document.body.appendChild(c),g=document.getElementById(i.iframeId)}function e(){return l?!!m||(console.log("You must wait for iframe ready message before using the api."),!1):(console.log("You must call xdLocalStorage.init() before using it."),!1)}function f(){return"complete"===document.readyState}var g,h="cross-domain-local-message",i={iframeId:"cross-domain-iframe",iframeUrl:void 0,initCallback:function(){}},j=-1,k={},l=!1,m=!0;return{init:function(a){if(!a.iframeUrl)throw"You must specify iframeUrl";if(l)return void console.log("xdLocalStorage was already initialized!");l=!0,f()?d(a):document.addEventListener?document.addEventListener("readystatechange",function(){f()&&d(a)}):document.attachEvent("readystatechange",function(){f()&&d(a)})},setItem:function(a,b,d){e()&&c("set",a,b,d)},getItem:function(a,b){e()&&c("get",a,null,b)},removeItem:function(a,b){e()&&c("remove",a,null,b)},key:function(a,b){e()&&c("key",a,null,b)},getSize:function(a){e()&&c("size",null,null,a)},getLength:function(a){e()&&c("length",null,null,a)},clear:function(a){e()&&c("clear",null,null,a)},wasInit:function(){return l}}}(); \ No newline at end of file diff --git a/src/shared/helpers/temp-stand.ts b/src/shared/helpers/temp-stand.ts index 4beebdc..88d2426 100644 --- a/src/shared/helpers/temp-stand.ts +++ b/src/shared/helpers/temp-stand.ts @@ -9,9 +9,19 @@ export const loadLocalStorageFromDevIfNeeded = async () => { return; } // Сделал бы через DefinePlugin, но ради этого делать eject не хочется - const { default: xdLocalStorage } = await import("./xd-local-storage"); - const userCredentialRaw = await new Promise((resolve) => - xdLocalStorage.getItem(CREDENTIAL_KEY, resolve), + // @ts-ignore + const { default: createGuest } = await import("cross-domain-storage/guest"); + const storage = createGuest("https://dev.github-client.gq/dev/temp-stands.html"); + const userCredentialRaw = await new Promise((resolve, reject) => + storage.get(CREDENTIAL_KEY, (error: any, data: string | undefined) => { + if (error) { + reject(error); + return; + } + resolve(data); + }), ); - localStorage.setItem(CREDENTIAL_KEY, userCredentialRaw); + if (userCredentialRaw) { + localStorage.setItem(CREDENTIAL_KEY, userCredentialRaw); + } }; From 7ee7caf956764ec4ce0abd59ae421b7b391276db Mon Sep 17 00:00:00 2001 From: niyazm524 Date: Fri, 27 Nov 2020 17:54:33 +0300 Subject: [PATCH 7/9] feat(temp-stands): fixed regexp expression --- public/dev/main.306cbe88.js | 172 +++++++++++++++++++++++++++++++++--- 1 file changed, 162 insertions(+), 10 deletions(-) diff --git a/public/dev/main.306cbe88.js b/public/dev/main.306cbe88.js index 8046aba..89e460a 100644 --- a/public/dev/main.306cbe88.js +++ b/public/dev/main.306cbe88.js @@ -1,10 +1,162 @@ -parcelRequire=function(e,r,t,n){var i,o="function"==typeof parcelRequire&&parcelRequire,u="function"==typeof require&&require;function f(t,n){if(!r[t]){if(!e[t]){var i="function"==typeof parcelRequire&&parcelRequire;if(!n&&i)return i(t,!0);if(o)return o(t,!0);if(u&&"string"==typeof t)return u(t);var c=new Error("Cannot find module '"+t+"'");throw c.code="MODULE_NOT_FOUND",c}p.resolve=function(r){return e[t][1][r]||r},p.cache={};var l=r[t]=new f.Module(t);e[t][0].call(l.exports,p,l,l.exports,this)}return r[t].exports;function p(e){return f(p.resolve(e))}}f.isParcelRequire=!0,f.Module=function(e){this.id=e,this.bundle=f,this.exports={}},f.modules=e,f.cache=r,f.parent=o,f.register=function(r,t){e[r]=[function(e,r){r.exports=t},{}]};for(var c=0;c Date: Fri, 27 Nov 2020 22:29:40 +0300 Subject: [PATCH 8/9] feat(temp-stands): clean-up old attempts smell --- src/.deploy/index.ts | 1 + src/{shared/helpers => .deploy}/temp-stand.ts | 11 +- src/index.tsx | 6 +- src/shared/helpers/index.ts | 3 +- src/shared/helpers/xd-local-storage/index.ts | 109 ------------------ .../xd-local-storage/services/xd-utils.ts | 15 --- tsconfig.json | 3 - 7 files changed, 13 insertions(+), 135 deletions(-) create mode 100644 src/.deploy/index.ts rename src/{shared/helpers => .deploy}/temp-stand.ts (70%) delete mode 100644 src/shared/helpers/xd-local-storage/index.ts delete mode 100644 src/shared/helpers/xd-local-storage/services/xd-utils.ts diff --git a/src/.deploy/index.ts b/src/.deploy/index.ts new file mode 100644 index 0000000..4f3eedb --- /dev/null +++ b/src/.deploy/index.ts @@ -0,0 +1 @@ +export * from "./temp-stand"; diff --git a/src/shared/helpers/temp-stand.ts b/src/.deploy/temp-stand.ts similarity index 70% rename from src/shared/helpers/temp-stand.ts rename to src/.deploy/temp-stand.ts index 88d2426..2dfa001 100644 --- a/src/shared/helpers/temp-stand.ts +++ b/src/.deploy/temp-stand.ts @@ -4,14 +4,19 @@ const tempStandRegex = /^(github-client-47c49|dev-github-client)--pr\d+.+\.web\. const isTempStand = () => tempStandRegex.test(window.location.host); +const STAND_URL_ENV = "REACT_APP_DEV_STORAGE_URL"; +const devStorageUrl = process.env[STAND_URL_ENV]; + export const loadLocalStorageFromDevIfNeeded = async () => { - if (!isTempStand()) { + if (!isTempStand() || !devStorageUrl) { + if(!devStorageUrl) { + console.debug(`Note that you need to provide ${STAND_URL_ENV} env to make dev stand work`) + } return; } - // Сделал бы через DefinePlugin, но ради этого делать eject не хочется // @ts-ignore const { default: createGuest } = await import("cross-domain-storage/guest"); - const storage = createGuest("https://dev.github-client.gq/dev/temp-stands.html"); + const storage = createGuest(devStorageUrl); const userCredentialRaw = await new Promise((resolve, reject) => storage.get(CREDENTIAL_KEY, (error: any, data: string | undefined) => { if (error) { diff --git a/src/index.tsx b/src/index.tsx index 2ce9b01..523ac14 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -2,11 +2,11 @@ import React from "react"; import ReactDOM from "react-dom"; import App from "./app"; import * as serviceWorker from "./serviceWorker"; +import { loadLocalStorageFromDevIfNeeded } from ".deploy"; import "normalize.css"; import "antd/dist/antd.css"; -import { loadLocalStorageFromDevIfNeeded } from "./shared/helpers/temp-stand"; -function init() { +function renderApp() { ReactDOM.render( @@ -15,7 +15,7 @@ function init() { ); } -loadLocalStorageFromDevIfNeeded().then(init); +loadLocalStorageFromDevIfNeeded().then(renderApp); // If you want your app to work offline and load faster, you can change // unregister() to register() below. Note this comes with some pitfalls. diff --git a/src/shared/helpers/index.ts b/src/shared/helpers/index.ts index 8363b8a..e8b17db 100644 --- a/src/shared/helpers/index.ts +++ b/src/shared/helpers/index.ts @@ -1,5 +1,4 @@ import * as str from "./string"; import * as dom from "./dom"; -import * as tempStand from "./temp-stand"; -export { str, dom, tempStand }; +export { str, dom }; diff --git a/src/shared/helpers/xd-local-storage/index.ts b/src/shared/helpers/xd-local-storage/index.ts deleted file mode 100644 index f06f909..0000000 --- a/src/shared/helpers/xd-local-storage/index.ts +++ /dev/null @@ -1,109 +0,0 @@ -import XdUtils from "./services/xd-utils" - -/** - * Created by dagan on 07/04/2014. - */ - -interface xdLocalStorageAPI { - getItem: (key: string, callback: (value: string) => void) => void; -} - -export default (function () { - const MESSAGE_NAMESPACE = "cross-domain-local-message"; - - var defaultData = { - namespace: MESSAGE_NAMESPACE, - }; - - function postData(id: string, data: any) { - var mergedData = XdUtils.extend(data, defaultData); - mergedData.id = id; - window.parent.postMessage(JSON.stringify(mergedData), "*"); - } - - function getData(id: string, key: string) { - var value = localStorage.getItem(key); - var data = { - key: key, - value: value, - }; - postData(id, data); - } - - function setData(id: string, key: string, value: string) { - localStorage.setItem(key, value); - var checkGet = localStorage.getItem(key); - var data = { - success: checkGet === value, - }; - postData(id, data); - } - - function removeData(id: string, key: string) { - localStorage.removeItem(key); - postData(id, {}); - } - - function getKey(id: string, index: number) { - var key = localStorage.key(index); - postData(id, { key: key }); - } - - function getSize(id: string) { - var size = JSON.stringify(localStorage).length; - postData(id, { size: size }); - } - - function getLength(id: string) { - var length = localStorage.length; - postData(id, { length: length }); - } - - function clear(id: string) { - localStorage.clear(); - postData(id, {}); - } - - function receiveMessage(event: any) { - var data; - try { - data = JSON.parse(event.data); - } catch (err) { - //not our message, can ignore - } - - if (data && data.namespace === MESSAGE_NAMESPACE) { - if (data.action === "set") { - setData(data.id, data.key, data.value); - } else if (data.action === "get") { - getData(data.id, data.key); - } else if (data.action === "remove") { - removeData(data.id, data.key); - } else if (data.action === "key") { - getKey(data.id, data.key); - } else if (data.action === "size") { - getSize(data.id); - } else if (data.action === "length") { - getLength(data.id); - } else if (data.action === "clear") { - clear(data.id); - } - } - } - - if (window.addEventListener) { - window.addEventListener("message", receiveMessage, false); - } else { - window.onmessage = receiveMessage; - } - - function sendOnLoad() { - var data = { - namespace: MESSAGE_NAMESPACE, - id: "iframe-ready", - }; - window.parent.postMessage(JSON.stringify(data), "*"); - } - //on creation - sendOnLoad(); -})() as any as xdLocalStorageAPI; diff --git a/src/shared/helpers/xd-local-storage/services/xd-utils.ts b/src/shared/helpers/xd-local-storage/services/xd-utils.ts deleted file mode 100644 index 0432863..0000000 --- a/src/shared/helpers/xd-local-storage/services/xd-utils.ts +++ /dev/null @@ -1,15 +0,0 @@ -/** - * Created by Ofir_Dagan on 4/17/14. - */ - -export default { - extend(object: any, defaultObject: any) { - const result = defaultObject || {}; - for (let key in object) { - if (Object.prototype.hasOwnProperty.call(object, key)) { - result[key] = object[key]; - } - } - return result; - } -} diff --git a/tsconfig.json b/tsconfig.json index 7bd7d27..989911d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -22,8 +22,5 @@ }, "include": [ "src" - ], - "exclude": [ - "src/shared/helpers/xd-local-storage/index.ts" ] } From 7d78e0175dc255c9e254c5dc0ae004e5a8370951 Mon Sep 17 00:00:00 2001 From: niyazm524 Date: Fri, 27 Nov 2020 22:33:39 +0300 Subject: [PATCH 9/9] feat(temp-stands): ohh I forget, don't need anymore --- .eslintignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.eslintignore b/.eslintignore index e1ff32a..c39cafd 100644 --- a/.eslintignore +++ b/.eslintignore @@ -3,4 +3,3 @@ dist/** build/** *.gen.ts .github -src/shared/helpers/xd-local-storage/**/*