Update selected item color in Pass menu
[ProtonMail-WebClient.git] / packages / pass / hooks / useSelectItemAction.ts
blob7454020ab6ca058b617d42f7463aae5bbcb5c45f
1 import { useCallback } from 'react';
3 import type { ItemSelectOptions } from '@proton/pass/components/Navigation/NavigationProvider';
4 import { useNavigation } from '@proton/pass/components/Navigation/NavigationProvider';
5 import type { ItemRevision } from '@proton/pass/types';
6 import { B2BEventName } from '@proton/pass/types/data/b2b';
7 import { TelemetryEventName, TelemetryItemType } from '@proton/pass/types/data/telemetry';
8 import { getEpoch } from '@proton/pass/utils/time/epoch';
10 import { usePassCore } from '../components/Core/PassCoreProvider';
11 import { itemEq } from '../lib/items/item.predicates';
12 import { isEmptyString } from '../utils/string/is-empty-string';
14 /** Wraps the base `NavigationContextValue::selectItem` function and adds
15  * telemetry event dispatches depending on the current filtering context. */
16 export const useSelectItemAction = () => {
17     const { onTelemetry, onB2BEvent } = usePassCore();
18     const { selectItem, filters, selectedItem } = useNavigation();
19     const { search } = filters;
21     return useCallback(
22         (item: ItemRevision, options: ItemSelectOptions) => {
23             /* noop if item is already selected */
24             if (selectedItem && itemEq(selectedItem)(item)) return;
26             const { itemId, shareId } = item;
27             const { type } = item.data;
29             selectItem(shareId, itemId, options);
31             onTelemetry(TelemetryEventName.ItemRead, {}, { type: TelemetryItemType[type] });
32             onB2BEvent({ name: B2BEventName.ItemRead, timestamp: getEpoch(), itemId, shareId });
34             if (!isEmptyString(search)) onTelemetry(TelemetryEventName.SearchClick, {}, {});
35         },
36         [search, selectedItem]
37     );