Skip to content

Commit caa016d

Browse files
authored
Merge pull request #46 from quadratichq/example-on-first-load
load an example file on first open
2 parents d43f728 + d67db5e commit caa016d

File tree

4 files changed

+3673
-20
lines changed

4 files changed

+3673
-20
lines changed
Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
import { GridFileSchema } from "./GridFileSchema";
2-
import { qdb } from "../../gridDB/db";
3-
import { UpdateCellsDB } from "../../gridDB/Cells/UpdateCellsDB";
4-
import { UpdateDGraphDB } from "../../gridDB/DGraph/UpdateDGraphDB";
5-
import QuadraticDependencyGraph from "../../dgraph/QuadraticDependencyGraph";
1+
import { GridFileSchema } from './GridFileSchema';
2+
import { qdb } from '../../gridDB/db';
3+
import { UpdateCellsDB } from '../../gridDB/Cells/UpdateCellsDB';
4+
import { UpdateDGraphDB } from '../../gridDB/DGraph/UpdateDGraphDB';
5+
import QuadraticDependencyGraph from '../../dgraph/QuadraticDependencyGraph';
66

77
const readFileAsync = async (file: File) => {
88
// takes a File object and returns it as a string
99
return new Promise<string>((resolve, reject) => {
1010
let reader = new FileReader();
1111

1212
reader.onload = () => {
13-
resolve(reader.result?.toString() || "");
13+
resolve(reader.result?.toString() || '');
1414
};
1515

1616
reader.onerror = reject;
1717

18-
reader.readAsText(file, "UTF-8");
18+
reader.readAsText(file, 'UTF-8');
1919
});
2020
};
2121

2222
const openFileMenuAsync = async () => {
2323
// opens a input file menu for a single .grid file
2424
// once a file is selected, the File object is returned
2525
return new Promise<File>((resolve, reject) => {
26-
const elem = window.document.createElement("input");
27-
elem.type = "file";
28-
elem.accept = ".grid";
26+
const elem = window.document.createElement('input');
27+
elem.type = 'file';
28+
elem.accept = '.grid';
2929
document.body.appendChild(elem);
3030
elem.click();
3131
document.body.removeChild(elem);
@@ -41,21 +41,25 @@ const openFileMenuAsync = async () => {
4141
});
4242
};
4343

44-
export const OpenGridFile = async () => {
45-
// take file input selection from user
46-
const fileToLoad = await openFileMenuAsync();
47-
const result = await readFileAsync(fileToLoad);
48-
49-
// parse file
50-
const gridFile = JSON.parse(result) as GridFileSchema;
51-
44+
export const LoadGridFromJSON = async (gridFileJSON: GridFileSchema) => {
5245
// clear current grid
5346
await qdb.cells.clear();
5447
await qdb.qgrid.clear();
5548

5649
// Open file cells and dgraph
57-
await UpdateCellsDB(gridFile.cells);
50+
await UpdateCellsDB(gridFileJSON.cells);
5851
let qdg = new QuadraticDependencyGraph();
59-
qdg.load_from_json(gridFile.dgraph);
52+
qdg.load_from_json(gridFileJSON.dgraph);
6053
await UpdateDGraphDB(qdg);
6154
};
55+
56+
export const OpenGridFile = async () => {
57+
// take file input selection from user
58+
const fileToLoad = await openFileMenuAsync();
59+
const result = await readFileAsync(fileToLoad);
60+
61+
// parse file
62+
const gridFileJSON = JSON.parse(result) as GridFileSchema;
63+
64+
await LoadGridFromJSON(gridFileJSON);
65+
};

src/quadratic/QuadraticApp.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useLoading } from '../contexts/LoadingContext';
66
import { QuadraticLoading } from '../ui/QuadtraticLoading';
77
import { loadPython } from '../core/computations/python/loadPython';
88
import { TopBarLoading } from '../ui/components/TopBarLoading';
9+
import { WelcomeComponent } from './WelcomeComponent';
910

1011
export default function QuadraticApp() {
1112
const { loading, setLoading } = useLoading();
@@ -20,6 +21,8 @@ export default function QuadraticApp() {
2021

2122
return (
2223
<RecoilRoot>
24+
{/* Welcome Component for first time users */}
25+
{!loading && <WelcomeComponent></WelcomeComponent>}
2326
{/* Provider of WebGL Canvas and Quadratic Grid */}
2427
<QuadraticGrid></QuadraticGrid>
2528
{/* Provider of All React UI Components */}

src/quadratic/WelcomeComponent.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { useEffect } from 'react';
2+
import useLocalStorage from '../hooks/useLocalStorage';
3+
import { GridFileSchema } from '../core/actions/gridFile/GridFileSchema';
4+
import { LoadGridFromJSON } from '../core/actions/gridFile/OpenGridFile';
5+
import { qdb } from '../core/gridDB/db';
6+
import { example_grid } from './example_grid';
7+
8+
export const WelcomeComponent = () => {
9+
const [firstTime, setFirstTime] = useLocalStorage('firstTime', true);
10+
11+
useEffect(() => {
12+
// On first load, open an example file.
13+
if (firstTime) {
14+
setFirstTime(false);
15+
// Only open example file if the grid is empty.
16+
qdb.cells.count().then((number_of_cells) => {
17+
if (number_of_cells === 0)
18+
LoadGridFromJSON(example_grid as GridFileSchema);
19+
});
20+
}
21+
}, [firstTime, setFirstTime]);
22+
23+
return null;
24+
};

0 commit comments

Comments
 (0)