diff --git a/fxmanifest.lua b/fxmanifest.lua index 030f5dc..793ffb5 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -15,10 +15,10 @@ client_script 'ui.lua' server_script 'dist/build.js' files { - 'ui/public/index.html', - 'ui/public/**/*' + 'ui/build/index.html', + 'ui/build/**/*' } -ui_page 'src/ui/public/index.html' +ui_page 'ui/build/index.html' provide 'mysql-async' diff --git a/package.json b/package.json index c7dfb41..002c6ef 100644 --- a/package.json +++ b/package.json @@ -30,4 +30,4 @@ "postinstall-postinstall": "^2.1.0", "typescript": "^4.5.4" } -} +} \ No newline at end of file diff --git a/ui/.gitignore b/ui/.gitignore index da93220..4d29575 100644 --- a/ui/.gitignore +++ b/ui/.gitignore @@ -1,4 +1,23 @@ -/node_modules/ -/public/build/ +# 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/ui/README.md b/ui/README.md new file mode 100644 index 0000000..b58e0af --- /dev/null +++ b/ui/README.md @@ -0,0 +1,46 @@ +# 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: + +### `yarn start` + +Runs the app in the development mode.\ +Open [http://localhost:3000](http://localhost:3000) to view it in the browser. + +The page will reload if you make edits.\ +You will also see any lint errors in the console. + +### `yarn 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. + +### `yarn 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. + +### `yarn 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/). diff --git a/ui/craco.config.js b/ui/craco.config.js new file mode 100644 index 0000000..76682be --- /dev/null +++ b/ui/craco.config.js @@ -0,0 +1,24 @@ +const path = require("path"); +module.exports = { + webpack: { + configure: (webpackConfig) => { + // Because CEF has issues with loading source maps properly atm, + // lets use the best we can get in line with `eval-source-map` + if (webpackConfig.mode === 'development' && process.env.IN_GAME_DEV) { + webpackConfig.devtool = 'eval-source-map' + webpackConfig.output.path = path.join(__dirname, 'build') + } + + return webpackConfig + } + }, + + devServer: (devServerConfig) => { + if (process.env.IN_GAME_DEV) { + // Used for in-game dev mode + devServerConfig.writeToDisk = true + } + + return devServerConfig + } +} \ No newline at end of file diff --git a/ui/package.json b/ui/package.json index e22d758..0337fc3 100644 --- a/ui/package.json +++ b/ui/package.json @@ -1,30 +1,53 @@ { - "name": "svelte-app", - "version": "1.0.0", + "name": "web", + "version": "0.1.0", + "homepage": "web/build", "private": true, + "dependencies": { + "@chakra-ui/react": "^1.7.3", + "@emotion/react": "11", + "@emotion/styled": "11", + "@testing-library/jest-dom": "^5.15.0", + "@testing-library/react": "^12.1.2", + "@testing-library/user-event": "^12.1.10", + "@types/jest": "^26.0.15", + "@types/node": "^16.11.9", + "@types/react": "^17.0.35", + "@types/react-dom": "^17.0.11", + "framer-motion": "4", + "react": "^17.0.2", + "react-dom": "^17.0.2", + "react-scripts": "4.0.3", + "typescript": "^4.5.2", + "web-vitals": "^2.1.2" + }, "scripts": { - "build": "rollup -c", - "dev": "rollup -c -w", - "start": "sirv public --no-clear" + "start": "cross-env PUBLIC_URL=/ craco start", + "start:game": "cross-env IN_GAME_DEV=1 craco start", + "build": "craco build", + "test": "craco 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" + ] }, "devDependencies": { - "@rollup/plugin-commonjs": "^17.0.0", - "@rollup/plugin-node-resolve": "^11.0.0", - "@rollup/plugin-typescript": "^8.0.0", - "@tsconfig/svelte": "^2.0.0", - "rollup": "^2.3.4", - "rollup-plugin-css-only": "^3.1.0", - "rollup-plugin-livereload": "^2.0.0", - "rollup-plugin-svelte": "^7.0.0", - "rollup-plugin-terser": "^7.0.0", - "svelte": "^3.0.0", - "svelte-check": "^2.0.0", - "svelte-preprocess": "^4.0.0", - "tslib": "^2.0.0", - "typescript": "^4.0.0" - }, - "dependencies": { - "sirv-cli": "^1.0.0", - "svelte-spa-router": "^3.2.0" + "craco": "^0.0.3", + "cross-env": "^7.0.3" } } diff --git a/ui/public/global.css b/ui/public/global.css deleted file mode 100644 index a94d91a..0000000 --- a/ui/public/global.css +++ /dev/null @@ -1,11 +0,0 @@ -@import url('https://fonts.googleapis.com/css2?family=Inter&display=swap'); - -* { - margin: 0; - padding: 0; - text-decoration: none; -} - -body { - height: 100vh; -} diff --git a/ui/public/index.html b/ui/public/index.html index bd206a3..7bc4a2d 100644 --- a/ui/public/index.html +++ b/ui/public/index.html @@ -2,15 +2,22 @@ - - - Svelte app - - - - - + + + NUI React Boilerplate + + +
+ + diff --git a/ui/rollup.config.js b/ui/rollup.config.js deleted file mode 100644 index c584614..0000000 --- a/ui/rollup.config.js +++ /dev/null @@ -1,87 +0,0 @@ -import svelte from "rollup-plugin-svelte"; -import commonjs from "@rollup/plugin-commonjs"; -import resolve from "@rollup/plugin-node-resolve"; -import livereload from "rollup-plugin-livereload"; -import { terser } from "rollup-plugin-terser"; -import sveltePreprocess from "svelte-preprocess"; -import typescript from "@rollup/plugin-typescript"; -import css from "rollup-plugin-css-only"; - -const production = !process.env.ROLLUP_WATCH; - -function serve() { - let server; - - function toExit() { - if (server) server.kill(0); - } - - return { - writeBundle() { - if (server) return; - server = require("child_process").spawn( - "npm", - ["run", "start", "--", "--dev"], - { - stdio: ["ignore", "inherit", "inherit"], - shell: true, - } - ); - - process.on("SIGTERM", toExit); - process.on("exit", toExit); - }, - }; -} - -export default { - input: "src/main.ts", - output: { - sourcemap: false, - format: "iife", - name: "app", - file: "public/build/bundle.js", - }, - plugins: [ - svelte({ - preprocess: sveltePreprocess({ sourceMap: !production }), - compilerOptions: { - // enable run-time checks when not in production - dev: !production, - }, - }), - // we'll extract any component CSS out into - // a separate file - better for performance - css({ output: "bundle.css" }), - - // If you have external dependencies installed from - // npm, you'll most likely need these plugins. In - // some cases you'll need additional configuration - - // consult the documentation for details: - // https://github.com/rollup/plugins/tree/master/packages/commonjs - resolve({ - browser: true, - dedupe: ["svelte"], - }), - commonjs(), - typescript({ - sourceMap: !production, - inlineSources: !production, - }), - - // In dev mode, call `npm run start` once - // the bundle has been generated - !production && serve(), - - // Watch the `public` directory and refresh the - // browser on changes when not in production - !production && livereload("public"), - - // If we're building for production (npm run build - // instead of npm run dev), minify - production && terser(), - ], - watch: { - clearScreen: false, - }, -}; diff --git a/ui/src/App.svelte b/ui/src/App.svelte deleted file mode 100644 index b8be177..0000000 --- a/ui/src/App.svelte +++ /dev/null @@ -1,36 +0,0 @@ - - -
- -
- -
diff --git a/ui/src/components/App.css b/ui/src/components/App.css new file mode 100644 index 0000000..e69de29 diff --git a/ui/src/components/App.tsx b/ui/src/components/App.tsx new file mode 100644 index 0000000..0556a36 --- /dev/null +++ b/ui/src/components/App.tsx @@ -0,0 +1,25 @@ +import './App.css'; +import { Center, Box } from '@chakra-ui/react'; +import { debugData } from '../utils/debugData'; +import TopBar from './NavBars/TopBar'; +import SideBar from './NavBars/SideBar'; + +debugData([ + { + action: 'setVisible', + data: true, + }, +]); + +const App: React.FC = () => { + return ( +
+ + + + +
+ ); +}; + +export default App; diff --git a/ui/src/components/Main.svelte b/ui/src/components/Main.svelte deleted file mode 100644 index 2bfc574..0000000 --- a/ui/src/components/Main.svelte +++ /dev/null @@ -1,49 +0,0 @@ - - -
- -
-
- -
-
-
- - diff --git a/ui/src/components/NavBars/SideBar.tsx b/ui/src/components/NavBars/SideBar.tsx new file mode 100644 index 0000000..e8a597e --- /dev/null +++ b/ui/src/components/NavBars/SideBar.tsx @@ -0,0 +1,15 @@ +import { Box, VStack } from '@chakra-ui/react'; + +const SideBar: React.FC = () => { + return ( + + + es_extended + ox_inventory + luke_garages + + + ); +}; + +export default SideBar; diff --git a/ui/src/components/NavBars/TopBar.tsx b/ui/src/components/NavBars/TopBar.tsx new file mode 100644 index 0000000..d609d6c --- /dev/null +++ b/ui/src/components/NavBars/TopBar.tsx @@ -0,0 +1,13 @@ +import { Box } from '@chakra-ui/react'; + +const TopBar: React.FC = () => { + return ( + + + Resources + + + ); +}; + +export default TopBar; diff --git a/ui/src/components/menu/NavMenu.svelte b/ui/src/components/menu/NavMenu.svelte deleted file mode 100644 index 446d73e..0000000 --- a/ui/src/components/menu/NavMenu.svelte +++ /dev/null @@ -1,64 +0,0 @@ - - -
-
Home
- {#each $resources as resource} - - {/each} -
- - diff --git a/ui/src/components/routes/Home.svelte b/ui/src/components/routes/Home.svelte deleted file mode 100644 index 66ba0f3..0000000 --- a/ui/src/components/routes/Home.svelte +++ /dev/null @@ -1,18 +0,0 @@ - - -
- {#if initData} -
Resources: {initData.resources}
-
Total queries: {initData.totalQueries}
-
Total time: {initData.totalTime}
- {/if} -
diff --git a/ui/src/components/routes/ResourceData.svelte b/ui/src/components/routes/ResourceData.svelte deleted file mode 100644 index 56b195f..0000000 --- a/ui/src/components/routes/ResourceData.svelte +++ /dev/null @@ -1,40 +0,0 @@ - - -{#if isDataLoaded === true} - {JSON.stringify(queryData)} -{/if} diff --git a/ui/src/global.d.ts b/ui/src/global.d.ts deleted file mode 100644 index 1a25456..0000000 --- a/ui/src/global.d.ts +++ /dev/null @@ -1 +0,0 @@ -/// diff --git a/ui/src/hooks/useNuiEvent.ts b/ui/src/hooks/useNuiEvent.ts new file mode 100644 index 0000000..d0e1574 --- /dev/null +++ b/ui/src/hooks/useNuiEvent.ts @@ -0,0 +1,49 @@ +import {MutableRefObject, useEffect, useRef} from "react"; +import {noop} from "../utils/misc"; + +interface NuiMessageData { + action: string; + data: T; +} + +type NuiHandlerSignature = (data: T) => void; + +/** + * A hook that manage events listeners for receiving data from the client scripts + * @param action The specific `action` that should be listened for. + * @param handler The callback function that will handle data relayed by this hook + * + * @example + * useNuiEvent<{visibility: true, wasVisible: 'something'}>('setVisible', (data) => { + * // whatever logic you want + * }) + * + **/ + +export const useNuiEvent = ( + action: string, + handler: (data: T) => void +) => { + const savedHandler: MutableRefObject> = useRef(noop); + + // Make sure we handle for a reactive handler + useEffect(() => { + savedHandler.current = handler; + }, [handler]); + + useEffect(() => { + const eventListener = (event: MessageEvent>) => { + const { action: eventAction, data } = event.data; + + if (savedHandler.current) { + if (eventAction === action) { + savedHandler.current(data); + } + } + }; + + window.addEventListener("message", eventListener); + // Remove Event Listener on component cleanup + return () => window.removeEventListener("message", eventListener); + }, [action]); +}; diff --git a/ui/src/index.css b/ui/src/index.css new file mode 100644 index 0000000..93ea930 --- /dev/null +++ b/ui/src/index.css @@ -0,0 +1,18 @@ +@import url('https://fonts.googleapis.com/css2?family=Poppins&display=swap'); + +body { + margin: 0; + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', + 'Droid Sans', 'Helvetica Neue', sans-serif; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; + height: 100vh; +} + +#root { + height: 100%; +} + +code { + font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', monospace; +} diff --git a/ui/src/index.tsx b/ui/src/index.tsx new file mode 100644 index 0000000..920a8a0 --- /dev/null +++ b/ui/src/index.tsx @@ -0,0 +1,17 @@ +import React from 'react'; +import ReactDOM from 'react-dom'; +import './index.css'; +import App from './components/App'; +import {VisibilityProvider} from "./providers/VisibilityProvider"; +import {ChakraProvider } from '@chakra-ui/react' + +ReactDOM.render( + + + + + + + , + document.getElementById('root') +); diff --git a/ui/src/main.ts b/ui/src/main.ts deleted file mode 100644 index 83bc6ab..0000000 --- a/ui/src/main.ts +++ /dev/null @@ -1,7 +0,0 @@ -import App from "./App.svelte"; - -const app = new App({ - target: document.body, -}); - -export default app; diff --git a/ui/src/providers/VisibilityProvider.svelte b/ui/src/providers/VisibilityProvider.svelte deleted file mode 100644 index 582c90b..0000000 --- a/ui/src/providers/VisibilityProvider.svelte +++ /dev/null @@ -1,35 +0,0 @@ - - -
- {#if isVisible} - - {/if} -
diff --git a/ui/src/providers/VisibilityProvider.tsx b/ui/src/providers/VisibilityProvider.tsx new file mode 100644 index 0000000..bbbdf42 --- /dev/null +++ b/ui/src/providers/VisibilityProvider.tsx @@ -0,0 +1,48 @@ +import React, { Context, createContext, useContext, useEffect, useState } from 'react'; +import { useNuiEvent } from '../hooks/useNuiEvent'; +import { fetchNui } from '../utils/fetchNui'; + +const VisibilityCtx = createContext(null); + +interface VisibilityProviderValue { + setVisible: (visible: boolean) => void; + visible: boolean; +} + +// This should be mounted at the top level of your application, it is currently set to +// apply a CSS visibility value. If this is non-performant, this should be customized. +export const VisibilityProvider: React.FC = ({ children }) => { + const [visible, setVisible] = useState(false); + + useNuiEvent('setVisible', setVisible); + + // Handle pressing escape/backspace + useEffect(() => { + // Only attach listener when we are visible + if (!visible) return; + + const keyHandler = (e: KeyboardEvent) => { + if (['Escape'].includes(e.code)) { + fetchNui('exit'); + } + }; + + window.addEventListener('keydown', keyHandler); + + return () => window.removeEventListener('keydown', keyHandler); + }, [visible]); + + return ( + +
{children}
+
+ ); +}; + +export const useVisibility = () => + useContext(VisibilityCtx as Context); diff --git a/ui/src/react-app-env.d.ts b/ui/src/react-app-env.d.ts new file mode 100644 index 0000000..6431bc5 --- /dev/null +++ b/ui/src/react-app-env.d.ts @@ -0,0 +1 @@ +/// diff --git a/ui/src/setupTests.ts b/ui/src/setupTests.ts new file mode 100644 index 0000000..8f2609b --- /dev/null +++ b/ui/src/setupTests.ts @@ -0,0 +1,5 @@ +// jest-dom adds custom jest matchers for asserting on DOM nodes. +// allows you to do things like: +// expect(element).toHaveTextContent(/react/i) +// learn more: https://github.com/testing-library/jest-dom +import '@testing-library/jest-dom'; diff --git a/ui/src/store/stores.ts b/ui/src/store/stores.ts deleted file mode 100644 index fd3b270..0000000 --- a/ui/src/store/stores.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { writable } from "svelte/store"; - -export const visibility = writable(false); -export const currentResource = writable(''); -export const resources = writable([]) \ No newline at end of file diff --git a/ui/src/types/init.ts b/ui/src/types/init.ts deleted file mode 100644 index 0a37918..0000000 --- a/ui/src/types/init.ts +++ /dev/null @@ -1,5 +0,0 @@ -export type InitData = { - resources: string[]; - totalQueries: number; - totalTime: number; -} \ No newline at end of file diff --git a/ui/src/types/query.ts b/ui/src/types/query.ts deleted file mode 100644 index 48ce19b..0000000 --- a/ui/src/types/query.ts +++ /dev/null @@ -1,5 +0,0 @@ -export type QueryData = { - date: number; - query: string; - executionTime: number; -} \ No newline at end of file diff --git a/ui/src/utils/debugData.ts b/ui/src/utils/debugData.ts index aea5370..3a92fe6 100644 --- a/ui/src/utils/debugData.ts +++ b/ui/src/utils/debugData.ts @@ -13,7 +13,7 @@ interface DebugEvent { * @param timer - How long until it should trigger (ms) */ export const debugData =

(events: DebugEvent

[], timer = 1000): void => { - if (isEnvBrowser()) { + if (process.env.NODE_ENV === "development" && isEnvBrowser()) { for (const event of events) { setTimeout(() => { window.dispatchEvent( diff --git a/ui/src/utils/fetchNui.ts b/ui/src/utils/fetchNui.ts index 02c60d1..6069465 100644 --- a/ui/src/utils/fetchNui.ts +++ b/ui/src/utils/fetchNui.ts @@ -1,31 +1,28 @@ /** -* @param eventName - The endpoint eventname to target -* @param data - Data you wish to send in the NUI Callback -* -* @return returnData - A promise for the data sent back by the NuiCallbacks CB argument -*/ + * Simple wrapper around fetch API tailored for CEF/NUI use. This abstraction + * can be extended to include AbortController if needed or if the response isn't + * JSON. Tailor it to your needs. + * + * @param eventName - The endpoint eventname to target + * @param data - Data you wish to send in the NUI Callback + * + * @return returnData - A promise for the data sent back by the NuiCallbacks CB argument + */ -import { isEnvBrowser } from "./misc"; - -export async function fetchNui( - eventName: string, - data: unknown = {} -): Promise { +export async function fetchNui(eventName: string, data?: any): Promise { const options = { - method: "post", + method: 'post', headers: { - "Content-Type": "application/json; charset=UTF-8", + 'Content-Type': 'application/json; charset=UTF-8', }, body: JSON.stringify(data), }; - if (isEnvBrowser()) return undefined as any; - - const resourceName = (window as any).GetParentResourceName - ? (window as any).GetParentResourceName() - : "nui-frame-app"; + const resourceName = (window as any).GetParentResourceName ? (window as any).GetParentResourceName() : 'nui-frame-app'; const resp = await fetch(`https://${resourceName}/${eventName}`, options); - return await resp.json(); + const respFormatted = await resp.json() + + return respFormatted } diff --git a/ui/src/utils/misc.ts b/ui/src/utils/misc.ts index 32eae35..4c16a29 100644 --- a/ui/src/utils/misc.ts +++ b/ui/src/utils/misc.ts @@ -1 +1,6 @@ -export const isEnvBrowser = (): boolean => !(window as any).invokeNative; +// Will return whether the current environment is in a regular browser +// and not CEF +export const isEnvBrowser = (): boolean => !(window as any).invokeNative + +// Basic no operation function +export const noop = () => {} \ No newline at end of file diff --git a/ui/src/utils/useNuiEvent.ts b/ui/src/utils/useNuiEvent.ts deleted file mode 100644 index e1d8d2e..0000000 --- a/ui/src/utils/useNuiEvent.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { onMount, onDestroy } from "svelte"; - -interface NuiMessage { - action: string; - data: T; -} - -/** - * A function that manage events listeners for receiving data from the client scripts - * @param action The specific `action` that should be listened for. - * @param handler The callback function that will handle data relayed by this function - * - * @example - * useNuiEvent<{visibility: true, wasVisible: 'something'}>('setVisible', (data) => { - * // whatever logic you want - * }) - * - **/ - -export function useNuiEvent( - action: string, - handler: (data: T) => void -) { - const eventListener = (event: MessageEvent>) => { - const { action: eventAction, data } = event.data; - - eventAction === action && handler(data); - }; - onMount(() => window.addEventListener("message", eventListener)); - onDestroy(() => window.removeEventListener("message", eventListener)); -} diff --git a/ui/tsconfig.json b/ui/tsconfig.json index 8139637..a273b0c 100644 --- a/ui/tsconfig.json +++ b/ui/tsconfig.json @@ -1,13 +1,26 @@ { - "extends": "@tsconfig/svelte/tsconfig.json", - "compilerOptions": { - "strict": true, + "target": "es5", + "lib": [ + "dom", + "dom.iterable", + "esnext" + ], "allowJs": true, + "skipLibCheck": true, + "esModuleInterop": true, "allowSyntheticDefaultImports": true, - "noFallthroughCasesInSwitch": true + "strict": true, + "forceConsistentCasingInFileNames": true, + "noFallthroughCasesInSwitch": true, + "module": "esnext", + "moduleResolution": "node", + "resolveJsonModule": true, + "isolatedModules": true, + "noEmit": true, + "jsx": "react-jsx" }, - - "include": ["src/**/*"], - "exclude": ["node_modules/*", "__sapper__/*", "public/*"] + "include": [ + "src" + ] }