Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / shared / lib / helpers / collapsibleSidebar.ts
blob7b73445ec76f8fc7aa8bbd5311579f9bd29c79c4
1 import type { RefObject } from 'react';
2 import { useEffect } from 'react';
3 import { useState } from 'react';
5 import { TelemetryCollapsibleLeftSidebarEvents, TelemetryMeasurementGroups } from '@proton/shared/lib/api/telemetry';
6 import { sendTelemetryReport } from '@proton/shared/lib/helpers/metrics';
8 import type { APP_NAMES } from '../constants';
9 import type { Api } from '../interfaces';
11 export const enum COLLAPSE_EVENTS {
12     COLLAPSE = 'COLLAPSE',
13     EXPAND = 'EXPAND',
15 export const enum SOURCE_EVENT {
16     BUTTON_SIDEBAR = 'BUTTON_SIDEBAR',
17     BUTTON_FOLDERS = 'BUTTON_FOLDERS',
18     BUTTON_LABELS = 'BUTTON_LABELS',
21 export const sendRequestCollapsibleSidebarReport = ({
22     api,
23     action,
24     application,
25     sourceEvent,
26 }: {
27     api: Api;
28     action: COLLAPSE_EVENTS;
29     application: APP_NAMES;
30     sourceEvent: SOURCE_EVENT;
31 }) => {
32     void sendTelemetryReport({
33         api,
34         measurementGroup: TelemetryMeasurementGroups.collapsibleLeftSidebar,
35         event: TelemetryCollapsibleLeftSidebarEvents.toggleLeftSidebar,
36         dimensions: {
37             action,
38             application,
39             sourceEvent,
40         },
41     });
44 interface Props {
45     navigationRef: RefObject<HTMLElement>;
48 const checkIsScrollPresent = ({ navigationRef }: Props) => {
49     if (!navigationRef?.current) {
50         return false;
51     }
53     const navigationScrollHeight = navigationRef.current.scrollHeight;
54     const navigationClientHeight = navigationRef.current.clientHeight;
56     return navigationClientHeight !== navigationScrollHeight;
59 export const useLeftSidebarButton = ({ navigationRef }: Props) => {
60     const [isScrollPresent, setIsScrollPresent] = useState(false);
62     useEffect(() => {
63         const intervalID = window.setInterval(() => {
64             setIsScrollPresent(checkIsScrollPresent({ navigationRef }));
65         }, 350); // we'll optimize with observers later
67         return () => {
68             clearInterval(intervalID);
69         };
70     }, []);
72     return { isScrollPresent };