-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Refactor FE API v2 interface + current visitors bugfix #6351
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
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
224bbdc
more specific type to event:props:* dimension
RobertJoonas 643b53f
rename use-order-by.ts -> use-metric-order-by.ts
RobertJoonas 53e0a45
SortDirection: use string union type from query-api-schema instead of…
RobertJoonas 5915c9d
use query-api-schema pagination type too
RobertJoonas 19ecf02
add proper OrderBy type (with dimensions) and separate MetricOrderBy
RobertJoonas 7990e9c
use NonTimeDimension type in v2 breakdown components
RobertJoonas 320d00a
also create a Dimension type (incl time dims)
RobertJoonas 567d615
create use-query-api.ts
RobertJoonas 897104a
add a useQueryApi hook and use it in IndexBreakdown
RobertJoonas de0c419
Add CurrentVisitorsContext
RobertJoonas 2b419a1
top stats and main graph to useQueryApi
RobertJoonas cd9eb05
DetailsBreakdown: addSearchFilter -> searchEnabled
RobertJoonas 91f2ed9
Merge remote-tracking branch 'origin/master' into fe-api-v2-refactor
RobertJoonas f395656
extractIntervalFromDimensions utility
RobertJoonas b0d5034
create new statsQuery with spread
RobertJoonas 47380b4
remove redundant useMemo
RobertJoonas a8efa58
remove redundant as type casts
RobertJoonas 3b02b1f
move and improve comment
RobertJoonas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import React, { createContext, useContext, useEffect } from 'react' | ||
| import { useQuery, useQueryClient } from '@tanstack/react-query' | ||
| import { get } from './api' | ||
| import { useSiteContext } from './site-context' | ||
| import { useDashboardStateContext } from './dashboard-state-context' | ||
| import { CACHE_TTL_REALTIME } from './hooks/api-client' | ||
| import { isRealTimeDashboard } from './util/filters' | ||
|
|
||
| const CurrentVisitorsContext = createContext<number | null>(null) | ||
|
|
||
| export function CurrentVisitorsProvider({ | ||
| children | ||
| }: { | ||
| children: React.ReactNode | ||
| }) { | ||
| const site = useSiteContext() | ||
| const { dashboardState } = useDashboardStateContext() | ||
| const queryClient = useQueryClient() | ||
|
|
||
| const isEnabled = | ||
| isRealTimeDashboard(dashboardState) || dashboardState.filters.length === 0 | ||
|
|
||
| const { data } = useQuery<number>({ | ||
| queryKey: ['current-visitors'], | ||
| queryFn: () => | ||
| get(`/api/stats/${encodeURIComponent(site.domain)}/current-visitors`), | ||
| staleTime: CACHE_TTL_REALTIME, | ||
| enabled: isEnabled | ||
| }) | ||
|
|
||
| useEffect(() => { | ||
| const onTick = () => { | ||
| queryClient.invalidateQueries({ queryKey: ['current-visitors'] }) | ||
| } | ||
| document.addEventListener('tick', onTick) | ||
| return () => document.removeEventListener('tick', onTick) | ||
| }, [queryClient]) | ||
|
|
||
| return ( | ||
| <CurrentVisitorsContext.Provider value={isEnabled ? (data ?? null) : null}> | ||
| {children} | ||
| </CurrentVisitorsContext.Provider> | ||
| ) | ||
| } | ||
|
|
||
| export const useCurrentVisitorsContext = () => | ||
| useContext(CurrentVisitorsContext) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue, non-blocking: I think
useQueryhas API for this so we don't actually need to hand roll it. Check the description formetaat https://tanstack.com/query/latest/docs/framework/react/reference/useQuery. I didn't try it yet locally, but I think we shouldThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that solves the problem with rendering previousData. As soon as
queryKeychanges,metadoes too. In other words, it will always represent thedashboardStateof the current (new) query, rather than placeholderData.