|
1 |
| -import React from "react"; |
| 1 | +import React, { useState, useEffect } from "react"; |
| 2 | +import { Container, Row, Col, Tabs, Tab } from "react-bootstrap"; |
| 3 | +import { Editor } from "@/components/Editor"; |
| 4 | +import { RuleConfig } from "@/components/RuleConfig"; |
| 5 | +import { Messages } from "@/components/Messages"; |
| 6 | +import { Header } from "@/components/Header"; |
| 7 | +import { Fixed } from "@/components/Fixed"; |
| 8 | +import { loadDemoLinter, DemoLinter } from "@/lib/linter"; |
| 9 | +import { DEFAULT_CODE, DEFAULT_RULE_CONFIG } from "@/components/constants"; |
2 | 10 | import type { FC } from "react";
|
3 |
| -import { Demo } from "@/components/Demo"; |
4 |
| - |
5 |
| -export const App: FC = () => ( |
6 |
| - <div> |
7 |
| - {" "} |
8 |
| - <Demo />{" "} |
9 |
| - </div> |
10 |
| -); |
| 11 | +import type { Linter } from "eslint"; |
| 12 | +import "bootstrap/dist/css/bootstrap.min.css"; |
| 13 | +import "@/css/app.css"; |
| 14 | + |
| 15 | +const parserOptions = { |
| 16 | + sourceType: "module", |
| 17 | + ecmaVersion: 10, |
| 18 | + project: ["./tsconfig.json"], |
| 19 | +} as const; |
| 20 | + |
| 21 | +export const App: FC = () => { |
| 22 | + const [messages, setMessages] = useState<Linter.LintMessage[]>([]); |
| 23 | + const [code, setCode] = useState<string>(DEFAULT_CODE); |
| 24 | + const [rules, setRules] = useState(DEFAULT_RULE_CONFIG); |
| 25 | + const [fixed, setFixed] = useState<string>(""); |
| 26 | + const [linter, setLinter] = useState<DemoLinter | null>(null); |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + if (linter) { |
| 30 | + const { messages, fixReport } = linter.lint(code, parserOptions, rules); |
| 31 | + setFixed(fixReport.output); |
| 32 | + setMessages(messages); |
| 33 | + } |
| 34 | + }, [rules, code, linter]); |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + (async () => setLinter(await loadDemoLinter()))(); |
| 38 | + }, []); |
| 39 | + |
| 40 | + return ( |
| 41 | + <> |
| 42 | + <Header /> |
| 43 | + <Container> |
| 44 | + <Row> |
| 45 | + <Col md={12}> |
| 46 | + <h5> Code</h5> |
| 47 | + <Editor |
| 48 | + initial={DEFAULT_CODE} |
| 49 | + onChange={setCode} |
| 50 | + messages={messages} |
| 51 | + /> |
| 52 | + </Col> |
| 53 | + </Row> |
| 54 | + <Row> |
| 55 | + <Col> |
| 56 | + <Tabs> |
| 57 | + <Tab eventKey="rules" title="Rules"> |
| 58 | + <RuleConfig |
| 59 | + initial={rules} |
| 60 | + ruleConfig={rules} |
| 61 | + onChange={(rulesString) => { |
| 62 | + try { |
| 63 | + const rules = JSON.parse(rulesString); |
| 64 | + setRules(rules); |
| 65 | + } catch {} |
| 66 | + }} |
| 67 | + /> |
| 68 | + </Tab> |
| 69 | + </Tabs> |
| 70 | + </Col> |
| 71 | + <Col> |
| 72 | + <Tabs> |
| 73 | + <Tab eventKey="messages" title="Messages"> |
| 74 | + <Messages messages={messages} /> |
| 75 | + </Tab> |
| 76 | + <Tab eventKey="fixed" title="Fixed"> |
| 77 | + <Fixed code={fixed} /> |
| 78 | + </Tab> |
| 79 | + </Tabs> |
| 80 | + </Col> |
| 81 | + </Row> |
| 82 | + </Container> |
| 83 | + </> |
| 84 | + ); |
| 85 | +}; |
0 commit comments