From 9377ec4da823c106b6354cc954e717e1af35f012 Mon Sep 17 00:00:00 2001 From: Ziyang Hu Date: Wed, 16 Nov 2022 17:09:02 +0800 Subject: [PATCH] client --- cozo-lib-wasm/cozo-web/.gitignore | 23 + cozo-lib-wasm/cozo-web/README.md | 70 + cozo-lib-wasm/cozo-web/package.json | 41 + cozo-lib-wasm/cozo-web/public/index.html | 43 + cozo-lib-wasm/cozo-web/public/manifest.json | 8 + cozo-lib-wasm/cozo-web/public/robots.txt | 3 + cozo-lib-wasm/cozo-web/src/App.css | 19 + cozo-lib-wasm/cozo-web/src/App.js | 150 + cozo-lib-wasm/cozo-web/src/App.test.js | 8 + cozo-lib-wasm/cozo-web/src/index.css | 13 + cozo-lib-wasm/cozo-web/src/index.js | 17 + cozo-lib-wasm/cozo-web/src/reportWebVitals.js | 13 + cozo-lib-wasm/cozo-web/src/setup.js | 11 + cozo-lib-wasm/cozo-web/src/setupTests.js | 5 + cozo-lib-wasm/cozo-web/yarn.lock | 9439 +++++++++++++++++ 15 files changed, 9863 insertions(+) create mode 100644 cozo-lib-wasm/cozo-web/.gitignore create mode 100644 cozo-lib-wasm/cozo-web/README.md create mode 100644 cozo-lib-wasm/cozo-web/package.json create mode 100644 cozo-lib-wasm/cozo-web/public/index.html create mode 100644 cozo-lib-wasm/cozo-web/public/manifest.json create mode 100644 cozo-lib-wasm/cozo-web/public/robots.txt create mode 100644 cozo-lib-wasm/cozo-web/src/App.css create mode 100644 cozo-lib-wasm/cozo-web/src/App.js create mode 100644 cozo-lib-wasm/cozo-web/src/App.test.js create mode 100644 cozo-lib-wasm/cozo-web/src/index.css create mode 100644 cozo-lib-wasm/cozo-web/src/index.js create mode 100644 cozo-lib-wasm/cozo-web/src/reportWebVitals.js create mode 100644 cozo-lib-wasm/cozo-web/src/setup.js create mode 100644 cozo-lib-wasm/cozo-web/src/setupTests.js create mode 100644 cozo-lib-wasm/cozo-web/yarn.lock diff --git a/cozo-lib-wasm/cozo-web/.gitignore b/cozo-lib-wasm/cozo-web/.gitignore new file mode 100644 index 00000000..4d29575d --- /dev/null +++ b/cozo-lib-wasm/cozo-web/.gitignore @@ -0,0 +1,23 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# production +/build + +# misc +.DS_Store +.env.local +.env.development.local +.env.test.local +.env.production.local + +npm-debug.log* +yarn-debug.log* +yarn-error.log* diff --git a/cozo-lib-wasm/cozo-web/README.md b/cozo-lib-wasm/cozo-web/README.md new file mode 100644 index 00000000..58beeacc --- /dev/null +++ b/cozo-lib-wasm/cozo-web/README.md @@ -0,0 +1,70 @@ +# Getting Started with Create React App + +This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). + +## Available Scripts + +In the project directory, you can run: + +### `npm start` + +Runs the app in the development mode.\ +Open [http://localhost:3000](http://localhost:3000) to view it in your browser. + +The page will reload when you make changes.\ +You may also see any lint errors in the console. + +### `npm test` + +Launches the test runner in the interactive watch mode.\ +See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. + +### `npm run build` + +Builds the app for production to the `build` folder.\ +It correctly bundles React in production mode and optimizes the build for the best performance. + +The build is minified and the filenames include the hashes.\ +Your app is ready to be deployed! + +See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. + +### `npm run eject` + +**Note: this is a one-way operation. Once you `eject`, you can't go back!** + +If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. + +Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own. + +You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it. + +## Learn More + +You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). + +To learn React, check out the [React documentation](https://reactjs.org/). + +### Code Splitting + +This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) + +### Analyzing the Bundle Size + +This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) + +### Making a Progressive Web App + +This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) + +### Advanced Configuration + +This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) + +### Deployment + +This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) + +### `npm run build` fails to minify + +This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) diff --git a/cozo-lib-wasm/cozo-web/package.json b/cozo-lib-wasm/cozo-web/package.json new file mode 100644 index 00000000..533c98ed --- /dev/null +++ b/cozo-lib-wasm/cozo-web/package.json @@ -0,0 +1,41 @@ +{ + "name": "cozowebui", + "version": "0.1.0", + "private": true, + "dependencies": { + "@blueprintjs/core": "^4.9.3", + "@blueprintjs/table": "^4.6.4", + "@testing-library/jest-dom": "^5.16.5", + "@testing-library/react": "^13.3.0", + "@testing-library/user-event": "^13.5.0", + "react": "^18.2.0", + "react-dom": "^18.2.0", + "react-scripts": "5.0.1", + "web-vitals": "^2.1.4", + "cozo-lib-wasm": "file:../pkg" + }, + "scripts": { + "start": "react-scripts start", + "build": "react-scripts build", + "test": "react-scripts test", + "eject": "react-scripts eject" + }, + "eslintConfig": { + "extends": [ + "react-app", + "react-app/jest" + ] + }, + "browserslist": { + "production": [ + ">0.2%", + "not dead", + "not op_mini all" + ], + "development": [ + "last 1 chrome version", + "last 1 firefox version", + "last 1 safari version" + ] + } +} diff --git a/cozo-lib-wasm/cozo-web/public/index.html b/cozo-lib-wasm/cozo-web/public/index.html new file mode 100644 index 00000000..6ecca3d5 --- /dev/null +++ b/cozo-lib-wasm/cozo-web/public/index.html @@ -0,0 +1,43 @@ + + + + + + + + + + + + + Cozo WebUI + + + +
+ + + diff --git a/cozo-lib-wasm/cozo-web/public/manifest.json b/cozo-lib-wasm/cozo-web/public/manifest.json new file mode 100644 index 00000000..7e4a1f7a --- /dev/null +++ b/cozo-lib-wasm/cozo-web/public/manifest.json @@ -0,0 +1,8 @@ +{ + "short_name": "CozoWebUI", + "name": "Cozo WebUI", + "start_url": ".", + "display": "standalone", + "theme_color": "#000000", + "background_color": "#ffffff" +} diff --git a/cozo-lib-wasm/cozo-web/public/robots.txt b/cozo-lib-wasm/cozo-web/public/robots.txt new file mode 100644 index 00000000..e9e57dc4 --- /dev/null +++ b/cozo-lib-wasm/cozo-web/public/robots.txt @@ -0,0 +1,3 @@ +# https://www.robotstxt.org/robotstxt.html +User-agent: * +Disallow: diff --git a/cozo-lib-wasm/cozo-web/src/App.css b/cozo-lib-wasm/cozo-web/src/App.css new file mode 100644 index 00000000..61996f78 --- /dev/null +++ b/cozo-lib-wasm/cozo-web/src/App.css @@ -0,0 +1,19 @@ +@import "~normalize.css"; +@import "~@blueprintjs/core/lib/css/blueprint.css"; +@import "~@blueprintjs/icons/lib/css/blueprint-icons.css"; +@import "~@blueprintjs/table/lib/css/table.css"; + +#error-message, #other-results { + padding: 10px; + overflow: scroll; + font-family: Menlo, Monaco, Consolas, "Courier New", monospace; + font-size: 90%; +} + +#query-box, #params-box { + font-family: Menlo, Monaco, Consolas, "Courier New", monospace; + font-size: 100%; + min-height: 200px; + max-height: 40vh; + min-width: 200px; +} \ No newline at end of file diff --git a/cozo-lib-wasm/cozo-web/src/App.js b/cozo-lib-wasm/cozo-web/src/App.js new file mode 100644 index 00000000..9ccb9b6a --- /dev/null +++ b/cozo-lib-wasm/cozo-web/src/App.js @@ -0,0 +1,150 @@ +import {Button, Intent, Tag, TextArea} from "@blueprintjs/core"; +import React, {useState} from 'react'; +import './App.css'; +import {Cell, Column, Table2} from "@blueprintjs/table"; + +let db; + +import('./setup') + .catch(e => console.error("Error importing `cozo-lib-wasm`:", e)) + .then(mod => { + console.log(mod) + // db = new mod.CozoDb(); + // console.log(db.run('xxxx')) + }) + +function App() { + const [params, setParams] = useState(''); + const [queryText, setQueryText] = useState(''); + const [inProgress, setInProgress] = useState(false); + const [statusMessage, setStatusMessage] = useState(''); + const [errorMessage, setErrorMessage] = useState(''); + const [queryResults, setQueryResults] = useState(null); + + function handleKeyDown(e) { + if (e.key === 'Enter' && e.shiftKey) { + e.preventDefault(); + e.stopPropagation(); + handleQuery('script'); + } + if (e.key === 'Tab' && !e.shiftKey) { + e.preventDefault(); + e.stopPropagation(); + typeInTextarea(' '); + } + } + + function typeInTextarea(newText, el = document.activeElement) { + const [start, end] = [el.selectionStart, el.selectionEnd]; + el.setRangeText(newText, start, end, 'end'); + } + + const renderCell = (colIdx) => (rowIdx) => + {displayValue(queryResults.rows[rowIdx][colIdx])} + + + function displayValue(v) { + if (typeof v === 'string') { + return v + } else { + return {JSON.stringify(v)} + } + } + + async function handleQuery(type) { + if (!db) { + setErrorMessage('Cannot open database'); + return; + } + const query = queryText.trim(); + if (query) { + let started = performance.now(); + setInProgress(true); + setErrorMessage(''); + setStatusMessage(''); + setQueryResults(null); + try { + let res = JSON.parse(db.run(query, JSON.parse(params.trim() || '{}'))); + if (res.ok) { + setStatusMessage(`finished with ${res.rows.length} rows`); + if (!res.headers) { + res.headers = []; + if (res.rows.length) { + for (let i = 0; i < res.rows[0].length; i++) { + res.headers.push('' + i); + } + } + } + } else { + setStatusMessage(`Failed to execute query`); + setErrorMessage(res.display || res.message); + } + setQueryResults(res); + } catch (e) { + setStatusMessage(`Encountered problem when executing query`); + setErrorMessage('' + e); + } finally { + setInProgress(false); + } + } + } + + return ( +
+
+
+