{"version":3,"file":"ChannelFilesView.mjs","names":[],"sources":["../../../../../../src/plugins/ChannelDetail/Views/ChannelFilesView/ChannelFilesView.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport type { ComponentProps } from 'react';\nimport React, { forwardRef, useCallback, useMemo } from 'react';\nimport { GroupedVirtuoso } from 'react-virtuoso';\n\nimport { useComponentContext, useTranslationContext } from '../../../../context';\nimport { getDateString } from '../../../../i18n/utils';\nimport { FileSizeIndicator as DefaultFileSizeIndicator } from '../../../../components/Attachment/components/FileSizeIndicator';\nimport { Prompt } from '../../../../components/Dialog';\nimport { FileIcon } from '../../../../components/FileIcon';\nimport { ListItemLayout } from '../../../../components/ListItemLayout';\nimport {\n  SectionNavigatorHeader,\n  type SectionNavigatorSectionContentProps,\n} from '../../SectionNavigator';\nimport { useChannelDetailContext } from '../../ChannelDetailContext';\nimport { ChannelDetailListLoadingIndicator } from '../../ChannelDetailListLoadingIndicator';\nimport { ChannelFilesEmptyList } from './ChannelFilesEmptyList';\nimport type { ChannelFileItem } from './ChannelFilesView.utils';\nimport { useChannelFilesSearch } from './useChannelFilesSearch';\n\n// Wraps each virtualized file row so the horizontal inset lands on a stable class instead of\n// padding the full-bleed sticky month headers.\nconst ChannelFilesItem = forwardRef<HTMLDivElement, ComponentProps<'div'>>(\n  function ChannelFilesItem({ className, ...props }, ref) {\n    return (\n      <div\n        {...props}\n        className={clsx('str-chat__channel-detail__files-view__item', className)}\n        ref={ref}\n      />\n    );\n  },\n);\n\n// Wraps each sticky month header. react-virtuoso sets `position: sticky` but leaves `top: auto`,\n// so headers would not pin and consecutive months would overlap; the class supplies `top: 0`.\nconst ChannelFilesGroup = forwardRef<HTMLDivElement, ComponentProps<'div'>>(\n  function ChannelFilesGroup({ className, ...props }, ref) {\n    return (\n      <div\n        {...props}\n        className={clsx('str-chat__channel-detail__files-view__group', className)}\n        ref={ref}\n      />\n    );\n  },\n);\n\nconst ChannelFilesSectionHeader = ({ timestamp }: { timestamp?: string }) => {\n  const { t, tDateTimeParser } = useTranslationContext();\n  const label = getDateString({\n    format: 'MMMM YYYY',\n    messageCreatedAt: timestamp,\n    t,\n    tDateTimeParser,\n  });\n\n  if (!label) return null;\n\n  return (\n    <div className='str-chat__channel-detail__files-view__section-header'>{label}</div>\n  );\n};\n\nconst getAttachmentFileName = (attachment: ChannelFileItem['attachment']) =>\n  attachment.title || attachment.fallback || '';\n\nconst ChannelFileListItem = ({ item }: { item: ChannelFileItem }) => {\n  const { FileSizeIndicator = DefaultFileSizeIndicator } = useComponentContext();\n  const { attachment } = item;\n  const fileName = getAttachmentFileName(attachment);\n  const assetUrl = attachment.asset_url;\n  const { file_size: fileSize, mime_type: mimeType } = attachment.custom ?? {};\n\n  const LeadingSlot = useMemo(\n    () =>\n      function FileListItemIcon() {\n        return (\n          <FileIcon\n            className='str-chat__channel-detail__files-view__list-item__icon'\n            fileName={fileName}\n            mimeType={mimeType}\n            size='md'\n          />\n        );\n      },\n    [mimeType, fileName],\n  );\n\n  const sharedProps = useMemo(\n    () => ({\n      LeadingSlot,\n      subtitle: <FileSizeIndicator fileSize={fileSize} />,\n      subtitleClassName: 'str-chat__channel-detail__files-view__list-item__size',\n      title: fileName,\n      titleClassName: 'str-chat__channel-detail__files-view__list-item__name',\n    }),\n    [FileSizeIndicator, fileSize, fileName, LeadingSlot],\n  );\n\n  const linkRootProps = useMemo(\n    () => ({\n      className: 'str-chat__channel-detail__files-view__list-item',\n      download: fileName || undefined,\n      href: assetUrl,\n      rel: 'noopener noreferrer',\n      target: '_blank',\n    }),\n    [assetUrl, fileName],\n  );\n\n  const divRootProps = useMemo(\n    () => ({ className: 'str-chat__channel-detail__files-view__list-item' }),\n    [],\n  );\n\n  if (assetUrl) {\n    return <ListItemLayout {...sharedProps} RootElement='a' rootProps={linkRootProps} />;\n  }\n\n  return <ListItemLayout {...sharedProps} RootElement='div' rootProps={divRootProps} />;\n};\n\nexport type ChannelFilesViewProps = SectionNavigatorSectionContentProps;\n\nexport const ChannelFilesView: React.ComponentType<ChannelFilesViewProps> = () => {\n  const { t } = useTranslationContext();\n  const { close } = useChannelDetailContext();\n  const { channelFilesSearchSource, fileItems, groupCounts, hasResultsLoaded, sections } =\n    useChannelFilesSearch();\n\n  const groupContent = useCallback(\n    (groupIndex: number) => (\n      <ChannelFilesSectionHeader timestamp={sections[groupIndex]?.timestamp} />\n    ),\n    [sections],\n  );\n\n  // In grouped mode `index` is the flat item index (0-based, excluding group headers), so it\n  // indexes `fileItems` directly. `computeItemKey` is not passed: GroupedVirtuoso calls it with\n  // the group+item location index and no item data, so the default position keys are used.\n  const itemContent = useCallback(\n    (index: number) => <ChannelFileListItem item={fileItems[index]} />,\n    [fileItems],\n  );\n\n  const atBottomStateChange = useCallback(\n    (atBottom: boolean) => {\n      if (atBottom) channelFilesSearchSource.search();\n    },\n    [channelFilesSearchSource],\n  );\n\n  const EmptyPlaceholder = useMemo(\n    () =>\n      function ChannelFilesEmptyPlaceholder() {\n        return hasResultsLoaded ? <ChannelFilesEmptyList /> : null;\n      },\n    [hasResultsLoaded],\n  );\n\n  const Footer = useMemo(\n    () =>\n      function ChannelFilesListFooter() {\n        return (\n          <ChannelDetailListLoadingIndicator searchSource={channelFilesSearchSource} />\n        );\n      },\n    [channelFilesSearchSource],\n  );\n\n  const components = useMemo(\n    () => ({\n      EmptyPlaceholder,\n      Footer,\n      Group: ChannelFilesGroup,\n      Item: ChannelFilesItem,\n    }),\n    [EmptyPlaceholder, Footer],\n  );\n\n  return (\n    <div className='str-chat__channel-detail__files-view'>\n      <SectionNavigatorHeader\n        close={close}\n        title={t('channelDetail.channelFilesView.files.title', 'Files')}\n      />\n      <Prompt.Body className='str-chat__channel-detail__files-view__body'>\n        <GroupedVirtuoso\n          atBottomStateChange={atBottomStateChange}\n          className='str-chat__virtualized-list str-chat__channel-detail__files-view__list'\n          components={components}\n          groupContent={groupContent}\n          groupCounts={groupCounts}\n          itemContent={itemContent}\n        />\n      </Prompt.Body>\n    </div>\n  );\n};\n"],"mappings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