import { useControllableValue } from 'ahooks'; import { message, Space, Tag, Timeline } from 'antd'; import type { SpaceSize } from 'antd/lib/space'; import { useCallback, useContext, useEffect, useMemo, useRef, type CSSProperties } from 'react'; import { useIntl } from 'umi'; import { AuditContainerContext } from '.'; import { requestReasonDel } from './api'; import type { ReasonDataType } from './ReasonItem'; import ReasonItem from './ReasonItem'; const styles: Record<'h3' | 'timeline', CSSProperties> = { h3: { marginBottom: 0, }, timeline: { marginTop: 6.5, marginLeft: 6.5, }, }; export interface ReasonProps { auditDetail?: { reason?: ReasonDataType[]; initiator?: any; isRenew?: boolean; }; style?: CSSProperties; foldLength?: number; fold?: boolean; readOnly?: boolean; showType?: 'all' | 'comment' | 'applicationReason'; size?: SpaceSize | [SpaceSize, SpaceSize]; onRefresh?: () => Promise; onFold?: (fold: boolean) => void; } const Reason = (props: ReasonProps) => { const { auditDetail, style, foldLength = 3, readOnly, showType = 'all', size = 'large', onRefresh, } = props; const { formatMessage } = useIntl(); const { reason: source, initiator, isRenew } = auditDetail ?? {}; const { wrapperRef } = useContext(AuditContainerContext); const timeoutRef = useRef(); const delHandler = useCallback( async (id: number) => { try { await requestReasonDel(id); onRefresh?.(); message.success(formatMessage({ id: 'component.AuditList.reason.del.success' })); } catch (e) {} }, [formatMessage, onRefresh], ); const [fold, setFold] = useControllableValue(props, { valuePropName: 'fold', trigger: 'onFold', }); const reason = useMemo(() => { // 评论为第一条后的内容 if (showType === 'comment') return source?.filter((__, index) => index > 0); // 申请理由为第一条 if (showType === 'applicationReason') return source?.length ? [source[0]] : []; // 所有 return source ?? []; }, [showType, source]); const yxReason = useMemo( () => reason?.filter((__, index: number) => (fold ? index < foldLength : true)) ?? [], [fold, foldLength, reason], ); const totalLength = reason?.length ?? 0; useEffect(() => () => clearTimeout(timeoutRef.current), []); const scrollBottom = useCallback(() => { clearTimeout(timeoutRef.current); timeoutRef.current = setTimeout(() => { const ele = wrapperRef?.current; if (ele) { ele.scrollTo({ behavior: 'smooth', top: ele.scrollHeight }); } }, 300); }, [wrapperRef]); return ( {showType === 'applicationReason' ? (

{formatMessage({ id: 'base.apply.reason' })}

) : (

{formatMessage({ id: `base.apply.reason.${showType}` }, { count: totalLength })}

)} {!!isRenew && ['applicationReason', 'all'].includes(showType) && ( {formatMessage({ id: 'base.renewal' })} )}
{yxReason.map((item: ReasonDataType, index: number) => ( foldLength} canDel={showType !== 'applicationReason'} fold={fold} restLength={totalLength - foldLength} onFold={() => setFold(true)} onExpand={() => { setFold(false); scrollBottom(); }} initiator={initiator} index={index} length={yxReason.length} readOnly={readOnly} onDelete={delHandler} /> ))}
); }; export default Reason;