{"version":3,"file":"WorkspaceNavigationContext.mjs","names":[],"sources":["../../../src/context/WorkspaceNavigationContext.tsx"],"sourcesContent":["import React, { createContext, useContext } from 'react';\n\nimport type { PropsWithChildren } from 'react';\nimport type {\n  LocalMessage,\n  Channel as StreamChannel,\n  Thread as StreamThread,\n} from 'stream-chat';\n\n/**\n * Target for {@link WorkspaceNavigation.openThread}: a thread instance, or the channel and parent\n * message the thread is derived from.\n */\nexport type OpenThreadTarget =\n  | StreamThread\n  | { channel: StreamChannel; message: LocalMessage };\n\n/**\n * A DOM event that may have triggered a workspace navigation. Limited to the React event types that\n * expose modifier keys (`metaKey`, `ctrlKey`, ...), so overrides can read them without narrowing.\n */\nexport type WorkspaceNavigationTriggerEvent =\n  | React.MouseEvent\n  | React.KeyboardEvent\n  | React.TouchEvent;\n\n/** Identifies a panel of the workspace layout (for the slot layout, a slot name). */\nexport type WorkspacePanelId = string;\n\nconst WorkspacePanelContext = createContext<WorkspacePanelId | undefined>(undefined);\n\n/**\n * Declares the panel its subtree renders in, so controls inside (e.g. a header's back and close\n * buttons) act on their own panel even when the same channel or thread is open in more than one.\n */\nexport const WorkspacePanelProvider = ({\n  children,\n  panel,\n}: PropsWithChildren<{ panel?: WorkspacePanelId }>) => (\n  <WorkspacePanelContext.Provider value={panel}>\n    {children}\n  </WorkspacePanelContext.Provider>\n);\n\n/** The panel the calling component renders in, or `undefined` outside one. */\nexport const useWorkspacePanel = (): WorkspacePanelId | undefined =>\n  useContext(WorkspacePanelContext);\n\n/** Options shared by the workspace navigation operations. */\nexport type WorkspaceNavigationOptions = {\n  /**\n   * Open beside the current content (e.g. on ctrl/⌘-click) instead of replacing it, when the layout\n   * has room for a secondary panel. Ignored in single-panel layouts.\n   */\n  additive?: boolean;\n  /**\n   * The DOM event that triggered the navigation, if any. SDK components forward it from user\n   * gestures (e.g. channel list and search result selection) so an override, e.g. through\n   * `ChatView`'s `deriveWorkspaceNavigation`, can read modifier keys to decide `additive`.\n   */\n  event?: WorkspaceNavigationTriggerEvent;\n  /**\n   * Open over the current content as a transient layer instead of replacing it; dismissing the\n   * entity restores what was beneath. Ignored by layouts that do not support layering.\n   */\n  layer?: boolean;\n  /**\n   * The panel the operation is triggered from (see {@link WorkspacePanelProvider}). Picks the panel\n   * when the same channel or thread is open in more than one; without it, the first panel showing\n   * the entity is used.\n   */\n  panel?: WorkspacePanelId;\n};\n\n/**\n * Slot-agnostic, intent-level navigation that core components use instead of the ChatView slot\n * system. The `slot-layout` plugin implements it; without a provider, navigation does nothing.\n */\nexport type WorkspaceNavigation = {\n  /**\n   * Whether the panel showing the channel or thread with `key` (a channel's `cid`, a thread's ID)\n   * is stacked over other content that {@link WorkspaceNavigation.goBack} would reveal.\n   */\n  canGoBack: (key?: string, panel?: WorkspacePanelId) => boolean;\n  /**\n   * Dismiss the channel with `cid` from the workspace. A panel opened beside the main one closes\n   * whole, with anything stacked in it; the main panel only steps back one layer.\n   */\n  closeChannel: (cid?: string, options?: WorkspaceNavigationOptions) => void;\n  /**\n   * Dismiss a thread from the workspace: the panel of `threadId`, or the active thread panel when\n   * omitted. Panels close as in {@link WorkspaceNavigation.closeChannel}. Does not deactivate the\n   * thread instance; the caller owns that.\n   */\n  closeThread: (threadId?: string, options?: WorkspaceNavigationOptions) => void;\n  /**\n   * Step back from the channel or thread with `key`, revealing what it is stacked over. Does\n   * nothing when {@link WorkspaceNavigation.canGoBack} is false.\n   */\n  goBack: (key?: string, options?: WorkspaceNavigationOptions) => void;\n  /**\n   * Whether the channel with `cid` is shown in the workspace: open in a panel and not covered by\n   * something stacked over it there.\n   */\n  isChannelActive: (cid?: string) => boolean;\n  /**\n   * Whether the channel panel for `cid` can be dismissed by the user: it is a secondary panel\n   * opened beside another channel, not the primary channel surface.\n   */\n  isChannelDismissable: (cid?: string, panel?: WorkspacePanelId) => boolean;\n  /**\n   * Whether the thread with `threadId` is shown in the workspace: open in a panel and not covered\n   * by something stacked over it there.\n   */\n  isThreadActive: (threadId?: string) => boolean;\n  /**\n   * Whether the thread panel for `threadId` (or the active thread) can be dismissed by the user:\n   * it is a side or secondary panel, not the primary thread surface.\n   */\n  isThreadDismissable: (threadId?: string, panel?: WorkspacePanelId) => boolean;\n  /** Whether the threads view is the active workspace view. */\n  isThreadsView: boolean;\n  /** Open `channel` in the workspace. */\n  openChannel: (channel: StreamChannel, options?: WorkspaceNavigationOptions) => void;\n  /** Channels open in the workspace. */\n  openChannels: StreamChannel[];\n  /** Open a thread, given as an instance or as `{ channel, message }`. */\n  openThread: (target: OpenThreadTarget, options?: WorkspaceNavigationOptions) => void;\n  /** Threads open in the workspace. */\n  openThreads: StreamThread[];\n};\n\nconst NOOP_OPEN_CHANNELS: WorkspaceNavigation['openChannels'] = [];\nconst NOOP_OPEN_THREADS: WorkspaceNavigation['openThreads'] = [];\n\n/** Inert navigation: every operation does nothing and no channels or threads are open. */\nexport const defaultWorkspaceNavigation: WorkspaceNavigation = {\n  canGoBack: () => false,\n  closeChannel: () => undefined,\n  closeThread: () => undefined,\n  goBack: () => undefined,\n  isChannelActive: () => false,\n  isChannelDismissable: () => false,\n  isThreadActive: () => false,\n  isThreadDismissable: () => false,\n  isThreadsView: false,\n  openChannel: () => undefined,\n  openChannels: NOOP_OPEN_CHANNELS,\n  openThread: () => undefined,\n  openThreads: NOOP_OPEN_THREADS,\n};\n\ntype WorkspaceNavigationAction =\n  | 'closeChannel'\n  | 'closeThread'\n  | 'goBack'\n  | 'openChannel'\n  | 'openThread';\n\nconst warnedUnprovidedActions = new Set<WorkspaceNavigationAction>();\n\nconst warnUnprovided = (action: WorkspaceNavigationAction) => {\n  if (warnedUnprovidedActions.has(action)) return;\n  warnedUnprovidedActions.add(action);\n  console.warn(\n    `stream-chat-react: WorkspaceNavigation.${action}() did nothing because no workspace navigation is provided. ` +\n      'Render the component inside ChatView (stream-chat-react/slot-layout), or provide your own navigation with WorkspaceNavigationProvider.',\n  );\n};\n\n// The context default: `defaultWorkspaceNavigation` with operations that warn once each, since a\n// click that silently does nothing is hard to trace to a missing `ChatView`. The exported default\n// stays silent because integrators spread it into their own provider value.\nconst unprovidedWorkspaceNavigation: WorkspaceNavigation = {\n  ...defaultWorkspaceNavigation,\n  closeChannel: () => warnUnprovided('closeChannel'),\n  closeThread: () => warnUnprovided('closeThread'),\n  goBack: () => warnUnprovided('goBack'),\n  openChannel: () => warnUnprovided('openChannel'),\n  openThread: () => warnUnprovided('openThread'),\n};\n\nconst WorkspaceNavigationContext = createContext<WorkspaceNavigation>(\n  unprovidedWorkspaceNavigation,\n);\n\nexport const WorkspaceNavigationProvider = ({\n  children,\n  value,\n}: PropsWithChildren<{ value: WorkspaceNavigation }>) => (\n  <WorkspaceNavigationContext.Provider value={value}>\n    {children}\n  </WorkspaceNavigationContext.Provider>\n);\n\n/**\n * Returns the workspace navigation. Outside a provider the navigation actions do nothing and each\n * logs a console warning once, so callers can navigate without guarding for a provider.\n */\nexport const useWorkspaceNavigation = (): WorkspaceNavigation =>\n  useContext(WorkspaceNavigationContext);\n"],"mappings":";;;AA6BA,IAAM,wBAAwB,cAA4C,KAAA,CAAS;;;;;AAMnF,IAAa,0BAA0B,EACrC,UACA,YAEA,oBAAC,sBAAsB,UAAvB;CAAgC,OAAO;CACpC;AAC6B,CAAA;;AAIlC,IAAa,0BACX,WAAW,qBAAqB;;AA0FlC,IAAa,6BAAkD;CAC7D,iBAAiB;CACjB,oBAAoB,KAAA;CACpB,mBAAmB,KAAA;CACnB,cAAc,KAAA;CACd,uBAAuB;CACvB,4BAA4B;CAC5B,sBAAsB;CACtB,2BAA2B;CAC3B,eAAe;CACf,mBAAmB,KAAA;CACnB,cAAc,CAAA;CACd,kBAAkB,KAAA;CAClB,aAAa,CAAA;AACf;AASA,IAAM,0CAA0B,IAAI,IAA+B;AAEnE,IAAM,kBAAkB,WAAsC;CAC5D,IAAI,wBAAwB,IAAI,MAAM,GAAG;CACzC,wBAAwB,IAAI,MAAM;CAClC,QAAQ,KACN,0CAA0C,OAAO,mMAEnD;AACF;AAcA,IAAM,6BAA6B,cACjC;CATA,GAAG;CACH,oBAAoB,eAAe,cAAc;CACjD,mBAAmB,eAAe,aAAa;CAC/C,cAAc,eAAe,QAAQ;CACrC,mBAAmB,eAAe,aAAa;CAC/C,kBAAkB,eAAe,YAAY;AAI7C,CACF;AAEA,IAAa,+BAA+B,EAC1C,UACA,YAEA,oBAAC,2BAA2B,UAA5B;CAA4C;CACzC;AACkC,CAAA;;;;;AAOvC,IAAa,+BACX,WAAW,0BAA0B"}