Skip to content

Commit

Permalink
added Dropdown to filter peers by type
Browse files Browse the repository at this point in the history
  • Loading branch information
heavycrystal committed Nov 30, 2023
1 parent ab3ec4c commit 9ae83a1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
31 changes: 22 additions & 9 deletions ui/app/peers/peersTable.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use client';
import { DropDialog } from '@/components/DropDialog';
import PeerButton from '@/components/PeerComponent';
import PeerTypeLabel from '@/components/PeerTypeComponent';
import { Peer } from '@/grpc_generated/peers';
import PeerTypeLabel, { DBTypeToGoodText } from '@/components/PeerTypeComponent';
import { DBType, Peer } from '@/grpc_generated/peers';
import { Label } from '@/lib/Label';
import { SearchField } from '@/lib/SearchField';
import { Table, TableCell, TableRow } from '@/lib/Table';
import { useMemo, useState } from 'react';
import ReactSelect from 'react-select';

function PeerRow({ peer }: { peer: Peer }) {
return (
Expand All @@ -31,26 +32,38 @@ function PeerRow({ peer }: { peer: Peer }) {

function PeersTable({ title, peers }: { title: string; peers: Peer[] }) {
const [searchQuery, setSearchQuery] = useState<string>('');
const [filteredType, setFilteredType] = useState<DBType | undefined>(undefined);
const rows = useMemo(
() =>
peers.filter((peer) => {
return peer.name.toLowerCase().includes(searchQuery.toLowerCase());
}).filter((peer) => {
return (filteredType == undefined) || (peer.type == filteredType);
}),
[peers, searchQuery]
[peers, searchQuery, filteredType]
);
const allTypesOption: { value: DBType | undefined; label: string; } = { value: undefined, label: 'All' };
const availableTypes: { value: DBType | undefined; label: string; }[] = [...new Set(peers.flatMap((peer) => {
return { value: peer.type, label: DBTypeToGoodText(peer.type) };
}))];
availableTypes.unshift(allTypesOption);

return (
<Table
title={<Label variant='headline'>{title}</Label>}
toolbar={{
left: <></>,
right: (
<SearchField
placeholder='Search by peer name'
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
setSearchQuery(e.target.value)
}
/>
<>
<ReactSelect
options={availableTypes}
onChange={(val, _) => {
setFilteredType(val?.value);
}}
defaultValue={allTypesOption}
/><SearchField
placeholder='Search by peer name'
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setSearchQuery(e.target.value)} /></>
),
}}
header={
Expand Down
2 changes: 1 addition & 1 deletion ui/components/PeerTypeComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Label } from '@/lib/Label';
import Image from 'next/image';
import { DBTypeToImageMapping } from './PeerComponent';

const DBTypeToGoodText = (ptype: DBType) => {
export const DBTypeToGoodText = (ptype: DBType) => {
switch (ptype) {
case DBType.POSTGRES:
return 'PostgreSQL';
Expand Down
2 changes: 1 addition & 1 deletion ui/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "es6",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
Expand Down

0 comments on commit 9ae83a1

Please sign in to comment.