Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

index-bar.tsx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { pinyin } from 'pinyin-pro'
  2. import type { FC, RefObject } from 'react'
  3. import type { ToolWithProvider } from '../types'
  4. import { CollectionType } from '../../tools/types'
  5. import classNames from '@/utils/classnames'
  6. export const CUSTOM_GROUP_NAME = '@@@custom@@@'
  7. export const WORKFLOW_GROUP_NAME = '@@@workflow@@@'
  8. export const DATA_SOURCE_GROUP_NAME = '@@@data_source@@@'
  9. export const AGENT_GROUP_NAME = '@@@agent@@@'
  10. /*
  11. {
  12. A: {
  13. 'google': [ // plugin organize name
  14. ...tools
  15. ],
  16. 'custom': [ // custom tools
  17. ...tools
  18. ],
  19. 'workflow': [ // workflow as tools
  20. ...tools
  21. ]
  22. }
  23. }
  24. */
  25. export const groupItems = (items: ToolWithProvider[], getFirstChar: (item: ToolWithProvider) => string) => {
  26. const groups = items.reduce((acc: Record<string, Record<string, ToolWithProvider[]>>, item) => {
  27. const firstChar = getFirstChar(item)
  28. if (!firstChar || firstChar.length === 0)
  29. return acc
  30. let letter
  31. // transform Chinese to pinyin
  32. if (/[\u4E00-\u9FA5]/.test(firstChar))
  33. letter = pinyin(firstChar, { pattern: 'first', toneType: 'none' })[0].toUpperCase()
  34. else
  35. letter = firstChar.toUpperCase()
  36. if (!/[A-Z]/.test(letter))
  37. letter = '#'
  38. if (!acc[letter])
  39. acc[letter] = {}
  40. let groupName: string = ''
  41. if (item.type === CollectionType.builtIn)
  42. groupName = item.author
  43. else if (item.type === CollectionType.custom)
  44. groupName = CUSTOM_GROUP_NAME
  45. else if (item.type === CollectionType.workflow)
  46. groupName = WORKFLOW_GROUP_NAME
  47. else if (item.type === CollectionType.datasource)
  48. groupName = DATA_SOURCE_GROUP_NAME
  49. else
  50. groupName = AGENT_GROUP_NAME
  51. if (!acc[letter][groupName])
  52. acc[letter][groupName] = []
  53. acc[letter][groupName].push(item)
  54. return acc
  55. }, {})
  56. const letters = Object.keys(groups).sort()
  57. // move '#' to the end
  58. const hashIndex = letters.indexOf('#')
  59. if (hashIndex !== -1) {
  60. letters.splice(hashIndex, 1)
  61. letters.push('#')
  62. }
  63. return { letters, groups }
  64. }
  65. type IndexBarProps = {
  66. letters: string[]
  67. itemRefs: RefObject<{ [key: string]: HTMLElement | null }>
  68. className?: string
  69. }
  70. const IndexBar: FC<IndexBarProps> = ({ letters, itemRefs, className }) => {
  71. const handleIndexClick = (letter: string) => {
  72. const element = itemRefs.current?.[letter]
  73. if (element)
  74. element.scrollIntoView({ behavior: 'smooth' })
  75. }
  76. return (
  77. <div className={classNames('index-bar sticky top-[20px] flex h-full w-6 flex-col items-center justify-center text-xs font-medium text-text-quaternary', className)}>
  78. <div className={classNames('absolute left-0 top-0 h-full w-px bg-[linear-gradient(270deg,rgba(255,255,255,0)_0%,rgba(16,24,40,0.08)_30%,rgba(16,24,40,0.08)_50%,rgba(16,24,40,0.08)_70.5%,rgba(255,255,255,0)_100%)]')}></div>
  79. {letters.map(letter => (
  80. <div className="cursor-pointer hover:text-text-secondary" key={letter} onClick={() => handleIndexClick(letter)}>
  81. {letter}
  82. </div>
  83. ))}
  84. </div>
  85. )
  86. }
  87. export default IndexBar