Skip to content

Commit

Permalink
Mark dashboard stale
Browse files Browse the repository at this point in the history
  • Loading branch information
webjunkie committed Apr 25, 2024
1 parent a1f4a1d commit 206c7d3
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ export interface InsightCardProps extends Resizeable, React.HTMLAttributes<HTMLD
loadingQueued?: boolean
/** Whether the insight is loading. */
loading?: boolean
/** Whether the insight likely showing stale data. */
stale?: boolean
/** Whether an error occurred on the server. */
apiErrored?: boolean
/** Whether the card should be highlighted with a blue border. */
Expand Down Expand Up @@ -165,7 +167,7 @@ function VizComponentFallback(): JSX.Element {
}

export interface FilterBasedCardContentProps
extends Pick<InsightCardProps, 'insight' | 'loading' | 'apiErrored' | 'timedOut' | 'style'> {
extends Pick<InsightCardProps, 'insight' | 'loading' | 'apiErrored' | 'timedOut' | 'style' | 'stale'> {
insightProps: InsightLogicProps
tooFewFunnelSteps?: boolean
validationError?: string | null
Expand All @@ -186,6 +188,7 @@ export function FilterBasedCardContent({
tooFewFunnelSteps,
validationError,
context,
stale,
}: FilterBasedCardContentProps): JSX.Element {
const displayedType = getDisplayedType(insight.filters)
const VizComponent = displayMap[displayedType]?.element || VizComponentFallback
Expand All @@ -211,6 +214,7 @@ export function FilterBasedCardContent({
: undefined
}
>
{stale && !loading && <SpinnerOverlay mode="editing" />}
{loading && <SpinnerOverlay />}
{tooFewFunnelSteps ? (
<FunnelSingleStepState actionable={false} />
Expand All @@ -237,6 +241,7 @@ function InsightCardInternal(
ribbonColor,
loadingQueued,
loading,
stale,
apiErrored,
timedOut,
highlighted,
Expand Down Expand Up @@ -338,6 +343,7 @@ function InsightCardInternal(
insight={insight}
insightProps={insightLogicProps}
loading={loading}
stale={stale}
apiErrored={apiErrored}
timedOut={timedOut}
empty={empty}
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/lib/lemon-ui/Spinner/Spinner.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './Spinner.scss'

import { IconClock } from '@posthog/icons'
import { IconPencil } from '@posthog/icons'
import { twJoin, twMerge } from 'tailwind-merge'

export interface SpinnerProps {
Expand Down Expand Up @@ -38,12 +38,12 @@ export function SpinnerOverlay({
/** @default true */
visible?: boolean
/** @default "spinning" */
mode?: 'spinning' | 'waiting'
mode?: 'spinning' | 'editing'
}): JSX.Element {
return (
<div className={twJoin('SpinnerOverlay', sceneLevel && 'SpinnerOverlay--scene-level')} aria-hidden={!visible}>
{mode === 'waiting' ? (
<IconClock className="text-5xl text-primary z-10 animate-pulse drop-shadow-xl" />
{mode === 'editing' ? (
<IconPencil className="text-5xl text-primary z-10 drop-shadow-xl" />
) : (
<Spinner className={twMerge('text-5xl', className)} {...spinnerProps} />
)}
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/scenes/dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function DashboardScene(): JSX.Element {
dashboardMode,
receivedErrorsFromAPI,
} = useValues(dashboardLogic)
const { setDashboardMode, setDates, reportDashboardViewed, setProperties, abortAnyRunningQuery } =
const { setDashboardMode, setDates, reportDashboardViewed, setProperties, abortAnyRunningQuery, setStale } =
useActions(dashboardLogic)
const { groupsTaxonomicTypes } = useValues(groupsModel)

Expand Down Expand Up @@ -131,6 +131,7 @@ function DashboardScene(): JSX.Element {
TaxonomicFilterGroupType.Elements,
TaxonomicFilterGroupType.HogQLExpression,
]}
onPendingChanges={(stale: boolean) => setStale(stale)}
/>
</div>
)}
Expand Down
37 changes: 28 additions & 9 deletions frontend/src/scenes/dashboard/DashboardEditBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { LemonButton } from '@posthog/lemon-ui'
import { DateFilter } from 'lib/components/DateFilter/DateFilter'
import { PropertyFilters } from 'lib/components/PropertyFilters/PropertyFilters'
import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'
import { useState } from 'react'
import { useEffect, useState } from 'react'

import { AnyPropertyFilter, DashboardType, FilterType } from '~/types'

Expand All @@ -16,6 +16,7 @@ interface DashboardEditBarProps {
setDates: (dateFrom: string | null, dateTo: string | null) => void
setProperties: (properties: AnyPropertyFilter[]) => void
groupsTaxonomicTypes: TaxonomicFilterGroupType[]
onPendingChanges?: (stale: boolean) => void
}

export function DashboardEditBar({
Expand All @@ -25,6 +26,7 @@ export function DashboardEditBar({
setDates,
setProperties,
groupsTaxonomicTypes,
onPendingChanges,
}: DashboardEditBarProps): JSX.Element {
const [editMode, setEditMode] = useState(false)
const [tempDates, setTempDates] = useState<Dates>({
Expand All @@ -35,14 +37,28 @@ export function DashboardEditBar({
dashboard?.filters.properties ?? undefined
)

useEffect(() => {
const hasPendingChanges =
tempDates.dateFrom !== dashboardFilters?.date_from ||
tempDates.dateTo !== dashboardFilters?.date_to ||
JSON.stringify(tempProperties) !== JSON.stringify(dashboard?.filters.properties)
if (onPendingChanges) {
onPendingChanges(hasPendingChanges)
}
}, [tempDates, tempProperties])

const handleSave: () => void = () => {
if (canEditDashboard) {
setDates(tempDates.dateFrom ?? null, tempDates.dateTo ?? null)
if (tempProperties) {
setProperties(tempProperties)
}
setEditMode(false)
if (!canEditDashboard) {
return
}
setDates(tempDates.dateFrom ?? null, tempDates.dateTo ?? null)
if (tempProperties) {
setProperties(tempProperties)
}
if (onPendingChanges) {
onPendingChanges(false)
}
setEditMode(false)
}

const handleCancel: () => void = () => {
Expand All @@ -52,6 +68,9 @@ export function DashboardEditBar({
})
setTempProperties(dashboard?.filters.properties ?? undefined)
setEditMode(false)
if (onPendingChanges) {
onPendingChanges(false)
}
}

return (
Expand All @@ -70,7 +89,7 @@ export function DashboardEditBar({
)}
/>
<PropertyFilters
disabled={!editMode}
disabled={!canEditDashboard || !editMode}
onChange={setTempProperties}
pageKey={'dashboard_' + dashboard?.id}
propertyFilters={tempProperties}
Expand All @@ -84,7 +103,7 @@ export function DashboardEditBar({
TaxonomicFilterGroupType.HogQLExpression,
]}
/>
{!editMode ? (
{canEditDashboard && !editMode ? (
<LemonButton type="secondary" size="small" onClick={() => setEditMode(true)}>
Edit filters
</LemonButton>
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/scenes/dashboard/DashboardItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export function DashboardItems(): JSX.Element {
highlightedInsightId,
refreshStatus,
canEditDashboard,
stale,
} = useValues(dashboardLogic)
const {
updateLayouts,
Expand Down Expand Up @@ -137,6 +138,7 @@ export function DashboardItems(): JSX.Element {
<InsightCard
key={tile.id}
insight={insight}
stale={stale}
loadingQueued={isRefreshingQueued(insight.short_id)}
loading={isRefreshing(insight.short_id)}
apiErrored={refreshStatus[insight.short_id]?.error || false}
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/scenes/dashboard/dashboardLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export const dashboardLogic = kea<dashboardLogicType>([
setInitialLoadResponseBytes: (responseBytes: number) => ({ responseBytes }),
abortQuery: (payload: { dashboardQueryId: string; queryId: string; queryStartTime: number }) => payload,
abortAnyRunningQuery: true,
setStale: (stale: boolean) => ({ stale }),
}),

loaders(({ actions, props, values }) => ({
Expand Down Expand Up @@ -522,6 +523,12 @@ export const dashboardLogic = kea<dashboardLogicType>([
setTextTileId: (_, { textTileId }) => textTileId,
},
],
stale: [
false,
{
setStale: (_, { stale }) => stale,
},
],
})),
selectors(() => ({
asDashboardTemplate: [
Expand Down

0 comments on commit 206c7d3

Please sign in to comment.