Skip to content

Commit

Permalink
[FRE 876] Add persistSwapWidgetState prop (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
toddkao authored Jul 24, 2024
1 parent fe6981a commit 52e827f
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-dragons-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@skip-go/widget': minor
---

Add prop persistSwapWidgetState
1 change: 1 addition & 0 deletions docs/widget/configuration-options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ interface SwapWidgetProps {
brandColor: string; // color used for confirmation buttons
highlightColor: string; // color used when hovering over buttons, and in select chain/asset dropdown
};
persistSwapWidgetState?: boolean; // whether or not swap widget state should persist after refresh. Defaults to true
}
```

Expand Down
1 change: 1 addition & 0 deletions examples/nextjs/pages/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const Home: NextPage = () => {
srcAssetDenom:
'ibc/1480b8fd20ad5fcae81ea87584d269547dd4d436843c1d20f15e00eb64743ef4',
}}
persistSwapWidgetState={false}
/>
</SwapWidgetProvider>
</div>
Expand Down
1 change: 1 addition & 0 deletions packages/widget/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ interface SwapWidgetProps {
brandColor: string; // color used for confirmation buttons
highlightColor: string; // color used when hovering over buttons, and in select chain/asset dropdown
};
persistSwapWidgetState?: boolean; // whether or not swap widget state should persist after refresh. Defaults to true
}
````

Expand Down
8 changes: 6 additions & 2 deletions packages/widget/src/hooks/use-swap-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ export interface DefaultRouteConfig {
destAssetDenom?: string;
}

export function useSwapWidget() {
export function useSwapWidget(persistSwapWidgetState = true) {
/**
* intentional manual hydration to prevent ssr mismatch
* @see {useSwapWidgetStore}
*/
useEffect(() => void useSwapWidgetStore.persist.rehydrate(), []);
useEffect(() => {
if (persistSwapWidgetState) {
useSwapWidgetStore.persist.rehydrate();
}
}, []);

/////////////////////////////////////////////////////////////////////////////

Expand Down
2 changes: 1 addition & 1 deletion packages/widget/src/ui/PreviewRoute/SetAddressDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const SetAddressDialog = ({
return '0x...';
}
if (chainType === 'svm') {
return 'Enter solanma address...';
return 'Enter solana address...';
}
return 'Enter address...';
}, [chainType, bech32Prefix]);
Expand Down
4 changes: 3 additions & 1 deletion packages/widget/src/ui/Widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ export type SwapWidgetUIProps = Pick<
'className' | 'style'
> & {
toasterProps?: ToasterProps;
persistSwapWidgetState?: boolean;
};

export const SwapWidgetUI = ({
className,
style,
toasterProps,
persistSwapWidgetState,
}: SwapWidgetUIProps) => {
const theme = useTheme();
useEffect(() => void disclosure.rehydrate(), []);
Expand Down Expand Up @@ -102,7 +104,7 @@ export const SwapWidgetUI = ({
swapPriceImpactPercent,
usdDiffPercent,
shareable,
} = useSwapWidget();
} = useSwapWidget(persistSwapWidgetState);

const srcAccount = useAccount(sourceChain?.chainID);

Expand Down
2 changes: 2 additions & 0 deletions packages/widget/src/ui/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const SwapWidget: React.FC<SwapWidgetProps> = ({
style,
filter,
toasterProps,
persistSwapWidgetState,
...swapWidgetProviderProps
}) => {
useEffect(() => {
Expand Down Expand Up @@ -91,6 +92,7 @@ export const SwapWidget: React.FC<SwapWidgetProps> = ({
className={className}
style={style}
toasterProps={toasterProps}
persistSwapWidgetState={persistSwapWidgetState}
/>
</SwapWidgetProvider>
</ThemeProvider>
Expand Down

0 comments on commit 52e827f

Please sign in to comment.