mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-18 15:33:12 +09:00
Make Username reusable and fix active icon issue
This commit is contained in:
2 files changed
+76
-56
No files matched your search
@@ -1,5 +1,5 @@
|
|||||||
import React, { useRef } from "react";
|
import React, { useRef } from "react";
|
||||||
import { InputRef, Input, Space, Segmented, Button, Typography } from "antd";
|
import { InputRef, Input, Space, Segmented, Button, Typography, Tag } from "antd";
|
||||||
import type { FilterDropdownProps } from "antd/es/table/interface";
|
import type { FilterDropdownProps } from "antd/es/table/interface";
|
||||||
import { CheckOutlined, CloseOutlined, MinusOutlined } from "@ant-design/icons";
|
import { CheckOutlined, CloseOutlined, MinusOutlined } from "@ant-design/icons";
|
||||||
import { UserDetails } from "@clusterio/lib";
|
import { UserDetails } from "@clusterio/lib";
|
||||||
@@ -59,7 +59,28 @@ export function onFilterUser(
|
|||||||
&& matchesTriState(record.isBanned, filter.banned);
|
&& matchesTriState(record.isBanned, filter.banned);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useUserFilter() {
|
export function Username({
|
||||||
|
user,
|
||||||
|
withStatus = false,
|
||||||
|
} : {
|
||||||
|
user: UserDetails,
|
||||||
|
withStatus: boolean,
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<Space>
|
||||||
|
{user.name}
|
||||||
|
{withStatus && (
|
||||||
|
<span>
|
||||||
|
{user.isAdmin && <Tag color="gold">Admin</Tag>}
|
||||||
|
{user.isWhitelisted && <Tag>Whitelisted</Tag>}
|
||||||
|
{user.isBanned && <Tag color="red">Banned</Tag>}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</Space>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useUserFilter(withStatus?: boolean) {
|
||||||
const searchInput = useRef<InputRef>(null);
|
const searchInput = useRef<InputRef>(null);
|
||||||
|
|
||||||
function filterDropdown({
|
function filterDropdown({
|
||||||
@@ -77,9 +98,13 @@ export function useUserFilter() {
|
|||||||
? decodeFilter(selectedKeys[0])
|
? decodeFilter(selectedKeys[0])
|
||||||
: {};
|
: {};
|
||||||
|
|
||||||
function update(next: Partial<UserFilter>, search?: boolean) {
|
function update(next: Partial<UserFilter>) {
|
||||||
const updated = { ...filter, ...next };
|
const encoded = encodeFilter({ ...filter, ...next });
|
||||||
setSelectedKeys([encodeFilter(updated)]);
|
if (encoded === "{}") {
|
||||||
|
clearFilters?.({ closeDropdown: false });
|
||||||
|
} else {
|
||||||
|
setSelectedKeys([encoded]);
|
||||||
|
}
|
||||||
confirm({ closeDropdown: false });
|
confirm({ closeDropdown: false });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,55 +116,57 @@ export function useUserFilter() {
|
|||||||
placeholder="Search username"
|
placeholder="Search username"
|
||||||
value={filter.search}
|
value={filter.search}
|
||||||
allowClear
|
allowClear
|
||||||
onChange={(e) => update({ search: e.target.value })}
|
|
||||||
onSearch={() => confirm({ closeDropdown: true })}
|
onSearch={() => confirm({ closeDropdown: true })}
|
||||||
|
onChange={(e) => update({ search: e.target.value !== "" ? e.target.value : undefined })}
|
||||||
onClear={() => {
|
onClear={() => {
|
||||||
clearFilters?.({ closeDropdown: false });
|
clearFilters?.({ closeDropdown: false });
|
||||||
confirm({ closeDropdown: true });
|
confirm({ closeDropdown: true });
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Space size={8} align="center">
|
{withStatus &&<>
|
||||||
<Segmented
|
<Space size={8} align="center">
|
||||||
size="small"
|
<Segmented
|
||||||
value={triStateToSegmented(filter.admin)}
|
size="small"
|
||||||
onChange={value => update({ admin: segmentedToTriState(value) })}
|
value={triStateToSegmented(filter.admin)}
|
||||||
options={[
|
onChange={value => update({ admin: segmentedToTriState(value) })}
|
||||||
{ icon: <CloseOutlined />, value: "no", className: "seg-no" },
|
options={[
|
||||||
{ icon: <MinusOutlined />, value: "any" },
|
{ icon: <CloseOutlined />, value: "no", className: "seg-no" },
|
||||||
{ icon: <CheckOutlined />, value: "yes", className: "seg-yes" },
|
{ icon: <MinusOutlined />, value: "any" },
|
||||||
]}
|
{ icon: <CheckOutlined />, value: "yes", className: "seg-yes" },
|
||||||
/>
|
]}
|
||||||
<Text>Admin</Text>
|
/>
|
||||||
</Space>
|
<Text>Admin</Text>
|
||||||
|
</Space>
|
||||||
|
|
||||||
<Space size={8} align="center">
|
<Space size={8} align="center">
|
||||||
<Segmented
|
<Segmented
|
||||||
size="small"
|
size="small"
|
||||||
value={triStateToSegmented(filter.whitelisted)}
|
value={triStateToSegmented(filter.whitelisted)}
|
||||||
onChange={value => update({ whitelisted: segmentedToTriState(value) })}
|
onChange={value => update({ whitelisted: segmentedToTriState(value) })}
|
||||||
options={[
|
options={[
|
||||||
{ icon: <CloseOutlined />, value: "no", className: "seg-no" },
|
{ icon: <CloseOutlined />, value: "no", className: "seg-no" },
|
||||||
{ icon: <MinusOutlined />, value: "any" },
|
{ icon: <MinusOutlined />, value: "any" },
|
||||||
{ icon: <CheckOutlined />, value: "yes", className: "seg-yes" },
|
{ icon: <CheckOutlined />, value: "yes", className: "seg-yes" },
|
||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
<Text>Whitelist</Text>
|
<Text>Whitelist</Text>
|
||||||
</Space>
|
</Space>
|
||||||
|
|
||||||
<Space size={8} align="center">
|
<Space size={8} align="center">
|
||||||
<Segmented
|
<Segmented
|
||||||
size="small"
|
size="small"
|
||||||
value={triStateToSegmented(filter.banned)}
|
value={triStateToSegmented(filter.banned)}
|
||||||
onChange={value => update({ banned: segmentedToTriState(value) })}
|
onChange={value => update({ banned: segmentedToTriState(value) })}
|
||||||
options={[
|
options={[
|
||||||
{ icon: <CloseOutlined />, value: "no", className: "seg-no" },
|
{ icon: <CloseOutlined />, value: "no", className: "seg-no" },
|
||||||
{ icon: <MinusOutlined />, value: "any" },
|
{ icon: <MinusOutlined />, value: "any" },
|
||||||
{ icon: <CheckOutlined />, value: "yes", className: "seg-yes" },
|
{ icon: <CheckOutlined />, value: "yes", className: "seg-yes" },
|
||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
<Text>Banned</Text>
|
<Text>Banned</Text>
|
||||||
</Space>
|
</Space>
|
||||||
|
</>}
|
||||||
</Space>
|
</Space>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { Table, Tag, Space } from "antd";
|
import { Table, Tag } from "antd";
|
||||||
import { SearchOutlined } from "@ant-design/icons";
|
import { SearchOutlined } from "@ant-design/icons";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ import {
|
|||||||
useUsers,
|
useUsers,
|
||||||
} from "../model/user";
|
} from "../model/user";
|
||||||
|
|
||||||
import { onFilterUser, useUserFilter } from "./UsersFilters";
|
import { onFilterUser, Username, useUserFilter } from "./UsersFilters";
|
||||||
import Link from "./Link";
|
import Link from "./Link";
|
||||||
|
|
||||||
export interface UsersTableProps {
|
export interface UsersTableProps {
|
||||||
@@ -39,7 +39,7 @@ export default function UsersTable({ instanceId, onlyOnline = false, pagination,
|
|||||||
const [roles] = useRoles();
|
const [roles] = useRoles();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
const {filterDropdown, filterDropdownProps} = useUserFilter();
|
const {filterDropdown, filterDropdownProps} = useUserFilter(true);
|
||||||
|
|
||||||
const [users] = useUsers();
|
const [users] = useUsers();
|
||||||
const data = [...users.values()];
|
const data = [...users.values()];
|
||||||
@@ -60,14 +60,7 @@ export default function UsersTable({ instanceId, onlyOnline = false, pagination,
|
|||||||
title: "Name",
|
title: "Name",
|
||||||
key: "name",
|
key: "name",
|
||||||
render: (_: any, user: lib.UserDetails) => (
|
render: (_: any, user: lib.UserDetails) => (
|
||||||
<Space>
|
<Username user={user} withStatus />
|
||||||
{user.name}
|
|
||||||
<span>
|
|
||||||
{user.isAdmin && <Tag color="gold">Admin</Tag>}
|
|
||||||
{user.isWhitelisted && <Tag>Whitelisted</Tag>}
|
|
||||||
{user.isBanned && <Tag color="red">Banned</Tag>}
|
|
||||||
</span>
|
|
||||||
</Space>
|
|
||||||
),
|
),
|
||||||
defaultSortOrder: "ascend",
|
defaultSortOrder: "ascend",
|
||||||
sorter: (a: lib.UserDetails, b: lib.UserDetails) => strcmp(a.name, b.name),
|
sorter: (a: lib.UserDetails, b: lib.UserDetails) => strcmp(a.name, b.name),
|
||||||
|
|||||||
Reference in new issue
Block a user