<commit_before>import React, { useState } from 'react';
import { c } from 'ttag';
import Push from 'push.js';
import { Block, Badge, SmallButton } from 'react-components';

const DesktopNotificationPanel = () => {
    const [status, setStatus] = useState(Push.Permission.get() === Push.Permission.GRANTED);

    const test = () => {
        if (status) {
            Push.create(c('Info').t`You have a new email`, {
                body: 'Quarterly operations update - Q1 2016 ',
                icon: '/assets/img/notification-badge.gif',
                onClick() {
                    window.focus();
                }
            });
        }
    };

    const request = async () => {
        try {
            Push.Permission.request(
                () => {
                    setStatus(true);
                },
                () => {
                    setStatus(false);
                }
            );
        } catch (err) {
            /**
             * Hotfix to fix requesting the permission on non-promisified requests.
             * TypeError: undefined is not an object (evaluating 'this._win.Notification.requestPermission().then')
             * https://github.com/Nickersoft/push.js/issues/117
             */
        }
    };

    return (
        <Block>
            <Block>
                <span className="mr0-5">{c('Info').t`Desktop notifications are currently`}</span>
                {status ? (
                    <Badge type="success">{c('Desktop notification status').t`Enabled`}</Badge>
                ) : (
                    <Badge type="error">{c('Desktop notification status').t`Disabled`}</Badge>
                )}
            </Block>
            {status ? (
                <SmallButton onClick={test}>{c('Action').t`Send test notification`}</SmallButton>
            ) : (
                <SmallButton onClick={request}>{c('Action').t`Enable desktop notification`}</SmallButton>
            )}
        </Block>
    );
};

export default DesktopNotificationPanel;
<commit_msg>Use desktop notification helper<commit_after>import React, { useState } from 'react';
import { c } from 'ttag';
import { Block, Badge, SmallButton } from 'react-components';
import { create, request, isEnabled } from 'proton-shared/lib/helpers/desktopNotification';

const DesktopNotificationPanel = () => {
    const [status, setStatus] = useState(isEnabled());

    const handleTest = () => {
        if (status) {
            create(c('Info').t`You have a new email`, {
                body: 'Quarterly operations update - Q1 2016 ',
                icon: '/assets/img/notification-badge.gif',
                onClick() {
                    window.focus();
                }
            });
        }
    };

    const handleEnable = () => {
        request(() => setStatus(true), () => setStatus(false));
    };

    return (
        <Block>
            <Block>
                <span className="mr0-5">{c('Info').t`Desktop notifications are currently`}</span>
                {status ? (
                    <Badge type="success">{c('Desktop notification status').t`Enabled`}</Badge>
                ) : (
                    <Badge type="error">{c('Desktop notification status').t`Disabled`}</Badge>
                )}
            </Block>
            {status ? (
                <SmallButton onClick={handleTest}>{c('Action').t`Send test notification`}</SmallButton>
            ) : (
                <SmallButton onClick={handleEnable}>{c('Action').t`Enable desktop notification`}</SmallButton>
            )}
        </Block>
    );
};

export default DesktopNotificationPanel;

{'fraction_non_alphanumeric': 0.143727499307671, 'fraction_numerical': 0.004707837164220438, 'mean_word_length': 1.70561797752809, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 41, 'https://': 1, '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': '4046119', 'n_tokens_mistral': 981, 'n_tokens_neox': 915, 'n_words': 188}