mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-02 22:22:50 +00:00
feat(ui): Client-side wrapper
This commit is contained in:
1 parent
8fb5ca2e8c
commit
0290ea17e3
22 files changed
+57
-14
No files matched your search
+28
-7
@@ -4,12 +4,10 @@ import type { CFXParameters } from '../types';
|
||||
interface QueryData {
|
||||
date: number;
|
||||
query: string;
|
||||
executionTime: number
|
||||
executionTime: number;
|
||||
}
|
||||
|
||||
type QueryLog = {
|
||||
[invokingResource: string]: QueryData[];
|
||||
};
|
||||
type QueryLog = Record<string, QueryData[]>;
|
||||
|
||||
const logStorage: QueryLog = {};
|
||||
|
||||
@@ -22,9 +20,8 @@ export const logQuery = (invokingResource: string, query: string, executionTime:
|
||||
|
||||
if (!mysql_ui) return;
|
||||
|
||||
if (logStorage[invokingResource] === undefined) logStorage[invokingResource] = []
|
||||
if (logStorage[invokingResource] === undefined) logStorage[invokingResource] = [];
|
||||
logStorage[invokingResource].push({ query, executionTime, date: Date.now() });
|
||||
|
||||
};
|
||||
|
||||
RegisterCommand(
|
||||
@@ -32,7 +29,31 @@ RegisterCommand(
|
||||
(source: number) => {
|
||||
if (!mysql_ui) return;
|
||||
|
||||
emitNet(`oxmysql:openUi`, source, logStorage);
|
||||
let totalQueries: number = 0;
|
||||
let totalTime = 0;
|
||||
|
||||
for (const resource in logStorage) {
|
||||
const queries = logStorage[resource];
|
||||
|
||||
totalQueries += queries.length;
|
||||
totalTime += queries.reduce((totalTime, query) => (totalTime += query.executionTime), 0);
|
||||
}
|
||||
|
||||
emitNet(`oxmysql:openUi`, source, {
|
||||
resources: Object.keys(logStorage),
|
||||
totalQueries,
|
||||
totalTime,
|
||||
});
|
||||
},
|
||||
true
|
||||
);
|
||||
|
||||
onNet(`oxmysql:fetchResource`, (resource: string) => {
|
||||
if (typeof resource !== 'string') return;
|
||||
|
||||
const queries = logStorage[resource];
|
||||
|
||||
if (!queries) return;
|
||||
|
||||
emitNet(`oxmysql:loadResource`, source, queries);
|
||||
});
|
||||
@@ -1,4 +0,0 @@
|
||||
/node_modules/
|
||||
/public/build/
|
||||
|
||||
.DS_Store
|
||||
@@ -1,30 +0,0 @@
|
||||
{
|
||||
"name": "svelte-app",
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"build": "rollup -c",
|
||||
"dev": "rollup -c -w",
|
||||
"start": "sirv public --no-clear"
|
||||
},
|
||||
"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"
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
||||
|
||||
<title>Svelte app</title>
|
||||
|
||||
<link rel="stylesheet" href="./global.css" />
|
||||
<link rel="stylesheet" href="./build/bundle.css" />
|
||||
|
||||
<script defer src="./build/bundle.js"></script>
|
||||
</head>
|
||||
|
||||
<body></body>
|
||||
</html>
|
||||
@@ -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,
|
||||
},
|
||||
};
|
||||
@@ -1,18 +0,0 @@
|
||||
<script lang="ts">
|
||||
import VisibilityProvider from './providers/VisibilityProvider.svelte';
|
||||
import Main from './components/Main.svelte';
|
||||
import { debugData } from './utils/debugData';
|
||||
|
||||
debugData([
|
||||
{
|
||||
action: 'setVisible',
|
||||
data: true,
|
||||
},
|
||||
]);
|
||||
</script>
|
||||
|
||||
<main>
|
||||
<VisibilityProvider>
|
||||
<Main />
|
||||
</VisibilityProvider>
|
||||
</main>
|
||||
@@ -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>
|
||||
@@ -1,61 +0,0 @@
|
||||
<script lang="ts">
|
||||
import { currentResource } from '../../store/stores';
|
||||
const executingResources = ['luke_garages', 'ox_inventory', 'npwd'];
|
||||
</script>
|
||||
|
||||
<div class="container">
|
||||
<a href="#/"><div class="home-header">Home</div></a>
|
||||
{#each executingResources 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:visited {
|
||||
color: white;
|
||||
}
|
||||
</style>
|
||||
@@ -1 +0,0 @@
|
||||
<div>Some data here</div>
|
||||
@@ -1,38 +0,0 @@
|
||||
<script lang="ts">
|
||||
import { currentResource } from '../../store/stores';
|
||||
import { fetchNui } from '../../utils/fetchNui';
|
||||
import { onMount } from 'svelte';
|
||||
|
||||
export let params: { wild: string };
|
||||
|
||||
interface Data {
|
||||
[key: string]: number[];
|
||||
}
|
||||
|
||||
let queryData: number[];
|
||||
let resource: string;
|
||||
|
||||
const debugQueries: Data = {
|
||||
['luke_garages']: [300, 200, 100],
|
||||
['npwd']: [600, 300, 723],
|
||||
['ox_inventory']: [30, 270, 350],
|
||||
};
|
||||
|
||||
onMount(() => currentResource.set(params.wild));
|
||||
|
||||
currentResource.subscribe((value) => {
|
||||
resource = value;
|
||||
fetchNui('getResourceData', resource)
|
||||
.then((retData) => {
|
||||
queryData = retData;
|
||||
})
|
||||
.catch((e) => {
|
||||
queryData = debugQueries[resource];
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<div>
|
||||
{resource}
|
||||
{queryData}
|
||||
</div>
|
||||
Vendored
-1
@@ -1 +0,0 @@
|
||||
/// <reference types="svelte" />
|
||||
@@ -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>
|
||||
@@ -1,4 +0,0 @@
|
||||
import { writable } from "svelte/store";
|
||||
|
||||
export const visibility = writable(false);
|
||||
export const currentResource = writable('');
|
||||
@@ -1,30 +0,0 @@
|
||||
import {isEnvBrowser} from "./misc";
|
||||
|
||||
interface DebugEvent<T = any> {
|
||||
action: string;
|
||||
data: T;
|
||||
}
|
||||
|
||||
/**
|
||||
* Emulates dispatching an event using SendNuiMessage in the lua scripts.
|
||||
* This is used when developing in browser
|
||||
*
|
||||
* @param events - The event you want to cover
|
||||
* @param timer - How long until it should trigger (ms)
|
||||
*/
|
||||
export const debugData = <P>(events: DebugEvent<P>[], timer = 1000): void => {
|
||||
if (isEnvBrowser()) {
|
||||
for (const event of events) {
|
||||
setTimeout(() => {
|
||||
window.dispatchEvent(
|
||||
new MessageEvent("message", {
|
||||
data: {
|
||||
action: event.action,
|
||||
data: event.data,
|
||||
},
|
||||
})
|
||||
);
|
||||
}, timer);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1,27 +0,0 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
export async function fetchNui<T = any>(
|
||||
eventName: string,
|
||||
data: unknown = {}
|
||||
): Promise<T> {
|
||||
const options = {
|
||||
method: "post",
|
||||
headers: {
|
||||
"Content-Type": "application/json; charset=UTF-8",
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
};
|
||||
|
||||
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();
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
export const isEnvBrowser = (): boolean => !(window as any).invokeNative;
|
||||
@@ -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));
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
{
|
||||
"extends": "@tsconfig/svelte/tsconfig.json",
|
||||
|
||||
"compilerOptions": {
|
||||
"strict": true,
|
||||
"allowJs": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"noFallthroughCasesInSwitch": true
|
||||
},
|
||||
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules/*", "__sapper__/*", "public/*"]
|
||||
}
|
||||
Reference in new issue
Block a user