Merge branch 'auth' into dev
This commit is contained in:
@@ -1,19 +1,22 @@
|
|||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import './App.css'
|
import './App.css'
|
||||||
import InstancesTable from './components/InstanceTable.tsx'
|
import InstancesTable from './components/InstanceTable.tsx'
|
||||||
|
import Settings from './components/Settings.tsx'
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [count, setCount] = useState(0)
|
const [settingsOpen, setSettingsOpen] = useState(false)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<section id='main'>
|
<section id='main'>
|
||||||
<header>
|
<header>
|
||||||
<h1>VSSM - Dashboard</h1>
|
<h1>VSSM - Dashboard</h1>
|
||||||
|
<button onClick={() => setSettingsOpen(true)}>⚙</button>
|
||||||
</header>
|
</header>
|
||||||
<div className='contentBox'>
|
<div className='contentBox'>
|
||||||
<InstancesTable />
|
<InstancesTable />
|
||||||
</div>
|
</div>
|
||||||
|
{settingsOpen && <Settings onClose={() => setSettingsOpen(false)} />}
|
||||||
</section>
|
</section>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
const ADDRESS = "http://127.0.0.1:65000";
|
import { getSetting, SettingKey } from "./settings";
|
||||||
|
|
||||||
type Instance = {
|
type Instance = {
|
||||||
name: string;
|
name: string;
|
||||||
@@ -12,105 +12,79 @@ class api {
|
|||||||
|
|
||||||
} export default api;
|
} export default api;
|
||||||
|
|
||||||
|
async function apiFetch(path: string, options: RequestInit = {}, errorContext: string): Promise<Response> {
|
||||||
|
const token = getSetting(SettingKey.Token);
|
||||||
|
const headers = {
|
||||||
|
...options.headers,
|
||||||
|
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||||
|
};
|
||||||
|
|
||||||
|
const res = await fetch(`${getSetting(SettingKey.Address)}${path}`, { ...options, headers });
|
||||||
|
if (!res.ok) {
|
||||||
|
const message = await res.text();
|
||||||
|
throw new Error(`${errorContext}: ${message.trim()}`);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
// --- GET CALLS ---
|
// --- GET CALLS ---
|
||||||
|
|
||||||
export async function fetchInstanceList(): Promise<Instance[]> {
|
export async function fetchInstanceList(): Promise<Instance[]> {
|
||||||
const res = await fetch(`${ADDRESS}/instances/list`);
|
const res = await apiFetch("/instances/list", {}, "Could not fetch instance list");
|
||||||
if (!res.ok) {
|
|
||||||
const message = await res.text();
|
|
||||||
throw new Error(`Could not fetch instance list: ${message.trim()}`);
|
|
||||||
}
|
|
||||||
return res.json();
|
return res.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchInstanceLogs(name: string): Promise<string[]> {
|
export async function fetchInstanceLogs(name: string): Promise<string[]> {
|
||||||
const res = await fetch(`${ADDRESS}/instances/logs?name=${name}`);
|
const res = await apiFetch(`/instances/logs?name=${name}`, {}, "Could not fetch instance logs");
|
||||||
if (!res.ok) {
|
|
||||||
const message = await res.text();
|
|
||||||
throw new Error(`Could not fetch instance logs: ${message.trim()}`);
|
|
||||||
}
|
|
||||||
return res.json();
|
return res.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchInstanceConfig(name: string): Promise<string> {
|
export async function fetchInstanceConfig(name: string): Promise<string> {
|
||||||
const res = await fetch(`${ADDRESS}/instances/getConfig?name=${name}`);
|
const res = await apiFetch(`/instances/getConfig?name=${name}`, {}, "Could not fetch instance config");
|
||||||
if (!res.ok) {
|
|
||||||
const message = await res.text();
|
|
||||||
throw new Error(`Could not fetch instance config: ${message.trim()}`);
|
|
||||||
}
|
|
||||||
return res.text();
|
return res.text();
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- POST CALLS ---
|
// --- POST CALLS ---
|
||||||
|
|
||||||
export async function sendStartInstanceRequest(name: string) {
|
export async function sendStartInstanceRequest(name: string) {
|
||||||
const res = await fetch(`${ADDRESS}/instances/start?name=${name}`, { method: "POST" });
|
const res = await apiFetch(`/instances/start?name=${name}`, { method: "POST" }, "Start Instance Request failed");
|
||||||
if (!res.ok) {
|
|
||||||
const message = await res.text();
|
|
||||||
throw new Error(`Start Instance Request failed: ${message.trim()}`);
|
|
||||||
}
|
|
||||||
return res.text();
|
return res.text();
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function sendStopInstanceRequest(name: string) {
|
export async function sendStopInstanceRequest(name: string) {
|
||||||
const res = await fetch(`${ADDRESS}/instances/stop?name=${name}`, { method: "POST" });
|
const res = await apiFetch(`/instances/stop?name=${name}`, { method: "POST" }, "Stop Instance Request failed");
|
||||||
if (!res.ok) {
|
|
||||||
const message = await res.text();
|
|
||||||
throw new Error(`Stop Instance Request failed: ${message.trim()}`);
|
|
||||||
}
|
|
||||||
return res.text();
|
return res.text();
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function sendCreateInstanceRequest(name: string, version: string, port: number) {
|
export async function sendCreateInstanceRequest(name: string, version: string, port: number) {
|
||||||
const res = await fetch(`${ADDRESS}/instances/create?name=${name}&version=${version}&port=${port}`, { method: "POST" });
|
const res = await apiFetch(`/instances/create?name=${name}&version=${version}&port=${port}`, { method: "POST" }, "Create instance request failed");
|
||||||
if (!res.ok) {
|
|
||||||
const message = await res.text();
|
|
||||||
throw new Error(`Create instance request failed: ${message.trim()}`);
|
|
||||||
}
|
|
||||||
return res.text();
|
return res.text();
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function sendDeleteInstanceRequest(name: string, purge: boolean) {
|
export async function sendDeleteInstanceRequest(name: string, purge: boolean) {
|
||||||
const res = await fetch(`${ADDRESS}/instances/delete?name=${name}&purge=${purge}`, { method: "POST" });
|
const res = await apiFetch(`/instances/delete?name=${name}&purge=${purge}`, { method: "POST" }, "Delete instance request failed");
|
||||||
if (!res.ok) {
|
|
||||||
const message = await res.text();
|
|
||||||
throw new Error(`Delete instance request failed: ${message.trim()}`);
|
|
||||||
}
|
|
||||||
return res.text();
|
return res.text();
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function sendModifyInstanceRequest(name: string, newName: string, newPort: number) {
|
export async function sendModifyInstanceRequest(name: string, newName: string, newPort: number) {
|
||||||
const res = await fetch(`${ADDRESS}/instances/modify?name=${name}&newName=${newName}&newPort=${newPort}`, { method: "POST" });
|
const res = await apiFetch(`/instances/modify?name=${name}&newName=${newName}&newPort=${newPort}`, { method: "POST" }, "Modify instance request failed");
|
||||||
if (!res.ok) {
|
|
||||||
const message = await res.text();
|
|
||||||
throw new Error(`Modify instance request failed: ${message.trim()}`);
|
|
||||||
}
|
|
||||||
return res.text();
|
return res.text();
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function sendCommandRequest(name: string, payload: string) {
|
export async function sendCommandRequest(name: string, payload: string) {
|
||||||
const res = await fetch(`${ADDRESS}/instances/command?name=${name}`, {
|
const res = await apiFetch(`/instances/command?name=${name}`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({ command: payload }),
|
body: JSON.stringify({ command: payload }),
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
});
|
}, "Send Instance Command failed");
|
||||||
if (!res.ok) {
|
|
||||||
const message = await res.text();
|
|
||||||
throw new Error(`Send Instance Command failed: ${message.trim()}`);
|
|
||||||
}
|
|
||||||
return res.text();
|
return res.text();
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function sendSetInstanceConfigRequest(name: string, config: string) {
|
export async function sendSetInstanceConfigRequest(name: string, config: string) {
|
||||||
const res = await fetch(`${ADDRESS}/instances/setConfig?name=${name}`, {
|
const res = await apiFetch(`/instances/setConfig?name=${name}`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: config,
|
body: config,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
});
|
}, "Set instance config request failed");
|
||||||
if (!res.ok) {
|
|
||||||
const message = await res.text();
|
|
||||||
throw new Error(`Set instance config request failed: ${message.trim()}`);
|
|
||||||
}
|
|
||||||
return res.text();
|
return res.text();
|
||||||
}
|
}
|
||||||
45
vssm_web/vssm_web/src/components/Settings.tsx
Normal file
45
vssm_web/vssm_web/src/components/Settings.tsx
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import { useState } from "react";
|
||||||
|
import { getSetting, setSetting, SettingKey } from "../settings";
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
onClose: () => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function Settings({ onClose }: Props) {
|
||||||
|
const [address, setAddress] = useState(getSetting(SettingKey.Address));
|
||||||
|
const [token, setToken] = useState(getSetting(SettingKey.Token));
|
||||||
|
const [saved, setSaved] = useState(false);
|
||||||
|
|
||||||
|
function handleSave() {
|
||||||
|
setSetting(SettingKey.Address, address);
|
||||||
|
setSetting(SettingKey.Token, token);
|
||||||
|
setSaved(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="modalBackdrop" onClick={onClose}>
|
||||||
|
<div className="modalCard" onClick={e => e.stopPropagation()}>
|
||||||
|
<h3>Settings</h3>
|
||||||
|
|
||||||
|
<label>
|
||||||
|
API Address
|
||||||
|
<input type="text" value={address} onChange={e => { setAddress(e.target.value); setSaved(false); }} />
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<label>
|
||||||
|
Auth Token
|
||||||
|
<input type="text" value={token} onChange={e => { setToken(e.target.value); setSaved(false); }} />
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<p className="warningText">Stored locally in this browser only.</p>
|
||||||
|
|
||||||
|
{saved && <p>Saved.</p>}
|
||||||
|
|
||||||
|
<div className="flexboxHorizontal">
|
||||||
|
<button onClick={handleSave}>Save</button>
|
||||||
|
<button className="normalButton" onClick={onClose}>Close</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -154,6 +154,9 @@ header {
|
|||||||
height: auto;
|
height: auto;
|
||||||
padding: 5px 20px;
|
padding: 5px 20px;
|
||||||
margin-bottom: 25px;
|
margin-bottom: 25px;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
}
|
}
|
||||||
header h1 {
|
header h1 {
|
||||||
font-size: 4em;
|
font-size: 4em;
|
||||||
@@ -184,6 +187,15 @@ header h1 {
|
|||||||
min-width: 300px;
|
min-width: 300px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.modalCard label {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 6px;
|
||||||
|
font-weight: 600;
|
||||||
|
font-size: 0.9em;
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
|
|
||||||
/* Normal components styling */
|
/* Normal components styling */
|
||||||
button {
|
button {
|
||||||
transition: transform 0.15s ease-in-out 0s;
|
transition: transform 0.15s ease-in-out 0s;
|
||||||
|
|||||||
27
vssm_web/vssm_web/src/settings.ts
Normal file
27
vssm_web/vssm_web/src/settings.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
export const SettingKey = {
|
||||||
|
Address: "vssm_api_address",
|
||||||
|
Token: "vssm_auth_token",
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
export type SettingKey = typeof SettingKey[keyof typeof SettingKey];
|
||||||
|
|
||||||
|
const SettingDefaults: Record<SettingKey, string> = {
|
||||||
|
[SettingKey.Address]: "",
|
||||||
|
[SettingKey.Token]: "",
|
||||||
|
};
|
||||||
|
|
||||||
|
export function getSetting(key: SettingKey): string {
|
||||||
|
const value = localStorage.getItem(key);
|
||||||
|
if (!value) {
|
||||||
|
return SettingDefaults[key];
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setSetting(key: SettingKey, value: string) {
|
||||||
|
localStorage.setItem(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function clearSetting(key: SettingKey) {
|
||||||
|
localStorage.removeItem(key);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user