ModeSwitch.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { Segmented } from "antd";
  2. import { useEffect, useState } from "react";
  3. import { useIntl } from "react-intl";
  4. import type { IChannel } from "../channel/Channel"
  5. import ChannelPicker from "../channel/ChannelPicker";
  6. interface IWidget {
  7. currMode?: string;
  8. channel: string | null;
  9. onModeChange?: Function;
  10. onChannelChange?: Function;
  11. }
  12. const ModeSwitchWidget = ({
  13. currMode = "read",
  14. onModeChange,
  15. channel,
  16. onChannelChange,
  17. }: IWidget) => {
  18. const intl = useIntl();
  19. const [mode, setMode] = useState<string>(currMode);
  20. const [newMode, setNewMode] = useState<string>();
  21. const [channelPickerOpen, setChannelPickerOpen] = useState(false);
  22. useEffect(() => {
  23. setMode(currMode);
  24. }, [currMode]);
  25. return (
  26. <>
  27. <Segmented
  28. size="middle"
  29. style={{
  30. color: "rgb(134 134 134 / 90%)",
  31. backgroundColor: "rgb(129 129 129 / 17%)",
  32. display: "block",
  33. }}
  34. defaultValue={currMode}
  35. options={[
  36. {
  37. label: intl.formatMessage({ id: "buttons.read" }),
  38. value: "read",
  39. },
  40. {
  41. label: intl.formatMessage({ id: "buttons.translate" }),
  42. value: "edit",
  43. },
  44. {
  45. label: intl.formatMessage({ id: "buttons.wbw" }),
  46. value: "wbw",
  47. },
  48. ]}
  49. value={mode}
  50. onChange={(value) => {
  51. const _mode = value.toString();
  52. if (_mode !== "read" && channel === null) {
  53. setChannelPickerOpen(true);
  54. setNewMode(_mode);
  55. } else {
  56. if (typeof onModeChange !== "undefined") {
  57. onModeChange(_mode);
  58. }
  59. setMode(_mode);
  60. }
  61. }}
  62. />
  63. <ChannelPicker
  64. open={channelPickerOpen}
  65. defaultOwner="my"
  66. onClose={() => setChannelPickerOpen(false)}
  67. onSelect={(channels: IChannel[]) => {
  68. if (newMode) {
  69. setMode(newMode);
  70. }
  71. if (typeof onChannelChange !== "undefined") {
  72. onChannelChange(channels, newMode);
  73. }
  74. }}
  75. />
  76. </>
  77. );
  78. };
  79. export default ModeSwitchWidget;