Skip to content

Commit 3534e12

Browse files
committed
create table creator to get rid of type error
1 parent ac31d0c commit 3534e12

File tree

4 files changed

+103
-121
lines changed

4 files changed

+103
-121
lines changed

src/components/blocked-pubkeys/BlockedPubkeys.styles.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,8 @@
11
import styled from 'styled-components';
22
import Card from 'antd/lib/card/Card';
3-
import Table from 'antd/es/table';
3+
import { Table, TableProps } from 'antd';
44
import { Input } from 'antd';
55
import { BaseCol } from '../common/BaseCol/BaseCol';
6-
export const TableRoot = styled(Table)`
7-
border-radius: 12px;
8-
9-
& .ant-table-thead .ant-table-cell {
10-
background-color: var(--secondary-background-color);
11-
}
12-
13-
.ant-table-tbody {
14-
background-color: var(--layout-sider-bg-color);
15-
}
16-
.ant-table-placeholder .ant-table-cell {
17-
background-color: var(--layout-sider-bg-color);
18-
transition: none;
19-
}
20-
.ant-table-placeholder .ant-table-cell:hover {
21-
background-color: var(--layout-sider-bg-color);
22-
}
23-
`;
246
export const InputRoot = styled(Input)`
257
background-color: var(--layout-sider-bg-color);
268
& input {

src/components/blocked-pubkeys/BlockedPubkeys.tsx

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { useState } from 'react';
2-
import { Space, Typography } from 'antd';
1+
import React, { useState, FC } from 'react';
2+
import { Space, Typography, Table, TableProps } from 'antd';
33
import useBlockedPubkeys from '@app/hooks/useBlockedPubkeys';
44
import { BaseButton } from '@app/components/common/BaseButton/BaseButton';
55
import { ReloadOutlined } from '@ant-design/icons';
@@ -8,20 +8,37 @@ import { FlaggedPubkeysTable } from './components/FlaggedPubkeysTable';
88
import { BlockPubkeyForm } from './components/BlockPubkeyForm';
99
import { useModerationStats } from '@app/hooks/useModerationStats';
1010
import { LockFilled } from '@ant-design/icons';
11+
import styled from 'styled-components';
1112
import * as S from './BlockedPubkeys.styles';
1213

1314
const { Title, Text } = Typography;
1415

16+
export function createStyledTable<T extends object = any>() {
17+
const GenericTable: FC<TableProps<T>> = (props) => <Table {...props} />;
18+
19+
return styled(GenericTable)`
20+
border-radius: 12px;
21+
22+
& .ant-table-thead .ant-table-cell {
23+
background-color: var(--secondary-background-color);
24+
}
25+
26+
.ant-table-tbody {
27+
background-color: var(--layout-sider-bg-color);
28+
}
29+
.ant-table-placeholder .ant-table-cell {
30+
background-color: var(--layout-sider-bg-color);
31+
transition: none;
32+
}
33+
.ant-table-placeholder .ant-table-cell:hover {
34+
background-color: var(--layout-sider-bg-color);
35+
}
36+
`;
37+
}
1538
export const BlockedPubkeys: React.FC = () => {
1639
const [activeView, setActiveView] = useState<'blocked' | 'flagged'>('blocked');
17-
const {
18-
blockedPubkeys,
19-
count,
20-
loading,
21-
fetchBlockedPubkeys,
22-
addBlockedPubkey,
23-
removeBlockedPubkey,
24-
} = useBlockedPubkeys();
40+
const { blockedPubkeys, count, loading, fetchBlockedPubkeys, addBlockedPubkey, removeBlockedPubkey } =
41+
useBlockedPubkeys();
2542
const { fetchStats, loading: statsLoading } = useModerationStats();
2643

2744
// Refresh all data
@@ -31,56 +48,38 @@ export const BlockedPubkeys: React.FC = () => {
3148
};
3249

3350
return (
34-
<S.BaseColRoot >
51+
<S.BaseColRoot>
3552
<S.CardRoot>
3653
<Space direction="vertical" size="large" style={{ width: '100%' }}>
3754
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
3855
<div>
39-
<Title level={3}>Access Control <LockFilled /></Title>
40-
<Text style={{color:"var(--text-light-color)"}}>
56+
<Title level={3}>
57+
Access Control <LockFilled />
58+
</Title>
59+
<Text style={{ color: 'var(--text-light-color)' }}>
4160
Control access to your relay and manage flagged pubkeys
4261
</Text>
4362
</div>
44-
<BaseButton
45-
icon={<ReloadOutlined />}
46-
onClick={handleRefresh}
47-
loading={loading || statsLoading}
48-
>
63+
<BaseButton icon={<ReloadOutlined />} onClick={handleRefresh} loading={loading || statsLoading}>
4964
Refresh
5065
</BaseButton>
5166
</div>
52-
53-
{activeView === 'blocked' && (
54-
<BlockPubkeyForm onSubmit={addBlockedPubkey} disabled={loading} />
55-
)}
56-
67+
68+
{activeView === 'blocked' && <BlockPubkeyForm onSubmit={addBlockedPubkey} disabled={loading} />}
69+
5770
<S.NavContainer>
58-
<S.NavLink
59-
active={activeView === 'blocked'}
60-
onClick={() => setActiveView('blocked')}
61-
>
71+
<S.NavLink active={activeView === 'blocked'} onClick={() => setActiveView('blocked')}>
6272
Blocked Access
6373
</S.NavLink>
64-
<S.NavLink
65-
active={activeView === 'flagged'}
66-
onClick={() => setActiveView('flagged')}
67-
>
74+
<S.NavLink active={activeView === 'flagged'} onClick={() => setActiveView('flagged')}>
6875
Flagged Access
6976
</S.NavLink>
7077
</S.NavContainer>
71-
78+
7279
{activeView === 'blocked' ? (
73-
<BlockedPubkeysTable
74-
blockedPubkeys={blockedPubkeys}
75-
loading={loading}
76-
onUnblock={removeBlockedPubkey}
77-
/>
80+
<BlockedPubkeysTable blockedPubkeys={blockedPubkeys} loading={loading} onUnblock={removeBlockedPubkey} />
7881
) : (
79-
<FlaggedPubkeysTable
80-
blockedPubkeys={blockedPubkeys}
81-
onBlock={addBlockedPubkey}
82-
disabled={loading}
83-
/>
82+
<FlaggedPubkeysTable blockedPubkeys={blockedPubkeys} onBlock={addBlockedPubkey} disabled={loading} />
8483
)}
8584
</Space>
8685
</S.CardRoot>

src/components/blocked-pubkeys/components/BlockedPubkeysTable.tsx

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
import React, { useState } from 'react';
2-
import { Table, Input, Button, Modal, Tooltip, Space, Badge, Spin } from 'antd';
3-
import { DeleteOutlined, CopyOutlined, SearchOutlined, FlagOutlined } from '@ant-design/icons';
2+
import { Button, Modal, Tooltip, Space, Spin, TableColumnsType, Table } from 'antd';
3+
import { DeleteOutlined, CopyOutlined, SearchOutlined } from '@ant-design/icons';
44
import { BlockedPubkey } from '@app/api/blockedPubkeys.api';
55
import { useModerationStats } from '@app/hooks/useModerationStats';
66
import * as S from '../BlockedPubkeys.styles';
7+
import { createStyledTable } from '../BlockedPubkeys';
78

89
interface BlockedPubkeysTableProps {
910
blockedPubkeys: BlockedPubkey[];
1011
loading: boolean;
1112
onUnblock: (pubkey: string) => Promise<void>;
1213
}
13-
14-
export const BlockedPubkeysTable: React.FC<BlockedPubkeysTableProps> = ({
15-
blockedPubkeys,
16-
loading,
17-
onUnblock,
18-
}) => {
14+
const TableRoot = createStyledTable<BlockedPubkey>();
15+
export const BlockedPubkeysTable: React.FC<BlockedPubkeysTableProps> = ({ blockedPubkeys, loading, onUnblock }) => {
1916
const [searchText, setSearchText] = useState('');
2017
const [confirmModalVisible, setConfirmModalVisible] = useState(false);
2118
const [currentPubkey, setCurrentPubkey] = useState('');
2219
const { getFlagCountsForPubkeys, loading: statsLoading } = useModerationStats();
23-
20+
2421
// Get flag counts for all pubkeys in the table
25-
const pubkeyFlagCounts = getFlagCountsForPubkeys(blockedPubkeys.map(bp => bp.pubkey));
22+
const pubkeyFlagCounts = getFlagCountsForPubkeys(blockedPubkeys.map((bp) => bp.pubkey));
2623

2724
// Handle pubkey copy
2825
const handleCopy = (pubkey: string) => {
@@ -43,9 +40,10 @@ export const BlockedPubkeysTable: React.FC<BlockedPubkeysTableProps> = ({
4340
};
4441

4542
// Filter pubkeys based on search
46-
const filteredPubkeys = blockedPubkeys.filter(item =>
47-
item.pubkey.toLowerCase().includes(searchText.toLowerCase()) ||
48-
(item.reason && item.reason.toLowerCase().includes(searchText.toLowerCase()))
43+
const filteredPubkeys = blockedPubkeys.filter(
44+
(item) =>
45+
item.pubkey.toLowerCase().includes(searchText.toLowerCase()) ||
46+
(item.reason && item.reason.toLowerCase().includes(searchText.toLowerCase())),
4947
);
5048

5149
// Format pubkey for display (truncate)
@@ -56,7 +54,7 @@ export const BlockedPubkeysTable: React.FC<BlockedPubkeysTableProps> = ({
5654
return `${cleanPubkey.substring(0, 6)}...${cleanPubkey.substring(cleanPubkey.length - 6)}`;
5755
};
5856

59-
const columns = [
57+
const columns: TableColumnsType<BlockedPubkey> = [
6058
{
6159
title: 'Pubkey',
6260
dataIndex: 'pubkey',
@@ -65,12 +63,7 @@ export const BlockedPubkeysTable: React.FC<BlockedPubkeysTableProps> = ({
6563
<Space>
6664
{formatPubkey(pubkey)}
6765
<Tooltip title="Copy full pubkey">
68-
<Button
69-
icon={<CopyOutlined />}
70-
type="text"
71-
size="small"
72-
onClick={() => handleCopy(pubkey)}
73-
/>
66+
<Button icon={<CopyOutlined />} type="text" size="small" onClick={() => handleCopy(pubkey)} />
7467
</Tooltip>
7568
</Space>
7669
),
@@ -96,7 +89,7 @@ export const BlockedPubkeysTable: React.FC<BlockedPubkeysTableProps> = ({
9689
{statsLoading ? (
9790
<Spin size="small" />
9891
) : (
99-
<S.CircularBadge
92+
<S.CircularBadge
10093
color={count > 10 ? 'var(--error-color)' : count > 5 ? 'var(--warning-color)' : 'var(--primary-color)'}
10194
>
10295
{count}
@@ -105,19 +98,15 @@ export const BlockedPubkeysTable: React.FC<BlockedPubkeysTableProps> = ({
10598
</S.FlagCountContainer>
10699
);
107100
},
108-
sorter: (a: BlockedPubkey, b: BlockedPubkey) =>
101+
sorter: (a: BlockedPubkey, b: BlockedPubkey) =>
109102
(pubkeyFlagCounts[a.pubkey] || 0) - (pubkeyFlagCounts[b.pubkey] || 0),
110103
defaultSortOrder: 'descend' as const,
111104
},
112105
{
113106
title: 'Actions',
114107
key: 'actions',
115108
render: (_: any, record: BlockedPubkey) => (
116-
<Button
117-
danger
118-
icon={<DeleteOutlined />}
119-
onClick={() => showConfirmModal(record.pubkey)}
120-
>
109+
<Button danger icon={<DeleteOutlined />} onClick={() => showConfirmModal(record.pubkey)}>
121110
Unblock
122111
</Button>
123112
),
@@ -130,14 +119,14 @@ export const BlockedPubkeysTable: React.FC<BlockedPubkeysTableProps> = ({
130119
<S.InputRoot
131120
placeholder="Search pubkeys or reasons"
132121
value={searchText}
133-
onChange={e => setSearchText(e.target.value)}
122+
onChange={(e) => setSearchText(e.target.value)}
134123
prefix={<SearchOutlined />}
135124
allowClear
136125
/>
137126
</div>
138127

139128
<S.TableContainer>
140-
<S.TableRoot
129+
<TableRoot
141130
dataSource={filteredPubkeys}
142131
columns={columns}
143132
rowKey="pubkey"
@@ -160,7 +149,9 @@ export const BlockedPubkeysTable: React.FC<BlockedPubkeysTableProps> = ({
160149
okButtonProps={{ danger: true }}
161150
>
162151
<p>Are you sure you want to unblock this pubkey?</p>
163-
<p><strong>{currentPubkey.replace('blocked_pubkey:', '')}</strong></p>
152+
<p>
153+
<strong>{currentPubkey.replace('blocked_pubkey:', '')}</strong>
154+
</p>
164155
</Modal>
165156
</>
166157
);

0 commit comments

Comments
 (0)