| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- import { useIntl } from "react-intl";
- import {
- Button,
- Card,
- Dropdown,
- message,
- Modal,
- notification,
- Space,
- Tag,
- Typography,
- } from "antd";
- import {
- MoreOutlined,
- EditOutlined,
- DeleteOutlined,
- LinkOutlined,
- CheckOutlined,
- MessageOutlined,
- ExclamationCircleOutlined,
- CloseOutlined,
- SyncOutlined,
- } from "@ant-design/icons";
- import type { MenuProps } from "antd";
- import type { IComment } from "./DiscussionItem";
- import TimeShow from "../general/TimeShow";
- import Marked from "../general/Marked";
- import { delete_, put } from "../../request";
- import type { IDeleteResponse } from "../../api/Article";
- import { fullUrl } from "../../utils";
- import type { ICommentRequest, ICommentResponse } from "../../api/Comment";
- import { useState } from "react";
- import MdView from "../template/MdView";
- import type { TDiscussionType } from "./Discussion";
- import { discussionCountUpgrade } from "./DiscussionCount";
- const { Text } = Typography;
- interface IWidget {
- data: IComment;
- hideTitle?: boolean;
- onEdit?: Function;
- onSelect?: Function;
- onDelete?: Function;
- onReply?: Function;
- onClose?: Function;
- onConvert?: Function;
- }
- const DiscussionShowWidget = ({
- data,
- hideTitle = false,
- onEdit,
- onSelect,
- onDelete,
- ___onReply,
- onClose,
- onConvert,
- }: IWidget) => {
- const intl = useIntl();
- const [closed, setClosed] = useState(data.status);
- const showDeleteConfirm = (id: string, resId: 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/discussion/${id}`;
- console.info("Discussion delete api request", url);
- return delete_<IDeleteResponse>(url)
- .then((json) => {
- console.debug("api response", json);
- if (json.ok) {
- message.success("删除成功");
- discussionCountUpgrade(resId);
- if (typeof onDelete !== "undefined") {
- onDelete(id);
- }
- } else {
- message.error(json.message);
- }
- })
- .catch((e) => console.log("Oops errors!", e));
- },
- });
- };
- const close = (value: boolean) => {
- const url = `/v2/discussion/${data.id}`;
- const newData: ICommentRequest = {
- title: data.title,
- content: data.content,
- status: value ? "close" : "active",
- };
- console.info("api request", url, newData);
- put<ICommentRequest, ICommentResponse>(url, newData).then((json) => {
- console.log(json);
- if (json.ok) {
- setClosed(json.data.status);
- discussionCountUpgrade(data.resId);
- if (typeof onClose !== "undefined") {
- onClose(value);
- }
- } else {
- message.error(json.message);
- }
- });
- };
- const convert = (newType: TDiscussionType) => {
- const url = `/v2/discussion/${data.id}`;
- const newData: ICommentRequest = {
- title: data.title,
- content: data.content,
- status: data.status,
- type: newType,
- };
- console.info("api response", url, newData);
- put<ICommentRequest, ICommentResponse>(url, newData).then((json) => {
- console.info("api response", json);
- if (json.ok) {
- notification.info({ message: "转换成功" });
- if (typeof onConvert !== "undefined") {
- onConvert(newType);
- }
- }
- });
- };
- const onClick: MenuProps["onClick"] = (e) => {
- console.log("click ", e);
- switch (e.key) {
- case "copy-link":
- let url = `/discussion/topic/`;
- if (data.id) {
- if (data.parent) {
- url += `${data.parent}#${data.id}`;
- } else {
- url += data.id;
- }
- } else {
- url += `${data.tplId}?tpl=true&resId=${data.resId}&resType=${data.resType}`;
- }
- navigator.clipboard.writeText(fullUrl(url)).then(() => {
- message.success("链接地址已经拷贝到剪贴板");
- });
- break;
- case "copy-tpl":
- const tpl = `{{qa|id=${data.id}|style=collapse}}`;
- navigator.clipboard.writeText(tpl).then(() => {
- notification.success({ message: "链接地址已经拷贝到剪贴板" });
- });
- break;
- case "edit":
- if (typeof onEdit !== "undefined") {
- onEdit();
- }
- break;
- case "close":
- close(true);
- break;
- case "reopen":
- close(false);
- break;
- case "convert_qa":
- convert("qa");
- break;
- case "convert_help":
- convert("help");
- break;
- case "convert_discussion":
- convert("discussion");
- break;
- case "delete":
- if (data.id && data.resId) {
- showDeleteConfirm(data.id, data.resId, data.title ?? "");
- }
- break;
- default:
- break;
- }
- };
- console.log("children", data.childrenCount);
- const items: MenuProps["items"] = [
- {
- key: "copy-link",
- label: intl.formatMessage({
- id: "buttons.copy.link",
- }),
- icon: <LinkOutlined />,
- },
- {
- key: "copy-tpl",
- label: intl.formatMessage({
- id: "buttons.copy.tpl",
- }),
- icon: <LinkOutlined />,
- disabled: data.type !== "qa",
- },
- {
- type: "divider",
- },
- {
- key: "edit",
- label: intl.formatMessage({
- id: "buttons.edit",
- }),
- icon: <EditOutlined />,
- },
- {
- key: "close",
- label: intl.formatMessage({
- id: "buttons.close",
- }),
- icon: <CloseOutlined />,
- disabled: closed === "close",
- },
- {
- key: "reopen",
- label: intl.formatMessage({
- id: "buttons.open",
- }),
- icon: <CheckOutlined />,
- disabled: closed === "active",
- },
- {
- type: "divider",
- },
- {
- key: "convert",
- label: intl.formatMessage({
- id: "buttons.convert",
- }),
- icon: <SyncOutlined />,
- disabled: data.parent ? true : false,
- children: [
- { key: "convert_qa", label: "qa", disabled: data.type === "qa" },
- { key: "convert_help", label: "help", disabled: data.type === "help" },
- {
- key: "convert_discussion",
- label: "discussion",
- disabled: data.type === "discussion",
- },
- ],
- },
- {
- type: "divider",
- },
- {
- key: "delete",
- label: intl.formatMessage({
- id: "buttons.delete",
- }),
- icon: <DeleteOutlined />,
- danger: true,
- disabled: data.childrenCount && data.childrenCount > 0 ? true : false,
- },
- ];
- const editInfo = () => {
- return (
- <Space orientation="vertical" size={"small"}>
- {data.title && !hideTitle ? (
- <Text
- style={{ fontSize: 16 }}
- strong
- onClick={(e) => {
- if (typeof onSelect !== "undefined") {
- onSelect(e);
- }
- }}
- >
- {data.title}
- </Text>
- ) : undefined}
- <Text type="secondary" style={{ fontSize: "80%" }}>
- <Space>
- {!data.parent && closed === "close" ? (
- <Tag style={{ backgroundColor: "#8250df", color: "white" }}>
- {"closed"}
- </Tag>
- ) : undefined}
- {data.user.nickName}
- <TimeShow
- type="secondary"
- updatedAt={data.updatedAt}
- createdAt={data.createdAt}
- />
- </Space>
- </Text>
- </Space>
- );
- };
- const editMenu = () => {
- return (
- <Space>
- <span
- style={{
- display: data.childrenCount === 0 ? "none" : "inline",
- cursor: "pointer",
- }}
- onClick={(e) => {
- if (typeof onSelect !== "undefined") {
- onSelect(e, data);
- }
- }}
- >
- {data.childrenCount ? (
- <>
- <MessageOutlined /> {data.childrenCount}
- </>
- ) : undefined}
- </span>
- <Dropdown
- menu={{ items, onClick }}
- placement="bottomRight"
- trigger={["click"]}
- >
- <Button shape="circle" size="small" icon={<MoreOutlined />}></Button>
- </Dropdown>
- </Space>
- );
- };
- return (
- <Card
- size="small"
- title={data.type === "qa" && data.parent ? undefined : editInfo()}
- extra={data.type === "qa" && data.parent ? undefined : editMenu()}
- style={{ width: "100%" }}
- >
- <div>
- {data.html ? (
- <MdView html={data.html} />
- ) : (
- <Marked text={data.content} />
- )}
- </div>
- {data.type === "qa" && data.parent ? (
- <div style={{ display: "flex", justifyContent: "space-between" }}>
- <div></div>
- <div>
- {editInfo()}
- {editMenu()}
- </div>
- </div>
- ) : (
- <></>
- )}
- </Card>
- );
- };
- export default DiscussionShowWidget;
|