-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.tsx
85 lines (77 loc) · 2.51 KB
/
page.tsx
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
"use client";
import { Column } from "@/components/Column";
import { JSEditor } from "@/components/Editor/JSEditor";
import { PHPEditor } from "@/components/Editor/PHPEditor";
import { SQLEditor } from "@/components/Editor/SQLEditor";
import { QueryContext, Variables } from "@/components/QueryContext";
import { useEffect, useState } from "react";
const QUERY_KEY = "SQLConverter_Query";
const VARIABLES_KEY = "SQLConverter_Variables";
export default function Home() {
const [query, setQuery] = useState("");
const [variables, setVariables] = useState<Variables>({});
/**
* When the page first mounts, check localStorage for query/variable history
* If we find but aren't able to parse the variables we reset the history
*/
useEffect(() => {
let storedQuery = localStorage.getItem(QUERY_KEY);
let storedVariables: any = localStorage.getItem(VARIABLES_KEY);
if (storedVariables) {
try {
storedVariables = JSON.parse(storedVariables);
} catch (_1) {
storedQuery = null;
storedVariables = null;
}
}
setQuery(
storedQuery ||
`SELECT
*
FROM
People
WHERE
People.Forename = 'Frank' AND
People.Surname = @Surname AND
People.Age = @Age`
);
setVariables(
storedVariables || {
Surname: "Skinner",
Age: "66"
}
);
}, []);
/**
* Update localStorage whenever the query/variables change
*/
useEffect(() => {
localStorage.setItem(QUERY_KEY, query);
localStorage.setItem(VARIABLES_KEY, JSON.stringify(variables));
}, [query, variables]);
return (
<>
<main className="p-4 pb-0 gap-4 xl:p-6 xl:pb-0 xl:gap-6 h-full grid grid-cols-1 xl:grid-cols-3">
<QueryContext.Provider
value={{
query,
setQuery,
variables,
setVariables
}}
>
<Column title="SQL">
<SQLEditor></SQLEditor>
</Column>
<Column title="JS">
<JSEditor></JSEditor>
</Column>
<Column title="PHP">
<PHPEditor></PHPEditor>
</Column>
</QueryContext.Provider>
</main>
</>
);
}