All files / src/components/redirect RedirectComponent.tsx

9.25% Statements 5/54
0% Branches 0/64
7.69% Functions 1/13
9.61% Lines 5/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234                        66x       66x                                                 3x 2x 2x                                                                                                                                                                                                                                                                                                                                                                                            
import { ActionRedirectCountdown } from "@config/base";
import React, { ReactElement, useEffect, useState, useCallback } from "react";
import { useTranslation } from "react-i18next";
 
import { Modal, Tag } from "antd";
import { LoadingOutlined } from "@ant-design/icons";
import { getRedirectConfig, isInternalLink } from "./RedirectConfigs";
import { removePackagePart } from "@utils/StringUtils";
import { useLocation } from "react-router-dom";
import modalPropsBuilder from "../../form/ModalPropsBuilder";
 
// Function to open external link in a new tab
const openExternalLink = (url: string): void => {
	window.open(url, "_blank", "noopener,noreferrer");
};
 
const RedirectComponent = (props: {
	redirect?: string;
	forMultiple: boolean;
	fetchDataCallback?: () => void;
	// zIndex 是用于让调用 redirectComponent 的对象指定二层弹窗的 zIndex,
	// 确保在 redirect component 中弹出的 modal 在基础窗口或 modal 之上
	zIndex: number;
	showText?: boolean;
	autoRedirectCountdown?: number;
	ownerClass?: string;
	ownerId?: number;
	columnNameInOwnerClass?: string;
	hasRelateObjectField?: boolean;
}): ReactElement => {
	const {
		redirect,
		forMultiple,
		fetchDataCallback,
		zIndex,
		showText,
		autoRedirectCountdown,
		ownerClass,
		ownerId,
		columnNameInOwnerClass,
		hasRelateObjectField,
	} = props;
	const { t } = useTranslation();
	const location = useLocation();
	const currentUrl = location.pathname;
 
	const [initRedirected, setInitRedirected] = useState<boolean>(false);
	const [countdown, setCountdown] = useState<number>(
		autoRedirectCountdown ?? ActionRedirectCountdown,
	);
	const [showModal, setShowModal] = useState<boolean>(false);
	const [triggerSave, setTriggerSave] = useState<number | false>(false);
 
	const redirectConfig = getRedirectConfig(redirect);
	const {
		mode,
		domainName,
		domainId,
		formId,
		wizardId,
		columnName,
		sourceId,
		targetId,
		queryParameters,
	} = redirectConfig.parseParameters(redirect);
	const isShowRedirect = redirectConfig.mode === "show";
	const isCreateRedirect = redirectConfig.mode === "create";
	/** 查看详情的 Modal 的只读状态,在查看详情的 Modal 中,可以点击编辑按钮,将其状态修改为编辑状态 */
	const [detailModalReadonly, setDetailModalReadonly] =
		useState<boolean>(isShowRedirect);
 
	const redirectNotEmpty = redirect != null && redirect !== "";
	// 只有对非 multiple 类型的 action 才会启用 auto redirect
	const autoRedirect = redirectNotEmpty && !forMultiple;
	useEffect(() => {
		if (countdown > 0) {
			const timer = setTimeout(() => {
				setCountdown(countdown - 1);
			}, 1000);
			return () => clearTimeout(timer);
		}
	}, [countdown]);
 
	const handleRedirect = useCallback(() => {
		if (autoRedirect && redirect != null && !initRedirected) {
			if (isInternalLink(redirect)) {
				setShowModal(true);
			} else {
				openExternalLink(redirect);
			}
			setInitRedirected(true);
		}
	}, [autoRedirect, redirect, initRedirected]);
 
	useEffect(() => {
		if (autoRedirect && redirect != null && !initRedirected) {
			const timer = setTimeout(handleRedirect, ActionRedirectCountdown * 1000);
			return () => clearTimeout(timer);
		}
	}, [autoRedirect, redirect, initRedirected, handleRedirect]);
 
	const withoutPackageDomainName = removePackagePart(domainName);
	const modalTitle =
		domainName != null &&
		columnName != null &&
		sourceId != null &&
		targetId != null
			? t("Compare revision modal title", {
					domainName: t(`domainTitle:${withoutPackageDomainName}`),
					columnName: t(`field:${withoutPackageDomainName}.${columnName}`),
					sourceId,
					targetId,
				})
			: domainName != null && columnName != null && targetId != null
				? t("Compare history revision with latest version")
				: undefined;
 
	const modalzIndex = zIndex;
	const switchEditable = (editable: boolean): void => {
		setDetailModalReadonly(!editable);
	};
	const readonly =
		(isShowRedirect && detailModalReadonly) ||
		(!isShowRedirect && detailModalReadonly);
 
	// modalProps 只用于 create, update, list 和 detail
	// 不用于 wizard
	const modalProps = modalPropsBuilder({
		mode,
		domainName,
		t,
		onCancel: () => {
			setShowModal(false);
			fetchDataCallback?.();
		},
		open: showModal,
		isValid: true,
		zIndex: modalzIndex,
		id: domainId,
		readonly,
		hasRelateObjectField,
		modalTitle,
		refererUrl: currentUrl,
		switchEditable,
	});
	const content = showModal ? (
		redirectConfig.getContent({
			domainName,
			domainId,
			redirect,
			triggerSave,
			formId,
			wizardId,
			sourceId,
			targetId,
			columnName,
			callback: () => setTriggerSave(false),
			zIndex,
			onCancelCallback: () => {
				setShowModal(false);
				fetchDataCallback?.();
			},
			ownerClass,
			ownerId,
			columnNameInOwnerClass,
			queryParameters,
			switchEditable,
			readonly,
		})
	) : (
		<></>
	);
 
	const modalWidth =
		isCreateRedirect || hasRelateObjectField === false ? "60%" : "90%";
	const internalElem = isInternalLink(redirect) ? (
		redirectConfig.wrapInModal ? (
			<Modal
				{...modalProps}
				destroyOnClose={true}
				width={modalWidth}
				style={{ minWidth: "650px" }}
			>
				{content}
			</Modal>
		) : (
			content
		)
	) : (
		<></>
	);
	const dt = showText !== false;
 
	return (
		<span>
			<span
				role="link"
				className="link-span"
				onClick={handleRedirect}
				onKeyDown={(e) => {
					if (e.key === "Enter" || e.key === " ") {
						handleRedirect();
					}
				}}
				tabIndex={0}
			>
				{dt && autoRedirect && countdown > 0 && (
					<>
						<LoadingOutlined /> {t("Opening")}{" "}
					</>
				)}
				{dt && countdown <= 0 && (
					<>
						{t("Redirected to")}{" "}
						<Tag className="redirect-tag"> {redirect} </Tag>
					</>
				)}
				{dt && autoRedirect && countdown > 0 && (
					<>
						{" "}
						<br /> in {countdown} seconds...{" "}
					</>
				)}
			</span>
			{autoRedirect &&
				countdown <= 0 &&
				isInternalLink(redirect) &&
				internalElem}
		</span>
	);
};
 
export default RedirectComponent;