index-bar.tsx 2.8 KB

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