import React, { useState, useImperativeHandle, useEffect, forwardRef, memo, } from "react"; import { Button, Input, View, Text, Image } from "@tarojs/components"; import Taro from "@tarojs/taro"; import classnames from "classnames"; import CommonPopup from "../CommonPopup"; import { useUserActions, useUserInfo } from "@/store/userStore"; import { EvaluateCallback, EvaluateScene } from "@/store/evaluateStore"; import { useNtrpLevels } from "@/store/pickerOptionsStore"; import NTRPTestEntryCard from "../NTRPTestEntryCard"; import NtrpPopupGuide from "../NTRPPopupGuide"; import Picker from "../Picker/Picker"; import CloseIcon from "@/static/ntrp/ntrp_popup_close.svg"; import styles from "./index.module.scss"; export enum EvaluateType { EDIT = "edit", EVALUATE = "evaluate", } export enum DisplayConditionType { AUTO = "auto", ALWAYS = "always", } export enum SceneType { LIST = "list", PUBLISH = "publish", PERSONAL = "personal", DETAIL = "detail", } interface NTRPEvaluatePopupProps { // types: EvaluateType[]; // displayCondition: DisplayConditionType; // scene: SceneType; showGuide: boolean; type: EvaluateScene; // children: React.ReactNode; } // function showCondition(scene, ntrp) { // if (scene === "list") { // // TODO: 显示频率 // return Math.random() < 0.1 && [0, undefined].includes(ntrp); // } // return true; // return !ntrpLevels.includes(ntrp); // } const NTRPEvaluatePopup = (props: NTRPEvaluatePopupProps, ref) => { const { // types = ["edit", "evaluate"], // displayCondition = "auto", // scene = "list", type, showGuide = false, } = props; const [visible, setVisible] = useState(false); const [ntrp, setNtrp] = useState("1.5"); const [guideShow, setGuideShow] = useState(() => showGuide); const { updateUserInfo } = useUserActions(); const userInfo = useUserInfo(); const ntrpLevels = useNtrpLevels(); const [evaCallback, setEvaCallback] = useState({ type: "", next: () => {}, onCancel: () => {}, }); useImperativeHandle(ref, () => ({ show: (evaluateCallback) => { setVisible(true); setEvaCallback(evaluateCallback); }, })); // function handleEvaluate() { // setVisible(false); // // TODO: 实现NTRP评估逻辑 // Taro.navigateTo({ // url: `/other_pages/ntrp-evaluate/index?redirect=${encodeURIComponent( // getCurrentFullPath() // )}`, // }); // } // 当弹窗打开或用户信息变化时,从用户信息中提取并更新 ntrp 状态 useEffect(() => { if (visible) { if (userInfo?.ntrp_level) { // 从 ntrp_level 中提取数字部分(如 "2.5" 或 "NTRP 2.5") const match = String(userInfo.ntrp_level).match(/-?\d+(\.\d+)?/); if (match) { setNtrp(match[0]); } else { setNtrp("1.5"); } } else { setNtrp("1.5"); } } }, [visible, userInfo?.ntrp_level]); // const showEntry = // displayCondition === "auto" // ? showCondition(scene, ntrp) // : displayCondition === "always"; function handleClose() { console.log("hide ...."); setVisible(false); setGuideShow(showGuide); } async function handleChangeNtrp() { Taro.showLoading({ title: "修改中" }); // 更新用户信息,会自动更新 store 中的 ntrp_level await updateUserInfo({ ntrp_level: ntrp }); Taro.hideLoading(); Taro.showToast({ title: "NTRP水平修改成功", icon: "none", }); evaCallback.next({ flag: true, score: String(ntrp) }); handleClose(); } return ( <> {guideShow ? ( handleClose()} skipGuide={() => { setGuideShow(false); }} evaluateCallback={evaCallback} /> ) : ( 选择 NTRP 自评水平 {/* FIXME: 有异常渲染问题 */} {visible && ( { console.log(val[0]); if (val[0]?.value) { setNtrp(val[0]?.value as string); } }} /> )} )} ); }; export default memo(forwardRef(NTRPEvaluatePopup));