| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560 |
- import { type ActionType, ProTable } from "@ant-design/pro-components";
- import { FormattedMessage, useIntl } from "react-intl";
- import { Link } from "react-router";
- import { Alert, Badge, message, Modal, Progress, Typography } from "antd";
- import { Button, Dropdown, Popover } from "antd";
- import {
- PlusOutlined,
- ExclamationCircleOutlined,
- DeleteOutlined,
- TeamOutlined,
- } from "@ant-design/icons";
- import ChannelCreate from "./ChannelCreate";
- import { delete_, get } from "../../request";
- import type { IApiResponseChannelList, TChannelType } from "../../api/Channel";
- import { PublicityValueEnum } from "../studio/table";
- import type { IDeleteResponse } from "../../api/Article";
- import { useEffect, useRef, useState } from "react";
- import type { TRole } from "../../api/Auth";
- import ShareModal from "../share/ShareModal";
- import { EResType } from "../share/Share";
- import StudioName, { type IStudio } from "../auth/Studio";
- import StudioSelect from "./StudioSelect";
- import type { IChannel } from "./Channel";
- import { getSorterUrl } from "../../utils";
- import TransferCreate from "../transfer/TransferCreate";
- import { TransferOutLinedIcon } from "../../assets/icon";
- const { Text } = Typography;
- export const channelTypeFilter = {
- all: {
- text: <FormattedMessage id="channel.type.all.title" />,
- status: "Default",
- },
- translation: {
- text: <FormattedMessage id="channel.type.translation.label" />,
- status: "Success",
- },
- nissaya: {
- text: <FormattedMessage id="channel.type.nissaya.label" />,
- status: "Processing",
- },
- commentary: {
- text: <FormattedMessage id="channel.type.commentary.label" />,
- status: "Default",
- },
- original: {
- text: <FormattedMessage id="channel.type.original.label" />,
- status: "Default",
- },
- };
- export interface IResNumberResponse {
- ok: boolean;
- message: string;
- data: {
- my: number;
- collaboration: number;
- };
- }
- export const renderBadge = (count: number, active = false) => {
- return (
- <Badge
- count={count}
- style={{
- marginBlockStart: -2,
- marginInlineStart: 4,
- color: active ? "#1890FF" : "#999",
- backgroundColor: active ? "#E6F7FF" : "#eee",
- }}
- />
- );
- };
- export interface IChapter {
- book: number;
- paragraph: number;
- }
- interface IChannelItem {
- id: number;
- uid: string;
- title: string;
- summary: string;
- type: TChannelType;
- role?: TRole;
- studio?: IStudio;
- publicity: number;
- progress?: number;
- created_at: string;
- }
- interface IWidget {
- studioName?: string;
- type?: string;
- disableChannels?: string[];
- channelType?: TChannelType;
- chapter?: IChapter;
- onSelect?: Function;
- }
- const ChannelTableWidget = ({
- studioName,
- disableChannels,
- channelType,
- ___type,
- chapter,
- onSelect,
- }: IWidget) => {
- const intl = useIntl();
- const [openCreate, setOpenCreate] = useState(false);
- const [activeKey, setActiveKey] = useState<React.Key | undefined>("my");
- const [myNumber, setMyNumber] = useState<number>(0);
- const [collaborationNumber, setCollaborationNumber] = useState<number>(0);
- const [collaborator, setCollaborator] = useState<string>();
- const [transfer, setTransfer] = useState<string[]>();
- const [transferName, setTransferName] = useState<string>();
- const [transferOpen, setTransferOpen] = useState(false);
- useEffect(() => {
- ref.current?.reload();
- }, [disableChannels]);
- useEffect(() => {
- /**
- * 获取各种channel的数量
- */
- const url = `/v2/channel-my-number?studio=${studioName}`;
- console.log("url", url);
- get<IResNumberResponse>(url).then((json) => {
- if (json.ok) {
- setMyNumber(json.data.my);
- setCollaborationNumber(json.data.collaboration);
- }
- });
- }, [studioName]);
- const showDeleteConfirm = (id: string, title: string) => {
- Modal.confirm({
- icon: <ExclamationCircleOutlined />,
- title:
- intl.formatMessage({
- id: "message.delete.confirm",
- }) +
- intl.formatMessage({
- id: "message.irrevocable",
- }),
- content: title,
- okText: intl.formatMessage({
- id: "buttons.delete",
- }),
- okType: "danger",
- cancelText: intl.formatMessage({
- id: "buttons.no",
- }),
- onOk() {
- const url = `/v2/channel/${id}`;
- console.log("delete api request", url);
- return delete_<IDeleteResponse>(url)
- .then((json) => {
- console.info("api response", json);
- if (json.ok) {
- message.success("删除成功");
- ref.current?.reload();
- } else {
- message.error(json.message);
- }
- })
- .catch((e) => console.log("Oops errors!", e));
- },
- });
- };
- const ref = useRef<ActionType | null>(null);
- return (
- <>
- {channelType ? (
- <Alert
- message={`仅显示版本类型${channelType}`}
- type="success"
- closable
- />
- ) : undefined}
- <ProTable<IChannelItem>
- actionRef={ref}
- columns={[
- {
- title: intl.formatMessage({
- id: "dict.fields.sn.label",
- }),
- dataIndex: "id",
- key: "id",
- width: 50,
- search: false,
- },
- {
- title: intl.formatMessage({
- id: "forms.fields.title.label",
- }),
- dataIndex: "title",
- width: 250,
- key: "title",
- tooltip: "过长会自动收缩",
- ellipsis: true,
- render: (_text, row, index, _action) => {
- return (
- <>
- <div key={1}>
- <Button
- disabled={disableChannels?.includes(row.uid)}
- type="link"
- key={index}
- onClick={() => {
- if (typeof onSelect !== "undefined") {
- const channel: IChannel = {
- name: row.title,
- id: row.uid,
- type: row.type,
- };
- onSelect(channel);
- }
- }}
- >
- {row.title}
- </Button>
- </div>
- {activeKey !== "my" ? (
- <div key={3}>
- <Text type="secondary">
- <StudioName data={row.studio} />
- </Text>
- </div>
- ) : undefined}
- </>
- );
- },
- },
- {
- title: intl.formatMessage({
- id: "forms.fields.created-at.label",
- }),
- key: "progress",
- hideInTable: typeof chapter === "undefined",
- render(_dom, entity, _index, _action, _schema) {
- return (
- <Progress
- size="small"
- percent={Math.floor((entity.progress ?? 0) * 100)}
- style={{ width: 150 }}
- />
- );
- },
- },
- {
- title: intl.formatMessage({
- id: "forms.fields.summary.label",
- }),
- dataIndex: "summary",
- key: "summary",
- tooltip: "过长会自动收缩",
- ellipsis: true,
- },
- {
- title: intl.formatMessage({
- id: "forms.fields.role.label",
- }),
- dataIndex: "role",
- key: "role",
- width: 80,
- search: false,
- filters: true,
- onFilter: true,
- valueEnum: {
- all: {
- text: intl.formatMessage({
- id: "channel.type.all.title",
- }),
- status: "Default",
- },
- owner: {
- text: intl.formatMessage({
- id: "auth.role.owner",
- }),
- },
- manager: {
- text: intl.formatMessage({
- id: "auth.role.manager",
- }),
- },
- editor: {
- text: intl.formatMessage({
- id: "auth.role.editor",
- }),
- },
- member: {
- text: intl.formatMessage({
- id: "auth.role.member",
- }),
- },
- },
- },
- {
- title: intl.formatMessage({
- id: "forms.fields.type.label",
- }),
- dataIndex: "type",
- key: "type",
- width: 80,
- search: false,
- filters: true,
- onFilter: true,
- valueEnum: channelTypeFilter,
- },
- {
- title: intl.formatMessage({
- id: "forms.fields.publicity.label",
- }),
- dataIndex: "publicity",
- key: "publicity",
- width: 80,
- search: false,
- filters: true,
- onFilter: true,
- valueEnum: PublicityValueEnum(),
- },
- {
- title: intl.formatMessage({
- id: "forms.fields.created-at.label",
- }),
- key: "created_at",
- width: 100,
- search: false,
- dataIndex: "created_at",
- valueType: "date",
- sorter: true,
- },
- {
- title: intl.formatMessage({ id: "buttons.option" }),
- key: "option",
- width: 100,
- valueType: "option",
- hideInTable: activeKey !== "my",
- render: (_text, row, index, _action) => {
- return [
- <Dropdown.Button
- key={index}
- type="link"
- trigger={["click", "contextMenu"]}
- menu={{
- items: [
- {
- key: "share",
- label: (
- <ShareModal
- trigger={intl.formatMessage({
- id: "buttons.share",
- })}
- resId={row.uid}
- resType={EResType.channel}
- />
- ),
- icon: <TeamOutlined />,
- },
- {
- key: "transfer",
- label: intl.formatMessage({
- id: "columns.studio.transfer.title",
- }),
- icon: <TransferOutLinedIcon />,
- },
- {
- key: "remove",
- label: intl.formatMessage({
- id: "buttons.delete",
- }),
- icon: <DeleteOutlined />,
- danger: true,
- },
- ],
- onClick: (e) => {
- switch (e.key) {
- case "remove":
- showDeleteConfirm(row.uid, row.title);
- break;
- case "transfer":
- setTransfer([row.uid]);
- setTransferName(row.title);
- setTransferOpen(true);
- break;
- default:
- break;
- }
- },
- }}
- >
- <Link to={`/studio/${studioName}/channel/${row.uid}/setting`}>
- {intl.formatMessage({
- id: "buttons.setting",
- })}
- </Link>
- </Dropdown.Button>,
- ];
- },
- },
- ]}
- request={async (params = {}, sorter, filter) => {
- console.log(params, sorter, filter);
- let url = `/v2/channel?`;
- if (activeKey === "community") {
- url += `view=public`;
- } else {
- url += `view=studio&view2=${activeKey}&name=${studioName}`;
- }
- if (chapter) {
- url += `&book=${chapter.book}¶graph=${chapter.paragraph}`;
- }
- const offset =
- ((params.current ? params.current : 1) - 1) *
- (params.pageSize ? params.pageSize : 20);
- url += `&limit=${params.pageSize}&offset=${offset}`;
- url += collaborator ? "&collaborator=" + collaborator : "";
- url += params.keyword ? "&search=" + params.keyword : "";
- url += channelType ? "&type=" + channelType : "";
- if (chapter && activeKey === "community") {
- url += "&order=progress";
- } else {
- url += getSorterUrl(sorter);
- }
- console.log("url", url);
- const res: IApiResponseChannelList = await get(url);
- const items: IChannelItem[] = res.data.rows.map((item, id) => {
- return {
- id: id + 1,
- uid: item.uid,
- title: item.name,
- summary: item.summary,
- type: item.type,
- role: item.role,
- progress: item.progress,
- studio: item.studio,
- publicity: item.status,
- created_at: item.created_at,
- };
- });
- return {
- total: res.data.count,
- succcess: true,
- data: items,
- };
- }}
- rowKey="id"
- bordered
- pagination={{
- showQuickJumper: true,
- showSizeChanger: true,
- }}
- search={false}
- options={{
- search: true,
- }}
- toolBarRender={() => [
- activeKey !== "my" ? (
- <StudioSelect
- studioName={studioName}
- onSelect={(value: string) => {
- setCollaborator(value);
- ref.current?.reload();
- }}
- />
- ) : undefined,
- <Popover
- content={
- <ChannelCreate
- studio={studioName}
- onSuccess={() => {
- setOpenCreate(false);
- ref.current?.reload();
- }}
- />
- }
- placement="bottomRight"
- trigger="click"
- open={openCreate}
- onOpenChange={(open: boolean) => {
- setOpenCreate(open);
- }}
- >
- <Button key="button" icon={<PlusOutlined />} type="primary">
- {intl.formatMessage({ id: "buttons.create" })}
- </Button>
- </Popover>,
- ]}
- toolbar={{
- menu: {
- activeKey,
- items: [
- {
- key: "my",
- label: (
- <span>
- {intl.formatMessage({ id: "labels.this-studio" })}
- {renderBadge(myNumber, activeKey === "my")}
- </span>
- ),
- },
- {
- key: "collaboration",
- label: (
- <span>
- {intl.formatMessage({ id: "labels.collaboration" })}
- {renderBadge(
- collaborationNumber,
- activeKey === "collaboration"
- )}
- </span>
- ),
- },
- {
- key: "community",
- label: (
- <span>
- {intl.formatMessage({ id: "labels.community" })}
- {renderBadge(
- collaborationNumber,
- activeKey === "community"
- )}
- </span>
- ),
- },
- ],
- onChange(key) {
- console.log("show course", key);
- setActiveKey(key);
- setCollaborator(undefined);
- ref.current?.reload();
- },
- },
- }}
- />
- <TransferCreate
- studioName={studioName}
- resId={transfer}
- resType="channel"
- resName={transferName}
- open={transferOpen}
- onOpenChange={(visible: boolean) => setTransferOpen(visible)}
- />
- </>
- );
- };
- export default ChannelTableWidget;
|