Adjust/optimize table column rendering (#8839)
* Adjust/optimize table column rendering * Split helper * Adjust * Remove ddebug helper logs * Don't render children on empty array * Pass isEmpty to Body * Account hooks ordering
This commit is contained in:
parent
0f035babda
commit
86eba636a5
@ -5,8 +5,9 @@ import type { ActionStatus } from '@polkadot/react-components/Status/types';
|
||||
import type { KeyringAddress } from '@polkadot/ui-keyring/types';
|
||||
import type { BN } from '@polkadot/util';
|
||||
import type { AccountBalance, Delegation, SortedAccount } from '../types';
|
||||
import type { SortCategory } from '../util';
|
||||
|
||||
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import styled from 'styled-components';
|
||||
|
||||
import { Button, FilterInput, SortDropdown, SummaryBox, Table } from '@polkadot/react-components';
|
||||
@ -23,7 +24,7 @@ import Multisig from '../modals/MultisigCreate';
|
||||
import Proxy from '../modals/ProxiedAdd';
|
||||
import Qr from '../modals/Qr';
|
||||
import { useTranslation } from '../translate';
|
||||
import { sortAccounts, SortCategory, sortCategory } from '../util';
|
||||
import { SORT_CATEGORY, sortAccounts } from '../util';
|
||||
import Account from './Account';
|
||||
import BannerClaims from './BannerClaims';
|
||||
import BannerExtension from './BannerExtension';
|
||||
@ -93,7 +94,7 @@ function Overview ({ className = '', onStatusChange }: Props): React.ReactElemen
|
||||
const { allAccounts, hasAccounts } = useAccounts();
|
||||
const { isIpfs } = useIpfs();
|
||||
const { isLedgerEnabled } = useLedger();
|
||||
const [isCreateOpen, toggleCreate, setIsCreateOpen] = useToggle();
|
||||
const [isCreateOpen, toggleCreate] = useToggle();
|
||||
const [isImportOpen, toggleImport] = useToggle();
|
||||
const [isLedgerOpen, toggleLedger] = useToggle();
|
||||
const [isMultisigOpen, toggleMultisig] = useToggle();
|
||||
@ -108,6 +109,41 @@ function Overview ({ className = '', onStatusChange }: Props): React.ReactElemen
|
||||
const proxies = useProxies();
|
||||
const isLoading = useLoadingDelay();
|
||||
|
||||
const onSortChange = useCallback(
|
||||
(sortBy: SortCategory) => setSortBy(({ sortFromMax }) => ({ sortBy, sortFromMax })),
|
||||
[]
|
||||
);
|
||||
|
||||
const onSortDirectionChange = useCallback(
|
||||
() => setSortBy(({ sortBy, sortFromMax }) => ({ sortBy, sortFromMax: !sortFromMax })),
|
||||
[]
|
||||
);
|
||||
|
||||
const sortOptions = useRef(SORT_CATEGORY.map((text) => ({ text, value: text })));
|
||||
|
||||
const setBalance = useCallback(
|
||||
(account: string, balance: AccountBalance) =>
|
||||
setBalances(({ accounts }: Balances): Balances => {
|
||||
accounts[account] = balance;
|
||||
|
||||
const aggregate = (key: keyof AccountBalance) =>
|
||||
Object.values(accounts).reduce((total: BN, value: AccountBalance) => total.add(value[key]), BN_ZERO);
|
||||
|
||||
return {
|
||||
accounts,
|
||||
summary: {
|
||||
bonded: aggregate('bonded'),
|
||||
locked: aggregate('locked'),
|
||||
redeemable: aggregate('redeemable'),
|
||||
total: aggregate('total'),
|
||||
transferrable: aggregate('transferrable'),
|
||||
unbonding: aggregate('unbonding')
|
||||
}
|
||||
};
|
||||
}),
|
||||
[]
|
||||
);
|
||||
|
||||
const canStoreAccounts = useMemo(
|
||||
() => isElectron || (!isIpfs && settings.get().storage === 'on'),
|
||||
[isElectron, isIpfs]
|
||||
@ -132,7 +168,7 @@ function Overview ({ className = '', onStatusChange }: Props): React.ReactElemen
|
||||
[api]
|
||||
);
|
||||
|
||||
const accountsWithInfo = useMemo(
|
||||
const accountsMap = useMemo(
|
||||
() => allAccounts
|
||||
.map((address, index): Omit<SortedAccount, 'account'> & { account: KeyringAddress | undefined } => {
|
||||
const deleg = delegations && delegations[index]?.isDelegating && delegations[index]?.asDelegating;
|
||||
@ -149,19 +185,15 @@ function Overview ({ className = '', onStatusChange }: Props): React.ReactElemen
|
||||
isFavorite: favoritesMap[address ?? ''] ?? false
|
||||
};
|
||||
})
|
||||
.filter((a): a is SortedAccount => !!a.account),
|
||||
.filter((a): a is SortedAccount => !!a.account)
|
||||
.reduce((ret: Record<string, SortedAccount>, x) => {
|
||||
ret[x.address] = x;
|
||||
|
||||
return ret;
|
||||
}, {}),
|
||||
[allAccounts, favoritesMap, delegations]
|
||||
);
|
||||
|
||||
const accountsMap = useMemo(
|
||||
() => accountsWithInfo.reduce<Record<string, SortedAccount>>((ret, x) => {
|
||||
ret[x.address] = x;
|
||||
|
||||
return ret;
|
||||
}, {}),
|
||||
[accountsWithInfo]
|
||||
);
|
||||
|
||||
const header = useMemo(
|
||||
(): Record<GroupName, [React.ReactNode?, string?, number?, (() => void)?][]> => {
|
||||
const ret: Record<GroupName, [React.ReactNode?, string?, number?, (() => void)?][]> = {
|
||||
@ -184,75 +216,60 @@ function Overview ({ className = '', onStatusChange }: Props): React.ReactElemen
|
||||
[t]
|
||||
);
|
||||
|
||||
useEffect((): void => {
|
||||
// We add new accounts to the end
|
||||
setSorted((sortedAccounts) =>
|
||||
[...sortedAccounts.map((x) => accountsWithInfo.find((y) => x.address === y.address)).filter((x): x is SortedAccount => !!x),
|
||||
...accountsWithInfo.filter((x) => !sortedAccounts.find((y) => x.address === y.address))]);
|
||||
}, [accountsWithInfo]);
|
||||
|
||||
const accounts = balances.accounts;
|
||||
|
||||
useEffect((): void => {
|
||||
setSorted((sortedAccounts) =>
|
||||
sortAccounts(sortedAccounts, accountsMap, accounts, sortBy, sortFromMax));
|
||||
}, [accountsWithInfo, accountsMap, accounts, sortBy, sortFromMax]);
|
||||
|
||||
const _setBalance = useCallback(
|
||||
(account: string, balance: AccountBalance) =>
|
||||
setBalances(({ accounts }: Balances): Balances => {
|
||||
accounts[account] = balance;
|
||||
|
||||
const aggregate = (key: keyof AccountBalance) =>
|
||||
Object.values(accounts).reduce((total: BN, value: AccountBalance) => total.add(value[key]), BN_ZERO);
|
||||
|
||||
return {
|
||||
accounts,
|
||||
summary: {
|
||||
bonded: aggregate('bonded'),
|
||||
locked: aggregate('locked'),
|
||||
redeemable: aggregate('redeemable'),
|
||||
total: aggregate('total'),
|
||||
transferrable: aggregate('transferrable'),
|
||||
unbonding: aggregate('unbonding')
|
||||
}
|
||||
};
|
||||
}),
|
||||
[]
|
||||
);
|
||||
|
||||
const _openCreateModal = useCallback(() => setIsCreateOpen(true), [setIsCreateOpen]);
|
||||
|
||||
const grouped = useMemo(
|
||||
() => groupAccounts(sortedAccounts),
|
||||
[sortedAccounts]
|
||||
);
|
||||
|
||||
const accountComponents = useMemo(() => {
|
||||
const ret: Record<string, React.ReactNode> = {};
|
||||
|
||||
accountsWithInfo.forEach(({ account, address, delegation, isFavorite }, index) => {
|
||||
ret[address] =
|
||||
const accounts = useMemo(
|
||||
() => Object.values(accountsMap).reduce<Record<string, React.ReactNode>>((all, { account, address, delegation, isFavorite }, index) => {
|
||||
all[address] = (
|
||||
<Account
|
||||
account={account}
|
||||
delegation={delegation}
|
||||
filter={filterOn}
|
||||
isFavorite={isFavorite}
|
||||
key={`${index}:${address}`}
|
||||
key={address}
|
||||
proxy={proxies?.[index]}
|
||||
setBalance={_setBalance}
|
||||
setBalance={setBalance}
|
||||
toggleFavorite={toggleFavorite}
|
||||
/>;
|
||||
});
|
||||
/>
|
||||
);
|
||||
|
||||
return ret;
|
||||
}, [accountsWithInfo, filterOn, proxies, _setBalance, toggleFavorite]);
|
||||
return all;
|
||||
}, {}),
|
||||
[accountsMap, filterOn, proxies, setBalance, toggleFavorite]
|
||||
);
|
||||
|
||||
const onDropdownChange = () => (item: SortCategory) => setSortBy({ sortBy: item, sortFromMax });
|
||||
const groups = useMemo(
|
||||
() => GROUP_ORDER.reduce<Record<string, React.ReactNode[]>>((groups, group) => {
|
||||
const items = grouped[group];
|
||||
|
||||
const dropdownOptions = () => sortCategory.map((x) => ({ text: x, value: x }));
|
||||
if (items.length) {
|
||||
groups[group] = items.map((account) => accounts[account]);
|
||||
}
|
||||
|
||||
const onSortDirectionChange = () => () => setSortBy({ sortBy, sortFromMax: !sortFromMax });
|
||||
return groups;
|
||||
}, {}),
|
||||
[grouped, accounts]
|
||||
);
|
||||
|
||||
useEffect((): void => {
|
||||
setSorted((prev) => [
|
||||
...prev
|
||||
.map((x) => accountsMap[x.address])
|
||||
.filter((x): x is SortedAccount => !!x),
|
||||
...Object
|
||||
.keys(accountsMap)
|
||||
.filter((a) => !prev.find((y) => a === y.address))
|
||||
.map((a) => accountsMap[a])
|
||||
]);
|
||||
}, [accountsMap]);
|
||||
|
||||
useEffect((): void => {
|
||||
setSorted((sortedAccounts) =>
|
||||
sortAccounts(sortedAccounts, accountsMap, balances.accounts, sortBy, sortFromMax));
|
||||
}, [accountsMap, balances, sortBy, sortFromMax]);
|
||||
|
||||
return (
|
||||
<StyledDiv className={className}>
|
||||
@ -301,9 +318,9 @@ function Overview ({ className = '', onStatusChange }: Props): React.ReactElemen
|
||||
className='media--1500'
|
||||
defaultValue={sortBy}
|
||||
label={t<string>('sort by')}
|
||||
onChange={onDropdownChange()}
|
||||
onClick={onSortDirectionChange()}
|
||||
options={dropdownOptions()}
|
||||
onChange={onSortChange}
|
||||
onClick={onSortDirectionChange}
|
||||
options={sortOptions.current}
|
||||
sortDirection={
|
||||
sortFromMax
|
||||
? 'ascending'
|
||||
@ -322,7 +339,7 @@ function Overview ({ className = '', onStatusChange }: Props): React.ReactElemen
|
||||
<Button
|
||||
icon='plus'
|
||||
label={t<string>('Account')}
|
||||
onClick={_openCreateModal}
|
||||
onClick={toggleCreate}
|
||||
/>
|
||||
<Button
|
||||
icon='sync'
|
||||
@ -364,19 +381,17 @@ function Overview ({ className = '', onStatusChange }: Props): React.ReactElemen
|
||||
header={header.accounts}
|
||||
/>
|
||||
)
|
||||
: GROUP_ORDER.map((key) =>
|
||||
grouped[key].length
|
||||
? (
|
||||
<Table
|
||||
empty={t<string>('No accounts')}
|
||||
header={header[key]}
|
||||
isSplit
|
||||
key={key}
|
||||
>
|
||||
{grouped[key].map((a) => accountComponents[a])}
|
||||
</Table>
|
||||
)
|
||||
: null
|
||||
: GROUP_ORDER.map((group) =>
|
||||
groups[group] && (
|
||||
<Table
|
||||
empty={t<string>('No accounts')}
|
||||
header={header[group]}
|
||||
isSplit
|
||||
key={group}
|
||||
>
|
||||
{groups[group]}
|
||||
</Table>
|
||||
)
|
||||
)
|
||||
}
|
||||
</StyledDiv>
|
||||
|
||||
@ -68,15 +68,16 @@ export function tryCreateAccount (commitAccount: () => CreateResult, success: st
|
||||
return status;
|
||||
}
|
||||
|
||||
export const sortCategory = ['parent', 'name', 'date', 'balances'] as const;
|
||||
export type SortCategory = typeof sortCategory[number];
|
||||
export const SORT_CATEGORY = ['parent', 'name', 'date', 'balances'] as const;
|
||||
|
||||
const comparator = (accounts: Record<string, SortedAccount | undefined>, balances: Record<string, AccountBalance | undefined>, category: SortCategory, fromMax: boolean) => {
|
||||
export type SortCategory = typeof SORT_CATEGORY[number];
|
||||
|
||||
function comparator (accountsMap: Record<string, SortedAccount>, balances: Record<string, AccountBalance | undefined>, category: SortCategory, fromMax: boolean): (a: SortedAccount, b: SortedAccount) => number {
|
||||
function accountQualifiedName (account: SortedAccount | undefined): string {
|
||||
if (account) {
|
||||
const parent = (account.account?.meta.parentAddress || '') as string;
|
||||
|
||||
return accountQualifiedName(accounts[parent]) + account.address;
|
||||
return accountQualifiedName(accountsMap[parent]) + account.address;
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
@ -104,7 +105,7 @@ const comparator = (accounts: Record<string, SortedAccount | undefined>, balance
|
||||
case 'balances':
|
||||
return make((acc) => balances[acc.address]?.total ?? BN_ZERO, (a, b) => a.cmp(b));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function sortAccounts (accountsList: SortedAccount[], accountsMap: Record<string, SortedAccount>, balances: Record<string, AccountBalance>, by: SortCategory, fromMax: boolean): SortedAccount[] {
|
||||
return [...accountsList]
|
||||
@ -117,6 +118,8 @@ export function sortAccounts (accountsList: SortedAccount[], accountsMap: Record
|
||||
: -1);
|
||||
}
|
||||
|
||||
export const getJudgementColor = (name: DisplayedJudgement): 'green' | 'red' => {
|
||||
return (name === 'Erroneous' || name === 'Low quality') ? 'red' : 'green';
|
||||
};
|
||||
export function getJudgementColor (name: DisplayedJudgement): 'green' | 'red' {
|
||||
return (name === 'Erroneous' || name === 'Low quality')
|
||||
? 'red'
|
||||
: 'green';
|
||||
}
|
||||
|
||||
@ -12,19 +12,15 @@ interface Props {
|
||||
className?: string;
|
||||
empty?: React.ReactNode | false;
|
||||
emptySpinner?: React.ReactNode;
|
||||
isEmpty: boolean;
|
||||
noBodyTag?: boolean;
|
||||
}
|
||||
|
||||
function Body ({ children, className = '', empty, emptySpinner, noBodyTag }: Props): React.ReactElement<Props> {
|
||||
function Body ({ children, className = '', empty, emptySpinner, isEmpty, noBodyTag }: Props): React.ReactElement<Props> {
|
||||
const bodyClassName = `${className} ui--Table-Body`;
|
||||
|
||||
return children
|
||||
return isEmpty
|
||||
? (
|
||||
noBodyTag
|
||||
? <>{children}</>
|
||||
: <tbody className={bodyClassName}>{children}</tbody>
|
||||
)
|
||||
: (
|
||||
<tbody className={bodyClassName}>
|
||||
<tr>
|
||||
<td colSpan={100}>{
|
||||
@ -34,6 +30,11 @@ function Body ({ children, className = '', empty, emptySpinner, noBodyTag }: Pro
|
||||
}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
)
|
||||
: (
|
||||
noBodyTag
|
||||
? <>{children}</>
|
||||
: <tbody className={bodyClassName}>{children}</tbody>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2023 @polkadot/react-components authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import React, { useMemo } from 'react';
|
||||
import React from 'react';
|
||||
import styled from 'styled-components';
|
||||
|
||||
import { useWindowColumns } from '@polkadot/react-hooks';
|
||||
@ -29,69 +29,28 @@ interface Props {
|
||||
noBodyTag?: boolean;
|
||||
}
|
||||
|
||||
interface BodyState {
|
||||
body: React.ReactNode | React.ReactNode[];
|
||||
isEmpty: boolean;
|
||||
isSplittable: boolean;
|
||||
interface ColumnProps {
|
||||
children: React.ReactNode[];
|
||||
className?: string;
|
||||
numColumns: number;
|
||||
}
|
||||
|
||||
function extractBody (body: React.ReactNode, isSplit: boolean): BodyState {
|
||||
if (!Array.isArray(body)) {
|
||||
return {
|
||||
body,
|
||||
isEmpty: !body,
|
||||
isSplittable: false
|
||||
};
|
||||
}
|
||||
|
||||
const isEmpty = body.length === 0;
|
||||
const isSplittable = !isEmpty && isSplit;
|
||||
|
||||
return {
|
||||
body: isEmpty
|
||||
? null
|
||||
: body,
|
||||
isEmpty,
|
||||
isSplittable
|
||||
};
|
||||
function Split ({ children, className = '', numColumns }: ColumnProps): React.ReactElement<ColumnProps> {
|
||||
return (
|
||||
<div className={`${className} ui--Table-Split-${numColumns}`}>
|
||||
<table className='noMargin'>
|
||||
<tbody className='ui--Table-Body'>
|
||||
{children}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function splitBody (body: React.ReactNode[], numColumns: number): React.ReactNode[][] {
|
||||
const result = new Array<React.ReactNode[]>(numColumns);
|
||||
const numRows = Math.ceil(body.length / numColumns);
|
||||
|
||||
for (let i = 0; i < numColumns; i++) {
|
||||
result[i] = new Array<React.ReactNode>(numRows);
|
||||
}
|
||||
|
||||
// start at -1, on the first iteration it would
|
||||
// increment to 0 and all is ok
|
||||
let row = -1;
|
||||
|
||||
for (let i = 0; i < body.length; i++) {
|
||||
const col = i % numColumns;
|
||||
|
||||
if (col === 0) {
|
||||
row++;
|
||||
}
|
||||
|
||||
result[col][row] = body[i];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function TableBase ({ children, className = '', empty, emptySpinner, filter, footer, header, headerChildren, isFixed, isInline, isSplit = false, legend, maxColumns, noBodyTag }: Props): React.ReactElement<Props> {
|
||||
function TableBase ({ children, className = '', empty, emptySpinner, filter, footer, header, headerChildren, isFixed, isInline, isSplit, legend, maxColumns, noBodyTag }: Props): React.ReactElement<Props> {
|
||||
const numColumns = useWindowColumns(maxColumns);
|
||||
const { body, isEmpty, isSplittable } = useMemo(
|
||||
() => extractBody(children, isSplit),
|
||||
[children, isSplit]
|
||||
);
|
||||
|
||||
const bodySplit = useMemo(
|
||||
() => body && isSplittable && (numColumns > 1) && splitBody(body as React.ReactNode[], numColumns),
|
||||
[body, isSplittable, numColumns]
|
||||
);
|
||||
const isArray = Array.isArray(children);
|
||||
const isEmpty = !children || (isArray && children.length === 0);
|
||||
|
||||
const headerNode = (
|
||||
<Head
|
||||
@ -103,48 +62,45 @@ function TableBase ({ children, className = '', empty, emptySpinner, filter, foo
|
||||
</Head>
|
||||
);
|
||||
|
||||
const tableClassName = `${(isFixed && !isEmpty) ? 'isFixed' : 'isNotFixed'} ${isInline ? 'isInline' : ''} ${bodySplit ? 'noMargin' : ''}`;
|
||||
if (isSplit && isArray && !isEmpty && (numColumns > 1)) {
|
||||
return (
|
||||
<StyledDiv className={`${className} ui--Table isSplit`}>
|
||||
{legend}
|
||||
<table className='noMargin'>
|
||||
{headerNode}
|
||||
</table>
|
||||
<div className='ui--Table-Split'>
|
||||
{(numColumns === 2 ? [0, 1] : [0, 1, 2]).map((column) => (
|
||||
<Split
|
||||
key={column}
|
||||
numColumns={numColumns}
|
||||
>
|
||||
{children.filter((_, i) => (i % numColumns) === column)}
|
||||
</Split>
|
||||
))}
|
||||
</div>
|
||||
</StyledDiv>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<StyledDiv className={`${className} ui--Table ${bodySplit ? 'isSplit' : ''}`}>
|
||||
<StyledDiv className={`${className} ui--Table`}>
|
||||
{legend}
|
||||
{bodySplit
|
||||
? (
|
||||
<>
|
||||
<table className={tableClassName}>
|
||||
{headerNode}
|
||||
</table>
|
||||
<div className='ui--Table-Split'>
|
||||
{bodySplit.map((body, index) => (
|
||||
<div
|
||||
className={`ui--Table-Split-Column-${numColumns}`}
|
||||
key={`${numColumns}-${index}`}
|
||||
>
|
||||
<table className={tableClassName}>
|
||||
<Body>{body}</Body>
|
||||
</table>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
: (
|
||||
<table className={tableClassName}>
|
||||
{headerNode}
|
||||
<Body
|
||||
empty={empty}
|
||||
emptySpinner={emptySpinner}
|
||||
noBodyTag={noBodyTag}
|
||||
>
|
||||
{body}
|
||||
</Body>
|
||||
<Foot
|
||||
footer={footer}
|
||||
isEmpty={isEmpty}
|
||||
/>
|
||||
</table>
|
||||
)
|
||||
}
|
||||
<table className={`${(isFixed && !isEmpty) ? 'isFixed' : 'isNotFixed'} ${isInline ? 'isInline' : ''}`}>
|
||||
{headerNode}
|
||||
<Body
|
||||
empty={empty}
|
||||
emptySpinner={emptySpinner}
|
||||
isEmpty={isEmpty}
|
||||
noBodyTag={noBodyTag}
|
||||
>
|
||||
{children}
|
||||
</Body>
|
||||
<Foot
|
||||
footer={footer}
|
||||
isEmpty={isEmpty}
|
||||
/>
|
||||
</table>
|
||||
</StyledDiv>
|
||||
);
|
||||
}
|
||||
@ -161,12 +117,12 @@ const StyledDiv = styled.div`
|
||||
flex-wrap: nowrap;
|
||||
margin-bottom: 1.5rem;
|
||||
|
||||
> .ui--Table-Split-Column-3 {
|
||||
> .ui--Table-Split-3 {
|
||||
max-width: 33.3%;
|
||||
min-width: 33.3%;
|
||||
}
|
||||
|
||||
> .ui--Table-Split-Column-2 {
|
||||
> .ui--Table-Split-2 {
|
||||
max-width: 50%;
|
||||
min-width: 50%;
|
||||
}
|
||||
|
||||
@ -2,21 +2,29 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { createNamedHook } from './createNamedHook';
|
||||
import { useDebounce } from './useDebounce';
|
||||
import { useWindowSize } from './useWindowSize';
|
||||
|
||||
type NumColumns = 1 | 2 | 3;
|
||||
|
||||
// we don't have this anywhere
|
||||
const SIZE_COL_3 = 1500;
|
||||
// this aligns with Columar
|
||||
const SIZE_COL_2 = 1050;
|
||||
|
||||
function useWindowColumnsImpl (maxColumns: 1 | 2 | 3 = 3): number {
|
||||
// TODO For 2 vs 3 column we may want different breakpoints
|
||||
// (as set this basically aligns with Columar with an additional
|
||||
// 3-column variant added for larger screens- as per above)
|
||||
function useWindowColumnsImpl (maxColumns: NumColumns = 3): NumColumns {
|
||||
const windowSize = useWindowSize();
|
||||
|
||||
return maxColumns >= 3 && windowSize.width >= SIZE_COL_3
|
||||
? 3
|
||||
: maxColumns >= 2 && windowSize.width >= SIZE_COL_2
|
||||
? 2
|
||||
: 1;
|
||||
return useDebounce(
|
||||
maxColumns >= 3 && windowSize.width >= SIZE_COL_3
|
||||
? 3
|
||||
: maxColumns >= 2 && windowSize.width >= SIZE_COL_2
|
||||
? 2
|
||||
: 1
|
||||
);
|
||||
}
|
||||
|
||||
export const useWindowColumns = createNamedHook('useWindowColumns', useWindowColumnsImpl);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user