Skip to content

Commit c53d513

Browse files
ericglauernestognw
andauthored
Enable prefilling initial options from URL search parameters (#380)
Co-authored-by: Ernesto García <[email protected]>
1 parent ca6115b commit c53d513

File tree

5 files changed

+57
-7
lines changed

5 files changed

+57
-7
lines changed

packages/ui/src/App.svelte

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
2828
import { saveAs } from 'file-saver';
2929
import { injectHyperlinks } from './utils/inject-hyperlinks';
30+
import { InitialOptions } from './initial-options';
3031
3132
const dispatch = createEventDispatcher();
3233
@@ -38,17 +39,34 @@
3839
dispatch('tab-change', tab);
3940
};
4041
42+
export let initialOpts: InitialOptions = {};
43+
let initialValuesSet = false;
44+
4145
let allOpts: { [k in Kind]?: Required<KindedOptions[k]> } = {};
4246
let errors: { [k in Kind]?: OptionsErrorMessages } = {};
4347
44-
let contract: Contract = new ContractBuilder('MyToken');
48+
let contract: Contract = new ContractBuilder(initialOpts.name ?? 'MyToken');
4549
4650
$: functionCall && applyFunctionCall()
4751
4852
$: opts = allOpts[tab];
4953
5054
$: {
5155
if (opts) {
56+
if (!initialValuesSet) {
57+
opts.name = initialOpts.name ?? opts.name;
58+
switch (opts.kind) {
59+
case 'ERC20':
60+
opts.premint = initialOpts.premint ?? opts.premint;
61+
case 'ERC721':
62+
opts.symbol = initialOpts.symbol ?? opts.symbol;
63+
break;
64+
case 'ERC1155':
65+
case 'Governor':
66+
case 'Custom':
67+
}
68+
initialValuesSet = true;
69+
}
5270
try {
5371
contract = buildGeneric(opts);
5472
errors[tab] = undefined;

packages/ui/src/cairo/App.svelte

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
2121
import { saveAs } from 'file-saver';
2222
import { injectHyperlinks } from './inject-hyperlinks';
23+
import { InitialOptions } from '../initial-options';
2324
2425
const dispatch = createEventDispatcher();
2526
@@ -31,15 +32,31 @@
3132
dispatch('tab-change', tab);
3233
};
3334
35+
export let initialOpts: InitialOptions = {};
36+
let initialValuesSet = false;
37+
3438
let allOpts: { [k in Kind]?: Required<KindedOptions[k]> } = {};
3539
let errors: { [k in Kind]?: OptionsErrorMessages } = {};
3640
37-
let contract: Contract = new ContractBuilder('MyToken');
41+
let contract: Contract = new ContractBuilder(initialOpts.name ?? 'MyToken');
3842
3943
$: opts = allOpts[tab];
4044
4145
$: {
4246
if (opts) {
47+
if (!initialValuesSet) {
48+
opts.name = initialOpts.name ?? opts.name;
49+
switch (opts.kind) {
50+
case 'ERC20':
51+
opts.premint = initialOpts.premint ?? opts.premint;
52+
case 'ERC721':
53+
opts.symbol = initialOpts.symbol ?? opts.symbol;
54+
break;
55+
case 'ERC1155':
56+
case 'Custom':
57+
}
58+
initialValuesSet = true;
59+
}
4360
try {
4461
contract = buildGeneric(opts);
4562
errors[tab] = undefined;

packages/ui/src/embed.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,19 @@ onDOMContentLoaded(function () {
3939
setSearchParam(w, src.searchParams, 'data-lang', 'lang');
4040
setSearchParam(w, src.searchParams, 'data-tab', 'tab');
4141
setSearchParam(w, src.searchParams, 'version', 'version');
42+
4243
const sync = w.getAttribute('data-sync-url');
4344

4445
if (sync === 'fragment') {
45-
const fragment = window.location.hash.replace('#', '');
46-
if (fragment) {
47-
src.searchParams.set('tab', fragment);
46+
// Uses format: #tab&key=value&key=value...
47+
const fragments = window.location.hash.replace('#', '').split('&');
48+
for (const fragment of fragments) {
49+
const [key, value] = fragment.split('=', 2);
50+
if (key && value) {
51+
src.searchParams.set(key, value);
52+
} else {
53+
src.searchParams.set('tab', fragment);
54+
}
4855
}
4956
}
5057

packages/ui/src/initial-options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export interface InitialOptions { name?: string, symbol?: string, premint?: string };

packages/ui/src/main.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import UnsupportedVersion from './UnsupportedVersion.svelte';
88
import semver from 'semver';
99
import { compatibleContractsSemver as compatibleSolidityContractsSemver } from '@openzeppelin/wizard';
1010
import { compatibleContractsSemver as compatibleCairoContractsSemver } from '@openzeppelin/wizard-cairo';
11+
import { InitialOptions } from './initial-options';
1112

1213
function postResize() {
1314
const { height } = document.documentElement.getBoundingClientRect();
@@ -25,16 +26,22 @@ const initialTab = params.get('tab') ?? undefined;
2526
const lang = params.get('lang') ?? undefined;
2627
const requestedVersion = params.get('version') ?? undefined;
2728

29+
const initialOpts: InitialOptions = {
30+
name: params.get('name') ?? undefined,
31+
symbol: params.get('symbol') ?? undefined,
32+
premint: params.get('premint') ?? undefined,
33+
}
34+
2835
let compatibleVersionSemver = lang === 'cairo' ? compatibleCairoContractsSemver : compatibleSolidityContractsSemver;
2936

3037
let app;
3138
if (requestedVersion && !semver.satisfies(requestedVersion, compatibleVersionSemver)) {
3239
postMessage({ kind: 'oz-wizard-unsupported-version' });
3340
app = new UnsupportedVersion({ target: document.body, props: { requestedVersion, compatibleVersionSemver }});
3441
} else if (lang === 'cairo') {
35-
app = new CairoApp({ target: document.body, props: { initialTab } });
42+
app = new CairoApp({ target: document.body, props: { initialTab, initialOpts } });
3643
} else {
37-
app = new App({ target: document.body, props: { initialTab } });
44+
app = new App({ target: document.body, props: { initialTab, initialOpts } });
3845
}
3946

4047
app.$on('tab-change', (e: CustomEvent) => {

0 commit comments

Comments
 (0)