grafana/public/app/plugins/datasource/influxdb/components/QueryEditorModeSwitcher.tsx
Gábor Farkas 3e59ae7e56
InfluxDB: Convert the InfluxQL query editor from Angular to React (#32168)
Co-authored-by: Giordano Ricci <me@giordanoricci.com>
2021-05-11 08:15:44 +02:00

57 lines
1.4 KiB
TypeScript

import React, { useEffect, useState } from 'react';
import { Button, ConfirmModal } from '@grafana/ui';
type Props = {
isRaw: boolean;
onChange: (newIsRaw: boolean) => void;
};
export const QueryEditorModeSwitcher = ({ isRaw, onChange }: Props): JSX.Element => {
const [isModalOpen, setModalOpen] = useState(false);
useEffect(() => {
// if the isRaw changes, we hide the modal
setModalOpen(false);
}, [isRaw]);
if (isRaw) {
return (
<>
<Button
icon="pen"
variant="secondary"
type="button"
onClick={() => {
// we show the are-you-sure modal
setModalOpen(true);
}}
></Button>
<ConfirmModal
isOpen={isModalOpen}
title="Switch to visual editor mode"
body="Are you sure to switch to visual editor mode? You will loose the changes done in raw query mode."
confirmText="Yes, switch to editor mode"
dismissText="No, stay in raw query mode"
onConfirm={() => {
onChange(false);
}}
onDismiss={() => {
setModalOpen(false);
}}
/>
</>
);
} else {
return (
<Button
icon="pen"
variant="secondary"
type="button"
onClick={() => {
onChange(true);
}}
></Button>
);
}
};