From fb3ca1276defbd9d3ef8eb47b0f2a8829f131495 Mon Sep 17 00:00:00 2001 From: MykolaGolubyev Date: Thu, 11 Apr 2019 08:01:03 -0400 Subject: [PATCH 1/2] remove url-search-params polyfill --- package.json | 3 +-- src/components/viewer/ComponentViewerStateCreator.ts | 2 -- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/package.json b/package.json index cc6fb95..064270f 100644 --- a/package.json +++ b/package.json @@ -23,8 +23,7 @@ ], "dependencies": { "react": "16.8.5", - "react-dom": "16.8.5", - "url-search-params-polyfill": "^5.1.0" + "react-dom": "16.8.5" }, "scripts": { "start": "react-scripts-ts start", diff --git a/src/components/viewer/ComponentViewerStateCreator.ts b/src/components/viewer/ComponentViewerStateCreator.ts index 1c5cd06..6806b54 100644 --- a/src/components/viewer/ComponentViewerStateCreator.ts +++ b/src/components/viewer/ComponentViewerStateCreator.ts @@ -1,5 +1,3 @@ -import 'url-search-params-polyfill'; - import { ComponentViewerState } from './ComponentViewerState'; import { Registry, Registries } from '../'; import { DemoEntryAndRegistry } from '../registry/DemoEntryAndRegistry'; From d8010444db04e8dbc98d12c05a59bfabe4fda760 Mon Sep 17 00:00:00 2001 From: MykolaGolubyev Date: Thu, 11 Apr 2019 08:01:47 -0400 Subject: [PATCH 2/2] append current search url when select mini app url to open --- package-lock.json | 5 ----- .../miniapp/MiniAppUrlsSelection.tsx | 20 +++++++++++++------ src/components/miniapp/urlUtils.test.ts | 8 ++++++++ src/components/miniapp/urlUtils.ts | 5 +++++ 4 files changed, 27 insertions(+), 11 deletions(-) create mode 100644 src/components/miniapp/urlUtils.test.ts diff --git a/package-lock.json b/package-lock.json index 4e32d72..f6a1cc6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20012,11 +20012,6 @@ "prepend-http": "^1.0.1" } }, - "url-search-params-polyfill": { - "version": "5.1.0", - "resolved": "https://registry.npmjs.org/url-search-params-polyfill/-/url-search-params-polyfill-5.1.0.tgz", - "integrity": "sha512-yjFY7uw2xRf9e8Mg4ZVkZwtp8dMCC4cbBkEIZiTDpuSY2WJ9+Quw0wRhxncv32qaMQwmBQT+P847rO8PrFhhDA==" - }, "use": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/use/-/use-3.1.1.tgz", diff --git a/src/components/miniapp/MiniAppUrlsSelection.tsx b/src/components/miniapp/MiniAppUrlsSelection.tsx index 35796a5..7c95ed1 100644 --- a/src/components/miniapp/MiniAppUrlsSelection.tsx +++ b/src/components/miniapp/MiniAppUrlsSelection.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { currentUrlMatchesRegexp } from './urlUtils'; +import { appendUrlSearch, currentUrlMatchesRegexp } from './urlUtils'; export interface Props { component: React.ComponentType; @@ -16,11 +16,19 @@ export function MiniAppUrlsSelection({component: Component, urlRegexp, urlsByLab return (
- {Object.keys(urlsByLabel).map(label => ( -
- {label} -
- ))} + {Object.keys(urlsByLabel).map(label => { + const url = urlsByLabel[label]; + return ( +
+ selectUrl(e, url)}>{label} +
+ ); + })}
); + + function selectUrl(e: React.MouseEvent, url: string) { + e.preventDefault(); + document.location.href = appendUrlSearch(url, document.location.search); + } } \ No newline at end of file diff --git a/src/components/miniapp/urlUtils.test.ts b/src/components/miniapp/urlUtils.test.ts new file mode 100644 index 0000000..4111609 --- /dev/null +++ b/src/components/miniapp/urlUtils.test.ts @@ -0,0 +1,8 @@ +import { appendUrlSearch } from './urlUtils'; + +describe('url utils', () => { + it('append url search', () => { + expect(appendUrlSearch('/just/path', '?a=2&b=3')).toEqual('/just/path?a=2&b=3'); + expect(appendUrlSearch('/just/path?c=3', '?a=2')).toEqual('/just/path?c=3&a=2'); + }); +}); \ No newline at end of file diff --git a/src/components/miniapp/urlUtils.ts b/src/components/miniapp/urlUtils.ts index c183f51..e07b801 100644 --- a/src/components/miniapp/urlUtils.ts +++ b/src/components/miniapp/urlUtils.ts @@ -4,4 +4,9 @@ export function currentUrl() { export function currentUrlMatchesRegexp(urlRegexp: RegExp) { return urlRegexp.test(currentUrl()); +} + +export function appendUrlSearch(originalUrl: string, search: string) { + const hasSearch = originalUrl.indexOf('?') !== -1; + return originalUrl + (hasSearch ? '&' : '?') + search.replace('?', ''); } \ No newline at end of file