MCPcopy Create free account
hub / github.com/dailydotdev/apps / useLogSharedProps

Function useLogSharedProps

packages/shared/src/hooks/log/useLogSharedProps.ts:11–100  ·  view source on GitHub ↗
(
  app: string,
  version: string,
  deviceId: string,
)

Source from the content-addressed store, hash-verified

9const COOKIES = ['_ga', '_fbp', '_fbc', 'gbuuid'];
10
11export default function useLogSharedProps(
12 app: string,
13 version: string,
14 deviceId: string,
15): [MutableRefObject<Partial<LogEvent>>, boolean] {
16 // Use ref instead of state to reduce renders
17 const sharedPropsRef = useRef<Partial<LogEvent>>({});
18 const { query } = useRouter();
19 const { themeMode, spaciness, insaneMode } = useContext(SettingsContext);
20 const { visit, anonymous, tokenRefreshed, user } = useContext(AuthContext);
21 const [sharedPropsSet, setSharedPropsSet] = useState(false);
22
23 const [visitId, setVisitId] = useState<string>();
24 useEffect(() => {
25 // Visit ID should be set only at the beginning
26 if (tokenRefreshed && !visitId) {
27 setVisitId(visit?.visitId);
28 }
29 }, [tokenRefreshed, visit?.visitId, setVisitId, visitId]);
30
31 useEffect(() => {
32 if (!visitId || !deviceId) {
33 return;
34 }
35
36 const queryObject = { ...query };
37
38 const initialQuerySearchParams = new URLSearchParams(
39 window.location.search,
40 );
41
42 // initial useRouter.query can be empty on prerendered static pages
43 // so we add any missing initial query params to the query object
44 // that we pass to log
45 initialQuerySearchParams.forEach((value, key) => {
46 if (!queryObject[key]) {
47 queryObject[key] = value;
48 }
49 });
50
51 const queryStr = JSON.stringify(queryObject);
52 const cookies = JSON.stringify(getCookies(COOKIES));
53
54 (sharedPropsRef.current?.device_id
55 ? Promise.resolve(sharedPropsRef.current.device_id)
56 : Promise.resolve(deviceId)
57 ).then((_deviceId) => {
58 sharedPropsRef.current = {
59 app_platform: app,
60 app_theme: themeMode,
61 app_version: version,
62 feed_density: spaciness,
63 feed_layout: insaneMode ? 'list' : 'cards',
64 // By default query equals '{}'
65 query_params: queryStr.length > 2 ? queryStr : undefined,
66 session_id: visit?.sessionId,
67 user_first_visit: anonymous?.firstVisit,
68 user_id: anonymous?.id,

Callers 1

LogContextProviderFunction · 0.85

Calls 2

getCookiesFunction · 0.90
useRouterFunction · 0.50

Tested by

no test coverage detected