Files
mai-bot/dashboard/dist/assets/survey-renderer-DVA7Y8cR.js

2 lines
11 KiB
JavaScript

import{r as o,j as e}from"./router-zNjPR4CY.js";import{L as C,g,I as q,C as y,f as w,b as k,d as I,X as L,Y as T,S as ee,B as N}from"./index-CuOHsLf7.js";import{P as se}from"./progress-C6-hh8fF.js";import{T as te}from"./textarea-8PIujbf-.js";import{R as re,a as ae}from"./radio-group-BBkaGEpR.js";import{C as le}from"./checkbox-DWiVrbnx.js";import{S as ne}from"./slider-mDhSC-zv.js";import{S as ie,a as ce,b as de,c as oe,d as ue}from"./select-DGqIoF9r.js";import{aX as me,L as $,W as A,e as xe,G as he,y as je}from"./icons-DTcdLw9j.js";function fe({question:s,value:l,onChange:c,error:m,disabled:x=!1}){const[h,u]=o.useState(null),d=x||s.readOnly,j=()=>{switch(s.type){case"single":return e.jsx(re,{value:l||"",onValueChange:c,disabled:d,className:"space-y-2",children:s.options?.map(a=>e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(ae,{value:a.value,id:`${s.id}-${a.id}`}),e.jsx(C,{htmlFor:`${s.id}-${a.id}`,className:"cursor-pointer font-normal",children:a.label})]},a.id))});case"multiple":{const a=l||[];return e.jsxs("div",{className:"space-y-2",children:[s.options?.map(i=>e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(le,{id:`${s.id}-${i.id}`,checked:a.includes(i.value),disabled:d||s.maxSelections!==void 0&&a.length>=s.maxSelections&&!a.includes(i.value),onCheckedChange:f=>{c(f?[...a,i.value]:a.filter(p=>p!==i.value))}}),e.jsx(C,{htmlFor:`${s.id}-${i.id}`,className:"cursor-pointer font-normal",children:i.label})]},i.id)),s.maxSelections&&e.jsxs("p",{className:"text-xs text-muted-foreground",children:["最多选择 ",s.maxSelections," 项"]})]})}case"text":return e.jsx(q,{value:l||"",onChange:a=>c(a.target.value),placeholder:s.placeholder||"请输入...",disabled:d,readOnly:s.readOnly,maxLength:s.maxLength,className:g(s.readOnly&&"bg-muted cursor-not-allowed")});case"textarea":return e.jsxs("div",{className:"space-y-1",children:[e.jsx(te,{value:l||"",onChange:a=>c(a.target.value),placeholder:s.placeholder||"请输入...",disabled:d,readOnly:s.readOnly,maxLength:s.maxLength,rows:4,className:g(s.readOnly&&"bg-muted cursor-not-allowed")}),s.maxLength&&e.jsxs("p",{className:"text-xs text-muted-foreground text-right",children:[(l||"").length," / ",s.maxLength]})]});case"rating":{const a=l||0,i=h!==null?h:a;return e.jsxs("div",{className:"flex items-center gap-1",children:[[1,2,3,4,5].map(f=>e.jsx("button",{type:"button",disabled:d,className:g("p-1 transition-colors focus:outline-none focus:ring-2 focus:ring-ring rounded",d&&"cursor-not-allowed opacity-50"),onMouseEnter:()=>!d&&u(f),onMouseLeave:()=>u(null),onClick:()=>!d&&c(f),children:e.jsx(me,{className:g("h-6 w-6 transition-colors",f<=i?"fill-yellow-400 text-yellow-400":"text-muted-foreground")})},f)),a>0&&e.jsxs("span",{className:"ml-2 text-sm text-muted-foreground",children:[a," / 5"]})]})}case"scale":{const a=s.min??1,i=s.max??10,f=s.step??1,p=l??a;return e.jsxs("div",{className:"space-y-4",children:[e.jsx(ne,{value:[p],onValueChange:([v])=>c(v),min:a,max:i,step:f,disabled:d}),e.jsxs("div",{className:"flex justify-between text-xs text-muted-foreground",children:[e.jsx("span",{children:s.minLabel||a}),e.jsx("span",{className:"font-medium text-foreground",children:p}),e.jsx("span",{children:s.maxLabel||i})]})]})}case"dropdown":return e.jsxs(ie,{value:l||"",onValueChange:c,disabled:d,children:[e.jsx(ce,{children:e.jsx(de,{placeholder:s.placeholder||"请选择..."})}),e.jsx(oe,{children:s.options?.map(a=>e.jsx(ue,{value:a.value,children:a.label},a.id))})]});default:return e.jsx("div",{className:"text-muted-foreground",children:"不支持的问题类型"})}};return e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsxs(C,{className:"text-base font-medium",children:[s.title,s.required&&e.jsx("span",{className:"text-destructive ml-1",children:"*"})]}),s.description&&e.jsx("p",{className:"text-sm text-muted-foreground",children:s.description})]}),j(),m&&e.jsx("p",{className:"text-sm text-destructive",children:m})]})}const U="https://maibot-plugin-stats.maibot-webui.workers.dev";function F(){const s="maibot_user_id";let l=localStorage.getItem(s);if(!l){const c=Math.random().toString(36).substring(2,10),m=Date.now().toString(36),x=Math.random().toString(36).substring(2,10);l=`fp_${c}_${m}_${x}`,localStorage.setItem(s,l)}return l}async function pe(s,l,c,m){try{const x=m?.userId||F(),h={surveyId:s,surveyVersion:l,userId:x,answers:c,submittedAt:new Date().toISOString(),allowMultiple:m?.allowMultiple,metadata:{userAgent:navigator.userAgent,language:navigator.language}},u=await fetch(`${U}/survey/submit`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(h)}),d=await u.json();return u.status===429?{success:!1,error:"提交过于频繁,请稍后再试"}:u.status===409?{success:!1,error:d.error||"你已经提交过这份问卷了"}:u.ok?{success:!0,submissionId:d.submissionId,message:d.message}:{success:!1,error:d.error||"提交失败"}}catch(x){return console.error("Error submitting survey:",x),{success:!1,error:"网络错误"}}}async function ge(s,l){try{const c=l||F(),m=new URLSearchParams({user_id:c,survey_id:s}),x=await fetch(`${U}/survey/check?${m}`);return x.ok?{success:!0,hasSubmitted:(await x.json()).hasSubmitted}:{success:!1,error:(await x.json()).error||"检查失败"}}catch(c){return console.error("Error checking submission:",c),{success:!1,error:"网络错误"}}}function Le({config:s,initialAnswers:l,onSubmitSuccess:c,onSubmitError:m,showProgress:x=!0,paginateQuestions:h=!1,className:u}){const d=o.useCallback(()=>!l||l.length===0?{}:l.reduce((t,r)=>(t[r.questionId]=r.value,t),{}),[l]),[j,a]=o.useState(()=>d()),[i,f]=o.useState({}),[p,v]=o.useState(0),[b,M]=o.useState(!1),[B,G]=o.useState(!1),[E,S]=o.useState(null),[O,H]=o.useState(null),[W,X]=o.useState(!1),[Y,z]=o.useState(!0);o.useEffect(()=>{l&&l.length>0&&a(t=>({...t,...d()}))},[l,d]),o.useEffect(()=>{(async()=>{if(!s.settings?.allowMultiple){const r=await ge(s.id);r.success&&r.hasSubmitted&&X(!0)}z(!1)})()},[s.id,s.settings?.allowMultiple]);const J=o.useCallback(()=>{const t=new Date;return!(s.settings?.startTime&&new Date(s.settings.startTime)>t||s.settings?.endTime&&new Date(s.settings.endTime)<t)},[s.settings?.startTime,s.settings?.endTime]),R=s.questions.filter(t=>{const r=j[t.id];return r==null?!1:Array.isArray(r)?r.length>0:typeof r=="string"?r.trim()!=="":!0}).length,K=R/s.questions.length*100,Z=o.useCallback((t,r)=>{a(n=>({...n,[t]:r})),f(n=>{const V={...n};return delete V[t],V})},[]),_=o.useCallback(()=>{const t={};for(const r of s.questions){if(r.required){const n=j[r.id];if(n==null){t[r.id]="此题为必填项";continue}if(Array.isArray(n)&&n.length===0){t[r.id]="请至少选择一项";continue}if(typeof n=="string"&&n.trim()===""){t[r.id]="此题为必填项";continue}}r.minLength&&typeof j[r.id]=="string"&&j[r.id].length<r.minLength&&(t[r.id]=`至少需要 ${r.minLength} 个字符`)}return f(t),Object.keys(t).length===0},[s.questions,j]),P=o.useCallback(async()=>{if(!_()){if(h){const t=s.questions.findIndex(r=>i[r.id]);t>=0&&v(t)}return}M(!0),S(null);try{const t=s.questions.filter(n=>j[n.id]!==void 0).map(n=>({questionId:n.id,value:j[n.id]})),r=await pe(s.id,s.version,t,{allowMultiple:s.settings?.allowMultiple});if(r.success&&r.submissionId)G(!0),H(r.submissionId),c?.(r.submissionId);else{const n=r.error||"提交失败";S(n),m?.(n)}}catch(t){const r=t instanceof Error?t.message:"提交失败";S(r),m?.(r)}finally{M(!1)}},[_,h,s,j,i,c,m]),D=o.useCallback(t=>{t>=0&&t<s.questions.length&&v(t)},[s.questions.length]);if(Y)return e.jsx(y,{className:g("w-full max-w-2xl mx-auto",u),children:e.jsx(w,{className:"py-12 flex items-center justify-center",children:e.jsx($,{className:"h-6 w-6 animate-spin text-muted-foreground"})})});if(W&&!s.settings?.allowMultiple)return e.jsxs(y,{className:g("w-full max-w-2xl mx-auto",u),children:[e.jsx(k,{children:e.jsx(I,{children:s.title})}),e.jsx(w,{className:"py-8",children:e.jsxs(L,{children:[e.jsx(A,{className:"h-4 w-4"}),e.jsx(T,{children:"你已经提交过这份问卷了,感谢参与!"})]})})]});if(!J())return e.jsxs(y,{className:g("w-full max-w-2xl mx-auto",u),children:[e.jsx(k,{children:e.jsx(I,{children:s.title})}),e.jsx(w,{className:"py-8",children:e.jsxs(L,{variant:"destructive",children:[e.jsx(A,{className:"h-4 w-4"}),e.jsx(T,{children:"问卷不在有效期内"})]})})]});if(B)return e.jsxs(y,{className:g("w-full max-w-2xl mx-auto",u),children:[e.jsx(k,{children:e.jsxs(I,{className:"flex items-center gap-2 text-green-600",children:[e.jsx(xe,{className:"h-6 w-6"}),"提交成功"]})}),e.jsxs(w,{className:"py-8",children:[e.jsx("p",{className:"text-center text-muted-foreground",children:s.settings?.thankYouMessage||"感谢你的参与!"}),O&&e.jsxs("p",{className:"text-center text-xs text-muted-foreground mt-4",children:["提交编号:",O]})]})]});const Q=h?[s.questions[p]]:s.questions;return e.jsxs("div",{className:g("h-full flex flex-col",u),children:[e.jsxs("div",{className:"rounded-lg border bg-card p-4 sm:p-6 mb-4 shrink-0",children:[e.jsx("h2",{className:"text-xl font-semibold",children:s.title}),s.description&&e.jsx("p",{className:"text-muted-foreground mt-1 text-sm",children:s.description}),x&&e.jsxs("div",{className:"space-y-1 pt-3",children:[e.jsxs("div",{className:"flex justify-between text-xs text-muted-foreground",children:[e.jsx("span",{children:"进度"}),e.jsxs("span",{children:[R," / ",s.questions.length]})]}),e.jsx(se,{value:K,className:"h-2"})]})]}),e.jsx(ee,{className:"flex-1 min-h-0",children:e.jsxs("div",{className:"space-y-4 pr-4",children:[Q.map((t,r)=>e.jsxs("div",{className:g("p-4 rounded-lg border bg-card",i[t.id]?"border-destructive bg-destructive/5":"border-border"),children:[h&&e.jsxs("div",{className:"text-xs text-muted-foreground mb-2",children:["问题 ",p+1," / ",s.questions.length]}),!h&&e.jsxs("div",{className:"text-xs text-muted-foreground mb-2",children:[r+1,"."]}),e.jsx(fe,{question:t,value:j[t.id],onChange:n=>Z(t.id,n),error:i[t.id],disabled:b})]},t.id)),E&&e.jsxs(L,{variant:"destructive",children:[e.jsx(A,{className:"h-4 w-4"}),e.jsx(T,{children:E})]}),e.jsx("div",{className:"flex justify-between items-center py-4",children:h?e.jsxs(e.Fragment,{children:[e.jsxs(N,{variant:"outline",onClick:()=>D(p-1),disabled:p===0||b,children:[e.jsx(he,{className:"h-4 w-4 mr-1"}),"上一题"]}),p===s.questions.length-1?e.jsxs(N,{onClick:P,disabled:b,children:[b&&e.jsx($,{className:"h-4 w-4 mr-2 animate-spin"}),"提交问卷"]}):e.jsxs(N,{onClick:()=>D(p+1),disabled:b,children:["下一题",e.jsx(je,{className:"h-4 w-4 ml-1"})]})]}):e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"text-sm text-muted-foreground",children:Object.keys(i).length>0&&e.jsxs("span",{className:"text-destructive",children:["还有 ",Object.keys(i).length," 个必填项未完成"]})}),e.jsxs(N,{onClick:P,disabled:b,size:"lg",children:[b&&e.jsx($,{className:"h-4 w-4 mr-2 animate-spin"}),"提交问卷"]})]})})]})})]})}export{Le as S};