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

Function useLogSharedProps

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

Source from the content-addressed store, hash-verified

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

Callers 1

LogContextProviderFunction · 0.85

Calls 3

getCookiesFunction · 0.90
useRouterFunction · 0.50

Tested by

no test coverage detected