<commit_before>import { useRef } from 'react';
import { Message } from 'proton-shared/lib/interfaces/mail/Message';
import { HotkeyTuple, useHotkeys } from 'react-components';

export interface ConversationHotkeysContext {
    messages: Message[];
    focusIndex: number | undefined;
}

export interface ConversationHotkeysHandlers {
    handleFocus: (index: number | undefined) => void;
    openMessage: (ID: string) => void;
    getFocusedId: () => string | undefined;
}

export const useConversationHotkeys = (
    { messages, focusIndex }: ConversationHotkeysContext,
    { handleFocus, openMessage, getFocusedId }: ConversationHotkeysHandlers
) => {
    const elementRef = useRef(null);
    const shortcutHandlers: HotkeyTuple[] = [
        [
            'ArrowLeft',
            (e) => {
                e.preventDefault();
                e.stopPropagation();
                const element =
                    (document.querySelector(
                        '[data-shortcut-target="item-container"][data-shortcut-target-selected="true"]'
                    ) as HTMLElement) ||
                    (document.querySelector('[data-shortcut-target="item-container"]') as HTMLElement);
                element?.focus();
                handleFocus(undefined);
            },
        ],
        [
            ['Meta', 'ArrowUp'],
            (e) => {
                e.stopPropagation();
                handleFocus(0);
            },
        ],
        [
            'ArrowUp',

            (e) => {
                e.stopPropagation();
                const previousIndex = focusIndex !== undefined ? Math.max(0, focusIndex - 1) : messages.length - 1;
                // leave the browser arrow scroll for last email
                if (previousIndex !== 0) {
                    e.preventDefault();
                }
                handleFocus(previousIndex);
            },
        ],
        [
            ['Meta', 'ArrowDown'],

            (e) => {
                e.stopPropagation();
                handleFocus(messages.length - 1);
            },
        ],
        [
            'ArrowDown',

            (e) => {
                e.stopPropagation();
                const lastIndex = messages.length - 1;
                const nextIndex = focusIndex !== undefined ? Math.min(lastIndex, focusIndex + 1) : 0;
                // leave the browser arrow scroll for last email
                if (nextIndex !== lastIndex) {
                    e.preventDefault();
                }
                handleFocus(nextIndex);
            },
        ],
        [
            'Enter',
            (e) => {
                e.preventDefault();
                e.stopPropagation();
                const id = getFocusedId();
                if (id) {
                    openMessage(id);
                }
            },
        ],
    ];<commit_msg>Remove useless preventDefault on Enter on Conversation hotkeys<commit_after>import { useRef } from 'react';
import { Message } from 'proton-shared/lib/interfaces/mail/Message';
import { HotkeyTuple, useHotkeys } from 'react-components';

export interface ConversationHotkeysContext {
    messages: Message[];
    focusIndex: number | undefined;
}

export interface ConversationHotkeysHandlers {
    handleFocus: (index: number | undefined) => void;
    openMessage: (ID: string) => void;
    getFocusedId: () => string | undefined;
}

export const useConversationHotkeys = (
    { messages, focusIndex }: ConversationHotkeysContext,
    { handleFocus, openMessage, getFocusedId }: ConversationHotkeysHandlers
) => {
    const elementRef = useRef(null);
    const shortcutHandlers: HotkeyTuple[] = [
        [
            'ArrowLeft',
            (e) => {
                e.preventDefault();
                e.stopPropagation();
                const element =
                    (document.querySelector(
                        '[data-shortcut-target="item-container"][data-shortcut-target-selected="true"]'
                    ) as HTMLElement) ||
                    (document.querySelector('[data-shortcut-target="item-container"]') as HTMLElement);
                element?.focus();
                handleFocus(undefined);
            },
        ],
        [
            ['Meta', 'ArrowUp'],
            (e) => {
                e.stopPropagation();
                handleFocus(0);
            },
        ],
        [
            'ArrowUp',

            (e) => {
                e.stopPropagation();
                const previousIndex = focusIndex !== undefined ? Math.max(0, focusIndex - 1) : messages.length - 1;
                // leave the browser arrow scroll for last email
                if (previousIndex !== 0) {
                    e.preventDefault();
                }
                handleFocus(previousIndex);
            },
        ],
        [
            ['Meta', 'ArrowDown'],

            (e) => {
                e.stopPropagation();
                handleFocus(messages.length - 1);
            },
        ],
        [
            'ArrowDown',

            (e) => {
                e.stopPropagation();
                const lastIndex = messages.length - 1;
                const nextIndex = focusIndex !== undefined ? Math.min(lastIndex, focusIndex + 1) : 0;
                // leave the browser arrow scroll for last email
                if (nextIndex !== lastIndex) {
                    e.preventDefault();
                }
                handleFocus(nextIndex);
            },
        ],
        [
            'Enter',
            (e) => {
                e.stopPropagation();
                const id = getFocusedId();
                if (id) {
                    openMessage(id);
                }
            },
        ],
    ];
{'fraction_non_alphanumeric': 0.11668122270742358, 'fraction_numerical': 0.0031441048034934497, 'mean_word_length': 1.3898163606010017, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5045405', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1417, 'n_words': 288}