Skip to content

Commit

Permalink
[skip ci] refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tom2drum committed Jan 11, 2024
1 parent d3517cc commit 5102f40
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 15 deletions.
2 changes: 2 additions & 0 deletions lib/growthbook/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const STORAGE_KEY = 'growthbook:experiments';
export const STORAGE_LIMIT = 20;
5 changes: 2 additions & 3 deletions lib/growthbook/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { GrowthBook } from '@growthbook/growthbook-react';
import config from 'configs/app';
import * as mixpanel from 'lib/mixpanel';

import { STORAGE_KEY, STORAGE_LIMIT } from './consts';

export interface GrowthBookFeatures {
test_value: string;
}
Expand Down Expand Up @@ -37,9 +39,6 @@ export const growthBook = (() => {
});
})();

const STORAGE_KEY = 'growthbook:experiments';
const STORAGE_LIMIT = 20;

function getStorageValue(): Array<unknown> | undefined {
const item = window.localStorage.getItem(STORAGE_KEY);
if (!item) {
Expand Down
17 changes: 17 additions & 0 deletions lib/growthbook/useLoadFeatures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';

import { SECOND } from 'lib/consts';

import { growthBook } from './init';

export default function useLoadFeatures() {
React.useEffect(() => {
growthBook?.setAttributes({
...growthBook.getAttributes(),
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
language: window.navigator.language,
});

growthBook?.loadFeatures({ timeout: SECOND });
}, []);
}
6 changes: 6 additions & 0 deletions lib/mixpanel/getUuid.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as cookies from 'lib/cookies';
import * as growthBook from 'lib/growthbook/consts';
import isBrowser from 'lib/isBrowser';

export default function getUuid() {
const cookie = cookies.get(cookies.NAMES.UUID);
Expand All @@ -10,5 +12,9 @@ export default function getUuid() {
const uuid = crypto.randomUUID();
cookies.set(cookies.NAMES.UUID, uuid);

if (isBrowser()) {
window.localStorage.removeItem(growthBook.STORAGE_KEY);
}

return uuid;
}
8 changes: 0 additions & 8 deletions lib/mixpanel/useInit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { deviceType } from 'react-device-detect';

import config from 'configs/app';
import * as cookies from 'lib/cookies';
import { growthBook } from 'lib/growthbook/init';
import getQueryParamString from 'lib/router/getQueryParamString';

import getUuid from './getUuid';
Expand All @@ -27,13 +26,6 @@ export default function useMixpanelInit() {

const mixpanelConfig: Partial<Config> = {
debug: Boolean(debugFlagQuery.current || debugFlagCookie),
loaded: function() {
growthBook?.setAttributes({
...growthBook.getAttributes(),
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
language: window.navigator.language,
});
},
};
const isAuth = Boolean(cookies.get(cookies.NAMES.API_TOKEN));

Expand Down
6 changes: 2 additions & 4 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import type { NextPageWithLayout } from 'nextjs/types';

import config from 'configs/app';
import useQueryClientConfig from 'lib/api/useQueryClientConfig';
import { SECOND } from 'lib/consts';
import { AppContextProvider } from 'lib/contexts/app';
import { ChakraProvider } from 'lib/contexts/chakra';
import { ScrollDirectionProvider } from 'lib/contexts/scrollDirection';
import { growthBook } from 'lib/growthbook/init';
import useLoadFeatures from 'lib/growthbook/useLoadFeatures';
import { SocketProvider } from 'lib/socket/context';
import theme from 'theme';
import AppErrorBoundary from 'ui/shared/AppError/AppErrorBoundary';
Expand Down Expand Up @@ -42,9 +42,7 @@ const ERROR_SCREEN_STYLES: ChakraProps = {

function MyApp({ Component, pageProps }: AppPropsWithLayout) {

React.useEffect(() => {
growthBook?.loadFeatures({ timeout: SECOND });
}, []);
useLoadFeatures();

const queryClient = useQueryClientConfig();

Expand Down

0 comments on commit 5102f40

Please sign in to comment.