Refactor client config (#529)

* Refactor client config

* Fix server tests

* Fix lint
This commit is contained in:
Charles Bochet
2023-07-07 11:10:42 -07:00
committed by GitHub
parent 11d18cc269
commit 26b033abc9
38 changed files with 386 additions and 180 deletions

View File

@ -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],
);
}

View File

@ -1,4 +0,0 @@
export function useIsTelemetryEnabled() {
// TODO: replace by clientConfig
return process.env.IS_TELEMETRY_ENABLED !== 'false';
}

View File

@ -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) {

View File

@ -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),

View File

@ -1,2 +1 @@
export * from './select';
export * from './update';

View File

@ -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
}
}
`;

View File

@ -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: '',
});

View File

@ -1,6 +0,0 @@
import { atom } from 'recoil';
export const displayGoogleLogin = atom<boolean>({
key: 'displayGoogleLogin',
default: true,
});

View File

@ -1,6 +0,0 @@
import { atom } from 'recoil';
export const prefillLoginWithSeed = atom<boolean>({
key: 'prefillLoginWithSeed',
default: true,
});

View 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
}
}
}
`;

View File

@ -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 },
});

View File

@ -0,0 +1,6 @@
import { atom } from 'recoil';
export const isDebugModeState = atom<boolean>({
key: 'isDebugModeState',
default: false,
});

View File

@ -0,0 +1,6 @@
import { atom } from 'recoil';
export const isDemoModeState = atom<boolean>({
key: 'isDemoModeState',
default: false,
});

View 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 },
});