Skip to content

Commit 45e0d75

Browse files
Append current search url when select mini app url to open (#36)
1 parent b01b702 commit 45e0d75

6 files changed

Lines changed: 28 additions & 15 deletions

File tree

package-lock.json

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
],
2424
"dependencies": {
2525
"react": "16.8.5",
26-
"react-dom": "16.8.5",
27-
"url-search-params-polyfill": "^5.1.0"
26+
"react-dom": "16.8.5"
2827
},
2928
"scripts": {
3029
"start": "react-scripts-ts start",

src/components/miniapp/MiniAppUrlsSelection.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { currentUrlMatchesRegexp } from './urlUtils';
2+
import { appendUrlSearch, currentUrlMatchesRegexp } from './urlUtils';
33

44
export interface Props {
55
component: React.ComponentType;
@@ -16,11 +16,19 @@ export function MiniAppUrlsSelection({component: Component, urlRegexp, urlsByLab
1616

1717
return (
1818
<div>
19-
{Object.keys(urlsByLabel).map(label => (
20-
<div>
21-
<a href={urlsByLabel[label]}>{label}</a>
22-
</div>
23-
))}
19+
{Object.keys(urlsByLabel).map(label => {
20+
const url = urlsByLabel[label];
21+
return (
22+
<div key={label}>
23+
<a href={url} onClick={(e) => selectUrl(e, url)}>{label}</a>
24+
</div>
25+
);
26+
})}
2427
</div>
2528
);
29+
30+
function selectUrl(e: React.MouseEvent<HTMLElement>, url: string) {
31+
e.preventDefault();
32+
document.location.href = appendUrlSearch(url, document.location.search);
33+
}
2634
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { appendUrlSearch } from './urlUtils';
2+
3+
describe('url utils', () => {
4+
it('append url search', () => {
5+
expect(appendUrlSearch('/just/path', '?a=2&b=3')).toEqual('/just/path?a=2&b=3');
6+
expect(appendUrlSearch('/just/path?c=3', '?a=2')).toEqual('/just/path?c=3&a=2');
7+
});
8+
});

src/components/miniapp/urlUtils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@ export function currentUrl() {
44

55
export function currentUrlMatchesRegexp(urlRegexp: RegExp) {
66
return urlRegexp.test(currentUrl());
7+
}
8+
9+
export function appendUrlSearch(originalUrl: string, search: string) {
10+
const hasSearch = originalUrl.indexOf('?') !== -1;
11+
return originalUrl + (hasSearch ? '&' : '?') + search.replace('?', '');
712
}

src/components/viewer/ComponentViewerStateCreator.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'url-search-params-polyfill';
2-
31
import { ComponentViewerState } from './ComponentViewerState';
42
import { Registry, Registries } from '../';
53
import { DemoEntryAndRegistry } from '../registry/DemoEntryAndRegistry';

0 commit comments

Comments
 (0)