Logs Panel: Add option extra UI functionality for log context (#83123)

* use ref rather than state

* add `getLogRowContextUi` to panel
This commit is contained in:
Sven Grossmann 2024-02-20 21:07:47 +01:00 committed by GitHub
parent 53c19e4988
commit f2c0309d71
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,6 +9,7 @@ import {
Field, Field,
GrafanaTheme2, GrafanaTheme2,
hasLogsContextSupport, hasLogsContextSupport,
hasLogsContextUiSupport,
Labels, Labels,
LogRowContextOptions, LogRowContextOptions,
LogRowModel, LogRowModel,
@ -60,10 +61,10 @@ export const LogsPanel = ({
const [scrollTop, setScrollTop] = useState(0); const [scrollTop, setScrollTop] = useState(0);
const logsContainerRef = useRef<HTMLDivElement>(null); const logsContainerRef = useRef<HTMLDivElement>(null);
const [contextRow, setContextRow] = useState<LogRowModel | null>(null); const [contextRow, setContextRow] = useState<LogRowModel | null>(null);
const [closeCallback, setCloseCallback] = useState<(() => void) | null>(null);
const timeRange = data.timeRange; const timeRange = data.timeRange;
const dataSourcesMap = useDatasourcesFromTargets(data.request?.targets); const dataSourcesMap = useDatasourcesFromTargets(data.request?.targets);
const [scrollElement, setScrollElement] = useState<HTMLDivElement | null>(null); const [scrollElement, setScrollElement] = useState<HTMLDivElement | null>(null);
let closeCallback = useRef<() => void>();
const { eventBus } = usePanelContext(); const { eventBus } = usePanelContext();
const onLogRowHover = useCallback( const onLogRowHover = useCallback(
@ -85,15 +86,18 @@ export const LogsPanel = ({
const onCloseContext = useCallback(() => { const onCloseContext = useCallback(() => {
setContextRow(null); setContextRow(null);
if (closeCallback) { if (closeCallback.current) {
closeCallback(); closeCallback.current();
} }
}, [closeCallback]); }, [closeCallback]);
const onOpenContext = useCallback((row: LogRowModel, onClose: () => void) => { const onOpenContext = useCallback(
setContextRow(row); (row: LogRowModel, onClose: () => void) => {
setCloseCallback(onClose); setContextRow(row);
}, []); closeCallback.current = onClose;
},
[closeCallback]
);
const onPermalinkClick = useCallback( const onPermalinkClick = useCallback(
async (row: LogRowModel) => { async (row: LogRowModel) => {
@ -150,6 +154,31 @@ export const LogsPanel = ({
[data.request?.targets, dataSourcesMap] [data.request?.targets, dataSourcesMap]
); );
const getLogRowContextUi = useCallback(
(origRow: LogRowModel, runContextQuery?: () => void): React.ReactNode => {
if (!origRow.dataFrame.refId || !dataSourcesMap) {
return <></>;
}
const query = data.request?.targets[0];
if (!query) {
return <></>;
}
const dataSource = dataSourcesMap.get(origRow.dataFrame.refId);
if (!hasLogsContextUiSupport(dataSource)) {
return <></>;
}
if (!dataSource.getLogRowContextUi) {
return <></>;
}
return dataSource.getLogRowContextUi(origRow, runContextQuery, query);
},
[data.request?.targets, dataSourcesMap]
);
// Important to memoize stuff here, as panel rerenders a lot for example when resizing. // Important to memoize stuff here, as panel rerenders a lot for example when resizing.
const [logRows, deduplicatedRows, commonLabels] = useMemo(() => { const [logRows, deduplicatedRows, commonLabels] = useMemo(() => {
const logs = data const logs = data
@ -210,6 +239,7 @@ export const LogsPanel = ({
getRowContext={(row, options) => getLogRowContext(row, contextRow, options)} getRowContext={(row, options) => getLogRowContext(row, contextRow, options)}
logsSortOrder={sortOrder} logsSortOrder={sortOrder}
timeZone={timeZone} timeZone={timeZone}
getLogRowContextUi={getLogRowContextUi}
/> />
)} )}
<CustomScrollbar <CustomScrollbar