Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
dschlabach committed Nov 15, 2024
1 parent c8b7ace commit 8502bc3
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 31 deletions.
19 changes: 12 additions & 7 deletions playground/nextjs-app-router/components/AppProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,29 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => {
const [isSponsored, setIsSponsoredState] = useState<boolean>(false);

// Load initial values from localStorage
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: TODO Refactor this component
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: TODO: Refactor this component
useEffect(() => {
const urlState = initializeStateFromUrl();
console.log('urlState:', urlState);

// Common options
const storedActiveComponent =
urlState.activecomponent || localStorage.getItem('activeComponent');
urlState.activeComponent || localStorage.getItem('activeComponent');
const storedWalletType =
urlState.wallettype || localStorage.getItem('walletType');
urlState.walletType || localStorage.getItem('walletType');
const storedComponentTheme = (urlState.componentTheme ||
localStorage.getItem('componentTheme')) as ComponentTheme;
const storedComponentMode = (urlState.componentMode ||
localStorage.getItem('componentMode')) as ComponentMode;

// Component-specific options
const storedChainId = urlState.chain || localStorage.getItem('chainId');
const storedPaymasters =
urlState.paymasterurl || localStorage.getItem('paymasters');
const storedTransactionType =
urlState.transactiontype || localStorage.getItem('transactionType');
const storedDefaultMaxSlippage =
urlState.defaultmaxslippage || localStorage.getItem('defaultMaxSlippage');
const storedComponentTheme = (urlState.componenttheme ||
localStorage.getItem('componentTheme')) as ComponentTheme;
const storedComponentMode = (urlState.componentmode ||
localStorage.getItem('componentMode')) as ComponentMode;
const storedNFTToken =
urlState.nfttoken || localStorage.getItem('nftToken');
const storedIsSponsored =
Expand All @@ -131,6 +135,7 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => {
setDefaultMaxSlippage(Number(storedDefaultMaxSlippage));
}
if (storedComponentTheme) {
console.log('initializing component theme:', storedComponentTheme);
setComponentTheme(storedComponentTheme);
}
if (storedComponentMode) {
Expand Down
46 changes: 25 additions & 21 deletions playground/nextjs-app-router/components/Demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import TransactionDefaultDemo from './demo/TransactionDefault';
import WalletDemo from './demo/Wallet';
import WalletDefaultDemo from './demo/WalletDefault';
import { OnchainKitComponent } from '@/types/onchainkit';
import { getShareableUrl } from '@/lib/url-params';

const activeComponentMapping: Record<OnchainKitComponent, React.FC> = {
[OnchainKitComponent.Fund]: FundDemo,
Expand All @@ -43,8 +44,8 @@ function Demo() {
const [copied, setCopied] = useState(false);

const copyShareableLink = () => {
// const url = getShareableUrl(activeComponent);
// navigator.clipboard.writeText(url);
const url = getShareableUrl(activeComponent);
navigator.clipboard.writeText(url);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
};
Expand Down Expand Up @@ -105,25 +106,28 @@ function Demo() {
<form className="mt-4 grid gap-8">
<DemoOptions component={activeComponent} />
</form>
<div className="mt-auto pt-6 text-sm">
<a
className="opacity-100 transition-opacity duration-200 hover:opacity-70"
href="https://github.com/coinbase/onchainkit/tree/main/playground"
rel="noreferrer"
target="_blank"
title="View OnchainKit Playground on GitHub"
>
Github ↗
</a>
<a
className="pl-4 opacity-100 transition-opacity duration-200 hover:opacity-70"
href="https://onchainkit.xyz"
rel="noreferrer"
target="_blank"
title="View OnchainKit"
>
OnchainKit ↗
</a>
<div className="mt-auto flex items-center justify-between pt-6 text-sm">
<div>
<a
className="opacity-100 transition-opacity duration-200 hover:opacity-70"
href="https://github.com/coinbase/onchainkit/tree/main/playground"
rel="noreferrer"
target="_blank"
title="View OnchainKit Playground on GitHub"
>
Github ↗
</a>
<a
className="pl-4 opacity-100 transition-opacity duration-200 hover:opacity-70"
href="https://onchainkit.xyz"
rel="noreferrer"
target="_blank"
title="View OnchainKit"
>
OnchainKit ↗
</a>
</div>

<button
type="button"
onClick={copyShareableLink}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ import type { ComponentTheme as ComponentThemeType } from '@/types/onchainkit';

export function ComponentTheme() {
const { componentTheme, setComponentTheme } = useContext(AppContext);
console.log('componentTheme ():', componentTheme);

return (
<div className="grid gap-2">
<Label htmlFor="theme">Component Theme</Label>
<Select
value={componentTheme}
onValueChange={(value: ComponentThemeType) => setComponentTheme(value)}
onValueChange={(value: ComponentThemeType) => {
console.log('onValueChange raw value:', value);
console.log('onValueChange typeof value:', typeof value);
console.log('current componentTheme:', componentTheme);
// return setComponentTheme(value);
}}
>
<SelectTrigger id="theme">
<SelectValue placeholder="Select theme" />
Expand Down
16 changes: 14 additions & 2 deletions playground/nextjs-app-router/lib/url-params.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { OnchainKitComponent } from '@/types/onchainkit';

const commonOptions = [
'activeComponent',
'componentMode',
'componentTheme',
'walletType',
];

const URL_PARAM_MAPPING: Partial<Record<OnchainKitComponent, string[]>> = {
[OnchainKitComponent.Checkout]: ['chargeId', 'productId'],
[OnchainKitComponent.Transaction]: ['calls', 'contracts'],
Expand All @@ -13,14 +20,18 @@ export function getShareableUrl(activeComponent?: OnchainKitComponent) {
const relevantParams = getComponentQueryParams(activeComponent);
const params = new URLSearchParams();

for (const param of relevantParams) {
params.set(param, localStorage.getItem(param) || '');
for (const param of [...relevantParams, ...commonOptions]) {
const value = localStorage.getItem(param);
if (value) {
params.set(param, value);
}
}
return `${window.location.origin}?${params.toString()}`;
}

export function getComponentQueryParams(component: OnchainKitComponent) {
const options = URL_PARAM_MAPPING[component];

if (!options) {
return [];
}
Expand All @@ -37,6 +48,7 @@ export function initializeStateFromUrl(): Record<string, string> {
const state: Record<string, string> = {};

for (const [key, value] of params.entries()) {
localStorage.setItem(key, value);
state[key] = value;
}
return state;
Expand Down

0 comments on commit 8502bc3

Please sign in to comment.