Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set client boundary for Swap and Token components #1800

Merged
merged 14 commits into from
Jan 14, 2025
1 change: 0 additions & 1 deletion packemon.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const config = {
// Remove the original directive and split into lines
const lines = code.replace("'use client';", '').split('\n');
// Filter out any empty lines and reconstruct

return {
code: `'use client';\n${lines.filter((line) => line.trim()).join('\n')}`,
map: null,
Expand Down
43 changes: 41 additions & 2 deletions src/core-react/internal/utils/findComponent.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ReactNode } from 'react';
import type { ReactElement, ReactNode } from 'react';
import { describe, expect, it } from 'vitest';
import { Avatar } from '../../../ui/react/identity/components/Avatar';
import { Name } from '../../../ui/react/identity/components/Name';
import { findComponent } from './findComponent';
import { type ServerComponentPayload, findComponent } from './findComponent';

describe('findComponent', () => {
it('should find the Name component in the array', () => {
Expand Down Expand Up @@ -32,4 +32,43 @@ describe('findComponent', () => {
const foundNameComponent = childrenArray.find(findComponent(Name));
expect(foundNameComponent).toBeUndefined();
});

it('should find component in Next.js server component payload', () => {
const serverComponent: ReactElement & { type: ServerComponentPayload } = {
type: {
_payload: {
value: ['/path/to/Name', ['chunk1', 'chunk2'], 'Name'],
},
},
props: { address: '0x123456789' },
key: null,
ref: null,
} as unknown as ReactElement & { type: ServerComponentPayload };

const childrenArray: ReactNode[] = [
<div key="1">Random div</div>,
serverComponent,
<Avatar key="3" address="0x123456789" />,
];

const foundNameComponent = childrenArray.find(findComponent(Name));
expect(foundNameComponent).toBeDefined();
});

it('should return undefined for server component with different name', () => {
const serverComponent: ReactElement & { type: ServerComponentPayload } = {
type: {
_payload: {
value: ['/path/to/Different', ['chunk1'], 'Different'],
},
},
props: {},
key: null,
ref: null,
} as unknown as ReactElement & { type: ServerComponentPayload };

const childrenArray: ReactNode[] = [serverComponent];
const foundNameComponent = childrenArray.find(findComponent(Name));
expect(foundNameComponent).toBeUndefined();
});
});
21 changes: 19 additions & 2 deletions src/core-react/internal/utils/findComponent.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
import { isValidElement } from 'react';
import type { ComponentType, ReactElement, ReactNode } from 'react';

export function findComponent<T>(component: ComponentType<T>) {
// Type for Next.js Server Component Payload
// Temporary patch until we update to deafult children and remove internal findComponent
export interface ServerComponentPayload {
_payload: {
value: [string, string[], string]; // [modulePath, chunks, componentName]
};
}

export function findComponent<T>(Component: ComponentType<T>) {
return (child: ReactNode): child is ReactElement<T> => {
return isValidElement(child) && child.type === component;
const childType = (child as ReactElement<T>)?.type;

// Handle server component payload
if (childType && typeof childType === 'object' && '_payload' in childType) {
const serverPayload = childType as ServerComponentPayload;
return serverPayload._payload.value[2] === Component.name;
}

// Handle client component
return isValidElement(child) && child.type === Component;
};
}
1 change: 1 addition & 0 deletions src/swap/components/Swap.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import { Children, useMemo } from 'react';
import { useIsMounted } from '../../core-react/internal/hooks/useIsMounted';
import { useTheme } from '../../core-react/internal/hooks/useTheme';
Expand Down
1 change: 1 addition & 0 deletions src/swap/components/SwapAmountInput.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import { useCallback, useEffect, useMemo } from 'react';
import { useValue } from '../../core-react/internal/hooks/useValue';
import { getRoundedAmount } from '../../core/utils/getRoundedAmount';
Expand Down
1 change: 1 addition & 0 deletions src/swap/components/SwapButton.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import { Spinner } from '@/internal/components/Spinner';
import { background, border, cn, color, pressable, text } from '@/styles/theme';
import { ConnectWallet } from '@/wallet/components/ConnectWallet';
Expand Down
1 change: 1 addition & 0 deletions src/swap/components/SwapDefault.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import type { SwapDefaultReact } from '../types';
import { Swap } from './Swap';
import { SwapAmountInput } from './SwapAmountInput';
Expand Down
1 change: 1 addition & 0 deletions src/swap/components/SwapMessage.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import { cn, color, text } from '../../styles/theme';
import type { SwapMessageReact } from '../types';
import { getSwapMessage } from '../utils/getSwapMessage';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import { render, screen } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';
import { SwapSettingsSlippageDescription } from './SwapSettingsSlippageDescription';
Expand Down
1 change: 1 addition & 0 deletions src/swap/components/SwapSettingsSlippageInput.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import { useCallback, useState } from 'react';
import {
background,
Expand Down
1 change: 1 addition & 0 deletions src/swap/components/SwapSettingsSlippageTitle.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import { cn, color, text } from '../../styles/theme';
import type { SwapSettingsSlippageTitleReact } from '../types';

Expand Down
1 change: 1 addition & 0 deletions src/swap/components/SwapToast.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import { useCallback } from 'react';
import { cn, color, text } from '../../styles/theme';

Expand Down
1 change: 1 addition & 0 deletions src/swap/components/SwapToggleButton.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import { toggleSvg } from '../../internal/svg/toggleSvg';
import { border, cn, pressable } from '../../styles/theme';
import type { SwapToggleButtonReact } from '../types';
Expand Down
3 changes: 3 additions & 0 deletions src/swap/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// 🌲☀🌲
// Components
export { Swap } from './components/Swap';
export { SwapAmountInput } from './components/SwapAmountInput';
export { SwapButton } from './components/SwapButton';
Expand All @@ -10,6 +11,8 @@ export { SwapSettingsSlippageInput } from './components/SwapSettingsSlippageInpu
export { SwapSettingsSlippageTitle } from './components/SwapSettingsSlippageTitle';
export { SwapToast } from './components/SwapToast';
export { SwapToggleButton } from './components/SwapToggleButton';

// Types
export type {
/** @deprecated Prefer import from `api` module */
BuildSwapTransaction,
Expand Down
4 changes: 3 additions & 1 deletion src/token/components/TokenChip.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use client';

import { useTheme } from '../../core-react/internal/hooks/useTheme';
import { background, cn, pressable, text } from '../../styles/theme';
import type { TokenChipReact } from '../types';
import { TokenImage } from './TokenImage';

/**
* Small button that display a given token symbol and image.
* Small button that displays a given token symbol and image.
*
* WARNING: This component is under development and
* may change in the next few weeks.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😆

Expand Down
2 changes: 2 additions & 0 deletions src/token/components/TokenImage.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client';

import { useMemo } from 'react';
import { cn } from '../../styles/theme';
import type { TokenImageReact } from '../types';
Expand Down
2 changes: 2 additions & 0 deletions src/token/components/TokenRow.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client';

import { memo } from 'react';
import { useTheme } from '../../core-react/internal/hooks/useTheme';
import { cn, color, pressable, text } from '../../styles/theme';
Expand Down
2 changes: 2 additions & 0 deletions src/token/components/TokenSearch.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client';

import { useCallback, useState } from 'react';
import { useTheme } from '../../core-react/internal/hooks/useTheme';
import { TextInput } from '../../internal/components/TextInput';
Expand Down
2 changes: 2 additions & 0 deletions src/token/components/TokenSelectDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client';

import { useCallback, useEffect, useRef, useState } from 'react';
import { useTheme } from '../../core-react/internal/hooks/useTheme';
import { background, border, cn } from '../../styles/theme';
Expand Down
1 change: 1 addition & 0 deletions src/token/components/TokenSelectModal.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import { useCallback, useEffect, useRef, useState } from 'react';
import { background, cn, text } from '../../styles/theme';
import type { Token, TokenSelectModalReact } from '../types';
Expand Down
5 changes: 5 additions & 0 deletions src/token/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
// 🌲☀🌲
// Components
export { TokenChip } from './components/TokenChip';
export { TokenImage } from './components/TokenImage';
export { TokenRow } from './components/TokenRow';
export { TokenSearch } from './components/TokenSearch';
export { TokenSelectDropdown } from './components/TokenSelectDropdown';
export { TokenSelectModal } from './components/TokenSelectModal';

// Utils
export { formatAmount } from './utils/formatAmount';

// Types
export type {
FormatAmountOptions,
FormatAmountResponse,
Expand Down
Loading