# All files / src/components/streams StreamDetailPanel.tsx

82.35% Statements 14/17
94.11% Branches 16/17
77.77% Functions 7/9
82.35% Lines 14/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175

    5x

    25x
    25x

    25x

    25x

    15x

    15x
    15x

    15x
    15x
    10x

    15x

    25x

    25x

     /**
     * StreamDetailPanel — right pull-out drawer showing stream metadata.
     *
     * Renders as an absolutely-positioned overlay anchored to the right of its
     * containing block (which must have `position: relative`).
     *
     * Read-only: participants link to their sessions; low-level wiring (fds, full
     * GUIDs, permission/delivery mode) is tucked behind an "Advanced" disclosure.
     * The Conversation section shows the durable room transcript (RFC #1264 Phase 2).
     *
     * @module
     */

    import { useEffect, type JSX } from "react";
    import type { StreamData, StreamMessageData } from "../../hooks/types.js";
    import { useAppNavigate, sessionUrl } from "../../utils/navigation.js";
    import { streamKind, type StreamKind } from "../../utils/streamCoordination.js";
    import { StreamTranscript } from "./StreamTranscript.js";
    import styles from "./StreamDetailPanel.module.scss";

    /** Props for the StreamDetailPanel component. */
    export interface StreamDetailPanelProps {
      /** The stream to display details for. */
      stream: StreamData;
      /** The stream's transcript messages, oldest first (scrollback + live merged). */
      messages?: StreamMessageData[];
      /** Whether the transcript is currently loading. */
      transcriptLoading?: boolean;
      /** Called when the user requests to close the panel. */
      onClose: () => void;
    }

    /** Human-readable kind label. */
    const KIND_LABEL: Record<StreamKind, string> = {
      chatroom: "Chatroom",
      pipe: "Pipe",
      channel: "Channel",
    };

    /** Render a permission badge with appropriate color. */
    function PermissionBadge({ permission }: { permission: string }): JSX.Element {
      const cls =
        permission === "rw" ? styles.badgeRw : permission === "r" ? styles.badgeR : styles.badgeW;
      return <span className={cls}>{permission}</span>;
    }

    /** Render a delivery mode badge with appropriate color. */
    function DeliveryModeBadge({ mode }: { mode: string }): JSX.Element {
      const cls =
        mode === "async"
          ? styles.badgeAsync
          : mode === "detach"
            ? styles.badgeDetach
            : styles.badgeSync;
      return <span className={cls}>{mode}</span>;
    }

    /**
     * Pull-out right drawer showing stream metadata: overview, participants, and an
     * Advanced disclosure with low-level wiring. Conversation content is V2.
     */
    export function StreamDetailPanel({
      stream,
      messages,
      transcriptLoading,
      onClose,
    }: StreamDetailPanelProps): JSX.Element {
      const navigate = useAppNavigate();

      // Close on Escape key
      useEffect(() => {
        const handleKeyDown = (e: KeyboardEvent): void => {
          Iif (e.key === "Escape") {
            onClose();
          }
        };
        document.addEventListener("keydown", handleKeyDown);
        return () => {
          document.removeEventListener("keydown", handleKeyDown);
        };
      }, [onClose]);

      return (
        <div className={styles.panel} data-testid="stream-detail-panel">
          <div className={styles.header}>
            <h3 className={styles.title}>{stream.name}</h3>
            <button
              type="button"
              className={styles.closeButton}
              onClick={onClose}
              aria-label="Close stream details"
            >
              &times;
            </button>
          </div>

          <div className={styles.body}>
            {/* Overview */}
            <div className={styles.section}>
              <div className={styles.sectionLabel}>Overview</div>
              <div className={styles.metaRow}>
                <span className={styles.metaKey}>Kind</span>
                <span className={styles.metaValue}>{KIND_LABEL[streamKind(stream)]}</span>
              </div>
              <div className={styles.metaRow}>
                <span className={styles.metaKey}>Participants</span>
                <span className={styles.metaValue}>{stream.subscriberCount}</span>
              </div>
              <div className={styles.metaRow}>
                <span className={styles.metaKey}>Buffered</span>
                <span className={styles.metaValue}>{stream.messageBufferDepth} msgs</span>
              </div>
            </div>

            {/* Participants */}
            <div className={styles.section}>
              <div className={styles.sectionLabel}>Participants</div>
              {stream.subscribers.length === 0 ? (
                <div className={styles.emptySubscribers}>No active subscribers</div>
              ) : (
                stream.subscribers.map((sub) => (
                  <div
                    key={sub.subscriptionId}
                    className={styles.subscriberCard}
                    data-testid={`subscriber-card-${sub.subscriptionId}`}
                  >
                    <button
                      type="button"
                      className={styles.sessionLink}
                      onClick={() => {
                        navigate(sessionUrl(sub.sessionId));
                      }}
                      title={sub.sessionId}
                    >
                      {sub.sessionId.slice(0, 12)}…
                    </button>
                    {sub.createdBySpawn && <span className={styles.spawnTag}>spawn</span>}
                  </div>
                ))
              )}
            </div>

            {/* Live conversation transcript (RFC #1264 Phase 2) */}
            <div className={styles.section}>
              <div className={styles.sectionLabel}>Conversation</div>
              <StreamTranscript messages={messages ?? []} loading={transcriptLoading ?? false} />
            </div>

            {/* Advanced wiring */}
            <details className={styles.advanced} data-testid="stream-advanced">
              <summary className={styles.advancedSummary}>Advanced</summary>
              <div className={styles.metaRow}>
                <span className={styles.metaKey}>Stream ID</span>
                <span className={styles.metaValueMono}>{stream.id}</span>
              </div>
              {stream.subscribers.map((sub) => (
                <div key={sub.subscriptionId} className={styles.subscriberCard}>
                  <div className={styles.subscriberHeader}>
                    <span className={styles.fdNumber}>fd {sub.fd}</span>
                    <span className={styles.metaValueMono} title={sub.subscriptionId}>
                      {sub.subscriptionId.slice(0, 12)}…
                    </span>
                  </div>
                  <div className={styles.badges}>
                    <PermissionBadge permission={sub.permission} />
                    <DeliveryModeBadge mode={sub.deliveryMode} />
                  </div>
                </div>
              ))}
            </details>
          </div>
        </div>
      );
    }