mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-02 14:38:50 +00:00
chore(nui): WIP React rewrite
This commit is contained in:
1 parent
ce802e7553
commit
b2b1a64c8f
35 files changed
+391
-460
No files matched your search
@@ -1,36 +0,0 @@
|
||||
<script lang="ts">
|
||||
import VisibilityProvider from './providers/VisibilityProvider.svelte';
|
||||
import Main from './components/Main.svelte';
|
||||
import { debugData } from './utils/debugData';
|
||||
import { useNuiEvent } from './utils/useNuiEvent';
|
||||
import { resources } from './store/stores';
|
||||
import type { InitData } from './types/init';
|
||||
|
||||
debugData([
|
||||
{
|
||||
action: 'setVisible',
|
||||
data: true,
|
||||
},
|
||||
]);
|
||||
|
||||
debugData([
|
||||
{
|
||||
action: 'init',
|
||||
data: {
|
||||
resources: ['ox_inventory', 'luke_garages', 'npwd'],
|
||||
totalQueries: 350,
|
||||
totalTime: 75000,
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
useNuiEvent<InitData>('init', (data) => {
|
||||
resources.set(data.resources);
|
||||
});
|
||||
</script>
|
||||
|
||||
<main>
|
||||
<VisibilityProvider>
|
||||
<Main />
|
||||
</VisibilityProvider>
|
||||
</main>
|
||||
Whitespace-only changes.
@@ -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 (
|
||||
<Center w="100%" h="100%">
|
||||
<Box width="60%" height="60%" backgroundColor="#191E26" borderRadius="1vh" color="white" fontFamily="Poppins">
|
||||
<TopBar />
|
||||
<SideBar />
|
||||
</Box>
|
||||
</Center>
|
||||
);
|
||||
};
|
||||
|
||||
export default App;
|
||||
@@ -1,49 +0,0 @@
|
||||
<script lang="ts">
|
||||
import Router from 'svelte-spa-router';
|
||||
import Resources from './menu/NavMenu.svelte';
|
||||
import Home from './routes/Home.svelte';
|
||||
import ResourceData from './routes/ResourceData.svelte';
|
||||
|
||||
const routes = {
|
||||
'/': Home,
|
||||
'/*': ResourceData,
|
||||
};
|
||||
</script>
|
||||
|
||||
<div class="container">
|
||||
<Resources />
|
||||
<div class="content-wrapper">
|
||||
<div class="content-container">
|
||||
<Router {routes} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.container {
|
||||
width: 55%;
|
||||
height: 65%;
|
||||
background-color: rgb(44, 62, 80);
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
left: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
border-radius: 0.25vh;
|
||||
}
|
||||
|
||||
.content-wrapper {
|
||||
margin-left: 18vh;
|
||||
height: 100%;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
}
|
||||
|
||||
.content-container {
|
||||
width: 85%;
|
||||
height: 85%;
|
||||
background-color: #34495e;
|
||||
border-radius: 0.25vh;
|
||||
color: white;
|
||||
font-family: 'Inter', sans-serif;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Box, VStack } from '@chakra-ui/react';
|
||||
|
||||
const SideBar: React.FC = () => {
|
||||
return (
|
||||
<Box p="1.2vh" fontSize="1.5vh" position="fixed">
|
||||
<VStack align="left">
|
||||
<Box>es_extended</Box>
|
||||
<Box>ox_inventory</Box>
|
||||
<Box>luke_garages</Box>
|
||||
</VStack>
|
||||
</Box>
|
||||
);
|
||||
};
|
||||
|
||||
export default SideBar;
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Box } from '@chakra-ui/react';
|
||||
|
||||
const TopBar: React.FC = () => {
|
||||
return (
|
||||
<Box w="100%" h="5%">
|
||||
<Box p="1.2vh" fontSize="2.2vh">
|
||||
Resources
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
};
|
||||
|
||||
export default TopBar;
|
||||
@@ -1,64 +0,0 @@
|
||||
<script lang="ts">
|
||||
import { currentResource } from '../../store/stores';
|
||||
import { resources } from '../../store/stores';
|
||||
</script>
|
||||
|
||||
<div class="container">
|
||||
<a href="#/"><div class="home-header">Home</div></a>
|
||||
{#each $resources as resource}
|
||||
<div class="resource-wrapper">
|
||||
<a href={`#/${resource}`}>
|
||||
<div class="resource" on:click={() => currentResource.set(resource)}>
|
||||
{resource}
|
||||
</div>
|
||||
</a>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.container {
|
||||
width: 18vh;
|
||||
position: fixed;
|
||||
left: 0;
|
||||
top: 0;
|
||||
background-color: #34495e;
|
||||
height: 100%;
|
||||
border-top-left-radius: 0.25vh;
|
||||
border-bottom-left-radius: 0.25vh;
|
||||
color: white;
|
||||
font-family: 'Inter', sans-serif;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.home-header {
|
||||
font-size: 2.2vh;
|
||||
text-align: left;
|
||||
padding: 1vh;
|
||||
border-bottom: 0.25vh solid grey;
|
||||
}
|
||||
|
||||
.resource-wrapper {
|
||||
padding: 1vh;
|
||||
}
|
||||
|
||||
.resource {
|
||||
font-family: 'Inter', sans-serif;
|
||||
font-size: 2.2vh;
|
||||
width: 100%;
|
||||
height: 5%;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.resource-wrapper:hover,
|
||||
.home-header:hover {
|
||||
background-color: grey;
|
||||
}
|
||||
|
||||
a,
|
||||
a:visited,
|
||||
a:active,
|
||||
a:focus {
|
||||
color: white;
|
||||
}
|
||||
</style>
|
||||
@@ -1,18 +0,0 @@
|
||||
<script lang="ts">
|
||||
import { useNuiEvent } from '../../utils/useNuiEvent';
|
||||
import type { InitData } from '../../types/init';
|
||||
|
||||
let initData: InitData;
|
||||
|
||||
useNuiEvent<InitData>('init', (data) => {
|
||||
initData = data;
|
||||
});
|
||||
</script>
|
||||
|
||||
<div>
|
||||
{#if initData}
|
||||
<div>Resources: {initData.resources}</div>
|
||||
<div>Total queries: {initData.totalQueries}</div>
|
||||
<div>Total time: {initData.totalTime}</div>
|
||||
{/if}
|
||||
</div>
|
||||
@@ -1,40 +0,0 @@
|
||||
<script lang="ts">
|
||||
import { currentResource } from '../../store/stores';
|
||||
import { fetchNui } from '../../utils/fetchNui';
|
||||
import { onMount } from 'svelte';
|
||||
import { useNuiEvent } from '../../utils/useNuiEvent';
|
||||
import { debugData } from '../../utils/debugData';
|
||||
import type { QueryData } from '../../types/query';
|
||||
|
||||
export let params: { wild: string };
|
||||
|
||||
let queryData: QueryData[];
|
||||
let isDataLoaded: boolean = false;
|
||||
|
||||
onMount(() => currentResource.set(params.wild));
|
||||
|
||||
currentResource.subscribe((resource) => {
|
||||
isDataLoaded = false;
|
||||
fetchNui('fetchResource', resource);
|
||||
debugData([
|
||||
{
|
||||
action: 'loadResource',
|
||||
data: [
|
||||
{ date: Date.now(), query: 'SELECT * FROM `somewhere`', executionTime: 2 },
|
||||
{ date: Date.now(), query: 'SELECT * FROM `elsewhere`', executionTime: 5 },
|
||||
{ date: Date.now(), query: 'SELECT * FROM `somewhere_elsewhere`', executionTime: 3 },
|
||||
{ date: Date.now(), query: 'SELECT * FROM `selsewhere`', executionTime: 7 },
|
||||
],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
useNuiEvent('loadResource', (data: QueryData[]) => {
|
||||
queryData = data;
|
||||
isDataLoaded = true;
|
||||
});
|
||||
</script>
|
||||
|
||||
{#if isDataLoaded === true}
|
||||
{JSON.stringify(queryData)}
|
||||
{/if}
|
||||
Vendored
-1
@@ -1 +0,0 @@
|
||||
/// <reference types="svelte" />
|
||||
@@ -0,0 +1,49 @@
|
||||
import {MutableRefObject, useEffect, useRef} from "react";
|
||||
import {noop} from "../utils/misc";
|
||||
|
||||
interface NuiMessageData<T = unknown> {
|
||||
action: string;
|
||||
data: T;
|
||||
}
|
||||
|
||||
type NuiHandlerSignature<T> = (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 = <T = any>(
|
||||
action: string,
|
||||
handler: (data: T) => void
|
||||
) => {
|
||||
const savedHandler: MutableRefObject<NuiHandlerSignature<T>> = useRef(noop);
|
||||
|
||||
// Make sure we handle for a reactive handler
|
||||
useEffect(() => {
|
||||
savedHandler.current = handler;
|
||||
}, [handler]);
|
||||
|
||||
useEffect(() => {
|
||||
const eventListener = (event: MessageEvent<NuiMessageData<T>>) => {
|
||||
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]);
|
||||
};
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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(
|
||||
<React.StrictMode>
|
||||
<VisibilityProvider>
|
||||
<ChakraProvider>
|
||||
<App />
|
||||
</ChakraProvider>
|
||||
</VisibilityProvider>
|
||||
</React.StrictMode>,
|
||||
document.getElementById('root')
|
||||
);
|
||||
@@ -1,7 +0,0 @@
|
||||
import App from "./App.svelte";
|
||||
|
||||
const app = new App({
|
||||
target: document.body,
|
||||
});
|
||||
|
||||
export default app;
|
||||
@@ -1,35 +0,0 @@
|
||||
<script lang="ts">
|
||||
import { useNuiEvent } from '../utils/useNuiEvent';
|
||||
import { fetchNui } from '../utils/fetchNui';
|
||||
import { onMount } from 'svelte';
|
||||
import { visibility } from '../store/stores';
|
||||
|
||||
let isVisible: boolean;
|
||||
|
||||
visibility.subscribe((visible) => {
|
||||
isVisible = visible;
|
||||
});
|
||||
|
||||
useNuiEvent<boolean>('setVisible', (visible) => {
|
||||
visibility.set(visible);
|
||||
});
|
||||
|
||||
onMount(() => {
|
||||
const keyHandler = (e: KeyboardEvent) => {
|
||||
if (isVisible && ['Escape'].includes(e.code)) {
|
||||
fetchNui('hideUI');
|
||||
visibility.set(false);
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener('keydown', keyHandler);
|
||||
|
||||
return () => window.removeEventListener('keydown', keyHandler);
|
||||
});
|
||||
</script>
|
||||
|
||||
<main>
|
||||
{#if isVisible}
|
||||
<slot />
|
||||
{/if}
|
||||
</main>
|
||||
@@ -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<VisibilityProviderValue | null>(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<boolean>('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 (
|
||||
<VisibilityCtx.Provider
|
||||
value={{
|
||||
visible,
|
||||
setVisible,
|
||||
}}
|
||||
>
|
||||
<div style={{ visibility: visible ? 'visible' : 'hidden', height: '100%' }}>{children}</div>
|
||||
</VisibilityCtx.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useVisibility = () =>
|
||||
useContext<VisibilityProviderValue>(VisibilityCtx as Context<VisibilityProviderValue>);
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
/// <reference types="react-scripts" />
|
||||
@@ -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';
|
||||
@@ -1,5 +0,0 @@
|
||||
import { writable } from "svelte/store";
|
||||
|
||||
export const visibility = writable(false);
|
||||
export const currentResource = writable('');
|
||||
export const resources = writable<string[]>([])
|
||||
@@ -1,5 +0,0 @@
|
||||
export type InitData = {
|
||||
resources: string[];
|
||||
totalQueries: number;
|
||||
totalTime: number;
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
export type QueryData = {
|
||||
date: number;
|
||||
query: string;
|
||||
executionTime: number;
|
||||
}
|
||||
@@ -13,7 +13,7 @@ interface DebugEvent<T = any> {
|
||||
* @param timer - How long until it should trigger (ms)
|
||||
*/
|
||||
export const debugData = <P>(events: DebugEvent<P>[], timer = 1000): void => {
|
||||
if (isEnvBrowser()) {
|
||||
if (process.env.NODE_ENV === "development" && isEnvBrowser()) {
|
||||
for (const event of events) {
|
||||
setTimeout(() => {
|
||||
window.dispatchEvent(
|
||||
|
||||
+16
-19
@@ -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<T = any>(
|
||||
eventName: string,
|
||||
data: unknown = {}
|
||||
): Promise<T> {
|
||||
export async function fetchNui<T = any>(eventName: string, data?: any): Promise<T> {
|
||||
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
|
||||
}
|
||||
@@ -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 = () => {}
|
||||
@@ -1,31 +0,0 @@
|
||||
import { onMount, onDestroy } from "svelte";
|
||||
|
||||
interface NuiMessage<T = unknown> {
|
||||
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<T = unknown>(
|
||||
action: string,
|
||||
handler: (data: T) => void
|
||||
) {
|
||||
const eventListener = (event: MessageEvent<NuiMessage<T>>) => {
|
||||
const { action: eventAction, data } = event.data;
|
||||
|
||||
eventAction === action && handler(data);
|
||||
};
|
||||
onMount(() => window.addEventListener("message", eventListener));
|
||||
onDestroy(() => window.removeEventListener("message", eventListener));
|
||||
}
|
||||
Reference in new issue
Block a user