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

Draft
wants to merge 2 commits into
base: dms/use-client-2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,
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