| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- import { useEffect, useRef, useState } from "react";
- import { type ActionType, ProList } from "@ant-design/pro-components";
- import { useIntl } from "react-intl";
- import { Link } from "react-router";
- import { Button, message, Modal, Popover, Space } from "antd";
- import { ExclamationCircleOutlined, PlusOutlined } from "@ant-design/icons";
- import { delete_, get } from "../../request";
- import type { IDeleteResponse } from "../../api/Article";
- import { getSorterUrl } from "../../utils";
- import type {
- IProjectData,
- IProjectListResponse,
- TProjectType,
- } from "../../api/task";
- import ProjectCreate from "./ProjectCreate";
- import ProjectEditDrawer from "./ProjectEditDrawer";
- import User from "../auth/User";
- import TimeShow from "../general/TimeShow";
- import ShareModal from "../share/ShareModal";
- import { EResType } from "../share/Share";
- import ProjectClone from "./ProjectClone";
- export interface IResNumberResponse {
- ok: boolean;
- message: string;
- data: {
- my: number;
- collaboration: number;
- };
- }
- export type TView = "current" | "studio" | "shared" | "community";
- interface IWidget {
- studioName?: string;
- type?: TProjectType;
- view?: TView;
- readonly?: boolean;
- onSelect?: (data: IProjectData) => void;
- }
- const ProjectListWidget = ({
- studioName,
- view = "studio",
- type = "instance",
- readonly = false,
- onSelect,
- }: IWidget) => {
- const intl = useIntl();
- const [openCreate, setOpenCreate] = useState(false);
- const [editId, setEditId] = useState<string>();
- const [open, setOpen] = useState(false);
- 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/project/${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);
- useEffect(() => {
- ref.current?.reload();
- }, [view]);
- return (
- <>
- <ProList<IProjectData>
- actionRef={ref}
- metas={{
- title: {
- render: (_text, row, _index, _action) => {
- return readonly ? (
- <>{row.title}</>
- ) : (
- <Link to={`/studio/${studioName}/task/project/${row.id}`}>
- {row.title}
- </Link>
- );
- },
- },
- description: {
- dataIndex: "description",
- render(_dom, entity, _index, _action, _schema) {
- return (
- <Space>
- <User {...entity.editor} showAvatar={false} />
- <TimeShow
- createdAt={entity.created_at}
- updatedAt={entity.updated_at}
- />
- </Space>
- );
- },
- },
- content: {
- dataIndex: "description",
- },
- subTitle: {
- render: (_text, _row, _index, _action) => {
- return <></>;
- },
- },
- actions: {
- render: (_text, row) => [
- <Button
- size="small"
- type="link"
- key="edit"
- onClick={() => {
- setEditId(row.id);
- setOpen(true);
- }}
- >
- {intl.formatMessage({
- id: "buttons.edit",
- })}
- </Button>,
- <ProjectClone
- key="clone"
- projectId={row.id}
- studioName={studioName}
- trigger={
- <Button size="small" type="link" key="clone">
- {intl.formatMessage({
- id: "buttons.clone",
- })}
- </Button>
- }
- />,
- <ShareModal
- key="share"
- trigger={
- <Button type="link" size="small">
- {intl.formatMessage({
- id: "buttons.share",
- })}
- </Button>
- }
- resId={row.id}
- resType={EResType.workflow}
- />,
- ],
- },
- }}
- onRow={(record) => {
- return {
- onClick: () => {
- console.info(`点击了行:${record.title}`);
- onSelect?.(record);
- },
- };
- }}
- request={async (params = {}, sorter, filter) => {
- console.log(params, sorter, filter);
- let url = `/v2/project?view=${view}&type=${type}`;
- url += `&studio=${studioName}`;
- const offset =
- ((params.current ? params.current : 1) - 1) *
- (params.pageSize ? params.pageSize : 20);
- url += `&limit=${params.pageSize}&offset=${offset}`;
- url += params.keyword ? "&keyword=" + params.keyword : "";
- url += getSorterUrl(sorter);
- console.info("project list api request", url);
- const res = await get<IProjectListResponse>(url);
- console.info("project list api response", res);
- return {
- total: res.data.count,
- succcess: res.ok,
- data: res.data.rows,
- };
- }}
- rowKey="id"
- bordered
- pagination={{
- showQuickJumper: true,
- showSizeChanger: true,
- }}
- search={false}
- options={{
- search: true,
- }}
- toolbar={{
- actions: [
- view === "studio" ? (
- <Popover
- content={
- <ProjectCreate
- studio={studioName}
- type={"workflow"}
- onCreate={() => {
- 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>
- ) : (
- <></>
- ),
- ],
- }}
- />
- <ProjectEditDrawer
- studioName={studioName}
- projectId={editId}
- openDrawer={open}
- onClose={() => setOpen(false)}
- />
- </>
- );
- };
- export default ProjectListWidget;
|