index.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { View } from "@tarojs/components";
  2. import React, { useEffect, useState, forwardRef, useImperativeHandle, useRef } from "react";
  3. import AgentSettingList from './components/AgentSettingList'
  4. import AgentKnowledgeLib from './components/AgentKnowledgeLib'
  5. import AgentCard from './components/AgentCard'
  6. import AgentContactCard from './components/AgentContactCard'
  7. import BottomBar from "@/components/BottomBar";
  8. import WemetaButton from "@/components/buttons/WemetaButton";
  9. import { useAgentStore } from "@/store/agentStore";
  10. export default forwardRef(function Index({save}: {save: ()=> void}, ref) {
  11. console.log('agent setting')
  12. const agentEdit = useAgentStore((state) => state.agentEdit)
  13. const enableSave = useAgentStore((state) => state.enableSave)
  14. const [isPolishing, setIsPolishing] = useState(false)
  15. // Create ref for AgentSettingList
  16. const agentSettingListRef = useRef<{ polishAllTexts: () => Promise<void> }>(null);
  17. // Expose methods to parent components
  18. useImperativeHandle(ref, () => ({
  19. polishAllTexts: async () => {
  20. if (agentSettingListRef.current) {
  21. setIsPolishing(true);
  22. await agentSettingListRef.current.polishAllTexts();
  23. setIsPolishing(false);
  24. }
  25. }
  26. }));
  27. // 控制按钮状态和文本
  28. const isNameValid = (agentEdit?.name?.length || 0) >= 2
  29. const isDisabled = !isNameValid || isPolishing || !enableSave
  30. const buttonText = agentEdit?.agentId ? '保存' : '创建'
  31. return (
  32. <View className="pb-118">
  33. <AgentCard></AgentCard>
  34. <View className="mb-12">
  35. <AgentContactCard></AgentContactCard>
  36. </View>
  37. <AgentSettingList ref={agentSettingListRef}></AgentSettingList>
  38. <View className="py-12">
  39. <AgentKnowledgeLib></AgentKnowledgeLib>
  40. </View>
  41. <BottomBar>
  42. <WemetaButton disabled={isDisabled} className="flex-1" onClick={save}>{buttonText}</WemetaButton>
  43. </BottomBar>
  44. </View>
  45. );
  46. })