{"version":3,"file":"PinnedMessagesView.mjs","names":[],"sources":["../../../../../../src/plugins/ChannelDetail/Views/PinnedMessagesView/PinnedMessagesView.tsx"],"sourcesContent":["import type { LocalMessage, MessageResponse, MessageSearchSource } from 'stream-chat';\nimport React, { useCallback, useMemo } from 'react';\n\nimport {\n  useComponentContext,\n  useTranslationContext,\n  useWorkspaceNavigation,\n} from '../../../../context';\nimport { getDateString } from '../../../../i18n/utils';\nimport { Avatar as DefaultAvatar } from '../../../../components/Avatar';\nimport { extractDisplayInfo as defaultExtractDisplayInfo } from '../../../../components/Avatar/utils';\nimport { ListItemLayout } from '../../../../components/ListItemLayout';\nimport { VirtualizedList } from '../../VirtualizedList';\nimport { Prompt } from '../../../../components/Dialog';\nimport {\n  SectionNavigatorHeader,\n  type SectionNavigatorSectionContentProps,\n} from '../../SectionNavigator';\nimport { ChannelDetailSearchInput } from '../../ChannelDetailSearchInput';\nimport { getUserDisplayName } from '../ChannelMembersView/ChannelMembersView.utils';\nimport { ChannelDetailListLoadingIndicator } from '../../ChannelDetailListLoadingIndicator';\nimport { PinnedMessagesEmptyList } from './PinnedMessagesEmptyList';\nimport { usePinnedMessagesSearch } from './usePinnedMessagesSearch';\nimport { useChannelDetailContext } from '../../ChannelDetailContext';\nimport { ChannelDetailEmptyList } from '../../ChannelDetailEmptyList';\nimport { convertTimestampToDate } from 'stream-chat';\n\ntype PinnedMessage = MessageResponse | LocalMessage;\n\nconst computeItemKey = (_: number, message: PinnedMessage) => message.id;\n\n/**\n * A wire timestamp as an ISO string, for `getDateString` and the `dateTime` attribute. A\n * nanosecond value is out of `Date`'s range, so it goes through `convertTimestampToDate`.\n */\nconst normalizeTimestamp = (timestamp: PinnedMessage['created_at']) =>\n  timestamp == null ? undefined : convertTimestampToDate(timestamp)?.toISOString();\n\nconst getPinnedMessagePreview = (\n  message: PinnedMessage,\n  t: ReturnType<typeof useTranslationContext>['t'],\n) => {\n  const text = message.text?.trim();\n  if (text) return text;\n\n  const attachment = message.attachments?.[0];\n  const attachmentPreview =\n    attachment?.title || attachment?.text || attachment?.fallback || attachment?.type;\n\n  return (\n    attachmentPreview ||\n    t('channelDetail.pinnedMessagesView.pinnedMessage.label', 'Pinned message')\n  );\n};\n\nconst PinnedMessageDate = ({ message }: { message: PinnedMessage }) => {\n  const { t, tDateTimeParser } = useTranslationContext();\n  const normalizedTimestamp = normalizeTimestamp(message.created_at);\n\n  const when = useMemo(\n    () =>\n      getDateString({\n        messageCreatedAt: normalizedTimestamp,\n        t,\n        tDateTimeParser,\n        timestampTranslationKey: 'timestamp.ChannelDetailPinnedMessageTimestamp',\n      }),\n    [normalizedTimestamp, t, tDateTimeParser],\n  );\n\n  if (!when) return null;\n\n  return (\n    <time\n      className='str-chat__channel-detail__pinned-messages-view__list-item__date'\n      dateTime={normalizedTimestamp}\n    >\n      {when}\n    </time>\n  );\n};\n\nconst PinnedMessagesViewItem = ({\n  message,\n  onSelect,\n}: {\n  message: PinnedMessage;\n  onSelect: (message: PinnedMessage) => void;\n}) => {\n  const { t } = useTranslationContext();\n  const { Avatar = DefaultAvatar, extractDisplayInfo = defaultExtractDisplayInfo } =\n    useComponentContext();\n  const displayName = getUserDisplayName(message.user ?? undefined);\n\n  const LeadingSlot = useMemo(\n    () =>\n      function MessageAuthorAvatar() {\n        const displayInfo = extractDisplayInfo({ user: message.user ?? undefined });\n\n        return <Avatar {...displayInfo} size='md' />;\n      },\n    [Avatar, extractDisplayInfo, message.user],\n  );\n\n  const TrailingSlot = useMemo(\n    () =>\n      function MessageDate() {\n        return <PinnedMessageDate message={message} />;\n      },\n    [message],\n  );\n\n  const rootProps = useMemo(\n    () => ({\n      className: 'str-chat__channel-detail__pinned-messages-view__list-item',\n      onClick: () => onSelect(message),\n    }),\n    [message, onSelect],\n  );\n\n  return (\n    <ListItemLayout\n      LeadingSlot={LeadingSlot}\n      RootElement='button'\n      rootProps={rootProps}\n      subtitle={getPinnedMessagePreview(message, t)}\n      subtitleClassName='str-chat__channel-detail__pinned-messages-view__list-item__message-preview'\n      title={displayName}\n      TrailingSlot={TrailingSlot}\n    />\n  );\n};\n\nexport type PinnedMessagesViewProps = SectionNavigatorSectionContentProps & {\n  /** Custom message search source for pinned messages. */\n  searchSource?: MessageSearchSource;\n};\n\nexport const PinnedMessagesView: React.ComponentType<PinnedMessagesViewProps> = ({\n  searchSource,\n}) => {\n  const { openChannel } = useWorkspaceNavigation();\n  const { t } = useTranslationContext();\n  const { channel, close } = useChannelDetailContext();\n  const {\n    displayedMessages,\n    handleSearchChange,\n    hasPinnedMessages,\n    hasSearchResultsLoaded,\n    pinnedMessagesSearchSource,\n  } = usePinnedMessagesSearch({ searchSource });\n\n  const handleSelectMessage = useCallback(\n    (message: PinnedMessage) => {\n      openChannel(channel);\n      void channel.messagePaginator.jumpToMessage(message.id);\n      close?.();\n    },\n    [channel, close, openChannel],\n  );\n\n  const renderItem = useCallback(\n    (_: number, message: PinnedMessage) => (\n      <PinnedMessagesViewItem message={message} onSelect={handleSelectMessage} />\n    ),\n    [handleSelectMessage],\n  );\n\n  const EmptyPlaceholder = useMemo(\n    () =>\n      function PinnedMessagesEmptyPlaceholder() {\n        if (!hasPinnedMessages) return <PinnedMessagesEmptyList />;\n        if (hasSearchResultsLoaded)\n          return (\n            <ChannelDetailEmptyList>\n              {t(\n                'channelDetail.pinnedMessagesView.noMessagesFound.text',\n                'No messages found',\n              )}\n            </ChannelDetailEmptyList>\n          );\n        return null;\n      },\n    [hasPinnedMessages, hasSearchResultsLoaded, t],\n  );\n\n  const Footer = useMemo(\n    () =>\n      function PinnedMessagesListFooter() {\n        return (\n          <ChannelDetailListLoadingIndicator searchSource={pinnedMessagesSearchSource} />\n        );\n      },\n    [pinnedMessagesSearchSource],\n  );\n\n  return (\n    <div className='str-chat__channel-detail__pinned-messages-view'>\n      <SectionNavigatorHeader\n        close={close}\n        description={t(\n          'channelDetail.pinnedMessagesView.browsePinnedMessages.description',\n          'Browse pinned messages',\n        )}\n        title={t(\n          'channelDetail.pinnedMessagesView.pinnedMessages.title',\n          'Pinned messages',\n        )}\n      />\n      <Prompt.Body className='str-chat__channel-detail__pinned-messages-view__body'>\n        {hasPinnedMessages && (\n          <ChannelDetailSearchInput onSearchChange={handleSearchChange} />\n        )}\n        <VirtualizedList\n          className='str-chat__channel-detail__pinned-messages-view__list'\n          computeItemKey={computeItemKey}\n          data={displayedMessages}\n          EmptyPlaceholder={EmptyPlaceholder}\n          Footer={Footer}\n          itemContent={renderItem}\n          loadNext={hasPinnedMessages ? pinnedMessagesSearchSource.search : undefined}\n        />\n      </Prompt.Body>\n    </div>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA6BA,IAAM,kBAAkB,GAAW,YAA2B,QAAQ;;;;;AAMtE,IAAM,sBAAsB,cAC1B,aAAa,OAAO,KAAA,IAAY,uBAAuB,SAAS,CAAC,EAAE,YAAY;AAEjF,IAAM,2BACJ,SACA,MACG;CACH,MAAM,OAAO,QAAQ,MAAM,KAAK;CAChC,IAAI,MAAM,OAAO;CAEjB,MAAM,aAAa,QAAQ,cAAc;CAIzC,OAFE,YAAY,SAAS,YAAY,QAAQ,YAAY,YAAY,YAAY,QAI7E,EAAE,wDAAwD,gBAAgB;AAE9E;AAEA,IAAM,qBAAqB,EAAE,cAA0C;CACrE,MAAM,EAAE,GAAG,oBAAoB,sBAAsB;CACrD,MAAM,sBAAsB,mBAAmB,QAAQ,UAAU;CAEjE,MAAM,OAAO,cAET,cAAc;EACZ,kBAAkB;EAClB;EACA;EACA,yBAAyB;CAC3B,CAAC,GACH;EAAC;EAAqB;EAAG;CAAe,CAC1C;CAEA,IAAI,CAAC,MAAM,OAAO;CAElB,OACE,oBAAC,QAAD;EACE,WAAU;EACV,UAAU;YAET;CACG,CAAA;AAEV;AAEA,IAAM,0BAA0B,EAC9B,SACA,eAII;CACJ,MAAM,EAAE,MAAM,sBAAsB;CACpC,MAAM,EAAE,QAAA,WAAS,QAAe,oBAAA,uBAAqB,uBACnD,oBAAoB;CACtB,MAA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