-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d1f865
commit 5466608
Showing
47 changed files
with
1,652 additions
and
277 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,63 @@ | ||
import { TelemetryProvider } from '@/contexts/TelemetryContext' | ||
import { OidcProvider } from '@/oidc' | ||
import { routeTree } from '@/router/router' | ||
import { MuiDsfrThemeProvider } from '@codegouvfr/react-dsfr/mui' | ||
import { startReactDsfr } from '@codegouvfr/react-dsfr/spa' | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
import { | ||
Link, | ||
RouterProvider, | ||
createRouter, | ||
type LinkProps, | ||
} from '@tanstack/react-router' | ||
|
||
startReactDsfr({ | ||
defaultColorScheme: 'system', | ||
Link, | ||
}) | ||
|
||
declare module '@codegouvfr/react-dsfr/spa' { | ||
interface RegisterLink { | ||
Link: (props: LinkProps) => JSX.Element | ||
} | ||
} | ||
|
||
const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
mutations: { | ||
networkMode: 'always', | ||
}, | ||
}, | ||
}) | ||
|
||
const router = createRouter({ | ||
routeTree, | ||
context: { | ||
queryClient, | ||
}, | ||
defaultPreloadStaleTime: 0, | ||
}) | ||
|
||
declare module '@tanstack/react-router' { | ||
interface Register { | ||
router: typeof router | ||
} | ||
} | ||
|
||
/** Wraps and inits the providers used in the app */ | ||
export function App() { | ||
return ( | ||
<MuiDsfrThemeProvider> | ||
<QueryClientProvider client={queryClient}> | ||
<OidcProvider> | ||
<TelemetryProvider> | ||
<RouterProvider | ||
router={router} | ||
basepath={import.meta.env.VITE_BASE_PATH} | ||
/> | ||
</TelemetryProvider> | ||
</OidcProvider> | ||
</QueryClientProvider> | ||
</MuiDsfrThemeProvider> | ||
) | ||
} |
This file contains 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,5 @@ | ||
export enum MODE_TYPE { | ||
COLLECT = 'collect', | ||
REVIEW = 'review', | ||
VISUALIZE = 'visualize', | ||
} |
This file contains 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,6 @@ | ||
export enum PAGE_TYPE { | ||
WELCOME = 'welcomePage', | ||
VALIDATION = 'validationPage', | ||
END = 'endPage', | ||
LUNATIC = 'lunaticPage', | ||
} |
This file contains 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,13 @@ | ||
export enum TELEMETRY_EVENT_TYPE { | ||
CONTACT_SUPPORT = 'contact-support', | ||
CONTROL = 'control', | ||
CONTROL_SKIP = 'control-skip', | ||
EXIT = 'exit', | ||
INIT = 'initialized', | ||
INPUT = 'input', | ||
NEW_PAGE = 'new-page', | ||
} | ||
|
||
export enum TELEMETRY_EVENT_EXIT_SOURCE { | ||
LOGOUT = 'logout', | ||
} |
This file contains 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,52 @@ | ||
import { computeInitEvent } from '@/utils/telemetry' | ||
import '@testing-library/jest-dom' | ||
import { renderHook } from '@testing-library/react' | ||
import { TelemetryContext, useTelemetry } from './TelemetryContext' | ||
|
||
describe('Telemetry context', () => { | ||
test('push events', () => { | ||
const mock = vi.fn() | ||
|
||
const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
<TelemetryContext.Provider | ||
value={{ | ||
isTelemetryDisabled: false, | ||
pushEvent: mock, | ||
setDefaultValues: () => {}, | ||
}} | ||
> | ||
{children} | ||
</TelemetryContext.Provider> | ||
) | ||
|
||
const { result } = renderHook(() => useTelemetry(), { wrapper }) | ||
|
||
const myEvent = computeInitEvent() | ||
result.current.pushEvent(myEvent) | ||
|
||
expect(mock).toHaveBeenCalledWith(myEvent) | ||
}) | ||
|
||
test('set default values', () => { | ||
const mock = vi.fn() | ||
|
||
const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
<TelemetryContext.Provider | ||
value={{ | ||
isTelemetryDisabled: false, | ||
pushEvent: () => {}, | ||
setDefaultValues: mock, | ||
}} | ||
> | ||
{children} | ||
</TelemetryContext.Provider> | ||
) | ||
|
||
const { result } = renderHook(() => useTelemetry(), { wrapper }) | ||
|
||
const myValues = { idSU: 'abc' } | ||
result.current.setDefaultValues(myValues) | ||
|
||
expect(mock).toHaveBeenCalledWith(myValues) | ||
}) | ||
}) |
This file contains 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,104 @@ | ||
/* eslint-disable react-refresh/only-export-components */ | ||
import { addParadata } from '@/api/07-paradata-events' | ||
import { useBatch } from '@/shared/hooks/useBatch' | ||
import type { | ||
DefaultParadataValues, | ||
TelemetryEvent, | ||
TelemetryParadata, | ||
} from '@/types/telemetry' | ||
import { | ||
createContext, | ||
useCallback, | ||
useContext, | ||
useMemo, | ||
useState, | ||
} from 'react' | ||
|
||
type TelemetryContextType = { | ||
isTelemetryDisabled: boolean | ||
pushEvent: (e: TelemetryParadata) => void | Promise<boolean> | ||
setDefaultValues: (e: DefaultParadataValues) => void | ||
triggerBatchTelemetryCallback?: () => Promise<void> | ||
} | ||
|
||
/** Mandatory values used as a context's last-resort fallback */ | ||
const defaultValues = { | ||
isTelemetryDisabled: true, | ||
pushEvent: (_: TelemetryParadata) => {}, | ||
setDefaultValues: (_: DefaultParadataValues) => {}, | ||
} | ||
|
||
/** | ||
* Exposes shared functions to handle telemetry events. | ||
* | ||
* Should be used with the useBatch hook to reduce external API load. | ||
*/ | ||
export const TelemetryContext: React.Context<TelemetryContextType> = | ||
createContext(defaultValues) | ||
|
||
/** | ||
* Returns the current telemetry context value. | ||
* | ||
* @version 1.2.4 | ||
* @see https://react.dev/reference/react/useContext | ||
*/ | ||
export function useTelemetry() { | ||
return useContext(TelemetryContext) | ||
} | ||
|
||
/** Initializes the telemetry context with a batch system */ | ||
export function TelemetryProvider({ | ||
children, | ||
}: Readonly<{ | ||
children: React.ReactElement | ||
}>) { | ||
const isTelemetryDisabled = import.meta.env.VITE_TELEMETRY_DISABLED === 'true' | ||
|
||
const [defaultValues, setDefaultValues] = useState<DefaultParadataValues>({ | ||
userAgent: navigator.userAgent, | ||
}) | ||
|
||
/** Push events to telemetry API after an arbitrary number of events or a delay. */ | ||
const pushEvents = useCallback(async (events: Array<TelemetryEvent>) => { | ||
if (events.length > 0) { | ||
return addParadata({ idSU: events[0].idSU, events }) | ||
} | ||
}, []) | ||
|
||
const { addDatum, triggerTimeoutEvent } = useBatch(pushEvents) | ||
|
||
/** Add the event to a batch mechanism. */ | ||
const pushEvent = useCallback( | ||
(event: TelemetryParadata) => { | ||
addDatum({ ...defaultValues, ...event }) | ||
}, | ||
[addDatum, defaultValues] | ||
) | ||
|
||
/** Add values that will be appended to every telemetry event (e.g. user id) */ | ||
const updateDefaultValues = useCallback( | ||
(newDefaultValues: DefaultParadataValues) => { | ||
setDefaultValues((oldValues: DefaultParadataValues) => ({ | ||
...oldValues, | ||
...newDefaultValues, | ||
})) | ||
}, | ||
[] | ||
) | ||
|
||
const telemetryContextValues = useMemo( | ||
() => ({ | ||
isTelemetryDisabled, | ||
pushEvent, | ||
setDefaultValues: updateDefaultValues, | ||
triggerBatchTelemetryCallback: triggerTimeoutEvent, | ||
}), | ||
[isTelemetryDisabled, pushEvent, triggerTimeoutEvent, updateDefaultValues] | ||
) | ||
|
||
return ( | ||
<TelemetryContext.Provider value={telemetryContextValues}> | ||
{children} | ||
</TelemetryContext.Provider> | ||
) | ||
} |
This file contains 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 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.