{"version":3,"file":"utils.a11y.mjs","names":[],"sources":["../../../../src/components/ChannelListItem/utils.a11y.ts"],"sourcesContent":["import type { Channel, LocalMessage, StreamChat } from 'stream-chat';\n\nimport type { ChatContextValue } from '../../context';\nimport type { TranslationContextValue } from '../../context/TranslationContext';\nimport {\n  type AccessibleLabelConfig,\n  type AccessibleLabelPart,\n  activeLabelPart,\n  composeAccessibleLabel,\n  unreadCountLabelPart,\n} from '../../a11y/accessibleLabel';\nimport { getDateString } from '../../i18n/utils';\nimport { MessageDeliveryStatus } from './hooks/useMessageDeliveryStatus';\nimport { getLatestMessagePreviewText } from './utils';\nimport { convertTimestampToDate } from 'stream-chat';\n\n/** Data `ChannelListItemUI` gathers from its props and contexts for each label part. */\nexport type ChannelListItemLabelData = {\n  /** Whether the row is the active (currently open) channel. */\n  active?: boolean;\n  /** The channel whose latest message is summarized. */\n  channel: Channel;\n  /** Client, used to detect whether the latest message is the current user's own. */\n  client: StreamChat;\n  /** Channel display name. */\n  displayTitle?: string;\n  isMessageAIGenerated?: ChatContextValue['isMessageAIGenerated'];\n  /**\n   * The channel's latest message. When omitted, {@link composeChannelListItemAccessibleLabel} uses\n   * the `latestMessage` of `channel.messagePaginator.aggregateState`, so every part describes the\n   * same message.\n   */\n  latestMessage?: LocalMessage;\n  /** Delivery/read status of the latest message when it is the current user's own. */\n  messageDeliveryStatus?: MessageDeliveryStatus;\n  t: TranslationContextValue['t'];\n  tDateTimeParser: TranslationContextValue['tDateTimeParser'];\n  /** Unread count for the channel. */\n  unreadCount?: number;\n  userLanguage?: TranslationContextValue['userLanguage'];\n};\n\n/** Produces one segment of the accessible name; return `undefined`/empty to omit it. */\nexport type ChannelListItemLabelPart = AccessibleLabelPart<ChannelListItemLabelData>;\n\n/**\n * The default label parts, each overridable via {@link ChannelListItemLabelConfig.parts}. The\n * active state is announced by a part because `aria-selected` alone is not reliably spoken for an\n * option with a custom name.\n */\nexport const defaultChannelListItemLabelParts = {\n  active: activeLabelPart,\n  // Link previews are excluded (the `linkPreview` part announces those). A single attachment with\n  // no text is skipped because the `lastMessage` part already announces its type.\n  attachments: ({ latestMessage, t }) => {\n    const count =\n      latestMessage?.attachments?.filter((attachment) => !attachment.og_scrape_url)\n        .length ?? 0;\n    if (count === 0 || (count === 1 && !latestMessage?.text)) return undefined;\n    return t('channelListItem.attachmentCount.ariaLabel', {\n      count,\n      defaultValue_one: '{{ count }} attachment',\n      defaultValue_other: '{{ count }} attachments',\n    });\n  },\n  deliveryStatus: ({ messageDeliveryStatus, t }) => {\n    if (!messageDeliveryStatus) return undefined;\n    const statusLabelByStatus: Record<MessageDeliveryStatus, string> = {\n      [MessageDeliveryStatus.SENT]: t('channelListItem.sent.ariaLabel', 'Sent'),\n      [MessageDeliveryStatus.DELIVERED]: t(\n        'channelListItem.delivered.ariaLabel',\n        'Delivered',\n      ),\n      [MessageDeliveryStatus.READ]: t('channelListItem.read.ariaLabel', 'Read'),\n    };\n    return t(\n      'channelListItem.deliveryStatus.ariaLabel',\n      'Delivery status: {{ deliveryStatus }}',\n      {\n        deliveryStatus: statusLabelByStatus[messageDeliveryStatus],\n      },\n    );\n  },\n  lastMessage: ({\n    channel,\n    client,\n    isMessageAIGenerated,\n    latestMessage,\n    t,\n    userLanguage,\n  }) => {\n    const preview = getLatestMessagePreviewText(\n      channel,\n      t,\n      userLanguage,\n      isMessageAIGenerated,\n      // Pre-resolved by the composer, so the preview describes the same message as the other parts.\n      latestMessage,\n    );\n    // Empty channel: there is no sender to name.\n    if (!latestMessage) return preview;\n    const isOwn = !!client.userID && latestMessage.user?.id === client.userID;\n    // \"You\" for the current user; an unknown name is omitted rather than reading out a user ID.\n    const sender = isOwn ? t('common.you.label', 'You') : latestMessage.user?.name;\n    return sender\n      ? t(\n          'channelListItem.lastMessage.withSenderAndMessagePreview.ariaLabel',\n          'Last message from {{ sender }}: {{ messagePreview }}',\n          {\n            messagePreview: preview,\n            sender,\n          },\n        )\n      : t(\n          'channelListItem.lastMessage.withMessagePreview.ariaLabel',\n          'Last message: {{ messagePreview }}',\n          { messagePreview: preview },\n        );\n  },\n  linkPreview: ({ latestMessage, t }) => {\n    const link = latestMessage?.attachments?.find(\n      (attachment) => attachment.og_scrape_url,\n    );\n    if (!link) return undefined;\n    return link.title\n      ? t(\n          'channelListItem.sharedLinkTitle.ariaLabel',\n          'Shared a link with title: {{ linkTitle }}',\n          { linkTitle: link.title },\n        )\n      : t('channelListItem.sharedLink.ariaLabel', 'Shared a link');\n  },\n  name: ({ displayTitle }) => displayTitle || undefined,\n  time: ({ latestMessage, t, tDateTimeParser }) => {\n    const createdAt = latestMessage?.created_at;\n    if (createdAt == null) return undefined;\n    const when = getDateString({\n      messageCreatedAt: convertTimestampToDate(createdAt)?.toISOString(),\n      t,\n      tDateTimeParser,\n      timestampTranslationKey: 'timestamp.ChannelPreviewTimestamp',\n    });\n    // The prefix keeps the time from being read as part of the delivery status.\n    return when\n      ? t('common.lastActivity.ariaLabel', 'Last activity: {{ time }}', {\n          time: String(when),\n        })\n      : undefined;\n  },\n  unreadCount: unreadCountLabelPart,\n} satisfies Record<string, ChannelListItemLabelPart>;\n\n/** Default order of the parts in the composed label. */\nexport const DEFAULT_CHANNEL_LIST_ITEM_LABEL_ORDER = [\n  'name',\n  'active',\n  'unreadCount',\n  'lastMessage',\n  'attachments',\n  'linkPreview',\n  'deliveryStatus',\n  'time',\n] as const;\n\nexport type ChannelListItemLabelConfig = AccessibleLabelConfig<ChannelListItemLabelData>;\n\n/**\n * Composes a channel list row's accessible name from named parts, by default in the order of\n * {@link DEFAULT_CHANNEL_LIST_ITEM_LABEL_ORDER}.\n *\n * `ChannelListItemUI` calls this with its `accessibleLabelConfig` prop. To customize the label,\n * supply your own `ChannelListItemUI` through `ComponentContext` and forward the config:\n * {@link ChannelListItemLabelConfig.parts} is merged over {@link defaultChannelListItemLabelParts}\n * (a new key is added, a same-named key replaces the default), and\n * {@link ChannelListItemLabelConfig.order} decides which parts render and in what sequence, so a\n * part missing from it is never rendered. A part returns its segment, or `undefined`/`''` to omit\n * it. Set {@link ChannelListItemLabelConfig.build} to return the whole string instead.\n *\n * Parts receive {@link ChannelListItemLabelData}; anything else (mute or pin state, `frozen`,\n * custom `channel.data`) is read off `channel`. `t` accepts only {@link TranslationKey}s, so wrap\n * your own keys in {@link asDynamicKey} and pass the English copy as the second argument.\n *\n * ```tsx\n * const accessibleLabelConfig: ChannelListItemLabelConfig = {\n *   parts: {\n *     pinned: ({ channel, t }) =>\n *       channel.state.membership?.pinned_at\n *         ? t(asDynamicKey('myApp.channelList.pinned'), 'Pinned')\n *         : undefined,\n *   },\n *   order: ['name', 'pinned', ...DEFAULT_CHANNEL_LIST_ITEM_LABEL_ORDER.slice(1)],\n * };\n * // \"General. Pinned. 3 unread messages. Last message from Alice: hi. 2 days ago\"\n * ```\n *\n * @param data - Data for the label parts.\n * @param config - Overrides for the parts, their order, the separator, or the whole label.\n * @returns The non-empty segments joined by the separator, or the result of `config.build`.\n */\nexport const composeChannelListItemAccessibleLabel = (\n  data: ChannelListItemLabelData,\n  config: ChannelListItemLabelConfig = {},\n): string =>\n  composeAccessibleLabel(\n    // Resolved once so every part describes the same message.\n    {\n      ...data,\n      latestMessage:\n        data.latestMessage ??\n        data.channel.messagePaginator.aggregateState.getLatestValue().latestMessage ??\n        undefined,\n    },\n    defaultChannelListItemLabelParts,\n    DEFAULT_CHANNEL_LIST_ITEM_LABEL_ORDER,\n    config,\n  );\n"],"mappings":";;;;;;;;;;;AAkDA,IAAa,mCAAmC;CAC9C,QAAQ;CAGR,cAAc,EAAE,eAAe,QAAQ;EACrC,MAAM,QACJ,eAAe,aAAa,QAAQ,eAAe,CAAC,WAAW,aAAa,CAAC,CAC1E,UAAU;EACf,IAAI,UAAU,KAAM,UAAU,KAAK,CAAC,eAAe,MAAO,OAAO,KAAA;EACjE,OAAO,EAAE,6CAA6C;GACpD;GACA,kBAAkB;GAClB,oBAAoB;EACtB,CAAC;CACH;CACA,iBAAiB,EAAE,uBAAuB,QAAQ;EA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