{"version":3,"file":"ChannelMembersHeaderActions.defaults.mjs","names":[],"sources":["../../../../../../src/plugins/ChannelDetail/Views/ChannelMembersView/ChannelMembersHeaderActions.defaults.tsx"],"sourcesContent":["import type { Channel } from 'stream-chat';\nimport React, {\n  type ComponentPropsWithoutRef,\n  forwardRef,\n  useMemo,\n  useState,\n} from 'react';\n\nimport {\n  useComponentContext,\n  useComponentContextIcons,\n  useTranslationContext,\n} from '../../../../context';\nimport { Button } from '../../../../components/Button';\nimport { useSplitActionSet } from '../../../../components/Chat/hooks/useSplitActionSet';\nimport {\n  ContextMenu,\n  ContextMenuButton,\n  useContextMenuContext,\n  useDialogIsOpen,\n  useDialogOnNearestManager,\n} from '../../../../components/Dialog';\nimport { useStableId } from '../../../../components/UtilityComponents/useStableId';\nimport { useChannelDetailContext } from '../../ChannelDetailContext';\nimport { canUpdateChannelMembers } from './ChannelMembersView.utils';\nimport type {\n  ChannelMembersHeaderActionsProps,\n  ChannelMembersModeController,\n} from './ChannelMembersView';\n\nexport type ChannelMembersHeaderActionType = 'addMembers' | (string & {});\n\nexport type ChannelMembersHeaderActionComponentProps = {\n  modeController: ChannelMembersModeController;\n};\n\n/** What a header action's `isVisible` rule is checked against. */\nexport type ChannelMembersHeaderActionVisibilityFilterParams = {\n  /** The channel whose members are shown, e.g. to read `channel.data.own_capabilities`. */\n  channel: Channel;\n};\n\ntype BaseChannelMembersHeaderActionItem = {\n  Component: React.ComponentType<ChannelMembersHeaderActionComponentProps>;\n  /**\n   * Hides the action for a channel, e.g. an app-defined action the user lacks the capability for.\n   * Cannot show an action the base filter removes (`addMembers` without `update-channel-members`).\n   * Shown when omitted.\n   */\n  isVisible?: (state: ChannelMembersHeaderActionVisibilityFilterParams) => boolean;\n  type: ChannelMembersHeaderActionType;\n};\n\n/** Rendered inline in the members header. */\nexport type QuickChannelMembersHeaderActionItem = BaseChannelMembersHeaderActionItem & {\n  placement: 'quick';\n};\n\n/**\n * Rendered inside the actions dropdown menu, so the component can close the menu with `closeMenu`\n * from `useContextMenuContext()`.\n */\nexport type DropdownChannelMembersHeaderActionItem =\n  BaseChannelMembersHeaderActionItem & {\n    placement: 'dropdown';\n  };\n\n/**\n * The button that opens the actions dropdown menu. Rendered only while at least one `dropdown` item\n * passes the filter. It receives the ref the menu anchors to, `aria-expanded` and the `onClick`\n * that toggles the menu; a custom toggle is a `forwardRef` component (so the ref reaches its button\n * on every supported React version) that spreads the props onto its button. A toggle that ignores\n * the props never opens the menu.\n */\nexport type QuickDropdownToggleChannelMembersHeaderActionItem = {\n  Component: React.ForwardRefExoticComponent<\n    ComponentPropsWithoutRef<'button'> & React.RefAttributes<HTMLButtonElement>\n  >;\n  placement: 'quick-dropdown-toggle';\n};\n\nexport type ChannelMembersHeaderActionItem =\n  | QuickChannelMembersHeaderActionItem\n  | DropdownChannelMembersHeaderActionItem\n  | QuickDropdownToggleChannelMembersHeaderActionItem;\n\n/**\n * Where a header action renders: inline (`quick`), inside the actions dropdown menu (`dropdown`),\n * or as the button that opens that menu (`quick-dropdown-toggle`).\n */\nexport type ChannelMembersHeaderActionPlacement =\n  ChannelMembersHeaderActionItem['placement'];\n\n/**\n * Drops header actions the user cannot use: `addMembers` requires `update-channel-members`. An\n * action that rule allows still shows only while its own `isVisible` holds. The dropdown toggle is\n * always kept. Applied by {@link DefaultHeaderActions}.\n *\n * @param channelMembersHeaderActionSet - Actions to filter.\n * @returns The actions to show.\n */\nexport const useBaseChannelMembersHeaderActionSetFilter = (\n  channelMembersHeaderActionSet: ChannelMembersHeaderActionItem[],\n) => {\n  const { channel } = useChannelDetailContext();\n  const canManageChannelMembers = canUpdateChannelMembers(channel);\n\n  return useMemo(() => {\n    const visibilityFilterParams: ChannelMembersHeaderActionVisibilityFilterParams = {\n      channel,\n    };\n\n    const passesBaseRule = (action: ChannelMembersHeaderActionItem) =>\n      action.placement === 'quick-dropdown-toggle' ||\n      action.type !== 'addMembers' ||\n      canManageChannelMembers;\n\n    const passesOwnRule = (action: ChannelMembersHeaderActionItem) =>\n      action.placement === 'quick-dropdown-toggle' ||\n      !action.isVisible ||\n      action.isVisible(visibilityFilterParams);\n\n    return channelMembersHeaderActionSet.filter(\n      (action) => passesBaseRule(action) && passesOwnRule(action),\n    );\n  }, [canManageChannelMembers, channel, channelMembersHeaderActionSet]);\n};\n\nconst AddMembersHeaderAction = ({\n  modeController,\n}: ChannelMembersHeaderActionComponentProps) => {\n  const { t } = useTranslationContext();\n\n  if (modeController.mode !== 'browse') return null;\n\n  return (\n    <Button\n      appearance='outline'\n      aria-label={t(\n        'channelDetail.channelMembersHeader.addChannelMembers.ariaLabel',\n        'Add channel members',\n      )}\n      className='str-chat__channel-detail__channel-members-view__add-button'\n      onClick={() => modeController.setMode('add')}\n      size='md'\n      variant='secondary'\n    >\n      {t('channelDetail.channelMembersHeader.add.text', 'Add')}\n    </Button>\n  );\n};\n\nconst AddMembersMenuAction = ({\n  modeController,\n}: ChannelMembersHeaderActionComponentProps) => {\n  const { IconUserAdd } = useComponentContextIcons();\n  const { t } = useTranslationContext();\n  const { closeMenu } = useContextMenuContext();\n\n  if (modeController.mode !== 'browse') return null;\n\n  return (\n    <ContextMenuButton\n      aria-label={t(\n        'channelDetail.channelMembersHeader.addChannelMembers.ariaLabel',\n        'Add channel members',\n      )}\n      Icon={IconUserAdd}\n      onClick={() => {\n        modeController.setMode('add');\n        closeMenu();\n      }}\n    >\n      {t('channelDetail.channelMembersHeader.add.text', 'Add')}\n    </ContextMenuButton>\n  );\n};\n\nconst QuickDropdownToggle = forwardRef<\n  HTMLButtonElement,\n  ComponentPropsWithoutRef<'button'>\n>(function QuickDropdownToggle(props, ref) {\n  const { t } = useTranslationContext();\n\n  return (\n    <Button\n      appearance='outline'\n      aria-label={t(\n        'channelDetail.channelMembersHeader.openMembersActions.ariaLabel',\n        'Open members actions',\n      )}\n      className='str-chat__channel-detail__channel-members-view__actions-button'\n      ref={ref}\n      size='md'\n      variant='secondary'\n      {...props}\n    >\n      {t('channelDetail.channelMembersHeader.actions.text', 'Actions')}\n    </Button>\n  );\n});\n\nexport const DefaultChannelMembersHeaderActions = {\n  AddMembers: AddMembersHeaderAction,\n  AddMembersMenu: AddMembersMenuAction,\n  QuickDropdownToggle,\n};\n\nexport const defaultChannelMembersHeaderActionSet: ChannelMembersHeaderActionItem[] = [\n  {\n    Component: DefaultChannelMembersHeaderActions.QuickDropdownToggle,\n    placement: 'quick-dropdown-toggle',\n  },\n  {\n    Component: DefaultChannelMembersHeaderActions.AddMembers,\n    placement: 'quick',\n    type: 'addMembers',\n  },\n];\n\n/**\n * Renders the `quick` actions inline, followed by the `quick-dropdown-toggle` item and the\n * `dropdown` actions in the menu it opens. Renders nothing when no quick or dropdown action passes\n * {@link useBaseChannelMembersHeaderActionSetFilter}. Each instance opens its own menu dialog, so\n * two members views under one dialog manager never open each other's menu.\n */\nexport const DefaultHeaderActions = ({\n  headerActionSet,\n  modeController,\n}: ChannelMembersHeaderActionsProps) => {\n  const { ContextMenu: ContextMenuComponent = ContextMenu } = useComponentContext();\n  const actions = useBaseChannelMembersHeaderActionSetFilter(headerActionSet);\n  const { dropdownActionSet, quickActionSet, quickDropdownToggleAction } =\n    useSplitActionSet(actions);\n  const [referenceElement, setReferenceElement] = useState<HTMLButtonElement | null>(\n    null,\n  );\n  const instanceId = useStableId();\n  const dropdownDialogId = `channel-members-header-actions-${instanceId}`;\n  const { dialog, dialogManager } = useDialogOnNearestManager({ id: dropdownDialogId });\n  const dialogIsOpen = useDialogIsOpen(dropdownDialogId, dialogManager?.id);\n\n  if (quickActionSet.length + dropdownActionSet.length === 0) return null;\n\n  return (\n    <div className='str-chat__channel-detail__channel-members-view__header-actions'>\n      {quickActionSet.map(({ Component, type }) => (\n        <Component key={type} modeController={modeController} />\n      ))}\n\n      {quickDropdownToggleAction && dropdownActionSet.length > 0 && (\n        <>\n          <quickDropdownToggleAction.Component\n            aria-expanded={dialogIsOpen}\n            onClick={() => dialog.toggle()}\n            ref={setReferenceElement}\n          />\n          <ContextMenuComponent\n            aria-label='Members actions'\n            className='str-chat__channel-detail__channel-members-view__header-actions-menu'\n            dialogManagerId={dialogManager?.id}\n            id={dialog.id}\n            onClose={() => dialog.close()}\n            placement='bottom-start'\n            referenceElement={referenceElement}\n            tabIndex={-1}\n            trapFocus\n          >\n            {dropdownActionSet.map(({ Component, type }) => (\n              <Component key={type} modeController={modeController} />\n            ))}\n          </ContextMenuComponent>\n        </>\n      )}\n    </div>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAqGA,IAAa,8CACX,kCACG;CACH,MAAM,EAAE,YAAY,wBAAwB;CAC5C,MAAM,0BAA0B,wBAAwB,OAAO;CAE/D,OAAO,cAAc;EACnB,MAAM,yBAA2E,EAC/E,QACF;EAEA,MAAM,kBAAkB,WACtB,OAAO,cAAc,2BACrB,OAAO,SAAS,gBAChB;EAEF,MAAM,iBAAiB,WACrB,OAAO,cAAc,2BACrB,CAAC,OAAO,aACR,OAAO,UAAU,sBAAsB;EAEzC,OAAO,8BAA8B,QAClC,WAAW,eAAe,MAAM,KAAK,cAAc,MAAM,CAC5D;CACF,GAAG;EAAC;EAAyB;EAAS;CAA6B,CAAC;AACtE;AAEA,IAAM,0BAA0B,EAC9B,qBAC8C;CAC9C,MAAM,EAAE,MAAM,sBAAsB;CAEpC,IAAI,eAAe,SAAS,UAAU,OAAO;CAE7C,OACE,oBAAC,QAAD;EACE,YAAW;EACX,cAAY,EACV,kEACA,qBACF;EACA,WAAU;EACV,eAAe,eAAe,QAAQ,KAAK;EAC3C,MAAK;EACL,SAAQ;YAEP,EAAE,+CAA+C,KAAK;CACjD,CAAA;AAEZ;AAEA,IAAM,wBAAwB,EAC5B,qBAC8C;CAC9C,MAAM,EAAE,gBAAgB,yBAAyB;CACjD,MAAM,EAAE,MAAM,sBAAsB;CACpC,MAAM,EAAE,cAAc,sBAAsB;CAE5C,IAAI,eAAe,SAAS,UAAU,OAAO;CAE7C,OACE,oBAAC,mBAAD;EACE,cAAY,EACV,kEACA,qBACF;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