summaryrefslogtreecommitdiffstats
path: root/browser/components/storybook/.storybook/addon-fluent/FluentPanel.jsx
blob: 71714d8d25211b6f46863e0bea9b4bb2c8eb6a8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// eslint-disable-next-line no-unused-vars
import React, { useEffect, useState } from "react";
import { addons, useGlobals, useStorybookApi } from "@storybook/manager-api";
// eslint-disable-next-line no-unused-vars
import { AddonPanel, Table, Form } from "@storybook/components";
import { FLUENT_CHANGED, FLUENT_SET_STRINGS } from "./constants.mjs";
// eslint-disable-next-line import/no-unassigned-import
import "./fluent-panel.css";

export const FluentPanel = ({ active }) => {
  const [fileName, setFileName] = useState(null);
  const [strings, setStrings] = useState([]);
  const [{ fluentStrings }, updateGlobals] = useGlobals();
  const channel = addons.getChannel();
  const api = useStorybookApi();

  useEffect(() => {
    channel.on(FLUENT_CHANGED, handleFluentChanged);
    return () => {
      channel.off(FLUENT_CHANGED, handleFluentChanged);
    };
  }, [channel]);

  const handleFluentChanged = (nextStrings, fluentFile) => {
    setFileName(fluentFile);
    setStrings(nextStrings);
  };

  const onInput = e => {
    let nextStrings = [];
    for (let [key, value] of strings) {
      if (key == e.target.name) {
        let stringValue = e.target.value;
        if (stringValue.startsWith(".")) {
          stringValue = "\n" + stringValue;
        }
        nextStrings.push([key, stringValue]);
      } else {
        nextStrings.push([key, value]);
      }
    }
    let stringified = nextStrings
      .map(([key, value]) => `${key} = ${value}`)
      .join("\n");
    channel.emit(FLUENT_SET_STRINGS, stringified);
    updateGlobals({
      fluentStrings: { ...fluentStrings, [fileName]: nextStrings },
    });
    return { fileName, strings };
  };

  const addonTemplate = () => {
    if (strings.length === 0) {
      return (
        <AddonPanel active={!!active} api={api}>
          <div className="addon-panel-body">
            <div className="addon-panel-message">
              This story is not configured to use Fluent.
            </div>
          </div>
        </AddonPanel>
      );
    }

    return (
      <AddonPanel active={!!active} api={api}>
        <div className="addon-panel-body">
          <Table aria-hidden="false" className="addon-panel-table">
            <thead className="addon-panel-table-head">
              <tr>
                <th>
                  <span>Identifier</span>
                </th>
                <th>
                  <span>String</span>
                </th>
              </tr>
            </thead>
            <tbody className="addon-panel-table-body">
              {strings.map(([identifier, value]) => (
                <tr key={identifier}>
                  <td>
                    <span>{identifier}</span>
                  </td>
                  <td>
                    <Form.Textarea
                      name={identifier}
                      onInput={onInput}
                      defaultValue={value
                        .trim()
                        .split("\n")
                        .map(s => s.trim())
                        .join("\n")}
                    ></Form.Textarea>
                  </td>
                </tr>
              ))}
            </tbody>
          </Table>
        </div>
      </AddonPanel>
    );
  };

  return addonTemplate();
};