123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 |
- import {useEffect, useRef, useState} from 'react';
- import styled from '@emotion/styled';
- import type {Query} from 'history';
- import debounce from 'lodash/debounce';
- import pick from 'lodash/pick';
- import {createDashboard} from 'sentry/actionCreators/dashboards';
- import {addSuccessMessage} from 'sentry/actionCreators/indicator';
- import {openImportDashboardFromFileModal} from 'sentry/actionCreators/modal';
- import Feature from 'sentry/components/acl/feature';
- import {Alert} from 'sentry/components/alert';
- import {Button} from 'sentry/components/button';
- import ButtonBar from 'sentry/components/buttonBar';
- import {CompactSelect} from 'sentry/components/compactSelect';
- import ErrorBoundary from 'sentry/components/errorBoundary';
- import FeedbackWidgetButton from 'sentry/components/feedback/widget/feedbackWidgetButton';
- import * as Layout from 'sentry/components/layouts/thirds';
- import NoProjectMessage from 'sentry/components/noProjectMessage';
- import {PageHeadingQuestionTooltip} from 'sentry/components/pageHeadingQuestionTooltip';
- import Pagination from 'sentry/components/pagination';
- import SearchBar from 'sentry/components/searchBar';
- import {SegmentedControl} from 'sentry/components/segmentedControl';
- import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
- import Switch from 'sentry/components/switchButton';
- import {IconAdd, IconGrid, IconList} from 'sentry/icons';
- import {t} from 'sentry/locale';
- import {space} from 'sentry/styles/space';
- import type {SelectValue} from 'sentry/types/core';
- import {trackAnalytics} from 'sentry/utils/analytics';
- import localStorage from 'sentry/utils/localStorage';
- import parseLinkHeader from 'sentry/utils/parseLinkHeader';
- import {useApiQuery} from 'sentry/utils/queryClient';
- import {decodeScalar} from 'sentry/utils/queryString';
- import normalizeUrl from 'sentry/utils/url/normalizeUrl';
- import useApi from 'sentry/utils/useApi';
- import {useLocalStorageState} from 'sentry/utils/useLocalStorageState';
- import {useLocation} from 'sentry/utils/useLocation';
- import {useNavigate} from 'sentry/utils/useNavigate';
- import useOrganization from 'sentry/utils/useOrganization';
- import {DashboardImportButton} from 'sentry/views/dashboards/manage/dashboardImport';
- import DashboardTable from 'sentry/views/dashboards/manage/dashboardTable';
- import {MetricsRemovedAlertsWidgetsAlert} from 'sentry/views/metrics/metricsRemovedAlertsWidgetsAlert';
- import RouteError from 'sentry/views/routeError';
- import {getDashboardTemplates} from '../data';
- import {assignDefaultLayout, getInitialColumnDepths} from '../layoutUtils';
- import type {DashboardDetails, DashboardListItem} from '../types';
- import DashboardGrid from './dashboardGrid';
- import {
- DASHBOARD_CARD_GRID_PADDING,
- DASHBOARD_GRID_DEFAULT_NUM_CARDS,
- DASHBOARD_GRID_DEFAULT_NUM_COLUMNS,
- DASHBOARD_GRID_DEFAULT_NUM_ROWS,
- DASHBOARD_TABLE_NUM_ROWS,
- MINIMUM_DASHBOARD_CARD_WIDTH,
- } from './settings';
- import TemplateCard from './templateCard';
- const SORT_OPTIONS: SelectValue<string>[] = [
- {label: t('My Dashboards'), value: 'mydashboards'},
- {label: t('Dashboard Name (A-Z)'), value: 'title'},
- {label: t('Date Created (Newest)'), value: '-dateCreated'},
- {label: t('Date Created (Oldest)'), value: 'dateCreated'},
- {label: t('Most Popular'), value: 'mostPopular'},
- {label: t('Recently Viewed'), value: 'recentlyViewed'},
- ];
- const SHOW_TEMPLATES_KEY = 'dashboards-show-templates';
- export const LAYOUT_KEY = 'dashboards-overview-layout';
- const GRID = 'grid';
- const TABLE = 'table';
- export type DashboardsLayout = 'grid' | 'table';
- function shouldShowTemplates(): boolean {
- const shouldShow = localStorage.getItem(SHOW_TEMPLATES_KEY);
- return shouldShow === 'true' || shouldShow === null;
- }
- function getDashboardsOverviewLayout(): DashboardsLayout {
- const dashboardsLayout = localStorage.getItem(LAYOUT_KEY);
- return dashboardsLayout === GRID || dashboardsLayout === TABLE
- ? dashboardsLayout
- : GRID;
- }
- function ManageDashboards() {
- const organization = useOrganization();
- const navigate = useNavigate();
- const location = useLocation();
- const api = useApi();
- const dashboardGridRef = useRef<HTMLDivElement>(null);
- const [showTemplates, setShowTemplatesLocal] = useLocalStorageState(
- SHOW_TEMPLATES_KEY,
- shouldShowTemplates()
- );
- const [dashboardsLayout, setDashboardsLayout] = useLocalStorageState(
- LAYOUT_KEY,
- getDashboardsOverviewLayout()
- );
- const [{rowCount, columnCount}, setGridSize] = useState({
- rowCount: DASHBOARD_GRID_DEFAULT_NUM_ROWS,
- columnCount: DASHBOARD_GRID_DEFAULT_NUM_COLUMNS,
- });
- const {
- data: dashboards,
- isLoading,
- isError,
- error,
- getResponseHeader,
- refetch: refetchDashboards,
- } = useApiQuery<DashboardListItem[]>(
- [
- `/organizations/${organization.slug}/dashboards/`,
- {
- query: {
- ...pick(location.query, ['cursor', 'query']),
- sort: getActiveSort().value,
- per_page:
- dashboardsLayout === GRID ? rowCount * columnCount : DASHBOARD_TABLE_NUM_ROWS,
- },
- },
- ],
- {staleTime: 0}
- );
- const dashboardsPageLinks = getResponseHeader?.('Link') ?? '';
- function setRowsAndColumns(containerWidth: number) {
- const numWidgetsFitInRow = Math.floor(
- containerWidth / (MINIMUM_DASHBOARD_CARD_WIDTH + DASHBOARD_CARD_GRID_PADDING)
- );
- if (numWidgetsFitInRow >= 3) {
- setGridSize({
- rowCount: DASHBOARD_GRID_DEFAULT_NUM_ROWS,
- columnCount: numWidgetsFitInRow,
- });
- } else if (numWidgetsFitInRow === 0) {
- setGridSize({
- rowCount: DASHBOARD_GRID_DEFAULT_NUM_CARDS,
- columnCount: 1,
- });
- } else {
- setGridSize({
- rowCount: DASHBOARD_GRID_DEFAULT_NUM_CARDS / numWidgetsFitInRow,
- columnCount: numWidgetsFitInRow,
- });
- }
- }
- useEffect(() => {
- const dashboardGridObserver = new ResizeObserver(
- debounce(entries => {
- entries.forEach(entry => {
- const currentWidth = entry.contentRect.width;
- setRowsAndColumns(currentWidth);
- const paginationObject = parseLinkHeader(dashboardsPageLinks);
- if (
- dashboards?.length &&
- paginationObject.next.results &&
- rowCount * columnCount > dashboards.length
- ) {
- refetchDashboards();
- }
- });
- }, 10)
- );
- const currentDashboardGrid = dashboardGridRef.current;
- if (currentDashboardGrid) {
- dashboardGridObserver.observe(currentDashboardGrid);
- }
- return () => {
- if (currentDashboardGrid) {
- dashboardGridObserver.unobserve(currentDashboardGrid);
- }
- };
- }, [columnCount, dashboards?.length, dashboardsPageLinks, refetchDashboards, rowCount]);
- function getActiveSort() {
- const urlSort = decodeScalar(location.query.sort, 'mydashboards');
- return SORT_OPTIONS.find(item => item.value === urlSort) || SORT_OPTIONS[0];
- }
- function handleSearch(query: string) {
- trackAnalytics('dashboards_manage.search', {
- organization,
- });
- navigate({
- pathname: location.pathname,
- query: {...location.query, cursor: undefined, query},
- });
- }
- const handleSortChange = (value: string) => {
- trackAnalytics('dashboards_manage.change_sort', {
- organization,
- sort: value,
- });
- navigate({
- pathname: location.pathname,
- query: {
- ...location.query,
- cursor: undefined,
- sort: value,
- },
- });
- };
- const toggleTemplates = () => {
- trackAnalytics('dashboards_manage.templates.toggle', {
- organization,
- show_templates: !showTemplates,
- });
- setShowTemplatesLocal(!showTemplates);
- };
- function getQuery() {
- const {query} = location.query;
- return typeof query === 'string' ? query : undefined;
- }
- function renderTemplates() {
- return (
- <TemplateContainer>
- {getDashboardTemplates(organization).map(dashboard => (
- <TemplateCard
- title={dashboard.title}
- description={dashboard.description}
- onPreview={() => onPreview(dashboard.id)}
- onAdd={() => onAdd(dashboard)}
- key={dashboard.title}
- />
- ))}
- </TemplateContainer>
- );
- }
- function renderActions() {
- const activeSort = getActiveSort();
- return (
- <StyledActions listView={organization.features.includes('dashboards-table-view')}>
- <SearchBar
- defaultQuery=""
- query={getQuery()}
- placeholder={t('Search Dashboards')}
- onSearch={query => handleSearch(query)}
- />
- <Feature features={'organizations:dashboards-table-view'}>
- <SegmentedControl<DashboardsLayout>
- onChange={setDashboardsLayout}
- size="md"
- value={dashboardsLayout}
- aria-label={t('Layout Control')}
- >
- <SegmentedControl.Item
- key="grid"
- textValue="grid"
- aria-label={t('Grid View')}
- icon={<IconGrid />}
- />
- <SegmentedControl.Item
- key="list"
- textValue="list"
- aria-label={t('List View')}
- icon={<IconList />}
- />
- </SegmentedControl>
- </Feature>
- <CompactSelect
- triggerProps={{prefix: t('Sort By')}}
- value={activeSort.value}
- options={SORT_OPTIONS}
- onChange={opt => handleSortChange(opt.value)}
- position="bottom-end"
- />
- </StyledActions>
- );
- }
- function renderNoAccess() {
- return (
- <Layout.Page>
- <Alert type="warning">{t("You don't have access to this feature")}</Alert>
- </Layout.Page>
- );
- }
- function renderDashboards() {
- return dashboardsLayout === GRID ? (
- <DashboardGrid
- api={api}
- dashboards={dashboards}
- organization={organization}
- location={location}
- onDashboardsChange={() => refetchDashboards()}
- isLoading={isLoading}
- rowCount={rowCount}
- columnCount={columnCount}
- />
- ) : (
- <DashboardTable
- api={api}
- dashboards={dashboards}
- organization={organization}
- location={location}
- onDashboardsChange={() => refetchDashboards()}
- isLoading={isLoading}
- />
- );
- }
- function renderPagination() {
- return (
- <PaginationRow
- pageLinks={dashboardsPageLinks}
- onCursor={(cursor, path, query, direction) => {
- const offset = Number(cursor?.split?.(':')?.[1] ?? 0);
- const newQuery: Query & {cursor?: string} = {...query, cursor};
- const isPrevious = direction === -1;
- if (offset <= 0 && isPrevious) {
- delete newQuery.cursor;
- }
- trackAnalytics('dashboards_manage.paginate', {organization});
- navigate({
- pathname: path,
- query: newQuery,
- });
- }}
- />
- );
- }
- function onCreate() {
- trackAnalytics('dashboards_manage.create.start', {
- organization,
- });
- navigate(
- normalizeUrl({
- pathname: `/organizations/${organization.slug}/dashboards/new/`,
- query: location.query,
- })
- );
- }
- async function onAdd(dashboard: DashboardDetails) {
- trackAnalytics('dashboards_manage.templates.add', {
- organization,
- dashboard_id: dashboard.id,
- dashboard_title: dashboard.title,
- was_previewed: false,
- });
- const newDashboard = await createDashboard(
- api,
- organization.slug,
- {
- ...dashboard,
- widgets: assignDefaultLayout(dashboard.widgets, getInitialColumnDepths()),
- },
- true
- );
- addSuccessMessage(`${dashboard.title} dashboard template successfully added.`);
- loadDashboard(newDashboard.id);
- }
- function loadDashboard(dashboardId: string) {
- navigate(
- normalizeUrl({
- pathname: `/organizations/${organization.slug}/dashboards/${dashboardId}/`,
- query: location.query,
- })
- );
- }
- function onPreview(dashboardId: string) {
- trackAnalytics('dashboards_manage.templates.preview', {
- organization,
- dashboard_id: dashboardId,
- });
- navigate(
- normalizeUrl({
- pathname: `/organizations/${organization.slug}/dashboards/new/${dashboardId}/`,
- query: location.query,
- })
- );
- }
- return (
- <Feature
- organization={organization}
- features="dashboards-edit"
- renderDisabled={renderNoAccess}
- >
- <SentryDocumentTitle title={t('Dashboards')} orgSlug={organization.slug}>
- <ErrorBoundary>
- {isError ? (
- <Layout.Page withPadding>
- <RouteError error={error} />
- </Layout.Page>
- ) : (
- <Layout.Page>
- <NoProjectMessage organization={organization}>
- <Layout.Header>
- <Layout.HeaderContent>
- <Layout.Title>
- {t('Dashboards')}
- <PageHeadingQuestionTooltip
- docsUrl="https://docs.sentry.io/product/dashboards/"
- title={t(
- 'A broad overview of your application’s health where you can navigate through error and performance data across multiple projects.'
- )}
- />
- </Layout.Title>
- </Layout.HeaderContent>
- <Layout.HeaderActions>
- <ButtonBar gap={1.5}>
- <TemplateSwitch>
- {t('Show Templates')}
- <Switch
- isActive={showTemplates}
- size="lg"
- toggle={toggleTemplates}
- />
- </TemplateSwitch>
- <FeedbackWidgetButton />
- <DashboardImportButton />
- <Button
- data-test-id="dashboard-create"
- onClick={event => {
- event.preventDefault();
- onCreate();
- }}
- size="sm"
- priority="primary"
- icon={<IconAdd isCircled />}
- >
- {t('Create Dashboard')}
- </Button>
- <Feature features="dashboards-import">
- <Button
- onClick={() => {
- openImportDashboardFromFileModal({
- organization,
- api,
- location,
- });
- }}
- size="sm"
- priority="primary"
- icon={<IconAdd isCircled />}
- >
- {t('Import Dashboard from JSON')}
- </Button>
- </Feature>
- </ButtonBar>
- </Layout.HeaderActions>
- </Layout.Header>
- <Layout.Body>
- <Layout.Main fullWidth>
- <MetricsRemovedAlertsWidgetsAlert organization={organization} />
- {showTemplates && renderTemplates()}
- {renderActions()}
- <div ref={dashboardGridRef} id="dashboard-list-container">
- {renderDashboards()}
- </div>
- {renderPagination()}
- </Layout.Main>
- </Layout.Body>
- </NoProjectMessage>
- </Layout.Page>
- )}
- </ErrorBoundary>
- </SentryDocumentTitle>
- </Feature>
- );
- }
- const StyledActions = styled('div')<{listView: boolean}>`
- display: grid;
- grid-template-columns: ${p =>
- p.listView ? 'auto max-content max-content' : 'auto max-content'};
- gap: ${space(2)};
- margin-bottom: ${space(2)};
- @media (max-width: ${p => p.theme.breakpoints.small}) {
- grid-template-columns: auto;
- }
- `;
- const TemplateSwitch = styled('label')`
- font-weight: ${p => p.theme.fontWeightNormal};
- font-size: ${p => p.theme.fontSizeLarge};
- display: flex;
- align-items: center;
- gap: ${space(1)};
- width: max-content;
- margin: 0;
- `;
- const TemplateContainer = styled('div')`
- display: grid;
- gap: ${space(2)};
- margin-bottom: ${space(0.5)};
- @media (min-width: ${p => p.theme.breakpoints.small}) {
- grid-template-columns: repeat(2, minmax(200px, 1fr));
- }
- @media (min-width: ${p => p.theme.breakpoints.large}) {
- grid-template-columns: repeat(4, minmax(200px, 1fr));
- }
- `;
- const PaginationRow = styled(Pagination)`
- margin-bottom: ${space(3)};
- `;
- export default ManageDashboards;
|