Add frontend
This commit is contained in:
23
frontend/.gitignore
vendored
Normal file
23
frontend/.gitignore
vendored
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
node_modules
|
||||||
|
|
||||||
|
# Output
|
||||||
|
.output
|
||||||
|
.vercel
|
||||||
|
.netlify
|
||||||
|
.wrangler
|
||||||
|
/.svelte-kit
|
||||||
|
/build
|
||||||
|
|
||||||
|
# OS
|
||||||
|
.DS_Store
|
||||||
|
Thumbs.db
|
||||||
|
|
||||||
|
# Env
|
||||||
|
.env
|
||||||
|
.env.*
|
||||||
|
!.env.example
|
||||||
|
!.env.test
|
||||||
|
|
||||||
|
# Vite
|
||||||
|
vite.config.js.timestamp-*
|
||||||
|
vite.config.ts.timestamp-*
|
||||||
1
frontend/.npmrc
Normal file
1
frontend/.npmrc
Normal file
@@ -0,0 +1 @@
|
|||||||
|
engine-strict=true
|
||||||
9
frontend/.prettierignore
Normal file
9
frontend/.prettierignore
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# Package Managers
|
||||||
|
package-lock.json
|
||||||
|
pnpm-lock.yaml
|
||||||
|
yarn.lock
|
||||||
|
bun.lock
|
||||||
|
bun.lockb
|
||||||
|
|
||||||
|
# Miscellaneous
|
||||||
|
/static/
|
||||||
15
frontend/.prettierrc
Normal file
15
frontend/.prettierrc
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"useTabs": true,
|
||||||
|
"singleQuote": true,
|
||||||
|
"trailingComma": "none",
|
||||||
|
"printWidth": 100,
|
||||||
|
"plugins": ["prettier-plugin-svelte"],
|
||||||
|
"overrides": [
|
||||||
|
{
|
||||||
|
"files": "*.svelte",
|
||||||
|
"options": {
|
||||||
|
"parser": "svelte"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
38
frontend/README.md
Normal file
38
frontend/README.md
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
# sv
|
||||||
|
|
||||||
|
Everything you need to build a Svelte project, powered by [`sv`](https://github.com/sveltejs/cli).
|
||||||
|
|
||||||
|
## Creating a project
|
||||||
|
|
||||||
|
If you're seeing this, you've probably already done this step. Congrats!
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# create a new project in the current directory
|
||||||
|
npx sv create
|
||||||
|
|
||||||
|
# create a new project in my-app
|
||||||
|
npx sv create my-app
|
||||||
|
```
|
||||||
|
|
||||||
|
## Developing
|
||||||
|
|
||||||
|
Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
npm run dev
|
||||||
|
|
||||||
|
# or start the server and open the app in a new browser tab
|
||||||
|
npm run dev -- --open
|
||||||
|
```
|
||||||
|
|
||||||
|
## Building
|
||||||
|
|
||||||
|
To create a production version of your app:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
npm run build
|
||||||
|
```
|
||||||
|
|
||||||
|
You can preview the production build with `npm run preview`.
|
||||||
|
|
||||||
|
> To deploy your app, you may need to install an [adapter](https://svelte.dev/docs/kit/adapters) for your target environment.
|
||||||
41
frontend/eslint.config.js
Normal file
41
frontend/eslint.config.js
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import prettier from 'eslint-config-prettier';
|
||||||
|
import { fileURLToPath } from 'node:url';
|
||||||
|
import { includeIgnoreFile } from '@eslint/compat';
|
||||||
|
import js from '@eslint/js';
|
||||||
|
import svelte from 'eslint-plugin-svelte';
|
||||||
|
import { defineConfig } from 'eslint/config';
|
||||||
|
import globals from 'globals';
|
||||||
|
import ts from 'typescript-eslint';
|
||||||
|
import svelteConfig from './svelte.config.js';
|
||||||
|
|
||||||
|
const gitignorePath = fileURLToPath(new URL('./.gitignore', import.meta.url));
|
||||||
|
|
||||||
|
export default defineConfig(
|
||||||
|
includeIgnoreFile(gitignorePath),
|
||||||
|
js.configs.recommended,
|
||||||
|
...ts.configs.recommended,
|
||||||
|
...svelte.configs.recommended,
|
||||||
|
prettier,
|
||||||
|
...svelte.configs.prettier,
|
||||||
|
{
|
||||||
|
languageOptions: {
|
||||||
|
globals: { ...globals.browser, ...globals.node }
|
||||||
|
},
|
||||||
|
rules: {
|
||||||
|
// typescript-eslint strongly recommend that you do not use the no-undef lint rule on TypeScript projects.
|
||||||
|
// see: https://typescript-eslint.io/troubleshooting/faqs/eslint/#i-get-errors-from-the-no-undef-rule-about-global-variables-not-being-defined-even-though-there-are-no-typescript-errors
|
||||||
|
'no-undef': 'off'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
files: ['**/*.svelte', '**/*.svelte.ts', '**/*.svelte.js'],
|
||||||
|
languageOptions: {
|
||||||
|
parserOptions: {
|
||||||
|
projectService: true,
|
||||||
|
extraFileExtensions: ['.svelte'],
|
||||||
|
parser: ts.parser,
|
||||||
|
svelteConfig
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
4330
frontend/package-lock.json
generated
Normal file
4330
frontend/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
45
frontend/package.json
Normal file
45
frontend/package.json
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
{
|
||||||
|
"name": "frontend",
|
||||||
|
"private": true,
|
||||||
|
"version": "0.0.1",
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "vite dev",
|
||||||
|
"build": "vite build",
|
||||||
|
"preview": "vite preview",
|
||||||
|
"prepare": "svelte-kit sync || echo ''",
|
||||||
|
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
||||||
|
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
|
||||||
|
"format": "prettier --write .",
|
||||||
|
"lint": "prettier --check . && eslint ."
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@eslint/compat": "^1.4.0",
|
||||||
|
"@eslint/js": "^9.36.0",
|
||||||
|
"@sveltejs/adapter-auto": "^6.1.0",
|
||||||
|
"@sveltejs/kit": "^2.43.2",
|
||||||
|
"@sveltejs/vite-plugin-svelte": "^6.2.0",
|
||||||
|
"@tailwindcss/forms": "^0.5.10",
|
||||||
|
"@tailwindcss/typography": "^0.5.18",
|
||||||
|
"@tailwindcss/vite": "^4.1.13",
|
||||||
|
"@types/node": "^22",
|
||||||
|
"eslint": "^9.36.0",
|
||||||
|
"eslint-config-prettier": "^10.1.8",
|
||||||
|
"eslint-plugin-svelte": "^3.12.4",
|
||||||
|
"globals": "^16.4.0",
|
||||||
|
"prettier": "^3.6.2",
|
||||||
|
"prettier-plugin-svelte": "^3.4.0",
|
||||||
|
"svelte": "^5.39.5",
|
||||||
|
"svelte-check": "^4.3.2",
|
||||||
|
"tailwindcss": "^4.1.13",
|
||||||
|
"typescript": "^5.9.2",
|
||||||
|
"typescript-eslint": "^8.44.1",
|
||||||
|
"vite": "^7.1.7"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@codemirror/lang-python": "^6.2.1",
|
||||||
|
"codemirror": "^6.0.2",
|
||||||
|
"monaco-editor": "^0.54.0",
|
||||||
|
"svelte-codemirror-editor": "^2.1.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
3
frontend/src/app.css
Normal file
3
frontend/src/app.css
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
@import 'tailwindcss';
|
||||||
|
@plugin '@tailwindcss/forms';
|
||||||
|
@plugin '@tailwindcss/typography';
|
||||||
13
frontend/src/app.d.ts
vendored
Normal file
13
frontend/src/app.d.ts
vendored
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
// See https://svelte.dev/docs/kit/types#app.d.ts
|
||||||
|
// for information about these interfaces
|
||||||
|
declare global {
|
||||||
|
namespace App {
|
||||||
|
// interface Error {}
|
||||||
|
// interface Locals {}
|
||||||
|
// interface PageData {}
|
||||||
|
// interface PageState {}
|
||||||
|
// interface Platform {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export {};
|
||||||
11
frontend/src/app.html
Normal file
11
frontend/src/app.html
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
|
%sveltekit.head%
|
||||||
|
</head>
|
||||||
|
<body data-sveltekit-preload-data="hover">
|
||||||
|
<div style="display: contents">%sveltekit.body%</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
124
frontend/src/lib/api.ts
Normal file
124
frontend/src/lib/api.ts
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
export const API_URL = 'http://127.0.0.1:8000';
|
||||||
|
|
||||||
|
export async function checkHealth(): Promise<'healthy' | 'unhealthy'> {
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${API_URL}/health`);
|
||||||
|
if (response.ok) {
|
||||||
|
return 'healthy';
|
||||||
|
} else {
|
||||||
|
return 'unhealthy';
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
return 'unhealthy';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type definitions for Script and Log
|
||||||
|
*/
|
||||||
|
export interface Log {
|
||||||
|
id: number;
|
||||||
|
script_id: number;
|
||||||
|
message: string;
|
||||||
|
created_at?: string;
|
||||||
|
}
|
||||||
|
export interface Script {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
script_content?: string;
|
||||||
|
created_at?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch all scripts
|
||||||
|
export async function fetchScripts(): Promise<Script[]> {
|
||||||
|
const response = await fetch(`${API_URL}/script`);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Failed to fetch scripts ' + response.statusText);
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a new script
|
||||||
|
export async function addScript(script: Omit<Script, 'id' | 'created_at'>): Promise<Script> {
|
||||||
|
const response = await fetch(`${API_URL}/script`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(script)
|
||||||
|
});
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Failed to add script');
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch a single script by ID
|
||||||
|
export async function fetchScriptById(id: number): Promise<Script> {
|
||||||
|
const response = await fetch(`${API_URL}/script/${id}`);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Failed to fetch script');
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update an existing script
|
||||||
|
export async function updateScript(id: number, updatedScript: Partial<Script>): Promise<Script> {
|
||||||
|
const response = await fetch(`${API_URL}/script/${id}`, {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(updatedScript)
|
||||||
|
});
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Failed to update script');
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch logs for a specific script
|
||||||
|
export async function fetchLogs(scriptId: number): Promise<Log[]> {
|
||||||
|
const response = await fetch(`${API_URL}/script/${scriptId}/log`);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Failed to fetch logs');
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a new log to a specific script
|
||||||
|
export async function addLog(scriptId: number, message: string): Promise<Log> {
|
||||||
|
const response = await fetch(`${API_URL}/script/${scriptId}/log`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ message })
|
||||||
|
});
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Failed to add log');
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete a log from a specific script
|
||||||
|
export async function deleteLog(scriptId: number, logId: number): Promise<void> {
|
||||||
|
const response = await fetch(`${API_URL}/script/${scriptId}/log/${logId}`, {
|
||||||
|
method: 'DELETE'
|
||||||
|
});
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Failed to delete log');
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete a script
|
||||||
|
export async function deleteScript(id: number): Promise<void> {
|
||||||
|
const response = await fetch(`${API_URL}/script/${id}`, {
|
||||||
|
method: 'DELETE'
|
||||||
|
});
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Failed to delete script');
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
}
|
||||||
1
frontend/src/lib/assets/favicon.svg
Normal file
1
frontend/src/lib/assets/favicon.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" width="107" height="128" viewBox="0 0 107 128"><title>svelte-logo</title><path d="M94.157 22.819c-10.4-14.885-30.94-19.297-45.792-9.835L22.282 29.608A29.92 29.92 0 0 0 8.764 49.65a31.5 31.5 0 0 0 3.108 20.231 30 30 0 0 0-4.477 11.183 31.9 31.9 0 0 0 5.448 24.116c10.402 14.887 30.942 19.297 45.791 9.835l26.083-16.624A29.92 29.92 0 0 0 98.235 78.35a31.53 31.53 0 0 0-3.105-20.232 30 30 0 0 0 4.474-11.182 31.88 31.88 0 0 0-5.447-24.116" style="fill:#ff3e00"/><path d="M45.817 106.582a20.72 20.72 0 0 1-22.237-8.243 19.17 19.17 0 0 1-3.277-14.503 18 18 0 0 1 .624-2.435l.49-1.498 1.337.981a33.6 33.6 0 0 0 10.203 5.098l.97.294-.09.968a5.85 5.85 0 0 0 1.052 3.878 6.24 6.24 0 0 0 6.695 2.485 5.8 5.8 0 0 0 1.603-.704L69.27 76.28a5.43 5.43 0 0 0 2.45-3.631 5.8 5.8 0 0 0-.987-4.371 6.24 6.24 0 0 0-6.698-2.487 5.7 5.7 0 0 0-1.6.704l-9.953 6.345a19 19 0 0 1-5.296 2.326 20.72 20.72 0 0 1-22.237-8.243 19.17 19.17 0 0 1-3.277-14.502 17.99 17.99 0 0 1 8.13-12.052l26.081-16.623a19 19 0 0 1 5.3-2.329 20.72 20.72 0 0 1 22.237 8.243 19.17 19.17 0 0 1 3.277 14.503 18 18 0 0 1-.624 2.435l-.49 1.498-1.337-.98a33.6 33.6 0 0 0-10.203-5.1l-.97-.294.09-.968a5.86 5.86 0 0 0-1.052-3.878 6.24 6.24 0 0 0-6.696-2.485 5.8 5.8 0 0 0-1.602.704L37.73 51.72a5.42 5.42 0 0 0-2.449 3.63 5.79 5.79 0 0 0 .986 4.372 6.24 6.24 0 0 0 6.698 2.486 5.8 5.8 0 0 0 1.602-.704l9.952-6.342a19 19 0 0 1 5.295-2.328 20.72 20.72 0 0 1 22.237 8.242 19.17 19.17 0 0 1 3.277 14.503 18 18 0 0 1-8.13 12.053l-26.081 16.622a19 19 0 0 1-5.3 2.328" style="fill:#fff"/></svg>
|
||||||
|
After Width: | Height: | Size: 1.5 KiB |
1
frontend/src/lib/index.ts
Normal file
1
frontend/src/lib/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
// place files you want to import through the `$lib` alias in this folder.
|
||||||
81
frontend/src/routes/+layout.svelte
Normal file
81
frontend/src/routes/+layout.svelte
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import '../app.css';
|
||||||
|
import { onMount } from 'svelte';
|
||||||
|
import { writable } from 'svelte/store';
|
||||||
|
import { checkHealth } from '$lib/api';
|
||||||
|
let { children } = $props();
|
||||||
|
|
||||||
|
interface Notification {
|
||||||
|
id: number;
|
||||||
|
type: 'success' | 'error';
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
let notifications: Notification[] = $state([]);
|
||||||
|
let notificationId = 0;
|
||||||
|
|
||||||
|
let healthStatus = writable<'healthy' | 'unhealthy'>('unhealthy');
|
||||||
|
|
||||||
|
async function updateHealthStatus() {
|
||||||
|
const status = await checkHealth();
|
||||||
|
healthStatus.set(status);
|
||||||
|
}
|
||||||
|
|
||||||
|
function showNotification(type: 'success' | 'error', message: string): void {
|
||||||
|
const id = notificationId++;
|
||||||
|
notifications = [...notifications, { id, type, message }];
|
||||||
|
setTimeout(() => {
|
||||||
|
notifications = notifications.filter((n) => n.id !== id);
|
||||||
|
}, 4000);
|
||||||
|
}
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
window.showNotification = showNotification;
|
||||||
|
updateHealthStatus();
|
||||||
|
setInterval(updateHealthStatus, 10000); // Check health every 10 seconds
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<nav class="bg-gray-800 text-white shadow-md">
|
||||||
|
<div class="container mx-auto flex justify-between items-center p-4">
|
||||||
|
<a href="/" class="text-2xl font-bold hover:text-gray-400">Project Monitor</a>
|
||||||
|
<div class="flex space-x-6">
|
||||||
|
<a href="/" class="text-lg hover:text-gray-400">Home</a>
|
||||||
|
<a href="/scripts" class="text-lg hover:text-gray-400">Scripts</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
|
||||||
|
<div class="relative">
|
||||||
|
{@render children()}
|
||||||
|
|
||||||
|
<div class="fixed bottom-4 right-4 space-y-2">
|
||||||
|
{#each notifications as notification (notification.id)}
|
||||||
|
<div
|
||||||
|
class="p-4 rounded shadow-lg text-white"
|
||||||
|
class:bg-green-500={notification.type === 'success'}
|
||||||
|
class:bg-red-500={notification.type === 'error'}
|
||||||
|
>
|
||||||
|
{notification.message}
|
||||||
|
</div>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="fixed bottom-4 left-4 group">
|
||||||
|
{#if $healthStatus === 'healthy'}
|
||||||
|
<span class="text-green-500">✅</span>
|
||||||
|
<div
|
||||||
|
class="absolute bottom-full left-1/2 hidden group-hover:flex group-hover:w-max bg-green-500 text-white px-2 py-1 rounded shadow-lg"
|
||||||
|
>
|
||||||
|
Connected to backend
|
||||||
|
</div>
|
||||||
|
{:else}
|
||||||
|
<span class="text-red-500">✘</span>
|
||||||
|
<div
|
||||||
|
class="absolute bottom-full left-1/2 hidden group-hover:flex group-hover:w-max bg-red-500 text-white px-2 py-1 rounded shadow-lg"
|
||||||
|
>
|
||||||
|
Not connected
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
12
frontend/src/routes/+page.svelte
Normal file
12
frontend/src/routes/+page.svelte
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<h1 class="text-4xl font-bold text-center mt-10 text-gray-800">Welcome to Project Monitor</h1>
|
||||||
|
<p class="text-center mt-4 text-lg text-gray-600">
|
||||||
|
Manage your scripts efficiently with our simple and intuitive interface.
|
||||||
|
</p>
|
||||||
|
<div class="flex justify-center mt-8">
|
||||||
|
<a
|
||||||
|
href="/scripts"
|
||||||
|
class="px-6 py-3 bg-blue-500 text-white rounded-lg shadow-md hover:bg-blue-600"
|
||||||
|
>
|
||||||
|
Go to Scripts
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
12
frontend/src/routes/scripts/+page.server.js
Normal file
12
frontend/src/routes/scripts/+page.server.js
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import { error } from '@sveltejs/kit';
|
||||||
|
import { fetchScripts } from '$lib/api';
|
||||||
|
|
||||||
|
/** @type {import('./$types').PageServerLoad} */
|
||||||
|
export async function load() {
|
||||||
|
try {
|
||||||
|
const scripts = await fetchScripts();
|
||||||
|
return { scripts };
|
||||||
|
} catch (err) {
|
||||||
|
throw error(500, 'Failed to fetch scripts - ' + err);
|
||||||
|
}
|
||||||
|
}
|
||||||
68
frontend/src/routes/scripts/+page.svelte
Normal file
68
frontend/src/routes/scripts/+page.svelte
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { addScript } from '$lib/api';
|
||||||
|
import type { Script } from '$lib/api';
|
||||||
|
import CodeMirror from 'svelte-codemirror-editor';
|
||||||
|
import { python } from '@codemirror/lang-python';
|
||||||
|
|
||||||
|
export let data: { scripts: Script[] };
|
||||||
|
let scripts: Script[] = data.scripts;
|
||||||
|
let newScript: Omit<Script, 'id' | 'created_at'> = { name: '', script_content: '' };
|
||||||
|
|
||||||
|
// Add a new script
|
||||||
|
async function handleAddScript() {
|
||||||
|
try {
|
||||||
|
const addedScript = await addScript(newScript);
|
||||||
|
scripts = [...scripts, addedScript];
|
||||||
|
newScript = { name: '', script_content: '' };
|
||||||
|
} catch (err) {
|
||||||
|
window.showNotification('Failed to add script. ' + err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<main class="p-4">
|
||||||
|
<h1 class="text-2xl font-bold mb-4">Scripts</h1>
|
||||||
|
|
||||||
|
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
|
||||||
|
{#each scripts as script (script.id)}
|
||||||
|
<a
|
||||||
|
href={`/scripts/${script.id}`}
|
||||||
|
class="block p-4 border rounded shadow bg-white hover:bg-gray-100"
|
||||||
|
>
|
||||||
|
<h2 class="text-lg font-semibold text-gray-800">{script.name}</h2>
|
||||||
|
</a>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mt-8">
|
||||||
|
<h2 class="text-xl font-semibold mb-2">Add New Script</h2>
|
||||||
|
<form
|
||||||
|
on:submit|preventDefault={handleAddScript}
|
||||||
|
class="space-y-4 p-4 border rounded shadow"
|
||||||
|
on:submit={() => (newScript.script_content = editor.getValue())}
|
||||||
|
>
|
||||||
|
<div>
|
||||||
|
<label for="name" class="block text-sm font-medium">Name</label>
|
||||||
|
<input
|
||||||
|
id="name"
|
||||||
|
type="text"
|
||||||
|
bind:value={newScript.name}
|
||||||
|
required
|
||||||
|
class="mt-1 block w-full p-2 border rounded"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label for="script_content" class="block text-sm font-medium">Content</label>
|
||||||
|
<CodeMirror bind:value={newScript.script_content} lang={python()} />
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="px-4 py-2 bg-green-500 text-white rounded"> Add Script </button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
main {
|
||||||
|
max-width: 800px;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
22
frontend/src/routes/scripts/[id]/+page.server.ts
Normal file
22
frontend/src/routes/scripts/[id]/+page.server.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { error } from '@sveltejs/kit';
|
||||||
|
import { fetchScriptById, fetchLogs } from '$lib/api';
|
||||||
|
import type { Log } from '$lib/api';
|
||||||
|
|
||||||
|
export async function load({ params }) {
|
||||||
|
const { id } = params;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const script = await fetchScriptById(parseInt(id));
|
||||||
|
const logs: Log[] = (await fetchLogs(script.id)).sort(
|
||||||
|
(a, b) => new Date(b.created_at!).getTime() - new Date(a.created_at!).getTime()
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!script) {
|
||||||
|
throw error(404, 'Script not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
return { script, logs };
|
||||||
|
} catch (err) {
|
||||||
|
throw error(500, 'Failed to fetch script data - ' + err);
|
||||||
|
}
|
||||||
|
}
|
||||||
154
frontend/src/routes/scripts/[id]/+page.svelte
Normal file
154
frontend/src/routes/scripts/[id]/+page.svelte
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { updateScript, deleteScript, addLog, deleteLog } from '$lib/api';
|
||||||
|
import type { Script, Log } from '$lib/api';
|
||||||
|
import CodeMirror from 'svelte-codemirror-editor';
|
||||||
|
import { python } from '@codemirror/lang-python';
|
||||||
|
|
||||||
|
export let data: { script: Script; logs: Log[] };
|
||||||
|
let script: Script = data.script;
|
||||||
|
let logs: Log[] = data.logs;
|
||||||
|
let updatedTitle: string = script.name || '';
|
||||||
|
let updatedContent: string = script.script_content || '';
|
||||||
|
let isEditMode: boolean = false;
|
||||||
|
|
||||||
|
let newLogMessage: string = '';
|
||||||
|
// Notifications are now handled globally via the layout
|
||||||
|
|
||||||
|
async function handleUpdateScript() {
|
||||||
|
if (script) {
|
||||||
|
try {
|
||||||
|
const updatedScript = await updateScript(script.id, {
|
||||||
|
name: updatedTitle,
|
||||||
|
script_content: updatedContent
|
||||||
|
});
|
||||||
|
script = updatedScript;
|
||||||
|
window.showNotification('success', 'Script updated successfully!');
|
||||||
|
isEditMode = false;
|
||||||
|
} catch (err) {
|
||||||
|
window.showNotification('error', 'Failed to update script. ' + err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleAddLog() {
|
||||||
|
if (newLogMessage.trim()) {
|
||||||
|
try {
|
||||||
|
const newLog = await addLog(script.id, newLogMessage);
|
||||||
|
logs = [newLog, ...logs];
|
||||||
|
newLogMessage = '';
|
||||||
|
window.showNotification('success', 'Log added successfully!');
|
||||||
|
} catch (err) {
|
||||||
|
window.showNotification('error', 'Failed to add log. ' + err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleDeleteLog(logId: number) {
|
||||||
|
try {
|
||||||
|
await deleteLog(script.id, logId);
|
||||||
|
logs = logs.filter((log) => log.id !== logId);
|
||||||
|
window.showNotification('success', 'Log deleted successfully!');
|
||||||
|
} catch (err) {
|
||||||
|
window.showNotification('error', 'Failed to delete log. ' + err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete the script
|
||||||
|
async function handleDeleteScript() {
|
||||||
|
if (script) {
|
||||||
|
try {
|
||||||
|
await deleteScript(script.id);
|
||||||
|
window.location.href = '/scripts'; // Redirect to scripts list after deletion
|
||||||
|
} catch (err) {
|
||||||
|
window.showNotification('error', 'Failed to delete script. ' + err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<main class="p-4">
|
||||||
|
<!-- Removed local notification container as notifications are now global -->
|
||||||
|
|
||||||
|
{#if script}
|
||||||
|
{#if isEditMode}
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
bind:value={updatedTitle}
|
||||||
|
required
|
||||||
|
class="text-2xl font-bold mb-4 w-full p-2 border rounded"
|
||||||
|
/>
|
||||||
|
<CodeMirror bind:value={updatedContent} lang={python()} />
|
||||||
|
<button
|
||||||
|
class="mt-4 px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600"
|
||||||
|
on:click={handleUpdateScript}
|
||||||
|
aria-label="Update the script title and content"
|
||||||
|
>
|
||||||
|
Update Script
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
class="mt-4 px-4 py-2 bg-gray-500 text-white rounded hover:bg-gray-600"
|
||||||
|
on:click={() => (isEditMode = false)}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</button>
|
||||||
|
{:else}
|
||||||
|
<a href="/scripts" class="text-blue-500 hover:underline mb-4 inline-block"
|
||||||
|
>← Return to Scripts</a
|
||||||
|
>
|
||||||
|
<h1 class="text-2xl font-bold mb-4">{script.name}</h1>
|
||||||
|
<pre
|
||||||
|
class="w-full p-2 border rounded font-mono text-sm bg-gray-100">{script.script_content}</pre>
|
||||||
|
<button
|
||||||
|
class="mt-4 px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600"
|
||||||
|
on:click={() => (isEditMode = true)}
|
||||||
|
>
|
||||||
|
Edit Script
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
class="mt-4 px-4 py-2 bg-red-500 text-white rounded hover:bg-red-600"
|
||||||
|
on:click={handleDeleteScript}
|
||||||
|
>
|
||||||
|
Delete Script
|
||||||
|
</button>
|
||||||
|
{/if}
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<section class="mt-8">
|
||||||
|
<h2 class="text-xl font-bold mb-4">Logs</h2>
|
||||||
|
<form on:submit|preventDefault={handleAddLog} class="mb-4">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
bind:value={newLogMessage}
|
||||||
|
placeholder="Enter new log message"
|
||||||
|
class="w-full p-2 border rounded mb-2"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
<button type="submit" class="px-4 py-2 bg-green-500 text-white rounded hover:bg-green-600">
|
||||||
|
Add Log
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
<ul class="space-y-4">
|
||||||
|
{#each logs as log (log.id)}
|
||||||
|
<li class="p-4 border rounded bg-gray-50 flex justify-between items-center">
|
||||||
|
<div>
|
||||||
|
<p class="text-sm text-gray-700">{log.message}</p>
|
||||||
|
<p class="text-xs text-gray-500">{log.created_at}</p>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
class="px-2 py-1 bg-red-500 text-white rounded hover:bg-red-600"
|
||||||
|
on:click={() => handleDeleteLog(log.id)}
|
||||||
|
>
|
||||||
|
Delete
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
{/each}
|
||||||
|
</ul>
|
||||||
|
</section>
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
main {
|
||||||
|
max-width: 800px;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
3
frontend/static/robots.txt
Normal file
3
frontend/static/robots.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# allow crawling everything by default
|
||||||
|
User-agent: *
|
||||||
|
Disallow:
|
||||||
18
frontend/svelte.config.js
Normal file
18
frontend/svelte.config.js
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import adapter from '@sveltejs/adapter-auto';
|
||||||
|
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
|
||||||
|
|
||||||
|
/** @type {import('@sveltejs/kit').Config} */
|
||||||
|
const config = {
|
||||||
|
// Consult https://svelte.dev/docs/kit/integrations
|
||||||
|
// for more information about preprocessors
|
||||||
|
preprocess: vitePreprocess(),
|
||||||
|
|
||||||
|
kit: {
|
||||||
|
// adapter-auto only supports some environments, see https://svelte.dev/docs/kit/adapter-auto for a list.
|
||||||
|
// If your environment is not supported, or you settled on a specific environment, switch out the adapter.
|
||||||
|
// See https://svelte.dev/docs/kit/adapters for more information about adapters.
|
||||||
|
adapter: adapter()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default config;
|
||||||
10
frontend/tailwind.config.cjs
Normal file
10
frontend/tailwind.config.cjs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
/** @type {import('tailwindcss').Config} */
|
||||||
|
module.exports = {
|
||||||
|
content: [
|
||||||
|
"./src/**/*.{html,js,svelte,ts}"
|
||||||
|
],
|
||||||
|
theme: {
|
||||||
|
extend: {},
|
||||||
|
},
|
||||||
|
plugins: [],
|
||||||
|
};
|
||||||
19
frontend/tsconfig.json
Normal file
19
frontend/tsconfig.json
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"extends": "./.svelte-kit/tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"allowJs": true,
|
||||||
|
"checkJs": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"forceConsistentCasingInFileNames": true,
|
||||||
|
"resolveJsonModule": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"sourceMap": true,
|
||||||
|
"strict": true,
|
||||||
|
"moduleResolution": "bundler"
|
||||||
|
}
|
||||||
|
// Path aliases are handled by https://svelte.dev/docs/kit/configuration#alias
|
||||||
|
// except $lib which is handled by https://svelte.dev/docs/kit/configuration#files
|
||||||
|
//
|
||||||
|
// To make changes to top-level options such as include and exclude, we recommend extending
|
||||||
|
// the generated config; see https://svelte.dev/docs/kit/configuration#typescript
|
||||||
|
}
|
||||||
7
frontend/vite.config.ts
Normal file
7
frontend/vite.config.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import tailwindcss from '@tailwindcss/vite';
|
||||||
|
import { sveltekit } from '@sveltejs/kit/vite';
|
||||||
|
import { defineConfig } from 'vite';
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
plugins: [tailwindcss(), sveltekit()]
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user