RunningToggle.tsx 1.5 KB
Newer Older
1
import React, {FunctionComponent, useEffect, useState} from 'react';
2
import {WithStyled, rem} from '~/utils/style';
3 4

import Button from '~/components/Button';
5
import Tippy from '@tippyjs/react';
6
import styled from 'styled-components';
7
import {useTranslation} from 'react-i18next';
8

9 10 11 12 13 14 15 16 17 18 19 20 21 22
const Wrapper = styled.div`
    display: flex;
    align-items: center;

    > span {
        width: ${rem(55)};
    }

    > div {
        flex-grow: 1;
        margin-left: ${rem(20)};
    }
`;

23 24
const StyledButton = styled(Button)`
    text-transform: uppercase;
25
    width: 100%;
26 27 28 29 30 31 32
`;

type RunningToggleProps = {
    running?: boolean;
    onToggle?: (running: boolean) => unknown;
};

33
const RunningToggle: FunctionComponent<RunningToggleProps & WithStyled> = ({running, onToggle, className}) => {
34 35 36
    const {t} = useTranslation('common');

    const [state, setState] = useState(!!running);
37 38

    useEffect(() => {
39
        onToggle?.(state);
40
    }, [onToggle, state]);
41

P
Peter Pan 已提交
42
    return (
43 44
        <Wrapper className={className}>
            <span>{t(state ? 'running' : 'stopped')}</span>
P
Peter Pan 已提交
45 46 47 48 49
            <Tippy
                theme="tooltip"
                content={t(state ? 'stop-realtime-refresh' : 'start-realtime-refresh') + ''}
                hideOnClick={false}
            >
50 51 52 53 54 55
                <div>
                    <StyledButton onClick={() => setState(s => !s)} type={state ? 'danger' : 'primary'} rounded>
                        {t(state ? 'stop' : 'run')}
                    </StyledButton>
                </div>
            </Tippy>
56
        </Wrapper>
P
Peter Pan 已提交
57
    );
58 59 60
};

export default RunningToggle;