date-picker.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { useCallback } from 'react'
  2. import dayjs from 'dayjs'
  3. import {
  4. RiCalendarLine,
  5. RiCloseCircleFill,
  6. } from '@remixicon/react'
  7. import DatePicker from '@/app/components/base/date-and-time-picker/date-picker'
  8. import cn from '@/utils/classnames'
  9. import type { TriggerProps } from '@/app/components/base/date-and-time-picker/types'
  10. import useTimestamp from '@/hooks/use-timestamp'
  11. import { useTranslation } from 'react-i18next'
  12. type Props = {
  13. className?: string
  14. value?: number
  15. onChange: (date: number | null) => void
  16. }
  17. const WrappedDatePicker = ({
  18. className,
  19. value,
  20. onChange,
  21. }: Props) => {
  22. const { t } = useTranslation()
  23. // const { userProfile: { timezone } } = useAppContext()
  24. const { formatTime: formatTimestamp } = useTimestamp()
  25. const handleDateChange = useCallback((date?: dayjs.Dayjs) => {
  26. if (date)
  27. onChange(date.unix())
  28. else
  29. onChange(null)
  30. }, [onChange])
  31. const renderTrigger = useCallback(({
  32. handleClickTrigger,
  33. }: TriggerProps) => {
  34. return (
  35. <div onClick={handleClickTrigger} className={cn('group flex items-center rounded-md bg-components-input-bg-normal', className)}>
  36. <div
  37. className={cn(
  38. 'grow',
  39. value ? 'text-text-secondary' : 'text-text-tertiary',
  40. )}
  41. >
  42. {value ? formatTimestamp(value, t('datasetDocuments.metadata.dateTimeFormat')) : t('dataset.metadata.chooseTime')}
  43. </div>
  44. <RiCloseCircleFill
  45. className={cn(
  46. 'hidden h-4 w-4 cursor-pointer hover:text-components-input-text-filled group-hover:block',
  47. value && 'text-text-quaternary',
  48. )}
  49. onClick={() => handleDateChange()}
  50. />
  51. <RiCalendarLine
  52. className={cn(
  53. 'block h-4 w-4 shrink-0 group-hover:hidden',
  54. value ? 'text-text-quaternary' : 'text-text-tertiary',
  55. )}
  56. />
  57. </div>
  58. )
  59. }, [className, value, formatTimestamp, t, handleDateChange])
  60. return (
  61. <DatePicker
  62. value={dayjs(value ? value * 1000 : Date.now())}
  63. onChange={handleDateChange}
  64. onClear={handleDateChange}
  65. renderTrigger={renderTrigger}
  66. triggerWrapClassName='w-full'
  67. popupZIndexClassname='z-[1000]'
  68. />
  69. )
  70. }
  71. export default WrappedDatePicker