use-feature.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import React, { useEffect } from 'react'
  2. function useFeature({
  3. introduction,
  4. setIntroduction,
  5. moreLikeThis,
  6. setMoreLikeThis,
  7. suggestedQuestionsAfterAnswer,
  8. setSuggestedQuestionsAfterAnswer,
  9. speechToText,
  10. setSpeechToText,
  11. citation,
  12. setCitation,
  13. moderation,
  14. setModeration,
  15. }: {
  16. introduction: string
  17. setIntroduction: (introduction: string) => void
  18. moreLikeThis: boolean
  19. setMoreLikeThis: (moreLikeThis: boolean) => void
  20. suggestedQuestionsAfterAnswer: boolean
  21. setSuggestedQuestionsAfterAnswer: (suggestedQuestionsAfterAnswer: boolean) => void
  22. speechToText: boolean
  23. setSpeechToText: (speechToText: boolean) => void
  24. citation: boolean
  25. setCitation: (citation: boolean) => void
  26. moderation: boolean
  27. setModeration: (moderation: boolean) => void
  28. }) {
  29. const [tempshowOpeningStatement, setTempShowOpeningStatement] = React.useState(!!introduction)
  30. useEffect(() => {
  31. // wait to api data back
  32. if (introduction)
  33. setTempShowOpeningStatement(true)
  34. }, [introduction])
  35. // const [tempMoreLikeThis, setTempMoreLikeThis] = React.useState(moreLikeThis)
  36. // useEffect(() => {
  37. // setTempMoreLikeThis(moreLikeThis)
  38. // }, [moreLikeThis])
  39. const featureConfig = {
  40. openingStatement: tempshowOpeningStatement,
  41. moreLikeThis,
  42. suggestedQuestionsAfterAnswer,
  43. speechToText,
  44. citation,
  45. moderation,
  46. }
  47. const handleFeatureChange = (key: string, value: boolean) => {
  48. switch (key) {
  49. case 'openingStatement':
  50. if (!value)
  51. setIntroduction('')
  52. setTempShowOpeningStatement(value)
  53. break
  54. case 'moreLikeThis':
  55. setMoreLikeThis(value)
  56. break
  57. case 'suggestedQuestionsAfterAnswer':
  58. setSuggestedQuestionsAfterAnswer(value)
  59. break
  60. case 'speechToText':
  61. setSpeechToText(value)
  62. break
  63. case 'citation':
  64. setCitation(value)
  65. break
  66. case 'moderation':
  67. setModeration(value)
  68. }
  69. }
  70. return {
  71. featureConfig,
  72. handleFeatureChange,
  73. }
  74. }
  75. export default useFeature