Refactor client config (#529)
* Refactor client config * Fix server tests * Fix lint
This commit is contained in:
@ -1,9 +1,9 @@
|
||||
import { useCallback } from 'react';
|
||||
import { useRecoilState } from 'recoil';
|
||||
|
||||
import { telemetryState } from '@/client-config/states/telemetryState';
|
||||
import { useCreateEventMutation } from '~/generated/graphql';
|
||||
|
||||
import { useIsTelemetryEnabled } from './useIsTelemetryEnabled';
|
||||
|
||||
interface EventLocation {
|
||||
pathname: string;
|
||||
}
|
||||
@ -13,12 +13,12 @@ export interface EventData {
|
||||
}
|
||||
|
||||
export function useEventTracker() {
|
||||
const telemetryEnabled = useIsTelemetryEnabled();
|
||||
const [telemetry] = useRecoilState(telemetryState);
|
||||
const [createEventMutation] = useCreateEventMutation();
|
||||
|
||||
return useCallback(
|
||||
(eventType: string, eventData: EventData) => {
|
||||
if (telemetryEnabled) {
|
||||
if (telemetry.enabled) {
|
||||
createEventMutation({
|
||||
variables: {
|
||||
type: eventType,
|
||||
@ -27,6 +27,6 @@ export function useEventTracker() {
|
||||
});
|
||||
}
|
||||
},
|
||||
[createEventMutation, telemetryEnabled],
|
||||
[createEventMutation, telemetry],
|
||||
);
|
||||
}
|
||||
|
||||
@ -1,4 +0,0 @@
|
||||
export function useIsTelemetryEnabled() {
|
||||
// TODO: replace by clientConfig
|
||||
return process.env.IS_TELEMETRY_ENABLED !== 'false';
|
||||
}
|
||||
@ -8,6 +8,7 @@ import { useRecoilState } from 'recoil';
|
||||
|
||||
import { isMockModeState } from '@/auth/states/isMockModeState';
|
||||
import { tokenPairState } from '@/auth/states/tokenPairState';
|
||||
import { isDebugModeState } from '@/client-config/states/isDebugModeState';
|
||||
import { CommentThreadTarget } from '~/generated/graphql';
|
||||
import { mockedCompaniesData } from '~/testing/mock-data/companies';
|
||||
import { mockedUsersData } from '~/testing/mock-data/users';
|
||||
@ -16,6 +17,7 @@ import { ApolloFactory } from '../services/apollo.factory';
|
||||
|
||||
export function useApolloFactory() {
|
||||
const apolloRef = useRef<ApolloFactory<NormalizedCacheObject> | null>(null);
|
||||
const [isDebugMode] = useRecoilState(isDebugModeState);
|
||||
|
||||
const [tokenPair, setTokenPair] = useRecoilState(tokenPairState);
|
||||
const [isMockMode] = useRecoilState(isMockModeState);
|
||||
@ -64,10 +66,11 @@ export function useApolloFactory() {
|
||||
setTokenPair(null);
|
||||
},
|
||||
extraLinks: isMockMode ? [mockLink] : [],
|
||||
isDebugMode,
|
||||
});
|
||||
|
||||
return apolloRef.current.getClient();
|
||||
}, [isMockMode, setTokenPair]);
|
||||
}, [isMockMode, setTokenPair, isDebugMode]);
|
||||
|
||||
useEffect(() => {
|
||||
if (apolloRef.current) {
|
||||
|
||||
@ -29,6 +29,7 @@ export interface Options<TCacheShape> extends ApolloClientOptions<TCacheShape> {
|
||||
onTokenPairChange?: (tokenPair: AuthTokenPair) => void;
|
||||
onUnauthenticatedError?: () => void;
|
||||
extraLinks?: ApolloLink[];
|
||||
isDebugMode?: boolean;
|
||||
}
|
||||
|
||||
export class ApolloFactory<TCacheShape> implements ApolloManager<TCacheShape> {
|
||||
@ -43,6 +44,7 @@ export class ApolloFactory<TCacheShape> implements ApolloManager<TCacheShape> {
|
||||
onTokenPairChange,
|
||||
onUnauthenticatedError,
|
||||
extraLinks,
|
||||
isDebugMode,
|
||||
...options
|
||||
} = opts;
|
||||
|
||||
@ -98,7 +100,7 @@ export class ApolloFactory<TCacheShape> implements ApolloManager<TCacheShape> {
|
||||
return forward(operation);
|
||||
}
|
||||
default:
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
if (isDebugMode) {
|
||||
console.warn(
|
||||
`[GraphQL error]: Message: ${
|
||||
graphQLError.message
|
||||
@ -114,7 +116,7 @@ export class ApolloFactory<TCacheShape> implements ApolloManager<TCacheShape> {
|
||||
}
|
||||
|
||||
if (networkError) {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
if (isDebugMode) {
|
||||
console.warn(`[Network error]: ${networkError}`);
|
||||
}
|
||||
onNetworkError?.(networkError);
|
||||
@ -127,8 +129,7 @@ export class ApolloFactory<TCacheShape> implements ApolloManager<TCacheShape> {
|
||||
errorLink,
|
||||
authLink,
|
||||
...(extraLinks ? extraLinks : []),
|
||||
// Only show logger in dev mode
|
||||
process.env.NODE_ENV !== 'production' ? logger : null,
|
||||
isDebugMode ? logger : null,
|
||||
retryLink,
|
||||
httpLink,
|
||||
].filter(assertNotNull),
|
||||
|
||||
@ -1,2 +1 @@
|
||||
export * from './select';
|
||||
export * from './update';
|
||||
|
||||
@ -1,10 +0,0 @@
|
||||
import { gql } from '@apollo/client';
|
||||
|
||||
export const GET_CLIENT_CONFIG = gql`
|
||||
query GetClientConfig {
|
||||
clientConfig {
|
||||
display_google_login
|
||||
prefill_login_with_seed
|
||||
}
|
||||
}
|
||||
`;
|
||||
@ -1,6 +1,6 @@
|
||||
import { atom } from 'recoil';
|
||||
|
||||
export const authFlowUserEmailState = atom({
|
||||
export const authFlowUserEmailState = atom<string>({
|
||||
key: 'authFlowUserEmailState',
|
||||
default: process.env.NODE_ENV === 'development' ? 'tim@apple.dev' : '',
|
||||
default: '',
|
||||
});
|
||||
|
||||
@ -1,6 +0,0 @@
|
||||
import { atom } from 'recoil';
|
||||
|
||||
export const displayGoogleLogin = atom<boolean>({
|
||||
key: 'displayGoogleLogin',
|
||||
default: true,
|
||||
});
|
||||
@ -1,6 +0,0 @@
|
||||
import { atom } from 'recoil';
|
||||
|
||||
export const prefillLoginWithSeed = atom<boolean>({
|
||||
key: 'prefillLoginWithSeed',
|
||||
default: true,
|
||||
});
|
||||
18
front/src/modules/client-config/queries/index.tsx
Normal file
18
front/src/modules/client-config/queries/index.tsx
Normal file
@ -0,0 +1,18 @@
|
||||
import { gql } from '@apollo/client';
|
||||
|
||||
export const GET_CLIENT_CONFIG = gql`
|
||||
query GetClientConfig {
|
||||
clientConfig {
|
||||
authProviders {
|
||||
google
|
||||
password
|
||||
}
|
||||
demoMode
|
||||
debugMode
|
||||
telemetry {
|
||||
enabled
|
||||
anonymizationEnabled
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
@ -0,0 +1,8 @@
|
||||
import { atom } from 'recoil';
|
||||
|
||||
import { AuthProviders } from '~/generated/graphql';
|
||||
|
||||
export const authProvidersState = atom<AuthProviders>({
|
||||
key: 'authProvidersState',
|
||||
default: { google: false, magicLink: false, password: true },
|
||||
});
|
||||
@ -0,0 +1,6 @@
|
||||
import { atom } from 'recoil';
|
||||
|
||||
export const isDebugModeState = atom<boolean>({
|
||||
key: 'isDebugModeState',
|
||||
default: false,
|
||||
});
|
||||
@ -0,0 +1,6 @@
|
||||
import { atom } from 'recoil';
|
||||
|
||||
export const isDemoModeState = atom<boolean>({
|
||||
key: 'isDemoModeState',
|
||||
default: false,
|
||||
});
|
||||
8
front/src/modules/client-config/states/telemetryState.ts
Normal file
8
front/src/modules/client-config/states/telemetryState.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { atom } from 'recoil';
|
||||
|
||||
import { Telemetry } from '~/generated/graphql';
|
||||
|
||||
export const telemetryState = atom<Telemetry>({
|
||||
key: 'telemetryState',
|
||||
default: { enabled: true, anonymizationEnabled: true },
|
||||
});
|
||||
Reference in New Issue
Block a user