ChatSender.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use client';
  2. import TextareaAutosize from 'react-textarea-autosize';
  3. import { BsCapslockFill } from "react-icons/bs";
  4. import * as R from 'ramda'
  5. import { BsFillStopFill } from "react-icons/bs";
  6. import React, { useMemo } from 'react';
  7. const Sender = ({ input, onInput, isSending, onCommit, onStop, effectButtons = [] }) => {
  8. const onKeyDown = (e) => {
  9. if (e.key === 'Enter' && R.none(R.equals(true), R.props(['altKey', 'shiftKey', 'ctrlKey', 'metaKey'], e))) {
  10. onCommit()
  11. e.preventDefault()
  12. }
  13. }
  14. const EffectElement = useMemo(() => {
  15. if (effectButtons?.length) {
  16. return (
  17. <div className="flex justify-start gap-1">
  18. {effectButtons}
  19. </div>
  20. )
  21. }
  22. return null
  23. }, [effectButtons])
  24. return (
  25. <div
  26. className="absolute flex flex-col gap-2 inset-x-2.5 bottom-2.5 w-auto"
  27. >
  28. {EffectElement}
  29. <TextareaAutosize className="textarea textarea-bordered pr-12 w-full resize-none" value={input} onChange={ev => onInput(ev.target.value)} maxRows={4} onKeyDown={onKeyDown} />
  30. {
  31. isSending
  32. ? (
  33. <button className="btn btn-secondary btn-sm absolute right-[8px] bottom-[8px] w-[2rem] px-0" onClick={onStop}>
  34. <BsFillStopFill size={18} />
  35. </button>
  36. )
  37. : (
  38. <button className="btn btn-primary btn-sm absolute right-[8px] bottom-[8px] w-[2rem] px-0" disabled={!input} onClick={onCommit}>
  39. <BsCapslockFill size={18} />
  40. </button>
  41. )
  42. }
  43. </div>
  44. )
  45. }
  46. export default React.memo(Sender)