Rewrite most of this

This commit is contained in:
Melon 2023-10-25 00:04:00 +01:00
parent c9862ccc92
commit 97a2cf089b
Signed by: melon
GPG Key ID: 6C9D970C50D26A25

View File

@ -1,6 +1,6 @@
<script lang="ts"> <script lang="ts">
import Flags from "../components/Flags.svelte"; import Flags from "../components/Flags.svelte";
import {getBearer, loginStore, type LoginStore} from "../stores/login"; import {getBearer} from "../stores/login";
const apiViolet = import.meta.env.VITE_API_VIOLET; const apiViolet = import.meta.env.VITE_API_VIOLET;
@ -34,19 +34,17 @@
active: boolean; active: boolean;
} }
let serverRoutes: Route[]; type CSPair<T> = {client: T; server: T} | CSPairNotC<T> | CSPairNotS<T>;
let serverRedirects: Redirect[]; type CSPairNotC<T> = {client: null; server: T};
type CSPairNotS<T> = {client: T; server: null};
let clientRoutes: Route[]; let routeData: Map<string, CSPair<Route>> = new Map();
let clientRedirects: Redirect[]; let redirectData: Map<string, CSPair<Redirect>> = new Map();
function getServerRoute(src: string) { let routeSrcs: Set<string> = new Set();
return serverRoutes.find(v => v.src === src); let redirectSrcs: Set<string> = new Set();
}
function getServerRedirect(src: string) { $: console.log(routeData);
return serverRedirects.find(v => v.src === src);
}
function routeEqual(a: Route, b: Route): boolean { function routeEqual(a: Route, b: Route): boolean {
return a.src === b.src && a.dst === b.dst && a.flags === b.flags && a.active === b.active; return a.src === b.src && a.dst === b.dst && a.flags === b.flags && a.active === b.active;
@ -56,14 +54,12 @@
return a.src === b.src && a.dst === b.dst && a.flags === b.flags && a.active === b.active; return a.src === b.src && a.dst === b.dst && a.flags === b.flags && a.active === b.active;
} }
function resetRoute(i: number, route: Route) { function noCPair<T>(pair: CSPair<T>): pair is CSPairNotC<T> {
clientRoutes[i] = JSON.parse(JSON.stringify(route)); return pair.client == null;
clientRoutes = clientRoutes;
} }
function resetRedirect(i: number, redirect: Redirect) { function noSPair<T>(pair: CSPair<T>): pair is CSPairNotS<T> {
clientRedirects[i] = JSON.parse(JSON.stringify(redirect)); return pair.server == null;
clientRedirects = clientRedirects;
} }
let promiseForRoutes = new Promise<void>((res, rej) => { let promiseForRoutes = new Promise<void>((res, rej) => {
@ -73,8 +69,10 @@
return x.json(); return x.json();
}) })
.then(x => { .then(x => {
serverRoutes = x as Route[]; let routes = x as Route[];
clientRoutes = JSON.parse(JSON.stringify(serverRoutes)); routes.forEach(x => {
routeData.set(x.src, {client: JSON.parse(JSON.stringify(x)), server: x});
});
res(); res();
}) })
.catch(x => rej(x)); .catch(x => rej(x));
@ -86,8 +84,10 @@
return x.json(); return x.json();
}) })
.then(x => { .then(x => {
serverRedirects = x as Redirect[]; let redirects = x as Redirect[];
clientRedirects = JSON.parse(JSON.stringify(serverRedirects)); redirects.forEach(x => {
redirectData.set(x.src, {client: JSON.parse(JSON.stringify(x)), server: x});
});
res(); res();
}) })
.catch(x => rej(x)); .catch(x => rej(x));
@ -105,21 +105,31 @@
<th>Flags</th> <th>Flags</th>
<th>Active</th> <th>Active</th>
</tr> </tr>
{#each clientRoutes as route, i (route.src)} {#each routeSrcs as src (src)}
{@const serverRoute = getServerRoute(route.src)} {@const route = routeData.get(src)}
{@const isNew = serverRoute == undefined} {#if route}
<tr class:created={isNew} class:modified={isNew || !routeEqual(route, serverRoute)}> {#if noCPair(route)}
<td>{route.src}</td> <tr class="deleted">
<td>{route.dst}</td> <td>{route.server.src}</td>
<td>{route.server.dst}</td>
<td><Flags bind:value={route.flags} editable keys={routeKeys} /></td>
<td><input type="checkbox" bind:checked={route.active} /></td>
</tr>
{/if}
{@const isCreated = !route.server}
{@const isDeleted = !route.client}
<tr class:created={isCreated} class:modified={isCreated || !routeEqual(route.client, route.server)} class:deleted={isDeleted}>
<td>{route.client.src}</td>
<td>{route.client.dst}</td>
<td><Flags bind:value={route.flags} editable keys={routeKeys} /></td> <td><Flags bind:value={route.flags} editable keys={routeKeys} /></td>
<td><input type="checkbox" bind:checked={route.active} /></td> <td><input type="checkbox" bind:checked={route.active} /></td>
<td>{route.active}</td>
<td> <td>
{#if !isNew} {#if !isCreated}
<button on:click={() => resetRoute(i, serverRoute)}>Reset</button> <button on:click={() => resetRoute(i, serverRoute)}>Reset</button>
{/if} {/if}
</td> </td>
</tr> </tr>
{/if}
{/each} {/each}
</table> </table>
{:catch err} {:catch err}
@ -158,10 +168,13 @@
{/await} {/await}
<style lang="scss"> <style lang="scss">
table > tr.created { table {
> tr.created {
background-color: #1a5100; background-color: #1a5100;
} }
table > tr.modified {
> tr.modified {
background-color: #515100; background-color: #515100;
} }
}
</style> </style>