button.tsx 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { useChatContext } from '@/app/components/base/chat/chat/context'
  2. import Button from '@/app/components/base/button'
  3. import cn from '@/utils/classnames'
  4. const MarkdownButton = ({ node }: any) => {
  5. const { onSend } = useChatContext()
  6. const variant = node.properties.dataVariant
  7. const message = node.properties.dataMessage
  8. const link = node.properties.dataLink
  9. const size = node.properties.dataSize
  10. function is_valid_url(url: string): boolean {
  11. try {
  12. const parsed_url = new URL(url)
  13. return ['http:', 'https:'].includes(parsed_url.protocol)
  14. }
  15. catch {
  16. return false
  17. }
  18. }
  19. return <Button
  20. variant={variant}
  21. size={size}
  22. className={cn('!h-8 !px-3 select-none')}
  23. onClick={() => {
  24. if (is_valid_url(link)) {
  25. window.open(link, '_blank')
  26. return
  27. }
  28. onSend?.(message)
  29. }}
  30. >
  31. <span className='text-[13px]'>{node.children[0]?.value || ''}</span>
  32. </Button>
  33. }
  34. MarkdownButton.displayName = 'MarkdownButton'
  35. export default MarkdownButton