|
1 | | -import React, { useState, useEffect } from "react"; |
| 1 | +import type { Preview } from "@storybook/react"; |
| 2 | + |
2 | 3 | import "../src/style/main.css"; |
3 | 4 | import "@fontsource/inter"; |
| 5 | + |
4 | 6 | import "./preview.css"; |
5 | 7 |
|
6 | | -import type { Preview } from "@storybook/react"; |
| 8 | +import React, { useEffect, useState } from "react"; |
7 | 9 |
|
8 | 10 | const preview: Preview = { |
9 | | - parameters: { |
10 | | - controls: { |
11 | | - matchers: { |
12 | | - color: /(background|color)$/i, |
13 | | - date: /Date$/i, |
14 | | - }, |
15 | | - expanded: true, |
16 | | - }, |
17 | | - layout: "fullscreen", |
| 11 | + parameters: { |
| 12 | + controls: { |
| 13 | + matchers: { |
| 14 | + color: /(background|color)$/i, |
| 15 | + date: /Date$/i, |
| 16 | + }, |
| 17 | + expanded: true, |
18 | 18 | }, |
| 19 | + layout: "fullscreen", |
| 20 | + }, |
19 | 21 | }; |
20 | 22 |
|
21 | | -const withJsonEditor = (Story, context) => { |
22 | | - const { args } = context; |
23 | | - const { oldValue, newValue } = args; |
| 23 | +function withJsonEditor(Story, context) { |
| 24 | + const { args } = context; |
| 25 | + const { oldValue, newValue } = args; |
24 | 26 |
|
25 | | - const [oldJson, setOldJson] = useState(JSON.stringify(oldValue, null, 2)); |
26 | | - const [newJson, setNewJson] = useState(JSON.stringify(newValue, null, 2)); |
27 | | - const [oldJsonError, setOldJsonError] = useState(""); |
28 | | - const [newJsonError, setNewJsonError] = useState(""); |
29 | | - const [parsedOldValue, setParsedOldValue] = useState(oldValue); |
30 | | - const [parsedNewValue, setParsedNewValue] = useState(newValue); |
| 27 | + const [oldJson, setOldJson] = useState(JSON.stringify(oldValue, null, 2)); |
| 28 | + const [newJson, setNewJson] = useState(JSON.stringify(newValue, null, 2)); |
| 29 | + const [oldJsonError, setOldJsonError] = useState(""); |
| 30 | + const [newJsonError, setNewJsonError] = useState(""); |
| 31 | + const [parsedOldValue, setParsedOldValue] = useState(oldValue); |
| 32 | + const [parsedNewValue, setParsedNewValue] = useState(newValue); |
31 | 33 |
|
32 | | - const validateJson = (jsonString: string) => { |
33 | | - try { |
34 | | - return { error: "", parsed: JSON.parse(jsonString) }; |
35 | | - } catch (error) { |
36 | | - return { error: "Invalid JSON format", parsed: null }; |
37 | | - } |
38 | | - }; |
| 34 | + const validateJson = (jsonString: string) => { |
| 35 | + try { |
| 36 | + return { error: "", parsed: JSON.parse(jsonString) }; |
| 37 | + } |
| 38 | + catch { |
| 39 | + return { error: "Invalid JSON format", parsed: null }; |
| 40 | + } |
| 41 | + }; |
39 | 42 |
|
40 | | - useEffect(() => { |
41 | | - const { error, parsed } = validateJson(oldJson); |
42 | | - setOldJsonError(error); |
43 | | - if (!error) { |
44 | | - setParsedOldValue(parsed); |
45 | | - } |
46 | | - }, [oldJson]); |
| 43 | + useEffect(() => { |
| 44 | + const { error, parsed } = validateJson(oldJson); |
| 45 | + setOldJsonError(error); |
| 46 | + if (!error) { |
| 47 | + setParsedOldValue(parsed); |
| 48 | + } |
| 49 | + }, [oldJson]); |
47 | 50 |
|
48 | | - useEffect(() => { |
49 | | - const { error, parsed } = validateJson(newJson); |
50 | | - setNewJsonError(error); |
51 | | - if (!error) { |
52 | | - setParsedNewValue(parsed); |
53 | | - } |
54 | | - }, [newJson]); |
| 51 | + useEffect(() => { |
| 52 | + const { error, parsed } = validateJson(newJson); |
| 53 | + setNewJsonError(error); |
| 54 | + if (!error) { |
| 55 | + setParsedNewValue(parsed); |
| 56 | + } |
| 57 | + }, [newJson]); |
55 | 58 |
|
56 | | - return ( |
57 | | - <div style={{ display: "flex", flexDirection: "column", gap: "20px" }}> |
58 | | - <div style={{ display: "flex", gap: "20px" }}> |
59 | | - <div style={{ flex: 1 }}> |
60 | | - <h3>Old Value</h3> |
61 | | - <textarea |
62 | | - style={{ |
63 | | - width: "100%", |
64 | | - height: "200px", |
65 | | - padding: "10px", |
66 | | - fontFamily: "monospace", |
67 | | - backgroundColor: "#f5f5f5", |
68 | | - border: oldJsonError ? "1px solid red" : "1px solid #ddd", |
69 | | - borderRadius: "4px", |
70 | | - }} |
71 | | - value={oldJson} |
72 | | - onChange={(e) => setOldJson(e.target.value)} |
73 | | - /> |
74 | | - {oldJsonError && <div style={{ color: "red" }}>{oldJsonError}</div>} |
75 | | - </div> |
76 | | - <div style={{ flex: 1 }}> |
77 | | - <h3>New Value</h3> |
78 | | - <textarea |
79 | | - style={{ |
80 | | - width: "100%", |
81 | | - height: "200px", |
82 | | - padding: "10px", |
83 | | - fontFamily: "monospace", |
84 | | - backgroundColor: "#f5f5f5", |
85 | | - border: newJsonError ? "1px solid red" : "1px solid #ddd", |
86 | | - borderRadius: "4px", |
87 | | - }} |
88 | | - value={newJson} |
89 | | - onChange={(e) => setNewJson(e.target.value)} |
90 | | - /> |
91 | | - {newJsonError && <div style={{ color: "red" }}>{newJsonError}</div>} |
92 | | - </div> |
93 | | - </div> |
94 | | - {!oldJsonError && !newJsonError && ( |
95 | | - <Story {...context} args={{ ...context.args, oldValue: parsedOldValue, newValue: parsedNewValue }} /> |
96 | | - )} |
| 59 | + return ( |
| 60 | + <div style={{ display: "flex", flexDirection: "column", gap: "20px" }}> |
| 61 | + <div style={{ display: "flex", gap: "20px" }}> |
| 62 | + <div style={{ flex: 1 }}> |
| 63 | + <h3>Old Value</h3> |
| 64 | + <textarea |
| 65 | + style={{ |
| 66 | + width: "100%", |
| 67 | + height: "200px", |
| 68 | + padding: "10px", |
| 69 | + fontFamily: "monospace", |
| 70 | + backgroundColor: "#f5f5f5", |
| 71 | + border: oldJsonError ? "1px solid red" : "1px solid #ddd", |
| 72 | + borderRadius: "4px", |
| 73 | + }} |
| 74 | + value={oldJson} |
| 75 | + onChange={e => setOldJson(e.target.value)} |
| 76 | + /> |
| 77 | + {oldJsonError && <div style={{ color: "red" }}>{oldJsonError}</div>} |
97 | 78 | </div> |
98 | | - ); |
99 | | -}; |
| 79 | + <div style={{ flex: 1 }}> |
| 80 | + <h3>New Value</h3> |
| 81 | + <textarea |
| 82 | + style={{ |
| 83 | + width: "100%", |
| 84 | + height: "200px", |
| 85 | + padding: "10px", |
| 86 | + fontFamily: "monospace", |
| 87 | + backgroundColor: "#f5f5f5", |
| 88 | + border: newJsonError ? "1px solid red" : "1px solid #ddd", |
| 89 | + borderRadius: "4px", |
| 90 | + }} |
| 91 | + value={newJson} |
| 92 | + onChange={e => setNewJson(e.target.value)} |
| 93 | + /> |
| 94 | + {newJsonError && <div style={{ color: "red" }}>{newJsonError}</div>} |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + {!oldJsonError && !newJsonError && ( |
| 98 | + <Story {...context} args={{ ...context.args, oldValue: parsedOldValue, newValue: parsedNewValue }} /> |
| 99 | + )} |
| 100 | + </div> |
| 101 | + ); |
| 102 | +} |
100 | 103 |
|
101 | 104 | export const decorators = [withJsonEditor]; |
102 | 105 |
|
|
0 commit comments