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
@@ -0,0 +1,18 @@
|
||||
<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>
|
||||
@@ -0,0 +1,49 @@
|
||||
<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,61 @@
|
||||
<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>
|
||||
@@ -0,0 +1 @@
|
||||
<div>Some data here</div>
|
||||
@@ -0,0 +1,38 @@
|
||||
<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
@@ -0,0 +1 @@
|
||||
/// <reference types="svelte" />
|
||||
@@ -0,0 +1,7 @@
|
||||
import App from "./App.svelte";
|
||||
|
||||
const app = new App({
|
||||
target: document.body,
|
||||
});
|
||||
|
||||
export default app;
|
||||
@@ -0,0 +1,35 @@
|
||||
<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,4 @@
|
||||
import { writable } from "svelte/store";
|
||||
|
||||
export const visibility = writable(false);
|
||||
export const currentResource = writable('');
|
||||
@@ -0,0 +1,30 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* @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();
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export const isEnvBrowser = (): boolean => !(window as any).invokeNative;
|
||||
@@ -0,0 +1,31 @@
|
||||
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