Skip to content

Commit baa67b4

Browse files
authored
fix(dashboards): Starred dashboards not changing in navbar (#90338)
Before, starring a dashboard on the manage dashboards page would show changes in the nav bar but if it was favourited anywhere else it would not. I've moved the logic to clear the api query cache to the `updateDashboardFavorite` function so that the cache is cleared every time a favourite dashboard is updated. As long as this function is being used to update the favourites, the navbar will show the changes. (This function is being used to update favourites in all places afaik)
1 parent adb432f commit baa67b4

File tree

5 files changed

+31
-22
lines changed

5 files changed

+31
-22
lines changed

static/app/actionCreators/dashboards.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import PageFiltersStore from 'sentry/stores/pageFiltersStore';
88
import type {PageFilters} from 'sentry/types/core';
99
import {defined} from 'sentry/utils';
1010
import {TOP_N} from 'sentry/utils/discover/types';
11+
import type {QueryClient} from 'sentry/utils/queryClient';
1112
import {
1213
type DashboardDetails,
1314
type DashboardListItem,
@@ -103,6 +104,7 @@ export function updateDashboardVisit(
103104

104105
export async function updateDashboardFavorite(
105106
api: Client,
107+
queryClient: QueryClient,
106108
orgId: string,
107109
dashboardId: string | string[],
108110
isFavorited: boolean
@@ -117,6 +119,12 @@ export async function updateDashboardFavorite(
117119
},
118120
}
119121
);
122+
queryClient.invalidateQueries({
123+
queryKey: [
124+
`/organizations/${orgId}/dashboards/`,
125+
{query: {filter: 'onlyFavorites'}},
126+
],
127+
});
120128
addSuccessMessage(isFavorited ? t('Added as favorite') : t('Removed as favorite'));
121129
} catch (response) {
122130
const errorResponse = response?.responseJSON ?? null;

static/app/views/dashboards/controls.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {t, tct} from 'sentry/locale';
1616
import {space} from 'sentry/styles/space';
1717
import type {Organization} from 'sentry/types/organization';
1818
import {trackAnalytics} from 'sentry/utils/analytics';
19+
import {useQueryClient} from 'sentry/utils/queryClient';
1920
import useApi from 'sentry/utils/useApi';
2021
import useOrganization from 'sentry/utils/useOrganization';
2122
import {useUser} from 'sentry/utils/useUser';
@@ -62,7 +63,7 @@ function Controls({
6263
onAddWidgetFromNewWidgetBuilder,
6364
}: Props) {
6465
const [isFavorited, setIsFavorited] = useState(dashboard.isFavorited);
65-
66+
const queryClient = useQueryClient();
6667
function renderCancelButton(label = t('Cancel')) {
6768
return (
6869
<Button
@@ -214,6 +215,7 @@ function Controls({
214215
setIsFavorited(!isFavorited);
215216
await updateDashboardFavorite(
216217
api,
218+
queryClient,
217219
organization.slug,
218220
dashboard.id,
219221
!isFavorited

static/app/views/dashboards/manage/dashboardGrid.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {t, tn} from 'sentry/locale';
2323
import {space} from 'sentry/styles/space';
2424
import type {Organization} from 'sentry/types/organization';
2525
import {trackAnalytics} from 'sentry/utils/analytics';
26+
import {useQueryClient} from 'sentry/utils/queryClient';
2627
import withApi from 'sentry/utils/withApi';
2728
import {
2829
DASHBOARD_CARD_GRID_PADDING,
@@ -55,6 +56,7 @@ function DashboardGrid({
5556
columnCount,
5657
isLoading,
5758
}: Props) {
59+
const queryClient = useQueryClient();
5860
// this acts as a cache for the dashboards being passed in. It preserves the previously populated dashboard list
5961
// to be able to show the 'previous' dashboards on resize
6062
const [currentDashboards, setCurrentDashboards] = useState<
@@ -102,7 +104,13 @@ function DashboardGrid({
102104
}
103105

104106
async function handleFavorite(dashboard: DashboardListItem, isFavorited: boolean) {
105-
await updateDashboardFavorite(api, organization.slug, dashboard.id, isFavorited);
107+
await updateDashboardFavorite(
108+
api,
109+
queryClient,
110+
organization.slug,
111+
dashboard.id,
112+
isFavorited
113+
);
106114
onDashboardsChange();
107115
trackAnalytics('dashboards_manage.toggle_favorite', {
108116
organization,

static/app/views/dashboards/manage/dashboardTable.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {t} from 'sentry/locale';
2929
import {space} from 'sentry/styles/space';
3030
import type {Organization} from 'sentry/types/organization';
3131
import {trackAnalytics} from 'sentry/utils/analytics';
32+
import {useQueryClient} from 'sentry/utils/queryClient';
3233
import {decodeScalar} from 'sentry/utils/queryString';
3334
import withApi from 'sentry/utils/withApi';
3435
import EditAccessSelector from 'sentry/views/dashboards/editAccessSelector';
@@ -78,6 +79,7 @@ function FavoriteButton({
7879
dashboardId,
7980
onDashboardsChange,
8081
}: FavoriteButtonProps) {
82+
const queryClient = useQueryClient();
8183
const [favorited, setFavorited] = useState(isFavorited);
8284
return (
8385
<Button
@@ -95,7 +97,13 @@ function FavoriteButton({
9597
onClick={async () => {
9698
try {
9799
setFavorited(!favorited);
98-
await updateDashboardFavorite(api, organization.slug, dashboardId, !favorited);
100+
await updateDashboardFavorite(
101+
api,
102+
queryClient,
103+
organization.slug,
104+
dashboardId,
105+
!favorited
106+
);
99107
onDashboardsChange();
100108
trackAnalytics('dashboards_manage.toggle_favorite', {
101109
organization,

static/app/views/dashboards/manage/index.tsx

+2-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {useEffect, useRef, useState} from 'react';
22
import styled from '@emotion/styled';
3-
import {useQueryClient} from '@tanstack/react-query';
43
import type {Query} from 'history';
54
import debounce from 'lodash/debounce';
65
import pick from 'lodash/pick';
@@ -95,7 +94,6 @@ function ManageDashboards() {
9594
const api = useApi();
9695
const dashboardGridRef = useRef<HTMLDivElement>(null);
9796
const prefersStackedNav = usePrefersStackedNav();
98-
const queryClient = useQueryClient();
9997

10098
const [showTemplates, setShowTemplatesLocal] = useLocalStorageState(
10199
SHOW_TEMPLATES_KEY,
@@ -225,21 +223,6 @@ function ManageDashboards() {
225223
});
226224
};
227225

228-
const handleDashboardsChange = () => {
229-
refetchDashboards();
230-
231-
// We also need to invalidate the cache for the query that is used by the
232-
// <DashboardsSecondaryNav /> component ('static/app/views/dashboards/navigation.tsx').
233-
// Otherwise, the starred / unstarred dashboards will not be reflected in the navigation
234-
// before a full page reload.
235-
queryClient.invalidateQueries({
236-
queryKey: [
237-
`/organizations/${organization.slug}/dashboards/`,
238-
{query: {filter: 'onlyFavorites'}},
239-
],
240-
});
241-
};
242-
243226
const toggleTemplates = () => {
244227
trackAnalytics('dashboards_manage.templates.toggle', {
245228
organization,
@@ -336,7 +319,7 @@ function ManageDashboards() {
336319
dashboards={dashboards}
337320
organization={organization}
338321
location={location}
339-
onDashboardsChange={handleDashboardsChange}
322+
onDashboardsChange={() => refetchDashboards()}
340323
isLoading={isLoading}
341324
rowCount={rowCount}
342325
columnCount={columnCount}
@@ -347,7 +330,7 @@ function ManageDashboards() {
347330
dashboards={dashboards}
348331
organization={organization}
349332
location={location}
350-
onDashboardsChange={handleDashboardsChange}
333+
onDashboardsChange={() => refetchDashboards()}
351334
isLoading={isLoading}
352335
/>
353336
);

0 commit comments

Comments
 (0)