mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 23:30:41 +09:00
Add a live filter override to useTableQueryState: setFilter updates the filter immediately (so every table's search filters as you type) without touching the URL, and commitFilter writes it to the URL when the filter dropdown closes. useColumnSearch and useUserFilter now filter live via setFilter and commit on close, so the Users table no longer rewrites the URL on every keystroke and the other tables gain live search. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
106 lines
3.0 KiB
TypeScript
106 lines
3.0 KiB
TypeScript
import React, { useContext, useEffect, useState } from "react";
|
|
import { useNavigate } from "react-router-dom";
|
|
import { Button, Form, Input, Modal, Table } from "antd";
|
|
|
|
import * as lib from "@clusterio/lib";
|
|
|
|
import { useRoles } from "../model/roles";
|
|
import { useAccount } from "../model/account";
|
|
import ControlContext from "./ControlContext";
|
|
import { notifyErrorHandler } from "../util/notify";
|
|
import PageHeader from "./PageHeader";
|
|
import PageLayout from "./PageLayout";
|
|
import PluginExtra from "./PluginExtra";
|
|
import useTableQueryState from "../util/useTableQueryState";
|
|
import useColumnSearch from "../util/useColumnSearch";
|
|
import useRowNavigation from "../util/useRowNavigation";
|
|
import Link from "./Link";
|
|
|
|
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
|
|
|
function CreateRoleButton() {
|
|
let control = useContext(ControlContext);
|
|
let navigate = useNavigate();
|
|
let [open, setOpen] = useState(false);
|
|
let [form] = Form.useForm();
|
|
|
|
async function createRole() {
|
|
let values = form.getFieldsValue();
|
|
if (!values.roleName) {
|
|
form.setFields([{ name: "roleName", errors: ["Name is required"] }]);
|
|
return;
|
|
}
|
|
|
|
let idRole = await control.send(
|
|
new lib.RoleCreateRequest(values.roleName, values.description || "", [])
|
|
);
|
|
setOpen(false);
|
|
navigate(`/roles/${idRole}/view`);
|
|
}
|
|
|
|
return <>
|
|
<Button
|
|
type="primary"
|
|
onClick={() => { setOpen(true); }}
|
|
>Create</Button>
|
|
<Modal
|
|
title="Create Role"
|
|
okText="Create"
|
|
open={open}
|
|
onOk={() => { createRole().catch(notifyErrorHandler("Error creating role")); }}
|
|
onCancel={() => { setOpen(false); }}
|
|
destroyOnHidden
|
|
>
|
|
<Form form={form}>
|
|
<Form.Item name="roleName" label="Name">
|
|
<Input/>
|
|
</Form.Item>
|
|
<Form.Item name="description" label="Description">
|
|
<Input/>
|
|
</Form.Item>
|
|
</Form>
|
|
</Modal>
|
|
</>;
|
|
}
|
|
|
|
export default function RolesPage() {
|
|
let account = useAccount();
|
|
const [roles] = useRoles();
|
|
const tableState = useTableQueryState<lib.Role>({ namespace: "role" });
|
|
const nameSearch = useColumnSearch<lib.Role>(tableState, "name", role => role.name, "Search roles");
|
|
const rowNav = useRowNavigation();
|
|
|
|
return <PageLayout nav={[{ name: "Roles" }]}>
|
|
<PageHeader
|
|
title="Roles"
|
|
extra={account.hasPermission("core.role.create") ? <CreateRoleButton /> : undefined}
|
|
/>
|
|
<Table
|
|
columns={[
|
|
{
|
|
title: "Name",
|
|
dataIndex: "name",
|
|
sorter: (a, b) => strcmp(a.name, b.name),
|
|
sortOrder: tableState.sortOrder("name"),
|
|
filteredValue: tableState.filteredValue("name"),
|
|
className: "table-link-cell",
|
|
render: (_, role) => <Link to={`/roles/${role.id}/view`} style={{ color: "inherit" }}>
|
|
{role.name}
|
|
</Link>,
|
|
...nameSearch,
|
|
},
|
|
{
|
|
title: "Description",
|
|
dataIndex: "description",
|
|
},
|
|
]}
|
|
dataSource={[...roles.values()]}
|
|
pagination={tableState.pagination}
|
|
rowKey={role => role.id}
|
|
onChange={tableState.onChange}
|
|
onRow={role => rowNav(`/roles/${role.id}/view`)}
|
|
/>
|
|
<PluginExtra component="RolesPage" />
|
|
</PageLayout>;
|
|
}
|