team-invite.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { useEffect, useState } from "react";
  2. import { useIntl } from "react-intl";
  3. import { useNavigate, useParams } from "react-router";
  4. import { Alert, Button, Card, message, Space, Spin, Typography } from "antd";
  5. import {
  6. acceptGroupInvite,
  7. fetchGroupInvite,
  8. type GroupInvite,
  9. } from "../api/group-invite";
  10. import { useAuth } from "../hooks/useAuth";
  11. import { useAppDispatch } from "../hooks";
  12. import { TO_HOME, TO_SIGN_IN } from "../reducers/current-user";
  13. import { refreshMyGroups } from "../features/teamspace/refreshMyGroups";
  14. /**
  15. * 打开团队邀请链接:「xxx 邀请你加入团队空间 yyy」,同意后以只读成员加入。
  16. *
  17. * 不登录也能看;点「登录后加入」去登录页,登录完经 `?url=` 回到这里再点同意。
  18. * 已经是成员的直接给「进入团队空间」。
  19. */
  20. const Widget = () => {
  21. const intl = useIntl();
  22. const navigate = useNavigate();
  23. const dispatch = useAppDispatch();
  24. const { token } = useParams();
  25. const { isAuthenticated } = useAuth();
  26. const [invite, setInvite] = useState<GroupInvite>();
  27. const [state, setState] = useState<"loading" | "ready" | "invalid">(
  28. "loading"
  29. );
  30. const [joining, setJoining] = useState(false);
  31. useEffect(() => {
  32. if (!token) {
  33. setState("invalid");
  34. return;
  35. }
  36. setState("loading");
  37. fetchGroupInvite(token)
  38. .then((data) => {
  39. setInvite(data);
  40. setState(data ? "ready" : "invalid");
  41. })
  42. .catch(() => setState("invalid"));
  43. }, [token]);
  44. const roleLabel = (role?: string | null) =>
  45. intl.formatMessage({ id: `labels.group.role.${role ?? "reader"}` });
  46. const groupId = invite?.group?.id;
  47. const enterTeam = () => navigate(`/workspace/team/${groupId}`);
  48. const accept = async () => {
  49. if (!token) {
  50. return;
  51. }
  52. setJoining(true);
  53. try {
  54. // 失败(链接刚好过期、签发人被降级等)由 unwrap 统一弹提示
  55. const joined = await acceptGroupInvite(token);
  56. message.success(
  57. intl.formatMessage(
  58. { id: "message.group.invite.joined" },
  59. { name: joined?.name ?? invite?.group?.name }
  60. )
  61. );
  62. // 头像弹窗里的账户列表要多出这个团队
  63. await refreshMyGroups(dispatch);
  64. enterTeam();
  65. } finally {
  66. setJoining(false);
  67. }
  68. };
  69. const signIn = () =>
  70. navigate(`${TO_SIGN_IN}?url=${btoa(window.location.href)}`);
  71. const title = intl.formatMessage({ id: "labels.group.invite.title" });
  72. const body = () => {
  73. if (state === "loading") {
  74. return <Spin />;
  75. }
  76. if (state === "invalid" || !invite) {
  77. return (
  78. <Alert
  79. type="warning"
  80. showIcon
  81. title={intl.formatMessage({ id: "labels.group.invite.invalid" })}
  82. />
  83. );
  84. }
  85. return (
  86. <Space orientation="vertical" size="middle" style={{ width: "100%" }}>
  87. <Typography.Title level={4} style={{ margin: 0 }}>
  88. {intl.formatMessage(
  89. { id: "labels.group.invite.headline" },
  90. {
  91. inviter: invite.inviter?.nickName,
  92. name: invite.group?.name,
  93. }
  94. )}
  95. </Typography.Title>
  96. {invite.group?.description ? (
  97. <Typography.Paragraph type="secondary" style={{ margin: 0 }}>
  98. {invite.group.description}
  99. </Typography.Paragraph>
  100. ) : undefined}
  101. {invite.member_role ? (
  102. <>
  103. <Alert
  104. type="info"
  105. showIcon
  106. title={intl.formatMessage(
  107. { id: "labels.group.invite.member" },
  108. { role: roleLabel(invite.member_role) }
  109. )}
  110. />
  111. <Button type="primary" onClick={enterTeam}>
  112. {intl.formatMessage({ id: "buttons.group.invite.enter" })}
  113. </Button>
  114. </>
  115. ) : (
  116. <>
  117. <Typography.Text>
  118. {intl.formatMessage(
  119. { id: "labels.group.invite.role" },
  120. { role: roleLabel(invite.role) }
  121. )}
  122. </Typography.Text>
  123. <Space>
  124. {isAuthenticated ? (
  125. <Button type="primary" loading={joining} onClick={accept}>
  126. {intl.formatMessage({ id: "buttons.group.invite.accept" })}
  127. </Button>
  128. ) : (
  129. <Button type="primary" onClick={signIn}>
  130. {intl.formatMessage({ id: "buttons.group.invite.sign-in" })}
  131. </Button>
  132. )}
  133. <Button onClick={() => navigate(TO_HOME)}>
  134. {intl.formatMessage({ id: "buttons.group.invite.decline" })}
  135. </Button>
  136. </Space>
  137. </>
  138. )}
  139. </Space>
  140. );
  141. };
  142. return (
  143. <>
  144. <title>{title}</title>
  145. <Card title={title} style={{ width: 440, maxWidth: "100%" }}>
  146. {body()}
  147. </Card>
  148. </>
  149. );
  150. };
  151. export default Widget;