Skip to content

fix(dashboards): Starred dashboards not changing in navbar #90338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions static/app/actionCreators/dashboards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import PageFiltersStore from 'sentry/stores/pageFiltersStore';
import type {PageFilters} from 'sentry/types/core';
import {defined} from 'sentry/utils';
import {TOP_N} from 'sentry/utils/discover/types';
import type {QueryClient} from 'sentry/utils/queryClient';
import {
type DashboardDetails,
type DashboardListItem,
Expand Down Expand Up @@ -103,6 +104,7 @@ export function updateDashboardVisit(

export async function updateDashboardFavorite(
api: Client,
queryClient: QueryClient,
orgId: string,
dashboardId: string | string[],
isFavorited: boolean
Expand All @@ -117,6 +119,12 @@ export async function updateDashboardFavorite(
},
}
);
queryClient.invalidateQueries({
queryKey: [
`/organizations/${orgId}/dashboards/`,
{query: {filter: 'onlyFavorites'}},
],
});
addSuccessMessage(isFavorited ? t('Added as favorite') : t('Removed as favorite'));
} catch (response) {
const errorResponse = response?.responseJSON ?? null;
Expand Down
4 changes: 3 additions & 1 deletion static/app/views/dashboards/controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {t, tct} from 'sentry/locale';
import {space} from 'sentry/styles/space';
import type {Organization} from 'sentry/types/organization';
import {trackAnalytics} from 'sentry/utils/analytics';
import {useQueryClient} from 'sentry/utils/queryClient';
import useApi from 'sentry/utils/useApi';
import useOrganization from 'sentry/utils/useOrganization';
import {useUser} from 'sentry/utils/useUser';
Expand Down Expand Up @@ -62,7 +63,7 @@ function Controls({
onAddWidgetFromNewWidgetBuilder,
}: Props) {
const [isFavorited, setIsFavorited] = useState(dashboard.isFavorited);

const queryClient = useQueryClient();
function renderCancelButton(label = t('Cancel')) {
return (
<Button
Expand Down Expand Up @@ -214,6 +215,7 @@ function Controls({
setIsFavorited(!isFavorited);
await updateDashboardFavorite(
api,
queryClient,
organization.slug,
dashboard.id,
!isFavorited
Expand Down
10 changes: 9 additions & 1 deletion static/app/views/dashboards/manage/dashboardGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {t, tn} from 'sentry/locale';
import {space} from 'sentry/styles/space';
import type {Organization} from 'sentry/types/organization';
import {trackAnalytics} from 'sentry/utils/analytics';
import {useQueryClient} from 'sentry/utils/queryClient';
import withApi from 'sentry/utils/withApi';
import {
DASHBOARD_CARD_GRID_PADDING,
Expand Down Expand Up @@ -55,6 +56,7 @@ function DashboardGrid({
columnCount,
isLoading,
}: Props) {
const queryClient = useQueryClient();
// this acts as a cache for the dashboards being passed in. It preserves the previously populated dashboard list
// to be able to show the 'previous' dashboards on resize
const [currentDashboards, setCurrentDashboards] = useState<
Expand Down Expand Up @@ -102,7 +104,13 @@ function DashboardGrid({
}

async function handleFavorite(dashboard: DashboardListItem, isFavorited: boolean) {
await updateDashboardFavorite(api, organization.slug, dashboard.id, isFavorited);
await updateDashboardFavorite(
api,
queryClient,
organization.slug,
dashboard.id,
isFavorited
);
onDashboardsChange();
trackAnalytics('dashboards_manage.toggle_favorite', {
organization,
Expand Down
10 changes: 9 additions & 1 deletion static/app/views/dashboards/manage/dashboardTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {t} from 'sentry/locale';
import {space} from 'sentry/styles/space';
import type {Organization} from 'sentry/types/organization';
import {trackAnalytics} from 'sentry/utils/analytics';
import {useQueryClient} from 'sentry/utils/queryClient';
import {decodeScalar} from 'sentry/utils/queryString';
import withApi from 'sentry/utils/withApi';
import EditAccessSelector from 'sentry/views/dashboards/editAccessSelector';
Expand Down Expand Up @@ -78,6 +79,7 @@ function FavoriteButton({
dashboardId,
onDashboardsChange,
}: FavoriteButtonProps) {
const queryClient = useQueryClient();
const [favorited, setFavorited] = useState(isFavorited);
return (
<Button
Expand All @@ -95,7 +97,13 @@ function FavoriteButton({
onClick={async () => {
try {
setFavorited(!favorited);
await updateDashboardFavorite(api, organization.slug, dashboardId, !favorited);
await updateDashboardFavorite(
api,
queryClient,
organization.slug,
dashboardId,
!favorited
);
onDashboardsChange();
trackAnalytics('dashboards_manage.toggle_favorite', {
organization,
Expand Down
21 changes: 2 additions & 19 deletions static/app/views/dashboards/manage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {useEffect, useRef, useState} from 'react';
import styled from '@emotion/styled';
import {useQueryClient} from '@tanstack/react-query';
import type {Query} from 'history';
import debounce from 'lodash/debounce';
import pick from 'lodash/pick';
Expand Down Expand Up @@ -95,7 +94,6 @@ function ManageDashboards() {
const api = useApi();
const dashboardGridRef = useRef<HTMLDivElement>(null);
const prefersStackedNav = usePrefersStackedNav();
const queryClient = useQueryClient();

const [showTemplates, setShowTemplatesLocal] = useLocalStorageState(
SHOW_TEMPLATES_KEY,
Expand Down Expand Up @@ -225,21 +223,6 @@ function ManageDashboards() {
});
};

const handleDashboardsChange = () => {
refetchDashboards();

// We also need to invalidate the cache for the query that is used by the
// <DashboardsSecondaryNav /> component ('static/app/views/dashboards/navigation.tsx').
// Otherwise, the starred / unstarred dashboards will not be reflected in the navigation
// before a full page reload.
queryClient.invalidateQueries({
queryKey: [
`/organizations/${organization.slug}/dashboards/`,
{query: {filter: 'onlyFavorites'}},
],
});
};

const toggleTemplates = () => {
trackAnalytics('dashboards_manage.templates.toggle', {
organization,
Expand Down Expand Up @@ -336,7 +319,7 @@ function ManageDashboards() {
dashboards={dashboards}
organization={organization}
location={location}
onDashboardsChange={handleDashboardsChange}
onDashboardsChange={() => refetchDashboards()}
isLoading={isLoading}
rowCount={rowCount}
columnCount={columnCount}
Expand All @@ -347,7 +330,7 @@ function ManageDashboards() {
dashboards={dashboards}
organization={organization}
location={location}
onDashboardsChange={handleDashboardsChange}
onDashboardsChange={() => refetchDashboards()}
isLoading={isLoading}
/>
);
Expand Down
Loading