refactor(web): initial svelte rewrite files

This commit is contained in:
Luke committed 2023-03-18 19:03:35 +01:00
1 parent be8580396c
commit e04a43a86c
53 files changed
+3462 -12172

No files matched your search

+12
View File
@@ -0,0 +1,12 @@
<script lang="ts">
import Router from 'svelte-spa-router';
import Navbar from './components/Navbar.svelte';
import { routes } from './routes';
</script>
<main class="w-full h-full flex justify-center items-center font-main">
<div class="bg-slate-900 text-white w-[1200px] h-[650px] flex rounded-md">
<Navbar />
<Router {routes} />
</div>
</main>
+14
View File
@@ -0,0 +1,14 @@
<script lang="ts">
import { push, location } from 'svelte-spa-router';
export let route: string;
</script>
<button
on:click={() => push(route)}
class="flex flex-col hover:bg-slate-600 w-[50px] h-[50px] justify-center items-center p-3 {$location === route
? 'bg-slate-600'
: 'bg-slate-700'} rounded-md"
>
<slot />
</button>
+20
View File
@@ -0,0 +1,20 @@
<script lang="ts">
import { IconLayoutDashboard, IconLogout, IconSourceCode } from '@tabler/icons-svelte';
import NavLink from './NavLink.svelte';
</script>
<div class="bg-slate-800 w-[80px] h-full rounded-tl-md rounded-bl-md p-4 flex flex-col justify-between items-center">
<div class="flex flex-col gap-2">
<NavLink route="/">
<IconLayoutDashboard />
</NavLink>
<NavLink route="/resources">
<IconSourceCode />
</NavLink>
</div>
<div
class="bg-red-500 bg-opacity-30 text-red-500 hover:bg-opacity-40 rounded-md w-[50px] h-[50px] flex justify-center items-center"
>
<IconLogout />
</div>
</div>
+15
View File
@@ -0,0 +1,15 @@
@import url(https://use.typekit.net/wxh5ury.css);
@tailwind base;
@tailwind components;
@tailwind utilities;
* {
margin: 0;
padding: 0;
}
#app {
width: 100vw;
height: 100vh;
}
+8
View File
@@ -0,0 +1,8 @@
import './index.css';
import App from './App.svelte';
const app = new App({
target: document.getElementById('app'),
});
export default app;
+1
View File
@@ -0,0 +1 @@
<h2>Dashboard</h2>
+1
View File
@@ -0,0 +1 @@
<h2>Resources</h2>
+9
View File
@@ -0,0 +1,9 @@
import Dashboard from './pages/dashboard/Dashboard.svelte';
import Resources from './pages/resources/Resources.svelte';
import type { RouteDefinition } from 'svelte-spa-router';
export const routes: RouteDefinition = {
'/': Dashboard,
'/resources': Resources,
};
+30
View File
@@ -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);
}
}
};
+24
View File
@@ -0,0 +1,24 @@
/**
* @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
View File
@@ -0,0 +1 @@
export const isEnvBrowser = (): boolean => !(window as any).invokeNative;
+28
View File
@@ -0,0 +1,28 @@
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));
}
+2
View File
@@ -0,0 +1,2 @@
/// <reference types="svelte" />
/// <reference types="vite/client" />