Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finally (сколько можно): работающие temp-стенды [2] #147

Merged
merged 11 commits into from
Nov 27, 2020
Merged
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ dist/**
build/**
*.gen.ts
.github
src/shared/helpers/xd-local-storage/**/*
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@types/react-dom": "^16.9.8",
"antd": "^4.8.5",
"classnames": "^2.2.6",
"cross-domain-storage": "^2.0.7",
niyazm524 marked this conversation as resolved.
Show resolved Hide resolved
"dayjs": "^1.9.6",
"firebase": "^8.0.1",
"graphql": "^15.3.0",
Expand Down
162 changes: 162 additions & 0 deletions public/dev/main.306cbe88.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions public/dev/temp-stands.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html><html lang="ru"><head><meta charset="UTF-8"><title>Dev stands helper</title><script src="main.306cbe88.js"></script></head><body> </body></html>
17 changes: 11 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById("root"),
);
function init() {
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById("root"),
);
}

loadLocalStorageFromDevIfNeeded().then(init);
niyazm524 marked this conversation as resolved.
Show resolved Hide resolved

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
Expand Down
3 changes: 2 additions & 1 deletion src/shared/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -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 };
niyazm524 marked this conversation as resolved.
Show resolved Hide resolved
27 changes: 27 additions & 0 deletions src/shared/helpers/temp-stand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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);

export const loadLocalStorageFromDevIfNeeded = async () => {
if (!isTempStand()) {
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 userCredentialRaw = await new Promise<string | undefined>((resolve, reject) =>
storage.get(CREDENTIAL_KEY, (error: any, data: string | undefined) => {
if (error) {
reject(error);
return;
}
resolve(data);
}),
);
if (userCredentialRaw) {
localStorage.setItem(CREDENTIAL_KEY, userCredentialRaw);
}
};
109 changes: 109 additions & 0 deletions src/shared/helpers/xd-local-storage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import XdUtils from "./services/xd-utils"

/**
* Created by dagan on 07/04/2014.
niyazm524 marked this conversation as resolved.
Show resolved Hide resolved
*/

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) {
niyazm524 marked this conversation as resolved.
Show resolved Hide resolved
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);
niyazm524 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

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;
niyazm524 marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 15 additions & 0 deletions src/shared/helpers/xd-local-storage/services/xd-utils.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@
},
"include": [
"src"
],
"exclude": [
"src/shared/helpers/xd-local-storage/index.ts"
]
}