Repository navigation
Expand file tree
/
Copy pathTopicRow.tsx
More file actions
204 lines (185 loc) · 6.51 KB
/
Copy pathTopicRow.tsx
File metadata and controls
204 lines (185 loc) · 6.51 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// SPDX-FileCopyrightText: Copyright (C) 2023-2026 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)<lichtblick@bmwgroup.com>
// SPDX-License-Identifier: MPL-2.0
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
import {
ChevronLeft16Regular,
ChevronRight16Regular,
ReOrderDotsVertical16Regular,
} from "@fluentui/react-icons";
import { Badge, IconButton, Tooltip, Typography } from "@mui/material";
import { FzfResultItem } from "fzf";
import { useCallback, useMemo } from "react";
import { quoteTopicNameIfNeeded } from "@lichtblick/message-path";
import { HighlightChars } from "@lichtblick/suite-base/components/HighlightChars";
import { useMessagePipeline } from "@lichtblick/suite-base/components/MessagePipeline";
import { MessagePipelineContext } from "@lichtblick/suite-base/components/MessagePipeline/types";
import { DraggedMessagePath } from "@lichtblick/suite-base/components/PanelExtensionAdapter";
import Stack from "@lichtblick/suite-base/components/Stack";
import { Topic } from "@lichtblick/suite-base/players/types";
import { useMessagePathDrag } from "@lichtblick/suite-base/services/messagePathDragging";
import { TopicStatsChip } from "./TopicStatsChip";
import { useTopicListStyles } from "./useTopicListStyles";
import { useTopicMessageNavigation } from "./useTopicMessageNavigation";
const selectSubscriptions = (ctx: MessagePipelineContext) => ctx.subscriptions;
export function TopicRow({
topicResult,
style,
selected,
onClick,
onContextMenu,
}: {
topicResult: FzfResultItem<Topic>;
style: React.CSSProperties;
selected: boolean;
onClick: React.MouseEventHandler<HTMLDivElement>;
onContextMenu: React.MouseEventHandler<HTMLDivElement>;
}): React.JSX.Element {
const { cx, classes } = useTopicListStyles();
const topic = topicResult.item;
const subscriptions = useMessagePipeline(selectSubscriptions);
const isTopicSubscribed = useMemo(
() => subscriptions.some((sub) => sub.topic === topic.name),
[subscriptions, topic.name],
);
const {
handleNextMessage,
handlePreviousMessage,
isNavigating,
canNavigateNext,
canNavigatePrevious,
} = useTopicMessageNavigation({
topicName: topic.name,
isTopicSubscribed,
selected,
});
const isNavigationDisabled = useMemo(
() => isNavigating || !selected || !isTopicSubscribed,
[isNavigating, selected, isTopicSubscribed],
);
const isPreviousDisabled = useMemo(
() => isNavigationDisabled || !canNavigatePrevious,
[isNavigationDisabled, canNavigatePrevious],
);
const isNextDisabled = useMemo(
() => isNavigationDisabled || !canNavigateNext,
[isNavigationDisabled, canNavigateNext],
);
const item: DraggedMessagePath = useMemo(
() => ({
path: quoteTopicNameIfNeeded(topic.name),
rootSchemaName: topic.schemaName,
isTopic: true,
isLeaf: false,
topicName: topic.name,
}),
[topic.name, topic.schemaName],
);
const { connectDragSource, connectDragPreview, cursor, isDragging, draggedItemCount } =
useMessagePathDrag({
item,
selected,
});
const combinedRef: React.Ref<HTMLDivElement> = useCallback(
(el) => {
connectDragSource(el);
connectDragPreview(el);
},
[connectDragPreview, connectDragSource],
);
const seekToNextMessage = useCallback(
(event: React.MouseEvent) => {
event.stopPropagation();
void handleNextMessage();
},
[handleNextMessage],
);
const seekToPreviousMessage = useCallback(
(event: React.MouseEvent) => {
event.stopPropagation();
void handlePreviousMessage();
},
[handlePreviousMessage],
);
const handleButtonMouseDown = useCallback((event: React.MouseEvent) => {
event.stopPropagation();
}, []);
return (
<div
ref={combinedRef}
className={cx(classes.row, {
[classes.isDragging]: isDragging,
[classes.selected]: selected,
})}
style={{ ...style, cursor }}
onClick={onClick}
onContextMenu={onContextMenu}
>
{draggedItemCount > 1 && (
<Badge color="primary" className={classes.countBadge} badgeContent={draggedItemCount} />
)}
{/* Extra Stack wrapper to enable growing without the */}
<Stack flex="auto" alignItems="flex-start" overflow="hidden">
<Typography variant="body2" noWrap className={classes.textContent}>
<HighlightChars str={topic.name} indices={topicResult.positions} />
{topic.aliasedFromName != undefined && (
<Typography variant="caption" className={classes.aliasedTopicName}>
from {topic.aliasedFromName}
</Typography>
)}
</Typography>
{topic.schemaName != undefined && (
<Typography
variant="caption"
color="text.secondary"
noWrap
className={classes.textContent}
>
<HighlightChars
str={topic.schemaName}
indices={topicResult.positions}
offset={topic.name.length + 1}
/>
</Typography>
)}
</Stack>
<Stack direction="column" alignItems="center" gap={0.5}>
<TopicStatsChip selected={selected} topicName={topic.name} />
<Stack direction="row" gap={0.5} alignItems="center">
<Tooltip title={isPreviousDisabled ? "" : "Previous message"}>
<span>
<IconButton
size="small"
aria-label="Previous message"
onClick={seekToPreviousMessage}
onMouseDown={handleButtonMouseDown}
disabled={isPreviousDisabled}
className={classes.navIconButton}
>
<ChevronLeft16Regular />
</IconButton>
</span>
</Tooltip>
<Tooltip title={isNextDisabled ? "" : "Next message"}>
<span>
<IconButton
size="small"
aria-label="Next message"
onClick={seekToNextMessage}
onMouseDown={handleButtonMouseDown}
disabled={isNextDisabled}
className={classes.navIconButton}
>
<ChevronRight16Regular />
</IconButton>
</span>
</Tooltip>
</Stack>
</Stack>
<div data-testid="TopicListDragHandle" className={classes.dragHandle}>
<ReOrderDotsVertical16Regular />
</div>
</div>
);
}