Skip to content

Commit 3819575

Browse files
authored
feat(ourlogs): Make table mode stable in url (#95595)
### Summary This makes table mode stable via url and doesn't switch you to aggregates when you load the the page if you manually switched to aggregate.
1 parent 13c6bca commit 3819575

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

static/app/views/explore/contexts/logs/logsPageParams.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ import {
2525
updateLocationWithAggregateSortBys,
2626
updateLocationWithLogSortBys,
2727
} from 'sentry/views/explore/contexts/logs/sortBys';
28+
import {
29+
getModeFromLocation,
30+
type Mode,
31+
updateLocationWithMode,
32+
} from 'sentry/views/explore/contexts/pageParamsContext/mode';
2833
import {OurLogKnownFieldKey} from 'sentry/views/explore/logs/types';
2934
import {useLogsQueryKeyWithInfinite} from 'sentry/views/explore/logs/useLogsQuery';
3035

@@ -52,6 +57,7 @@ interface LogsPageParams {
5257
readonly cursor: string;
5358
readonly fields: string[];
5459
readonly isTableFrozen: boolean | undefined;
60+
readonly mode: Mode;
5561
readonly refreshInterval: number;
5662
readonly search: MutableSearch;
5763
/**
@@ -176,6 +182,7 @@ export function LogsPageParamsProvider({
176182
...(groupBy ? [groupBy] : []),
177183
aggregate,
178184
]);
185+
const mode = getModeFromLocation(location);
179186
const pageFilters = usePageFilters();
180187
const projectIds = isTableFrozen
181188
? (limitToProjectIds ?? [-1])
@@ -209,6 +216,7 @@ export function LogsPageParamsProvider({
209216
groupBy,
210217
aggregateFn,
211218
aggregateParam,
219+
mode,
212220
..._testContext,
213221
}}
214222
>
@@ -238,6 +246,7 @@ function setLogsPageParams(location: Location, pageParams: LogPageParamsUpdate)
238246
updateNullableLocation(target, LOGS_GROUP_BY_KEY, pageParams.groupBy);
239247
updateNullableLocation(target, LOGS_AGGREGATE_FN_KEY, pageParams.aggregateFn);
240248
updateNullableLocation(target, LOGS_AGGREGATE_PARAM_KEY, pageParams.aggregateParam);
249+
updateLocationWithMode(target, pageParams.mode); // Can be swapped with updateNullableLocation if we merge page params.
241250
if (!pageParams.isTableFrozen) {
242251
updateLocationWithLogSortBys(target, pageParams.sortBys);
243252
updateLocationWithAggregateSortBys(target, pageParams.aggregateSortBys);
@@ -475,6 +484,21 @@ export function useLogsAnalyticsPageSource() {
475484
return analyticsPageSource;
476485
}
477486

487+
export function useLogsMode() {
488+
const {mode} = useLogsPageParams();
489+
return mode;
490+
}
491+
492+
export function useSetLogsMode() {
493+
const setPageParams = useSetLogsPageParams();
494+
return useCallback(
495+
(mode: Mode) => {
496+
setPageParams({mode});
497+
},
498+
[setPageParams]
499+
);
500+
}
501+
478502
function getLogCursorFromLocation(location: Location): string {
479503
if (!location.query?.[LOGS_CURSOR_KEY]) {
480504
return '';

static/app/views/explore/logs/logsTab.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ import {
3131
useLogsAggregateFunction,
3232
useLogsFields,
3333
useLogsGroupBy,
34+
useLogsMode,
3435
useLogsSearch,
3536
useSetLogsFields,
37+
useSetLogsMode,
3638
useSetLogsPageParams,
3739
} from 'sentry/views/explore/contexts/logs/logsPageParams';
40+
import {Mode} from 'sentry/views/explore/contexts/pageParamsContext/mode';
3841
import {useTraceItemAttributes} from 'sentry/views/explore/contexts/traceItemAttributeContext';
3942
import {useLogAnalytics} from 'sentry/views/explore/hooks/useAnalytics';
4043
import {
@@ -78,6 +81,8 @@ export function LogsTabContent({
7881
const logsSearch = useLogsSearch();
7982
const fields = useLogsFields();
8083
const groupBy = useLogsGroupBy();
84+
const mode = useLogsMode();
85+
const setMode = useSetLogsMode();
8186
const setFields = useSetLogsFields();
8287
const setLogsPageParams = useSetLogsPageParams();
8388
const tableData = useLogsPageDataQueryResult();
@@ -106,11 +111,6 @@ export function LogsTabContent({
106111
'explore.ourlogs.main-chart',
107112
DiscoverDatasets.OURLOGS
108113
);
109-
const [tableTab, setTableTab] = useState<'aggregates' | 'logs'>(
110-
(aggregateFunction && aggregateFunction !== 'count') || groupBy
111-
? 'aggregates'
112-
: 'logs'
113-
);
114114

115115
const {attributes: stringAttributes, isLoading: stringAttributesLoading} =
116116
useTraceItemAttributes('string');
@@ -177,6 +177,15 @@ export function LogsTabContent({
177177
{closeEvents: 'escape-key'}
178178
);
179179
}, [fields, setFields, stringAttributes, numberAttributes]);
180+
181+
const tableTab = mode === Mode.AGGREGATE ? 'aggregates' : 'logs';
182+
const setTableTab = useCallback(
183+
(tab: 'aggregates' | 'logs') => {
184+
setMode(tab === 'aggregates' ? Mode.AGGREGATE : Mode.SAMPLES);
185+
},
186+
[setMode]
187+
);
188+
180189
return (
181190
<SearchQueryBuilderProvider {...searchQueryBuilderProps}>
182191
<TopSectionBody noRowGap>

0 commit comments

Comments
 (0)