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

Configurable prop for UI kit components to refetch data #93

Closed
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
5,486 changes: 2,765 additions & 2,721 deletions .pnp.cjs

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion app/examples/react-16/src/components/CounterConnectedTest.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ const { REACT_APP_PROPEL_ACCESS_TOKEN, REACT_APP_METRIC_UNIQUE_NAME_1 } = proces

export function CounterConnectedTest() {
const [fontColor, setFontColor] = React.useState('#000')
const [refetchInterval, setRefetchInterval] = React.useState(undefined)

const handleSwitchRefetchInterval = () => {
setRefetchInterval(refetchInterval ? undefined : 1000)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this just be refetchInterval ?? 1000, and in all subsequent places, too?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something else I am thinking about: there is a lot of manual work keeping the React examples in sync between 16, 17, and 18. If we diff the files, are they really so different? If not, I think we should consider

  1. Checking in one, canonical version of the CounterConnectedTest.jsx, LeaderboardConnectedText.jsx, etc. (by the way, I just notice that these should be tsx, so we can test the types 🤔).
  2. At build time, copying the canonical version of the CounterConnectedTest.jsx, LeaderboardConnectedText.jsx, etc., into the React 16, 17, and 18 folders (if there are minor patches we need to apply, we can do that).

But this may make maintenance easier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think right now there is no difference between the code of each version, I like this idea of having a canonical version of each.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this just be refetchInterval ?? 1000, and in all subsequent places, too?

That wouldn't work, here we just want to switch from undefined => 1000 and from 1000 => undefined
refetchInterval ?? 1000 would set refetchInterval to 1000 when it's undefined but wouldn't set it to undefined when it's 1000

}

return (
<div className="p-4 border-2 bg-neutral-100 border-slate-600 rounded m-3">
Expand All @@ -17,7 +22,8 @@ export function CounterConnectedTest() {
timeRange: {
relative: RelativeTimeRange.LastNDays,
n: 30
}
},
refetchInterval
}}
styles={{ font: { size: '3rem', color: fontColor } }}
/>
Expand All @@ -28,6 +34,9 @@ export function CounterConnectedTest() {
type="color"
onChange={(event) => setFontColor(event.target.value)}
/>
<button className="border-2 bg-white p-1 h-9" onClick={handleSwitchRefetchInterval}>
Refetch Interval: {refetchInterval ? 'On 1000ms' : 'Off'}
</button>
</div>
</div>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export function LeaderboardConnectedTest() {
const [barsColor, setBarsColor] = React.useState('#ccc')
const [chartType, setChartType] = React.useState('bar')

const [refetchInterval, setRefetchInterval] = React.useState(undefined)

const handleSwitchRefetchInterval = () => {
setRefetchInterval(refetchInterval ? undefined : 1000)
}

return (
<div className="p-4 border-2 bg-neutral-100 border-slate-600 rounded m-3">
<h2 className="text-2xl">Leaderboard Connected</h2>
Expand All @@ -35,7 +41,8 @@ export function LeaderboardConnectedTest() {
timeRange: {
relative: RelativeTimeRange.LastNDays,
n: 30
}
},
refetchInterval
}}
variant={chartType}
styles={{
Expand All @@ -58,6 +65,9 @@ export function LeaderboardConnectedTest() {
<option value="bar">Bar</option>
<option value="table">Table</option>
</select>
<button className="border-2 bg-white p-1 h-9" onClick={handleSwitchRefetchInterval}>
Refetch Interval: {refetchInterval ? 'On 1000ms' : 'Off'}
</button>
</div>
</div>
)
Expand Down
11 changes: 10 additions & 1 deletion app/examples/react-16/src/components/TimeSeriesConnectedTest.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export function TimeSeriesConnectedTest() {
const [barsColor, setBarsColor] = React.useState('#ccc')
const [chartType, setChartType] = React.useState('bar')
const [pointStyle, setPointStyle] = React.useState('cross')
const [refetchInterval, setRefetchInterval] = React.useState(undefined)

const handleSwitchRefetchInterval = () => {
setRefetchInterval(refetchInterval ? undefined : 1000)
}

return (
<div className="p-4 border-2 bg-neutral-100 border-slate-600 rounded m-3">
Expand All @@ -19,7 +24,8 @@ export function TimeSeriesConnectedTest() {
relative: RelativeTimeRange.LastNDays,
n: 30
},
granularity: TimeSeriesGranularity.Day
granularity: TimeSeriesGranularity.Day,
refetchInterval
}}
variant={chartType}
styles={{
Expand Down Expand Up @@ -48,6 +54,9 @@ export function TimeSeriesConnectedTest() {
>
Switch point style
</button>
<button className="border-2 bg-white p-1 h-9" onClick={handleSwitchRefetchInterval}>
Refetch Interval: {refetchInterval ? 'On 1000ms' : 'Off'}
</button>
</div>
</div>
)
Expand Down
11 changes: 10 additions & 1 deletion app/examples/react-17/src/components/CounterConnectedTest.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ const { REACT_APP_PROPEL_ACCESS_TOKEN, REACT_APP_METRIC_UNIQUE_NAME_1 } = proces

export function CounterConnectedTest() {
const [fontColor, setFontColor] = React.useState('#000')
const [refetchInterval, setRefetchInterval] = React.useState(undefined)

const handleSwitchRefetchInterval = () => {
setRefetchInterval(refetchInterval ? undefined : 1000)
}

return (
<div className="p-4 border-2 bg-neutral-100 border-slate-600 rounded m-3">
Expand All @@ -17,7 +22,8 @@ export function CounterConnectedTest() {
timeRange: {
relative: RelativeTimeRange.LastNDays,
n: 30
}
},
refetchInterval
}}
styles={{ font: { size: '3rem', color: fontColor } }}
/>
Expand All @@ -28,6 +34,9 @@ export function CounterConnectedTest() {
type="color"
onChange={(event) => setFontColor(event.target.value)}
/>
<button className="border-2 bg-white p-1 h-9" onClick={handleSwitchRefetchInterval}>
Refetch Interval: {refetchInterval ? 'On 1000ms' : 'Off'}
</button>
</div>
</div>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export function LeaderboardConnectedTest() {
const [barsColor, setBarsColor] = React.useState('#ccc')
const [chartType, setChartType] = React.useState('bar')

const [refetchInterval, setRefetchInterval] = React.useState(undefined)

const handleSwitchRefetchInterval = () => {
setRefetchInterval(refetchInterval ? undefined : 1000)
}

return (
<div className="p-4 border-2 bg-neutral-100 border-slate-600 rounded m-3">
<h2 className="text-2xl">Leaderboard Connected</h2>
Expand All @@ -35,7 +41,8 @@ export function LeaderboardConnectedTest() {
timeRange: {
relative: RelativeTimeRange.LastNDays,
n: 30
}
},
refetchInterval
}}
variant={chartType}
styles={{
Expand All @@ -58,6 +65,9 @@ export function LeaderboardConnectedTest() {
<option value="bar">Bar</option>
<option value="table">Table</option>
</select>
<button className="border-2 bg-white p-1 h-9" onClick={handleSwitchRefetchInterval}>
Refetch Interval: {refetchInterval ? 'On 1000ms' : 'Off'}
</button>
</div>
</div>
)
Expand Down
11 changes: 10 additions & 1 deletion app/examples/react-17/src/components/TimeSeriesConnectedTest.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export function TimeSeriesConnectedTest() {
const [barsColor, setBarsColor] = React.useState('#ccc')
const [chartType, setChartType] = React.useState('bar')
const [pointStyle, setPointStyle] = React.useState('cross')
const [refetchInterval, setRefetchInterval] = React.useState(undefined)

const handleSwitchRefetchInterval = () => {
setRefetchInterval(refetchInterval ? undefined : 1000)
}

return (
<div className="p-4 border-2 bg-neutral-100 border-slate-600 rounded m-3">
Expand All @@ -19,7 +24,8 @@ export function TimeSeriesConnectedTest() {
relative: RelativeTimeRange.LastNDays,
n: 30
},
granularity: TimeSeriesGranularity.Day
granularity: TimeSeriesGranularity.Day,
refetchInterval
}}
variant={chartType}
styles={{
Expand Down Expand Up @@ -48,6 +54,9 @@ export function TimeSeriesConnectedTest() {
>
Switch point style
</button>
<button className="border-2 bg-white p-1 h-9" onClick={handleSwitchRefetchInterval}>
Refetch Interval: {refetchInterval ? 'On 1000ms' : 'Off'}
</button>
</div>
</div>
)
Expand Down
11 changes: 10 additions & 1 deletion app/examples/react-18/src/components/CounterConnectedTest.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ const { REACT_APP_PROPEL_ACCESS_TOKEN, REACT_APP_METRIC_UNIQUE_NAME_1 } = proces

export function CounterConnectedTest() {
const [fontColor, setFontColor] = React.useState('#000')
const [refetchInterval, setRefetchInterval] = React.useState(undefined)

const handleSwitchRefetchInterval = () => {
setRefetchInterval(refetchInterval ? undefined : 1000)
}

return (
<div className="p-4 border-2 bg-neutral-100 border-slate-600 rounded m-3">
Expand All @@ -17,7 +22,8 @@ export function CounterConnectedTest() {
timeRange: {
relative: RelativeTimeRange.LastNDays,
n: 30
}
},
refetchInterval
}}
styles={{ font: { size: '3rem', color: fontColor } }}
/>
Expand All @@ -28,6 +34,9 @@ export function CounterConnectedTest() {
type="color"
onChange={(event) => setFontColor(event.target.value)}
/>
<button className="border-2 bg-white p-1 h-9" onClick={handleSwitchRefetchInterval}>
Refetch Interval: {refetchInterval ? 'On 1000ms' : 'Off'}
</button>
</div>
</div>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export function LeaderboardConnectedTest() {
const [barsColor, setBarsColor] = React.useState('#ccc')
const [chartType, setChartType] = React.useState('bar')

const [refetchInterval, setRefetchInterval] = React.useState(undefined)

const handleSwitchRefetchInterval = () => {
setRefetchInterval(refetchInterval ? undefined : 1000)
}

return (
<div className="p-4 border-2 bg-neutral-100 border-slate-600 rounded m-3">
<h2 className="text-2xl">Leaderboard Connected</h2>
Expand All @@ -35,7 +41,8 @@ export function LeaderboardConnectedTest() {
timeRange: {
relative: RelativeTimeRange.LastNDays,
n: 30
}
},
refetchInterval
}}
variant={chartType}
styles={{
Expand All @@ -58,6 +65,9 @@ export function LeaderboardConnectedTest() {
<option value="bar">Bar</option>
<option value="table">Table</option>
</select>
<button className="border-2 bg-white p-1 h-9" onClick={handleSwitchRefetchInterval}>
Refetch Interval: {refetchInterval ? 'On 1000ms' : 'Off'}
</button>
</div>
</div>
)
Expand Down
11 changes: 10 additions & 1 deletion app/examples/react-18/src/components/TimeSeriesConnectedTest.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export function TimeSeriesConnectedTest() {
const [barsColor, setBarsColor] = React.useState('#ccc')
const [chartType, setChartType] = React.useState('bar')
const [pointStyle, setPointStyle] = React.useState('cross')
const [refetchInterval, setRefetchInterval] = React.useState(undefined)

const handleSwitchRefetchInterval = () => {
setRefetchInterval(refetchInterval ? undefined : 1000)
}

return (
<div className="p-4 border-2 bg-neutral-100 border-slate-600 rounded m-3">
Expand All @@ -19,7 +24,8 @@ export function TimeSeriesConnectedTest() {
relative: RelativeTimeRange.LastNDays,
n: 30
},
granularity: TimeSeriesGranularity.Day
granularity: TimeSeriesGranularity.Day,
refetchInterval
}}
variant={chartType}
styles={{
Expand Down Expand Up @@ -48,6 +54,9 @@ export function TimeSeriesConnectedTest() {
>
Switch point style
</button>
<button className="border-2 bg-white p-1 h-9" onClick={handleSwitchRefetchInterval}>
Refetch Interval: {refetchInterval ? 'On 1000ms' : 'Off'}
</button>
</div>
</div>
)
Expand Down
8 changes: 8 additions & 0 deletions packages/core/graphql/src/QueryClientProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react'
import { QueryClient, QueryClientProvider as ReactQueryProvider } from '@tanstack/react-query'

const queryClient = new QueryClient()

export function QueryClientProvider({ children }: { children: React.ReactNode }) {
return <ReactQueryProvider client={queryClient}>{children}</ReactQueryProvider>
}
6 changes: 6 additions & 0 deletions packages/core/graphql/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export * from './generated'

export * from './useCounter'
export * from './useTimeSeries'
export * from './useLeaderboard'

export * from './QueryClientProvider'

// export const PROPEL_GRAPHQL_API_ENDPOINT = process.env.PROPEL_GRAPHQL_API_ENDPOINT as string
export const PROPEL_GRAPHQL_API_ENDPOINT = 'https://api.us-east-2.propeldata.com/graphql'
71 changes: 71 additions & 0 deletions packages/core/graphql/src/useCounter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import request from 'graphql-request'
import { useQuery } from '@tanstack/react-query'

import {
CounterDocument,
CounterQuery,
CounterQueryVariables,
FilterInput,
Propeller,
TimeRangeInput
} from './generated'
import { PROPEL_GRAPHQL_API_ENDPOINT } from '.'

interface Query {
/** Time range that the chart will respond to. Will be ignored when value is passed */
timeRange?: TimeRangeInput
/** This should eventually be replaced to customer's app credentials. Will be ignored when value is passed */
accessToken?: string
/** Metric unique name will be ignored when value is passed */
metric?: string
/** Filters that the chart will respond to */
filters?: FilterInput[]
/** Propeller that the chart will respond to */
propeller?: Propeller
/** Interval in milliseconds for refetching the data */
refetchInterval?: number
}

interface Options {
/** Whether the query is enabled or not */
enabled?: boolean
}

export function useCounter(query?: Query, options?: Options) {
const fetcher = () =>
request<CounterQuery, CounterQueryVariables>(
PROPEL_GRAPHQL_API_ENDPOINT,
CounterDocument,
{
counterInput: {
metricName: query?.metric,
timeRange: {
relative: query?.timeRange?.relative ?? null,
n: query?.timeRange?.n ?? null,
start: query?.timeRange?.start ?? null,
stop: query?.timeRange?.stop ?? null
},
filters: query?.filters,
propeller: query?.propeller
}
},
{
authorization: `Bearer ${query?.accessToken}`
}
)

const queryOptions = {
refetchInterval: query?.refetchInterval,
enabled: options?.enabled && !!query,
retry: false
}

const cacheKey = ['propel-counter', query]

const { data, isLoading: isLoadingQuery, error } = useQuery(cacheKey, fetcher, queryOptions)

const value = data?.counter?.value
const isLoading = options?.enabled ? isLoadingQuery : false

return { value, isLoading, error }
}
Loading