Skip to content
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

chore(ui-kit): better naming for useEmptyableData hook #205

Merged
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
6 changes: 3 additions & 3 deletions packages/ui-kit/src/components/Leaderboard/Leaderboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
getTimeZone,
LeaderboardLabels,
useCombinedRefsCallback,
useConnectedData,
useEmptyableData,
withThemeWrapper
} from '../../helpers'
import { useLeaderboard } from '../../hooks/useLeaderboard'
Expand Down Expand Up @@ -92,8 +92,8 @@ export const LeaderboardComponent = React.forwardRef<HTMLDivElement, Leaderboard
}
}, [chartRef])

const { data, isEmptyState, setData } = useConnectedData<LeaderboardData>({
destroyChart,
const { data, isEmptyState, setData } = useEmptyableData<LeaderboardData>({
onEmptyData: destroyChart,
isDataEmpty: (data) => data.rows?.length === 0
})

Expand Down
6 changes: 3 additions & 3 deletions packages/ui-kit/src/components/PieChart/PieChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
getCustomChartLabelsPlugin,
getTimeZone,
useCombinedRefsCallback,
useConnectedData,
useEmptyableData,
withThemeWrapper
} from '../../helpers'
import { useCounter } from '../../hooks/useCounter'
Expand Down Expand Up @@ -136,8 +136,8 @@ export const PieChartComponent = React.forwardRef<HTMLDivElement, PieChartProps>
}
}, [chartRef])

const { data, isEmptyState, setData } = useConnectedData<PieChartData>({
destroyChart,
const { data, isEmptyState, setData } = useEmptyableData<PieChartData>({
onEmptyData: destroyChart,
isDataEmpty: (data) => data.rows?.length === 0
})

Expand Down
6 changes: 3 additions & 3 deletions packages/ui-kit/src/components/TimeSeries/TimeSeries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
customCanvasBackgroundColor,
formatLabels,
getTimeZone,
useConnectedData,
useEmptyableData,
useForwardedRefCallback,
withThemeWrapper
} from '../../helpers'
Expand Down Expand Up @@ -141,8 +141,8 @@ export const TimeSeriesComponent = React.forwardRef<HTMLDivElement, TimeSeriesPr
chartRef.current = null
}, [chartRef])

const { data, isEmptyState, setData } = useConnectedData<TimeSeriesData>({
destroyChart,
const { data, isEmptyState, setData } = useEmptyableData<TimeSeriesData>({
onEmptyData: destroyChart,
isDataEmpty: (data) => !data.values || !data.labels || data.values.length === 0
})

Expand Down
2 changes: 1 addition & 1 deletion packages/ui-kit/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export * from './storybookCodeTemplate'
export * from './themeUtils'
export * from './useCombinedRefs'
export * from './useCombinedRefsCallback'
export * from './useConnectedData'
export * from './useEmptyableData'
export * from './useForwardedRefCallback'
export * from './useStorybookAccessToken'
export * from './withThemeWrapper'
1 change: 0 additions & 1 deletion packages/ui-kit/src/helpers/useConnectedData/index.ts

This file was deleted.

29 changes: 0 additions & 29 deletions packages/ui-kit/src/helpers/useConnectedData/useConnectedData.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/ui-kit/src/helpers/useEmptyableData/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useEmptyableData'
37 changes: 37 additions & 0 deletions packages/ui-kit/src/helpers/useEmptyableData/useEmptyableData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react'

export type UseEmptyableDataProps<T> = {
isDataEmpty: (data: T) => boolean
onEmptyData: () => void
}

/**
* Returns a getter and setter for `data` where, whenever `data` changes.
*
* 1. Check if `data` is empty, using a user-supplied function `isDateEmpty`.
* 2. If it's empty, call a user-supplied function `onEmptyData`.
*
* Returns also a getter for whether the data is empty (`isEmptyState`).
*/

export const useEmptyableData = <T>({ onEmptyData, isDataEmpty }: UseEmptyableDataProps<T>) => {
const [isEmptyState, setIsEmptyState] = React.useState(false)
const [data, setData] = React.useState<T | undefined>()

React.useEffect(() => {
const isEmptyStateVal = !data || isDataEmpty(data)

setIsEmptyState(isEmptyStateVal)

if (isEmptyStateVal) {
onEmptyData()
return
}
}, [data, onEmptyData, isDataEmpty])

return {
data,
setData,
isEmptyState
}
}
Loading