95 Commits

Author SHA1 Message Date
fccview
6b89a9ef97 add license 2026-01-01 18:43:06 +00:00
fccview
0ed4942a30 make image full width in readme 2026-01-01 15:54:54 +00:00
fccview
aeab383116 fix heading image in readme 2026-01-01 15:52:06 +00:00
fccview
f098ded0c4 Merge pull request #77 from fccview/develop
Lift off
2026-01-01 14:28:29 +00:00
fccview
3ac9a5ca30 finish readme with latest screenshots 2026-01-01 14:22:21 +00:00
fccview
c708c013f3 fix modal on mobile and add proper screenshots 2026-01-01 14:17:11 +00:00
fccview
fb6531d00d fix few small issues, improve logo, add favicon 2026-01-01 13:56:52 +00:00
fccview
46e0838792 make standalone and fix fonts to work well with the new aesthetic 2026-01-01 11:36:50 +00:00
fccview
72f1c0a66d update screenshots 2026-01-01 09:37:34 +00:00
fccview
1adad49020 update minimal 2026-01-01 09:30:47 +00:00
fccview
c2d17e4c04 bump package.json with new version due to rebrand 2026-01-01 08:53:54 +00:00
fccview
02866c8ea5 continue fixing rebranding 2026-01-01 08:53:16 +00:00
fccview
e58c1070d6 add snake and continue with consistent styling 2026-01-01 08:18:17 +00:00
fccview
6fe92ef3fa continue rebranding 2025-12-31 22:23:43 +00:00
fccview
ebce8f698b start replacing all UI with terminal UI 2025-12-31 20:09:11 +00:00
fccview
b2dc0a3cb3 Merge pull request #68 from fccview/develop
Lift off!!
2025-12-14 09:09:41 +00:00
fccview
e40b0c0f63 bump package version 2025-12-14 09:06:55 +00:00
fccview
79fd223416 fix documentation, patch next vulnerability and deal with the next upgrad annoyances 2025-12-14 09:06:19 +00:00
fccview
eaca3fe44a remove test scripts 2025-11-20 19:00:14 +00:00
fccview
e033caacf6 try a new strategy for log watch 2025-11-20 18:59:01 +00:00
fccview
4beb7053f7 Merge branch 'develop' 2025-11-20 07:26:04 +00:00
fccview
d26ce0e810 update package.json version 2025-11-20 07:26:00 +00:00
fccview
d6b6aff44e Merge pull request #64 from fccview/develop
fix api auth issue
2025-11-20 07:25:41 +00:00
fccview
7954111d05 fix api auth issue 2025-11-20 07:22:51 +00:00
fccview
0ab3358e28 Merge pull request #60 from fccview/develop
Lift Off!
2025-11-19 20:46:15 +00:00
fccview
f53905c002 make sure evil single quote wont cause issues 2025-11-19 20:41:43 +00:00
fccview
90775cac7c add optional variable to automatically go to oidc provider rather than manually click on a button 2025-11-19 20:35:02 +00:00
fccview
54188eb1c0 limit max output of live logs and paginate 2025-11-19 20:23:14 +00:00
fccview
bf208e3075 fix draft feature and copy button 2025-11-19 20:01:27 +00:00
fccview
a5fb5ff484 update package.json version 2025-11-19 16:48:45 +00:00
fccview
25190f3154 Merge branch 'feature/1-5-0-improvements' into develop 2025-11-19 16:47:54 +00:00
fccview
437bdbd81f fix parenthesis issue and give immutable ids to old jobs too 2025-11-19 16:47:43 +00:00
fccview
d8ab3839c6 Merge branch 'main' into develop 2025-11-19 15:54:58 +00:00
fccview
13fe6c5f3d fix bash language highlight and enhance editor with tabbing functionality 2025-11-19 15:54:42 +00:00
fccview
9fb904d68a Merge pull request #51 from fccview/develop
fix issue with overflow for jobs
2025-11-13 20:13:34 +00:00
fccview
b95cd79239 add min height 2025-11-13 20:09:21 +00:00
fccview
7a4a22f8e9 fix issue with overflow for jobs 2025-11-13 20:03:29 +00:00
fccview
df6ab8774d Merge pull request #49 from fccview/develop
Lift off!!!
2025-11-13 16:07:50 +00:00
fccview
feeb56ece8 finish readme and prepare release 2025-11-13 15:57:31 +00:00
fccview
1b6f5b6e34 fix minor styling, allow custom translations and prepare for release 2025-11-13 15:11:49 +00:00
fccview
1f2379db59 fix minor styling 2025-11-13 14:55:21 +00:00
fccview
ef5153ce54 improve performance, add minimal mode, make UI make more sense 2025-11-13 14:30:21 +00:00
fccview
8faf4d26d0 send fixes to develop for automatic ID assignation 2025-11-12 20:56:54 +00:00
fccview
1fd2689296 add copy id/command 2025-11-11 18:12:09 +00:00
fccview
01c87ab82f add backups and sort out uuid situation 2025-11-11 15:25:59 +00:00
fccview
beebdf878e add oidc and update readme to be more accurate 2025-11-11 11:33:51 +00:00
fccview
7e3d5db2be add SSE event pushing for logs to show live updates on the UI for healthy/unhealthy logs and show logs as a job runs to fix the bug of jobs failing if they are too big 2025-11-11 09:18:55 +00:00
fccview
0cefff769b create system stats utilities to avoid pointless repetitions 2025-11-11 07:35:10 +00:00
fccview
1a10eebe01 fix translations 2025-11-11 07:29:27 +00:00
fccview
1f82e85833 major improvements to the wrapper and fix api route mess 2025-11-11 07:27:03 +00:00
fccview
30d856b9ce add logs and fix a bunch of major issues 2025-11-10 11:41:04 +00:00
fccview
11c96d0aed fix server side translations 2025-11-05 22:03:36 +00:00
fccview
b9fb009923 huge translations work 2025-11-05 21:41:15 +00:00
fccview
b1a4d081ad finish initial refactor 2025-11-05 20:30:01 +00:00
fccview
e129bac619 continue refactor, this is looking good! 2025-11-05 20:11:06 +00:00
fccview
2ba9cdc622 remove the annoying HOST_PROJECT_DIR and DOCKER env variables 2025-11-05 19:52:20 +00:00
fccview
ce379a8cc9 use nsenter regardless if docker or not, slowly removing the DOCKER env variable 2025-11-05 18:49:41 +00:00
fccview
14fba08cb2 start cleaning up and refactoring 2025-11-05 18:31:47 +00:00
fccview
7e3bd590e9 update develop and improve pipelines 2025-11-05 18:10:35 +00:00
fccview
e2e95968ef Merge pull request #43 from Navino16/feature/extra-groups-ids
Very happy with these changes, thank you for the help!
2025-10-08 16:04:56 +01:00
fccview
a4ae5ec148 update readme 2025-10-08 16:03:02 +01:00
fccview
8e8069ee92 remove docker exec user and update readme with a note for docker compose as root 2025-10-08 15:44:31 +01:00
fccview
f96c37b55c remove debugging logs 2025-10-08 15:01:18 +01:00
fccview
cda9685e6d Add Navino to readme table 2025-10-08 14:53:56 +01:00
fccview
8329c0d030 make it so root works via a user instead, also update permissions for scripts on creation so they are executable right off the bat 2025-10-08 14:34:34 +01:00
Nathan JAUNET
6e34474993 Preserve user permission for command execution 2025-10-02 17:23:44 +02:00
fccview
65ac81d97c Merge pull request #40 from fccview/bugfix/fix-scripts-issues
Bugfix/fix scripts issues
2025-09-20 21:14:07 +01:00
fccview
968fbae13c remove bugfix from runs 2025-09-20 21:06:00 +01:00
fccview
c739d29141 quick fix for script extra character and delete issues 2025-09-20 21:05:10 +01:00
fccview
389ee44e4e Merge pull request #28 from fccview/feature/pwa-and-auth
WIP auth and pwa integration
2025-09-01 19:23:11 +01:00
fccview
33ff5de463 update readme and docker compose to be a bit easier to maintain 2025-09-01 16:44:09 +01:00
fccview
7aeea3f46a add better error handling 2025-09-01 16:10:11 +01:00
fccview
9018f2caed fix pausing jobs without a comment 2025-09-01 15:36:45 +01:00
fccview
7383a13c13 fix pwa fully 2025-09-01 15:23:59 +01:00
fccview
da11d3503e WIP auth and pwa integration 2025-09-01 07:18:42 +01:00
fccview
0b9edc5f11 WIP auth and pwa integration 2025-08-31 21:19:51 +01:00
fccview
44b31a5702 i always forget to update the latest version in the docker compose file 2025-08-31 19:23:15 +01:00
fccview
7fc8cb9edb update readme 2025-08-31 19:20:10 +01:00
fccview
4dfdf8fc53 Merge pull request #27 from fccview/BUG-4
Fix editing jobs with description too similar
2025-08-31 19:16:55 +01:00
fccview
8cfc000893 Fix editing jobs with description too similar 2025-08-31 19:16:19 +01:00
fccview
1dde8f839e fix readme 2025-08-27 20:23:24 +01:00
fccview
2b7d591a95 Merge pull request #20 from fccview/feature/multi-user-support
Feature/multi user support
2025-08-27 20:21:59 +01:00
fccview
c0a9a74d7e syntax 2025-08-27 08:00:12 +01:00
fccview
376147fda0 Fix potential permission issues on manual runs 2025-08-27 07:57:31 +01:00
fccview
9445cdeebf Update app/_utils/cron/line-manipulation.ts
Co-authored-by: DVDAndroid <6277172+DVDAndroid@users.noreply.github.com>
2025-08-27 07:44:07 +01:00
fccview
170ea674c4 standardize codebase 2025-08-26 21:03:33 +01:00
fccview
80bd2e713f fix readme 2025-08-26 19:48:46 +01:00
fccview
801bcf22a2 Multi user support and enhancements 2025-08-26 19:46:06 +01:00
fccview
8fd7d0d80f Start working on multi users functionalities 2025-08-26 16:44:05 +01:00
fccview
95f113faa6 Merge pull request #19 from fccview/BUG-3
Fix script path bug and add support for pinned versions
2025-08-26 13:47:10 +01:00
fccview
2a437f3db8 Fix script path bug and add support for pinned versions 2025-08-26 13:44:08 +01:00
fccview
47e19246ce update readme and docker compose file. whopsie. 2025-08-26 10:08:10 +01:00
fccview
29917a4cad Merge pull request #7 from fccview/bugfix/replace-docker-crontab-functionality
Bugfix/replace docker crontab functionality
2025-08-26 09:46:28 +01:00
fccview
ac31906166 Merge pull request #10 from fccview/BUG-2
Fix scripts not working if description is empty
2025-08-26 09:34:43 +01:00
fccview
b267fb9ce6 Fix scripts not working if description is empty 2025-08-26 09:30:58 +01:00
178 changed files with 18459 additions and 3246 deletions

52
.github/workflows/docker-build.yml vendored Normal file
View File

@@ -0,0 +1,52 @@
name: Reusable Docker Build Logic
on:
workflow_call:
inputs:
platform: { required: true, type: string }
suffix: { required: true, type: string }
runner: { required: true, type: string }
secrets:
token: { required: true }
jobs:
build:
runs-on: ${{ inputs.runner }}
permissions: { contents: read, packages: write }
steps:
- { name: Checkout repository, uses: actions/checkout@v4 }
- name: "Prepare repository name in lowercase"
id: repo
run: echo "name=$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT
- { name: Set up QEMU, uses: docker/setup-qemu-action@v3 }
- { name: Set up Docker Buildx, uses: docker/setup-buildx-action@v3 }
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.token }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: |
ghcr.io/${{ steps.repo.outputs.name }}
tags: |
type=ref,event=branch,suffix=${{ inputs.suffix }}
type=ref,event=tag,suffix=${{ inputs.suffix }}
type=raw,value=latest,suffix=${{ inputs.suffix }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
platforms: ${{ inputs.platform }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max

View File

@@ -1,27 +1,69 @@
name: Docker name: Build and Publish Multi-Platform Docker Image
on: on:
push: push:
branches: ["main", "legacy", "feature/*", "bugfix/*"] branches: ["main", "develop"]
tags: ["v*.*.*"] tags: ["*"]
pull_request:
branches: ["main"]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs: jobs:
build: build-amd64:
runs-on: ubuntu-latest runs-on: ubuntu-latest
permissions: permissions:
contents: read contents: read
packages: write packages: write
steps: steps:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Prepare repository name in lowercase
id: repo
run: echo "name=$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: |
ghcr.io/${{ steps.repo.outputs.name }}
tags: |
type=ref,event=branch,suffix=-amd64
type=ref,event=tag,suffix=-amd64
type=raw,value=latest,suffix=-amd64,enable=${{ startsWith(github.ref, 'refs/tags/') }}
- name: Build and push AMD64 Docker image
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-arm64:
runs-on: ubuntu-22.04-arm
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Prepare repository name in lowercase
id: repo
run: echo "name=$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT
- name: Set up QEMU - name: Set up QEMU
uses: docker/setup-qemu-action@v3 uses: docker/setup-qemu-action@v3
@@ -31,27 +73,68 @@ jobs:
- name: Log in to the Container registry - name: Log in to the Container registry
uses: docker/login-action@v3 uses: docker/login-action@v3
with: with:
registry: ${{ env.REGISTRY }} registry: ghcr.io
username: ${{ github.actor }} username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }} password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker - name: Extract metadata
id: meta id: meta
uses: docker/metadata-action@v5 uses: docker/metadata-action@v5
with: with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} images: |
ghcr.io/${{ steps.repo.outputs.name }}
tags: | tags: |
type=ref,event=branch type=ref,event=branch,suffix=-arm64
type=ref,event=pr type=ref,event=tag,suffix=-arm64
type=semver,pattern={{version}} type=raw,value=latest,suffix=-arm64,enable=${{ startsWith(github.ref, 'refs/tags/') }}
type=semver,pattern={{major}}.{{minor}}
type=sha
- name: Build and push Docker image - name: Build and push ARM64 Docker image
uses: docker/build-push-action@v5 uses: docker/build-push-action@v5
with: with:
context: . context: .
platforms: linux/amd64,linux/arm64 platforms: linux/arm64
push: true push: true
tags: ${{ steps.meta.outputs.tags }} tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }} labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
manifest:
needs: [build-amd64, build-arm64]
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Prepare repository name in lowercase
id: repo
run: echo "name=$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata for final manifest
id: meta
uses: docker/metadata-action@v5
with:
images: |
ghcr.io/${{ steps.repo.outputs.name }}
tags: |
type=ref,event=branch
type=ref,event=tag
type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/') }}
- name: Create and push manifest list
run: |
echo "${{ steps.meta.outputs.tags }}" | while read -r tag; do
if [ -z "$tag" ]; then continue; fi
echo "Creating manifest for ${tag}"
docker buildx imagetools create --tag "${tag}" \
"${tag}-amd64" \
"${tag}-arm64"
done

7
.gitignore vendored
View File

@@ -10,4 +10,9 @@ node_modules
.next .next
.vscode .vscode
.DS_Store .DS_Store
.cursorignore .cursorignore
.idea
tsconfig.tsbuildinfo
docker-compose.test.yml
/data
claude.md

37
CONTRIBUTING.md Normal file
View File

@@ -0,0 +1,37 @@
# How to contribute
Hi, it's amazing having a community willing to push new feature to the app, and I am VERY open to contributors pushing their idea, it's what makes open source amazing.
That said for the sake of sanity let's all follow the same structure:
- When creating a new branch, do off from the develop branch, this will always be ahead of main and it's what gets released
- When creating a pull request, direct it back into develop, I'll then review it and merge it. Your code will end up in the next release that way and we all avoid conflicts!
- Please bear with on reviews, it may take a bit of time for me to go through it all on top of life/work/hobbies :)
## Some best practices
### Code Quality
- Follow the existing code style and structure
- Keep files modular and under 250-300 (split into smaller components if needed) lines unless it's a major server action, these can get intense I know
- Avoid code duplication - reuse existing functions and UI components, don't hardcode html when a component already exists (e.g. <button> vs <Button>)
- All imports should be at the top of the file unless it's for specific server actions
- Avoid using `any`
- Don't hardcode colors! Use the theme variables to make sure light/dark mode keep working well
- Make sure the UI is consistent with the current one, look for spacing issues, consistent spacing really makes a difference
### Pull Requests
- Keep PRs focused on a single feature or fix
- Update documentation if your changes affect user-facing features
- Test your changes locally before submitting
### Getting Started
1. Fork the repository
2. Create a feature branch from `develop`
3. Make your changes
4. Test thoroughly
5. Submit a pull request to `develop`
Thank you for contributing! <3

View File

@@ -1,13 +1,7 @@
FROM node:20-slim AS base FROM node:20-alpine AS base
RUN apt-get update && apt-get install -y \
pciutils \
curl \
iputils-ping \
util-linux \
&& rm -rf /var/lib/apt/lists/*
FROM base AS deps FROM base AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app WORKDIR /app
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
@@ -33,26 +27,27 @@ WORKDIR /app
ENV NODE_ENV=production ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1 ENV NEXT_TELEMETRY_DISABLED=1
RUN groupadd --system --gid 1001 nodejs RUN apk add --no-cache su-exec docker-cli pciutils curl iputils util-linux ca-certificates
RUN useradd --system --uid 1001 nextjs
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
RUN mkdir -p /app/scripts /app/data /app/snippets && \ RUN mkdir -p /app/scripts /app/data /app/snippets && \
chown -R nextjs:nodejs /app/scripts /app/data /app/snippets chown -R nextjs:nodejs /app/scripts /app/data /app/snippets
RUN mkdir -p /app/.next/cache && \
chown -R nextjs:nodejs /app/.next
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
COPY --from=builder /app/public ./public COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
COPY --from=builder --chown=nextjs:nodejs /app/app ./app
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/yarn.lock ./yarn.lock
COPY --from=deps --chown=nextjs:nodejs /app/node_modules ./node_modules
EXPOSE 3000 EXPOSE 3000
ENV PORT=3000 ENV PORT=3000
ENV HOSTNAME="0.0.0.0" ENV HOSTNAME="0.0.0.0"
CMD ["yarn", "start"] USER nextjs
CMD ["node", "server.js"]

661
LICENSE Normal file
View File

@@ -0,0 +1,661 @@
GNU AFFERO GENERAL PUBLIC LICENSE
Version 3, 19 November 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU Affero General Public License is a free, copyleft license for
software and other kinds of works, specifically designed to ensure
cooperation with the community in the case of network server software.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
our General Public Licenses are intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
Developers that use our General Public Licenses protect your rights
with two steps: (1) assert copyright on the software, and (2) offer
you this License which gives you legal permission to copy, distribute
and/or modify the software.
A secondary benefit of defending all users' freedom is that
improvements made in alternate versions of the program, if they
receive widespread use, become available for other developers to
incorporate. Many developers of free software are heartened and
encouraged by the resulting cooperation. However, in the case of
software used on network servers, this result may fail to come about.
The GNU General Public License permits making a modified version and
letting the public access it on a server without ever releasing its
source code to the public.
The GNU Affero General Public License is designed specifically to
ensure that, in such cases, the modified source code becomes available
to the community. It requires the operator of a network server to
provide the source code of the modified version running there to the
users of that server. Therefore, public use of a modified version, on
a publicly accessible server, gives the public access to the source
code of the modified version.
An older license, called the Affero General Public License and
published by Affero, was designed to accomplish similar goals. This is
a different license, not a version of the Affero GPL, but Affero has
released a new version of the Affero GPL which permits relicensing under
this license.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU Affero General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Remote Network Interaction; Use with the GNU General Public License.
Notwithstanding any other provision of this License, if you modify the
Program, your modified version must prominently offer all users
interacting with it remotely through a computer network (if your version
supports such interaction) an opportunity to receive the Corresponding
Source of your version by providing access to the Corresponding Source
from a network server at no charge, through some standard or customary
means of facilitating copying of software. This Corresponding Source
shall include the Corresponding Source for any work covered by version 3
of the GNU General Public License that is incorporated pursuant to the
following paragraph.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the work with which it is combined will remain governed by version
3 of the GNU General Public License.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU Affero General Public License from time to time. Such new versions
will be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU Affero General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU Affero General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU Affero General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If your software can interact with users remotely through a computer
network, you should also make sure that it provides a way for users to
get its source. For example, if your program is a web application, its
interface could display a "Source" link that leads users to an archive
of the code. There are many ways you could offer source, and different
solutions will be better for different programs; see section 13 for the
specific requirements.
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU AGPL, see
<https://www.gnu.org/licenses/>.

264
README.md
View File

@@ -1,90 +1,130 @@
<p align="center"> <p align="center">
<img src="public/heading.png" width="400px"> <img src="public/heading.png">
</p> </p>
# ATTENTION BREAKING UPDATE!! ## Quick links
> The latest `main` branch has completely changed the way this app used to run. - [Features](#features)
> The main reason being trying to address some security concerns and make the whole application work - [Quick Start](#quick-start)
> across multiple platform without too much trouble. - [Using Docker (Recommended)](#using-docker-recommended)
> - [API](#api)
> If you came here due to this change trying to figure out why your app stopped working you have two options: - [Single Sign-On (SSO) with OIDC](#single-sign-on-sso-with-oidc)
> - [Localization](#localization)
> 1 - Update your `docker-compose.yml` with the new one provided within this readme (or just copy [docker-compose.yml](docker-compose.yml)) - [Local Development](#local-development)
> - [Environment Variables](howto/ENV_VARIABLES.md)
> 2 - Keep your `docker-compose.yml` file as it is and use the legacy tag in the image `image: ghcr.io/fccview/cronmaster:legacy`. However bear in mind this will not be supported going forward, any issue regarding the legacy tag will be ignored and I will only support the main branch. Feel free to fork that specific branch in case you want to work on it yourself :) - [Authentication](#authentication)
- [Usage](#usage)
- [Viewing System Information](#viewing-system-information)
- [Managing Cron Jobs](#managing-cron-jobs)
- [Job Execution Logging](#job-execution-logging)
- [Managing Scripts](#managing-scripts)
<div align="center">
| Desktop | Mobile |
|---------|--------|
| ![Dark Mode Desktop](screenshots/home-dark.png) | ![Dark Mode Mobile](screenshots/home-dark-mobile.png) |
| ![Light Mode Desktop](screenshots/home-light.png) | ![Light Mode Mobile](screenshots/home-light-mobile.png) |
</div>
## Features ## Features
- **Modern UI**: Beautiful, responsive interface with dark/light mode. - **Modern UI**: Beautiful, responsive interface with dark/light mode.
- **System Information**: Display hostname, IP address, uptime, memory, network and CPU info. - **System Information**: Display uptime, memory, network, CPU, and GPU info.
- **Cron Job Management**: View, create, and delete cron jobs with comments. - **Cron Job Management**: View, create, and delete cron jobs with comments.
- **Script management**: View, create, and delete bash scripts on the go to use within your cron jobs. - **Script management**: View, create, and delete bash scripts on the go to use within your cron jobs.
- **Job Execution Logging**: Optional logging for cronjobs with automatic cleanup, capturing stdout, stderr, exit codes, and timestamps.
- **Live Updates (SSE)**: Real-time job status updates and live log streaming for long-running jobs (when logging is enabled).
- **Smart Job Execution**: Jobs with logging run in background with live updates, jobs without logging run synchronously with 5-minute timeout.
- **Authentication**: Secure password-based and/or OIDC (SSO) authentication with proper session management.
- **REST API**: Full REST API with optional API key authentication for external integrations.
- **Docker Support**: Runs entirely from a Docker container. - **Docker Support**: Runs entirely from a Docker container.
- **Easy Setup**: Quick presets for common cron schedules. - **Easy Setup**: Quick presets for common cron schedules.
## Before we start <br />
Hey there! 👋 Just a friendly heads-up: I'm a big believer in open source and love sharing my work with the community. Everything you find in my GitHub repos is and always will be 100% free. If someone tries to sell you a "premium" version of any of my projects while claiming to be me, please know that this is not legitimate. 🚫 ---
If you find my projects helpful and want to fuel my late-night coding sessions with caffeine, I'd be super grateful for any support! ☕ <p align="center">
<a href="http://discord.gg/invite/mMuk2WzVZu">
<img width="40" src="public/repo-images/discord_icon.webp">
</a>
<br />
<i>Join the discord server for more info</i>
<br />
</p>
<p align="center"> <p align="center">
<a href="https://www.buymeacoffee.com/fccview"> <a href="https://www.buymeacoffee.com/fccview">
<img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy me a coffee" width="150"> <img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy me a coffee" width="120">
</a> </a>
</p> </p>
<div align="center"> ---
<img width="500px" src="screenshots/jobs-view.png">
<img width="500px" src="screenshots/scripts-view.png" /> <a id="quick-start"></a>
</div>
## Quick Start ## Quick Start
<a id="using-docker-recommended"></a>
### Using Docker (Recommended) ### Using Docker (Recommended)
1. Create a `docker-compose.yml` file with this content: 1. Create a `docker-compose.yml` file with this minimal configuration:
```bash ```yaml
# For all configuration options, see howto/DOCKER.md
services: services:
cronjob-manager: cronmaster:
image: ghcr.io/fccview/cronmaster:main image: ghcr.io/fccview/cronmaster:latest
container_name: cronmaster-test container_name: cronmaster
user: "root" user: "root"
ports: ports:
# Feel free to change port, 3000 is very common so I like to map it to something else - "40123:3000"
- "40124:3000"
environment: environment:
- NODE_ENV=production - NODE_ENV=production
- DOCKER=true
- NEXT_PUBLIC_CLOCK_UPDATE_INTERVAL=30000 - NEXT_PUBLIC_CLOCK_UPDATE_INTERVAL=30000
- NEXT_PUBLIC_HOST_PROJECT_DIR=/path/to/cronmaster/directory - AUTH_PASSWORD=very_strong_password
# If docker struggles to find your crontab user, update this variable with it. - HOST_CRONTAB_USER=root
# Obviously replace fccview with your user - find it with: ls -asl /var/spool/cron/crontabs/
# - HOST_CRONTAB_USER=fccview
volumes: volumes:
# Mount Docker socket to execute commands on host
- /var/run/docker.sock:/var/run/docker.sock - /var/run/docker.sock:/var/run/docker.sock
# These are needed if you want to keep your data on the host machine and not wihin the docker volume.
# DO NOT change the location of ./scripts as all cronjobs that use custom scripts created via the app
# will target this foler (thanks to the NEXT_PUBLIC_HOST_PROJECT_DIR variable set above)
- ./scripts:/app/scripts - ./scripts:/app/scripts
- ./data:/app/data - ./data:/app/data
- ./snippets:/app/snippets - ./snippets:/app/snippets
# Use host PID namespace for host command execution
# Run in privileged mode for nsenter access
pid: "host" pid: "host"
privileged: true privileged: true
restart: unless-stopped restart: always
init: true init: true
# Default platform is set to amd64, uncomment to use arm64.
#platform: linux/arm64
``` ```
📖 **For all available configuration options, see [`howto/DOCKER.md`](howto/DOCKER.md)**
<a id="api"></a>
## API
`cr*nmaster` includes a REST API for programmatic access to your cron jobs and system information. This is perfect for integrations.
📖 **For the complete API documentation, see [howto/API.md](howto/API.md)**
<a id="single-sign-on-sso-with-oidc"></a>
## Single Sign-On (SSO) with OIDC
`cr*nmaster` supports any OIDC provider (Authentik, Auth0, Keycloak, Okta, Google, EntraID, etc.)
📖 **For the complete SSO documentation, see [howto/SSO.md](howto/SSO.md)**
<a id="localization"></a>
## Localization
`cr*nmaster` officially support [some languages](app/_translations) and allows you to create your custom translations locally on your own machine.
📖 **For the complete Translations documentation, see [howto/TRANSLATIONS.md](howto/TRANSLATIONS.md)**
### ARM64 Support ### ARM64 Support
The application supports both AMD64 and ARM64 architectures: The application supports both AMD64 and ARM64 architectures:
@@ -109,6 +149,8 @@ docker compose up --build
**Note**: The Docker implementation uses direct file access to read and write crontab files, ensuring real-time synchronization with the host system's cron jobs. This approach bypasses the traditional `crontab` command limitations in containerized environments **Note**: The Docker implementation uses direct file access to read and write crontab files, ensuring real-time synchronization with the host system's cron jobs. This approach bypasses the traditional `crontab` command limitations in containerized environments
<a id="local-development"></a>
### Local Development ### Local Development
1. Install dependencies: 1. Install dependencies:
@@ -125,49 +167,81 @@ yarn dev
3. Open your browser and navigate to `http://localhost:3000` 3. Open your browser and navigate to `http://localhost:3000`
<a id="environment-variables"></a>
### Environment Variables ### Environment Variables
The following environment variables can be configured: 📖 **For the complete environment variables reference, see [`howto/ENV_VARIABLES.md`](howto/ENV_VARIABLES.md)**
| Variable | Default | Description | This includes all configuration options for:
| ----------------------------------- | ------- | ------------------------------------------------------------------------------------------- |
| `NEXT_PUBLIC_CLOCK_UPDATE_INTERVAL` | `30000` | Clock update interval in milliseconds (30 seconds) |
| `NEXT_PUBLIC_HOST_PROJECT_DIR` | `N/A` | Mandatory variable to make sure cron runs on the right path. |
| `DOCKER` | `false` | ONLY set this to true if you are runnign the app via docker, in the docker-compose.yml file |
**Example**: To change the clock update interval to 60 seconds: - Core application settings
- Docker configuration
```bash - UI customization
NEXT_PUBLIC_CLOCK_UPDATE_INTERVAL=60000 docker-compose up - Logging settings
``` - Authentication (password, SSO/OIDC, API keys)
- Development and debugging options
**Example**: Your `docker-compose.yml` file or repository are in `~/homelab/cronmaster/`
```bash
NEXT_PUBLIC_HOST_PROJECT_DIR=/home/<your_user_here>/homelab/cronmaster
```
### Important Notes for Docker ### Important Notes for Docker
- Root user is required for cron operations and direct file access. There is no way around this, if you don't feel comfortable in running it as root feel free to run the app locally with `yarn install`, `yarn build` and `yarn start` - Root user is required for cron operations and direct file access. There is no way around this, if you don't feel comfortable in running it as root feel free to run the app locally with `yarn install`, `yarn build` and `yarn start`
- Crontab files are accessed directly via file system mounts at `/host/cron/crontabs` and `/host/crontab` for real-time reading and writing
- `NEXT_PUBLIC_HOST_PROJECT_DIR` is required in order for the scripts created within the app to run properly
- The `DOCKER=true` environment variable enables direct file access mode for crontab operations. This is REQUIRED when running the application in docker mode. - The `DOCKER=true` environment variable enables direct file access mode for crontab operations. This is REQUIRED when running the application in docker mode.
- The Docker socket and data volume mounts are required for proper functionality
**Important Note on Root Commands**: When running commands as `root` within Cronmaster, ensure that these commands also function correctly as `root` on your host machine. If a command works as `root` on your host but fails within Cronmaster, please open an issue with detailed information.
<a id="authentication"></a>
## Authentication
Cr\*nMaster supports multiple authentication methods to secure your instance:
### Password Authentication
Set a password to protect access to your Cronmaster instance:
```yaml
environment:
- AUTH_PASSWORD=your_secure_password
```
Users will be prompted to enter this password before accessing the application.
### SSO Authentication (OIDC)
Cr\*nMaster supports SSO via OIDC (OpenID Connect), compatible with providers like:
- Authentik
- Auth0
- Keycloak
- Okta
- Google
- Entra ID (Azure AD)
- And many more!
You can enable **both** password and SSO authentication simultaneously:
The login page will display both options, allowing users to choose their preferred method.
**For detailed setup instructions, see **[howto/SSO.md](howto/SSO.md)**
<a id="usage"></a>
## Usage ## Usage
<a id="viewing-system-information"></a>
### Viewing System Information ### Viewing System Information
The application automatically detects your operating system and displays: The application automatically detects your operating system and displays:
- Platform
- Hostname
- IP Address
- System Uptime - System Uptime
- Memory Usage - Memory Usage
- CPU Information - CPU Information
- GPU Information (if supported) - GPU Information (if supported)
<a id="managing-cron-jobs"></a>
### Managing Cron Jobs ### Managing Cron Jobs
1. **View Existing Jobs**: All current cron jobs are displayed with their schedules and commands 1. **View Existing Jobs**: All current cron jobs are displayed with their schedules and commands
@@ -176,6 +250,13 @@ The application automatically detects your operating system and displays:
4. **Add Comments**: Include descriptions for your cron jobs 4. **Add Comments**: Include descriptions for your cron jobs
5. **Delete Jobs**: Remove unwanted cron jobs with the delete button 5. **Delete Jobs**: Remove unwanted cron jobs with the delete button
6. **Clone Jobs**: Clone jobs to quickly edit the command in case it's similar 6. **Clone Jobs**: Clone jobs to quickly edit the command in case it's similar
7. **Enable Logging**: Optionally enable execution logging for any cronjob to capture detailed execution information
<a id="job-execution-logging"></a>
### Job Execution Logging
📖 **For complete logging documentation, see [howto/LOGS.md](howto/LOGS.md)**
### Cron Schedule Format ### Cron Schedule Format
@@ -187,6 +268,8 @@ The application uses standard cron format: `* * * * *`
- Fourth field: Month (1-12) - Fourth field: Month (1-12)
- Fifth field: Day of week (0-7, where 0 and 7 are Sunday) - Fifth field: Day of week (0-7, where 0 and 7 are Sunday)
<a id="managing-scripts"></a>
### Managing Scripts ### Managing Scripts
1. **View Existing Scripts**: All current user created scripts are displayed with their name and descriptions 1. **View Existing Scripts**: All current user created scripts are displayed with their name and descriptions
@@ -195,26 +278,9 @@ The application uses standard cron format: `* * * * *`
4. **Delete Scripts**: Remove unwanted scripts (this won't delete the cronjob, you will need to manually remove these yourself) 4. **Delete Scripts**: Remove unwanted scripts (this won't delete the cronjob, you will need to manually remove these yourself)
5. **Clone Scripts**: Clone scripts to quickly edit them in case they are similar to one another. 5. **Clone Scripts**: Clone scripts to quickly edit them in case they are similar to one another.
## Technologies Used
- **Next.js 14**: React framework with App Router
- **TypeScript**: Type-safe JavaScript
- **Tailwind CSS**: Utility-first CSS framework
- **Lucide React**: Beautiful icons
- **next-themes**: Dark/light mode support
- **Docker**: Containerization
## Contributing
1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests if applicable
5. Submit a pull request
## Community shouts ## Community shouts
I would like to thank the following members for raising issues and help test/debug them! I would like to thank the following members for raising issues and help test/debug them!
<table> <table>
<tbody> <tbody>
@@ -235,13 +301,31 @@ I would like to thank the following members for raising issues and help test/deb
<a href="https://github.com/mariushosting"><img width="100" height="100" src="https://avatars.githubusercontent.com/u/37554361?u=9007d0600680ac2b267bde2d8c19b05c06285a34&v=4&s=100"><br />mariushosting</a> <a href="https://github.com/mariushosting"><img width="100" height="100" src="https://avatars.githubusercontent.com/u/37554361?u=9007d0600680ac2b267bde2d8c19b05c06285a34&v=4&s=100"><br />mariushosting</a>
</td> </td>
</tr> </tr>
<tr>
<td align="center" valign="top" width="20%">
<a href="https://github.com/DVDAndroid"><img width="100" height="100" src="https://avatars.githubusercontent.com/u/6277172?u=78aa9b049a0c1a7ae5408d22219a8a91cfe45095&v=4&size=100"><br />DVDAndroid</a>
</td>
<td align="center" valign="top" width="20%">
<a href="https://github.com/ActxLeToucan"><img width="100" height="100" src="https://avatars.githubusercontent.com/u/56509120?u=b0a684dfa1fcf8f3f41c2ead37f6441716d8bd62&v=4&size=100"><br />ActxLeToucan</a>
</td>
<td align="center" valign="top" width="20%">
<a href="https://github.com/mrtimothyduong"><img width="100" height="100" src="https://avatars.githubusercontent.com/u/34667840?u=b54354da56681c17ca58366a68a6a94c80f77a1d&v=4&size=100"><br />mrtimothyduong</a>
</td>
<td align="center" valign="top" width="20%">
<a href="https://github.com/cerede2000"><img width="100" height="100" src="https://avatars.githubusercontent.com/u/38144752?v=4&size=100"><br />cerede2000</a>
</td>
<td align="center" valign="top" width="20%">
<a href="https://github.com/Navino16"><img width="100" height="100" src="https://avatars.githubusercontent.com/u/22234867?v=4&size=100"><br />Navino16</a>
</td>
</tr>
<tr>
<td align="center" valign="top" width="20%">
<a href="https://github.com/ShadowTox"><img width="100" height="100" src="https://avatars.githubusercontent.com/u/558536?v=4&size=100"><br />ShadowTox</a>
</td>
</tr>
</tbody> </tbody>
</table> </table>
## License ## Star History
This project is licensed under the MIT License. [![Star History Chart](https://api.star-history.com/svg?repos=fccview/cronmaster&type=Date)](https://www.star-history.com/#fccview/cronmaster&Date)
## Support
For issues and questions, please open an issue on the GitHub repository.

View File

@@ -1,137 +0,0 @@
"use client";
import { useEffect, useRef, useState } from "react";
import { EditorView } from "@codemirror/view";
import { EditorState } from "@codemirror/state";
import { javascript } from "@codemirror/lang-javascript";
import { oneDark } from "@codemirror/theme-one-dark";
import { Button } from "./ui/Button";
import { Terminal, Copy, Check } from "lucide-react";
interface BashEditorProps {
value: string;
onChange: (value: string) => void;
placeholder?: string;
className?: string;
label?: string;
}
export function BashEditor({
value,
onChange,
placeholder = "#!/bin/bash\n# Your bash script here\necho 'Hello World'",
className = "",
label = "Bash Script",
}: BashEditorProps) {
const [copied, setCopied] = useState(false);
const editorRef = useRef<HTMLDivElement>(null);
const editorViewRef = useRef<EditorView | null>(null);
useEffect(() => {
if (!editorRef.current) return;
const bashLanguage = javascript({
typescript: false,
jsx: false,
});
const state = EditorState.create({
doc: value || placeholder,
extensions: [
bashLanguage,
oneDark,
EditorView.updateListener.of((update: any) => {
if (update.docChanged) {
onChange(update.state.doc.toString());
}
}),
EditorView.theme({
"&": {
fontSize: "14px",
fontFamily:
'ui-monospace, SFMono-Regular, "SF Mono", Consolas, "Liberation Mono", Menlo, monospace',
height: "100%",
maxHeight: "100%",
},
".cm-content": {
padding: "12px",
minHeight: "200px",
},
".cm-line": {
lineHeight: "1.4",
},
".cm-scroller": {
fontFamily:
'ui-monospace, SFMono-Regular, "SF Mono", Consolas, "Liberation Mono", Menlo, monospace',
height: "100%",
maxHeight: "100%",
},
}),
],
});
const view = new EditorView({
state,
parent: editorRef.current,
});
editorViewRef.current = view;
return () => {
view.destroy();
};
}, []);
useEffect(() => {
if (editorViewRef.current) {
const currentValue = editorViewRef.current.state.doc.toString();
if (currentValue !== value) {
editorViewRef.current.dispatch({
changes: {
from: 0,
to: editorViewRef.current.state.doc.length,
insert: value,
},
});
}
}
}, [value]);
const handleCopy = async () => {
if (editorViewRef.current) {
const text = editorViewRef.current.state.doc.toString();
await navigator.clipboard.writeText(text);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
}
};
return (
<div className={className}>
{label && (
<div className="flex items-center justify-between mb-2">
<div className="flex items-center gap-2">
<Terminal className="h-4 w-4 text-cyan-500" />
<span className="text-sm font-medium">{label}</span>
</div>
<Button
variant="outline"
size="sm"
onClick={handleCopy}
className="btn-outline h-7 px-2"
>
{copied ? (
<Check className="h-3 w-3 mr-1" />
) : (
<Copy className="h-3 w-3 mr-1" />
)}
{copied ? "Copied!" : "Copy"}
</Button>
</div>
)}
<div className="border border-border overflow-hidden h-full">
<div ref={editorRef} className="h-full rounded-lg" />
</div>
</div>
);
}

View File

@@ -1,336 +0,0 @@
"use client";
import { Card, CardContent, CardHeader, CardTitle } from "./ui/Card";
import { Button } from "./ui/Button";
import { Trash2, Clock, Edit, Plus, Files } from "lucide-react";
import { CronJob } from "@/app/_utils/system";
import {
removeCronJob,
editCronJob,
createCronJob,
cloneCronJob,
} from "@/app/_server/actions/cronjobs";
import { useState } from "react";
import { CreateTaskModal } from "./modals/CreateTaskModal";
import { EditTaskModal } from "./modals/EditTaskModal";
import { DeleteTaskModal } from "./modals/DeleteTaskModal";
import { CloneTaskModal } from "./modals/CloneTaskModal";
import { type Script } from "@/app/_server/actions/scripts";
import { showToast } from "./ui/Toast";
interface CronJobListProps {
cronJobs: CronJob[];
scripts: Script[];
}
export function CronJobList({ cronJobs, scripts }: CronJobListProps) {
const [deletingId, setDeletingId] = useState<string | null>(null);
const [editingJob, setEditingJob] = useState<CronJob | null>(null);
const [isEditModalOpen, setIsEditModalOpen] = useState(false);
const [isNewCronModalOpen, setIsNewCronModalOpen] = useState(false);
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
const [isCloneModalOpen, setIsCloneModalOpen] = useState(false);
const [jobToDelete, setJobToDelete] = useState<CronJob | null>(null);
const [jobToClone, setJobToClone] = useState<CronJob | null>(null);
const [isCloning, setIsCloning] = useState(false);
const [editForm, setEditForm] = useState({
schedule: "",
command: "",
comment: "",
});
const [newCronForm, setNewCronForm] = useState({
schedule: "",
command: "",
comment: "",
selectedScriptId: null as string | null,
});
const handleDelete = async (id: string) => {
setDeletingId(id);
try {
const result = await removeCronJob(id);
if (result.success) {
showToast("success", "Cron job deleted successfully");
} else {
showToast("error", "Failed to delete cron job", result.message);
}
} catch (error) {
showToast(
"error",
"Failed to delete cron job",
"Please try again later."
);
} finally {
setDeletingId(null);
setIsDeleteModalOpen(false);
setJobToDelete(null);
}
};
const handleClone = async (newComment: string) => {
if (!jobToClone) return;
setIsCloning(true);
try {
const result = await cloneCronJob(jobToClone.id, newComment);
if (result.success) {
setIsCloneModalOpen(false);
setJobToClone(null);
showToast("success", "Cron job cloned successfully");
} else {
showToast("error", "Failed to clone cron job", result.message);
}
} finally {
setIsCloning(false);
}
};
const confirmDelete = (job: CronJob) => {
setJobToDelete(job);
setIsDeleteModalOpen(true);
};
const confirmClone = (job: CronJob) => {
setJobToClone(job);
setIsCloneModalOpen(true);
};
const handleEdit = (job: CronJob) => {
setEditingJob(job);
setEditForm({
schedule: job.schedule,
command: job.command,
comment: job.comment || "",
});
setIsEditModalOpen(true);
};
const handleEditSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!editingJob) return;
try {
const formData = new FormData();
formData.append("id", editingJob.id);
formData.append("schedule", editForm.schedule);
formData.append("command", editForm.command);
formData.append("comment", editForm.comment);
const result = await editCronJob(formData);
if (result.success) {
setIsEditModalOpen(false);
setEditingJob(null);
showToast("success", "Cron job updated successfully");
} else {
showToast("error", "Failed to update cron job", result.message);
}
} catch (error) {
showToast(
"error",
"Failed to update cron job",
"Please try again later."
);
}
};
const handleNewCronSubmit = async (e: React.FormEvent) => {
e.preventDefault();
try {
const formData = new FormData();
formData.append("schedule", newCronForm.schedule);
formData.append("command", newCronForm.command);
formData.append("comment", newCronForm.comment);
if (newCronForm.selectedScriptId) {
formData.append("selectedScriptId", newCronForm.selectedScriptId);
}
const result = await createCronJob(formData);
if (result.success) {
setIsNewCronModalOpen(false);
setNewCronForm({
schedule: "",
command: "",
comment: "",
selectedScriptId: null,
});
showToast("success", "Cron job created successfully");
} else {
showToast("error", "Failed to create cron job", result.message);
}
} catch (error) {
showToast(
"error",
"Failed to create cron job",
"Please try again later."
);
}
};
return (
<>
<Card className="glass-card">
<CardHeader>
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
<div className="flex items-center gap-3">
<div className="p-2 bg-primary/10 rounded-lg">
<Clock className="h-5 w-5 text-primary" />
</div>
<div>
<CardTitle className="text-xl brand-gradient">
Scheduled Tasks
</CardTitle>
<p className="text-sm text-muted-foreground">
{cronJobs.length} scheduled job
{cronJobs.length !== 1 ? "s" : ""}
</p>
</div>
</div>
<Button
onClick={() => setIsNewCronModalOpen(true)}
className="btn-primary glow-primary"
>
<Plus className="h-4 w-4 mr-2" />
New Task
</Button>
</div>
</CardHeader>
<CardContent>
{cronJobs.length === 0 ? (
<div className="text-center py-16">
<div className="mx-auto w-20 h-20 bg-gradient-to-br from-primary/20 to-blue-500/20 rounded-full flex items-center justify-center mb-6">
<Clock className="h-10 w-10 text-primary" />
</div>
<h3 className="text-xl font-semibold mb-3 brand-gradient">
No scheduled tasks yet
</h3>
<p className="text-muted-foreground mb-6 max-w-md mx-auto">
Create your first scheduled task to automate your system
operations and boost productivity.
</p>
<Button
onClick={() => setIsNewCronModalOpen(true)}
className="btn-primary glow-primary"
size="lg"
>
<Plus className="h-5 w-5 mr-2" />
Create Your First Task
</Button>
</div>
) : (
<div className="space-y-3">
{cronJobs.map((job) => (
<div
key={job.id}
className="glass-card p-4 border border-border/50 rounded-lg hover:bg-accent/30 transition-colors"
>
<div className="flex items-start justify-between gap-4">
<div className="flex-1 min-w-0">
<div className="flex items-center gap-3 mb-2">
<code className="text-sm bg-purple-500/10 text-purple-600 dark:text-purple-400 px-2 py-1 rounded font-mono border border-purple-500/20">
{job.schedule}
</code>
<div className="flex-1 min-w-0">
<pre
className="text-sm font-medium text-foreground truncate bg-muted/30 px-2 py-1 rounded border border-border/30"
title={job.command}
>
{job.command}
</pre>
</div>
</div>
{job.comment && (
<p
className="text-xs text-muted-foreground italic truncate"
title={job.comment}
>
{job.comment}
</p>
)}
</div>
<div className="flex items-center gap-2 flex-shrink-0">
<Button
variant="outline"
size="sm"
onClick={() => handleEdit(job)}
className="btn-outline h-8 px-3"
title="Edit cron job"
aria-label="Edit cron job"
>
<Edit className="h-3 w-3" />
</Button>
<Button
variant="outline"
size="sm"
onClick={() => confirmClone(job)}
className="btn-outline h-8 px-3"
title="Clone cron job"
aria-label="Clone cron job"
>
<Files className="h-3 w-3" />
</Button>
<Button
variant="destructive"
size="sm"
onClick={() => confirmDelete(job)}
disabled={deletingId === job.id}
className="btn-destructive h-8 px-3"
title="Delete cron job"
aria-label="Delete cron job"
>
{deletingId === job.id ? (
<div className="h-3 w-3 animate-spin rounded-full border-2 border-current border-t-transparent" />
) : (
<Trash2 className="h-3 w-3" />
)}
</Button>
</div>
</div>
</div>
))}
</div>
)}
</CardContent>
</Card>
<CreateTaskModal
isOpen={isNewCronModalOpen}
onClose={() => setIsNewCronModalOpen(false)}
onSubmit={handleNewCronSubmit}
scripts={scripts}
form={newCronForm}
onFormChange={(updates) =>
setNewCronForm((prev) => ({ ...prev, ...updates }))
}
/>
<EditTaskModal
isOpen={isEditModalOpen}
onClose={() => setIsEditModalOpen(false)}
onSubmit={handleEditSubmit}
form={editForm}
onFormChange={(updates) =>
setEditForm((prev) => ({ ...prev, ...updates }))
}
/>
<DeleteTaskModal
isOpen={isDeleteModalOpen}
onClose={() => setIsDeleteModalOpen(false)}
onConfirm={() =>
jobToDelete ? handleDelete(jobToDelete.id) : undefined
}
job={jobToDelete}
/>
<CloneTaskModal
cronJob={jobToClone}
isOpen={isCloneModalOpen}
onClose={() => setIsCloneModalOpen(false)}
onConfirm={handleClone}
isCloning={isCloning}
/>
</>
);
}

View File

@@ -0,0 +1,433 @@
"use client";
import {
Card,
CardContent,
CardHeader,
CardTitle,
} from "@/app/_components/GlobalComponents/Cards/Card";
import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { Switch } from "@/app/_components/GlobalComponents/UIElements/Switch";
import {
ClockIcon,
PlusIcon,
Archive,
CaretDownIcon,
CodeIcon,
ChatTextIcon,
GearIcon,
CircleNotchIcon,
FunnelIcon,
} from "@phosphor-icons/react";
import { CronJob } from "@/app/_utils/cronjob-utils";
import { Script } from "@/app/_utils/scripts-utils";
import { UserFilter } from "@/app/_components/FeatureComponents/User/UserFilter";
import { useCronJobState } from "@/app/_hooks/useCronJobState";
import { CronJobItem } from "@/app/_components/FeatureComponents/Cronjobs/Parts/CronJobItem";
import { MinimalCronJobItem } from "@/app/_components/FeatureComponents/Cronjobs/Parts/MinimalCronJobItem";
import { CronJobEmptyState } from "@/app/_components/FeatureComponents/Cronjobs/Parts/CronJobEmptyState";
import { CronJobListModals } from "@/app/_components/FeatureComponents/Modals/CronJobListsModals";
import { LogsModal } from "@/app/_components/FeatureComponents/Modals/LogsModal";
import { LiveLogModal } from "@/app/_components/FeatureComponents/Modals/LiveLogModal";
import { RestoreBackupModal } from "@/app/_components/FeatureComponents/Modals/RestoreBackupModal";
import { FiltersModal } from "@/app/_components/FeatureComponents/Modals/FiltersModal";
import { useTranslations } from "next-intl";
import { useSSEContext } from "@/app/_contexts/SSEContext";
import { useEffect, useState } from "react";
import { useRouter } from "next/navigation";
import {
fetchBackupFiles,
restoreCronJob,
deleteBackup,
backupAllCronJobs,
restoreAllCronJobs,
} from "@/app/_server/actions/cronjobs";
import { showToast } from "@/app/_components/GlobalComponents/UIElements/Toast";
interface CronJobListProps {
cronJobs: CronJob[];
scripts: Script[];
}
export const CronJobList = ({ cronJobs, scripts }: CronJobListProps) => {
const t = useTranslations();
const router = useRouter();
const { subscribe } = useSSEContext();
const [isBackupModalOpen, setIsBackupModalOpen] = useState(false);
const [backupFiles, setBackupFiles] = useState<
Array<{
filename: string;
job: CronJob;
backedUpAt: string;
}>
>([]);
const [scheduleDisplayMode, setScheduleDisplayMode] = useState<
"cron" | "human" | "both"
>("both");
const [loadedSettings, setLoadedSettings] = useState<boolean>(false);
const [isFiltersModalOpen, setIsFiltersModalOpen] = useState(false);
const [minimalMode, setMinimalMode] = useState(false);
const [isClient, setIsClient] = useState(false);
useEffect(() => {
setIsClient(true);
try {
const savedScheduleMode = localStorage.getItem(
"cronjob-schedule-display-mode"
);
if (
savedScheduleMode === "cron" ||
savedScheduleMode === "human" ||
savedScheduleMode === "both"
) {
setScheduleDisplayMode(savedScheduleMode);
}
const savedMinimalMode = localStorage.getItem("cronjob-minimal-mode");
if (savedMinimalMode === "true") {
setMinimalMode(true);
}
setLoadedSettings(true);
} catch (error) {
console.warn("Failed to load settings from localStorage:", error);
}
}, []);
useEffect(() => {
const unsubscribe = subscribe((event) => {
if (event.type === "job-completed" || event.type === "job-failed") {
router.refresh();
}
});
return unsubscribe;
}, [subscribe, router]);
useEffect(() => {
if (!isClient) return;
try {
localStorage.setItem(
"cronjob-schedule-display-mode",
scheduleDisplayMode
);
} catch (error) {
console.warn(
"Failed to save schedule display mode to localStorage:",
error
);
}
}, [scheduleDisplayMode, isClient]);
useEffect(() => {
if (!isClient) return;
try {
localStorage.setItem("cronjob-minimal-mode", minimalMode.toString());
} catch (error) {
console.warn("Failed to save minimal mode to localStorage:", error);
}
}, [minimalMode, isClient]);
const loadBackupFiles = async () => {
const backups = await fetchBackupFiles();
setBackupFiles(backups);
};
const handleRestore = async (filename: string) => {
const result = await restoreCronJob(filename);
if (result.success) {
showToast("success", t("cronjobs.restoreJobSuccess"));
router.refresh();
loadBackupFiles();
} else {
showToast("error", t("cronjobs.restoreJobFailed"), result.message);
}
};
const handleRestoreAll = async () => {
const result = await restoreAllCronJobs();
if (result.success) {
showToast("success", result.message);
router.refresh();
setIsBackupModalOpen(false);
} else {
showToast("error", "Failed to restore all jobs", result.message);
}
};
const handleBackupAll = async () => {
const result = await backupAllCronJobs();
if (result.success) {
showToast("success", result.message);
loadBackupFiles();
} else {
showToast("error", t("cronjobs.backupAllFailed"), result.message);
}
};
const handleDeleteBackup = async (filename: string) => {
const result = await deleteBackup(filename);
if (result.success) {
showToast("success", t("cronjobs.backupDeleted"));
loadBackupFiles();
} else {
showToast("error", "Failed to delete backup", result.message);
}
};
const {
deletingId,
runningJobId,
selectedUser,
setSelectedUser,
jobErrors,
errorModalOpen,
setErrorModalOpen,
selectedError,
setSelectedError,
isLogsModalOpen,
setIsLogsModalOpen,
jobForLogs,
isLiveLogModalOpen,
setIsLiveLogModalOpen,
liveLogRunId,
liveLogJobId,
liveLogJobComment,
filteredJobs,
isNewCronModalOpen,
setIsNewCronModalOpen,
isEditModalOpen,
setIsEditModalOpen,
isDeleteModalOpen,
setIsDeleteModalOpen,
isCloneModalOpen,
setIsCloneModalOpen,
jobToDelete,
jobToClone,
isCloning,
editForm,
setEditForm,
newCronForm,
setNewCronForm,
handleErrorClickLocal,
refreshJobErrorsLocal,
handleDeleteLocal,
handleCloneLocal,
handlePauseLocal,
handleResumeLocal,
handleRunLocal,
handleToggleLoggingLocal,
handleViewLogs,
confirmDelete,
confirmClone,
handleEdit,
handleEditSubmitLocal,
handleNewCronSubmitLocal,
handleBackupLocal,
} = useCronJobState({ cronJobs, scripts });
return (
<>
<Card className="glass-card">
<CardHeader>
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
<div className="flex items-center gap-3">
<div className="p-2 bg-primary/10 ascii-border">
<ClockIcon className="h-5 w-5 text-primary" />
</div>
<div>
<CardTitle className="text-xl brand-gradient">
{t("cronjobs.scheduledTasks")}
</CardTitle>
<p className="text-sm text-muted-foreground">
{t("cronjobs.nOfNJObs", {
filtered: filteredJobs.length,
total: cronJobs.length,
})}{" "}
{selectedUser &&
t("cronjobs.forUser", { user: selectedUser })}
</p>
</div>
</div>
<div className="flex gap-2 w-full justify-between sm:w-auto">
<div className="flex gap-2">
<Button
onClick={() => setIsFiltersModalOpen(true)}
variant="outline"
className="btn-outline"
title={t("cronjobs.filters")}
>
<FunnelIcon className="h-4 w-4" />
</Button>
<Button
onClick={() => setIsBackupModalOpen(true)}
variant="outline"
className="btn-outline"
title={t("cronjobs.backups")}
>
<Archive className="h-4 w-4" />
</Button>
</div>
<Button
onClick={() => setIsNewCronModalOpen(true)}
className="btn-primary glow-primary"
>
<PlusIcon className="h-4 w-4 mr-2" />
{t("cronjobs.newTask")}
</Button>
</div>
</div>
</CardHeader>
<CardContent>
<div className="mb-4 flex items-center justify-between">
<div className="flex items-center gap-3">
<label
className="text-sm font-medium text-foreground cursor-pointer"
onClick={() => setMinimalMode(!minimalMode)}
>
{t("cronjobs.minimalMode")}
</label>
<Switch checked={minimalMode} onCheckedChange={setMinimalMode} />
</div>
</div>
{filteredJobs.length === 0 ? (
<CronJobEmptyState
selectedUser={selectedUser}
onNewTaskClick={() => setIsNewCronModalOpen(true)}
/>
) : (
<div className="space-y-4 max-h-[55vh] min-h-[55vh] overflow-y-auto tui-scrollbar pr-1">
{loadedSettings ? (
filteredJobs.map((job) =>
minimalMode ? (
<MinimalCronJobItem
key={job.id}
job={job}
errors={jobErrors[job.id] || []}
runningJobId={runningJobId}
deletingId={deletingId}
scheduleDisplayMode={scheduleDisplayMode}
onRun={handleRunLocal}
onEdit={handleEdit}
onClone={confirmClone}
onResume={handleResumeLocal}
onPause={handlePauseLocal}
onToggleLogging={handleToggleLoggingLocal}
onViewLogs={handleViewLogs}
onDelete={confirmDelete}
onBackup={handleBackupLocal}
onErrorClick={handleErrorClickLocal}
/>
) : (
<CronJobItem
key={job.id}
job={job}
errors={jobErrors[job.id] || []}
runningJobId={runningJobId}
deletingId={deletingId}
scheduleDisplayMode={scheduleDisplayMode}
onRun={handleRunLocal}
onEdit={handleEdit}
onClone={confirmClone}
onResume={handleResumeLocal}
onPause={handlePauseLocal}
onToggleLogging={handleToggleLoggingLocal}
onViewLogs={handleViewLogs}
onDelete={confirmDelete}
onBackup={handleBackupLocal}
onErrorClick={handleErrorClickLocal}
onErrorDismiss={refreshJobErrorsLocal}
/>
)
)
) : (
<div className="flex items-center justify-center h-full min-h-[55vh]">
<CircleNotchIcon className="h-8 w-8 animate-spin text-primary" />
</div>
)}
</div>
)}
</CardContent>
</Card>
<CronJobListModals
cronJobs={cronJobs}
scripts={scripts}
isNewCronModalOpen={isNewCronModalOpen}
onNewCronModalClose={() => setIsNewCronModalOpen(false)}
onNewCronSubmit={handleNewCronSubmitLocal}
newCronForm={newCronForm}
onNewCronFormChange={(updates) =>
setNewCronForm((prev) => ({ ...prev, ...updates }))
}
isEditModalOpen={isEditModalOpen}
onEditModalClose={() => setIsEditModalOpen(false)}
onEditSubmit={handleEditSubmitLocal}
editForm={editForm}
onEditFormChange={(updates) =>
setEditForm((prev) => ({ ...prev, ...updates }))
}
isDeleteModalOpen={isDeleteModalOpen}
onDeleteModalClose={() => setIsDeleteModalOpen(false)}
onDeleteConfirm={() =>
jobToDelete ? handleDeleteLocal(jobToDelete.id) : undefined
}
jobToDelete={jobToDelete}
isCloneModalOpen={isCloneModalOpen}
onCloneModalClose={() => setIsCloneModalOpen(false)}
onCloneConfirm={handleCloneLocal}
jobToClone={jobToClone}
isCloning={isCloning}
isErrorModalOpen={errorModalOpen}
onErrorModalClose={() => {
setErrorModalOpen(false);
setSelectedError(null);
}}
selectedError={selectedError}
/>
{jobForLogs && (
<LogsModal
isOpen={isLogsModalOpen}
onClose={() => setIsLogsModalOpen(false)}
jobId={jobForLogs.id}
jobComment={jobForLogs.comment}
preSelectedLog={jobForLogs.logError?.lastFailedLog}
/>
)}
<LiveLogModal
isOpen={isLiveLogModalOpen}
onClose={() => setIsLiveLogModalOpen(false)}
runId={liveLogRunId}
jobId={liveLogJobId}
jobComment={liveLogJobComment}
/>
<RestoreBackupModal
isOpen={isBackupModalOpen}
onClose={() => setIsBackupModalOpen(false)}
backups={backupFiles}
onRestore={handleRestore}
onRestoreAll={handleRestoreAll}
onBackupAll={handleBackupAll}
onDelete={handleDeleteBackup}
onRefresh={loadBackupFiles}
/>
<FiltersModal
isOpen={isFiltersModalOpen}
onClose={() => setIsFiltersModalOpen(false)}
selectedUser={selectedUser}
onUserChange={setSelectedUser}
scheduleDisplayMode={scheduleDisplayMode}
onScheduleDisplayModeChange={setScheduleDisplayMode}
/>
</>
);
};

View File

@@ -0,0 +1,40 @@
"use client";
import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { ClockIcon, PlusIcon } from "@phosphor-icons/react";
interface CronJobEmptyStateProps {
selectedUser: string | null;
onNewTaskClick: () => void;
}
export const CronJobEmptyState = ({
selectedUser,
onNewTaskClick,
}: CronJobEmptyStateProps) => {
return (
<div className="text-center py-16">
<div className="mx-auto w-20 h-20 bg-gradient-to-br from-primary/20 to-blue-500/20 rounded-full flex items-center justify-center mb-6">
<ClockIcon className="h-10 w-10 text-primary" />
</div>
<h3 className="text-xl font-semibold mb-3 brand-gradient">
{selectedUser
? `No tasks for user ${selectedUser}`
: "No scheduled tasks yet"}
</h3>
<p className="text-muted-foreground mb-6 max-w-md mx-auto">
{selectedUser
? `No scheduled tasks found for user ${selectedUser}. Try selecting a different user or create a new task.`
: "Create your first scheduled task to automate your system operations and boost productivity."}
</p>
<Button
onClick={onNewTaskClick}
className="btn-primary glow-primary"
size="lg"
>
<PlusIcon className="h-5 w-5 mr-2" />
Create Your First Task
</Button>
</div>
);
};

View File

@@ -0,0 +1,380 @@
"use client";
import { useState, useEffect } from "react";
import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { DropdownMenu } from "@/app/_components/GlobalComponents/UIElements/DropdownMenu";
import {
TrashIcon,
PencilSimpleIcon,
FilesIcon,
UserIcon,
PlayIcon,
PauseIcon,
CodeIcon,
InfoIcon,
FileArrowDownIcon,
FileXIcon,
FileTextIcon,
WarningCircleIcon,
CheckCircleIcon,
WarningIcon,
DownloadIcon,
HashIcon,
CheckIcon,
} from "@phosphor-icons/react";
import { CronJob } from "@/app/_utils/cronjob-utils";
import { JobError } from "@/app/_utils/error-utils";
import { ErrorBadge } from "@/app/_components/GlobalComponents/Badges/ErrorBadge";
import {
parseCronExpression,
type CronExplanation,
} from "@/app/_utils/parser-utils";
import { unwrapCommand } from "@/app/_utils/wrapper-utils-client";
import { useLocale } from "next-intl";
import { useTranslations } from "next-intl";
import { copyToClipboard } from "@/app/_utils/global-utils";
interface CronJobItemProps {
job: CronJob;
errors: JobError[];
runningJobId: string | null;
deletingId: string | null;
scheduleDisplayMode: "cron" | "human" | "both";
onRun: (id: string) => void;
onEdit: (job: CronJob) => void;
onClone: (job: CronJob) => void;
onResume: (id: string) => void;
onPause: (id: string) => void;
onDelete: (job: CronJob) => void;
onToggleLogging: (id: string) => void;
onViewLogs: (job: CronJob) => void;
onBackup: (id: string) => void;
onErrorClick: (error: JobError) => void;
onErrorDismiss: () => void;
}
export const CronJobItem = ({
job,
errors,
runningJobId,
deletingId,
scheduleDisplayMode,
onRun,
onEdit,
onClone,
onResume,
onPause,
onDelete,
onToggleLogging,
onViewLogs,
onBackup,
onErrorClick,
onErrorDismiss,
}: CronJobItemProps) => {
const [cronExplanation, setCronExplanation] =
useState<CronExplanation | null>(null);
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
const [showCopyConfirmation, setShowCopyConfirmation] = useState(false);
const locale = useLocale();
const t = useTranslations();
const displayCommand = unwrapCommand(job.command);
const [commandCopied, setCommandCopied] = useState<string | null>(null);
useEffect(() => {
if (job.schedule) {
const explanation = parseCronExpression(job.schedule, locale);
setCronExplanation(explanation);
} else {
setCronExplanation(null);
}
}, [job.schedule]);
const dropdownMenuItems = [
{
label: t("cronjobs.editCronJob"),
icon: <PencilSimpleIcon className="h-3 w-3" />,
onClick: () => onEdit(job),
},
{
label: job.logsEnabled
? t("cronjobs.disableLogging")
: t("cronjobs.enableLogging"),
icon: job.logsEnabled ? (
<FileXIcon className="h-3 w-3" />
) : (
<FileArrowDownIcon className="h-3 w-3" />
),
onClick: () => onToggleLogging(job.id),
},
...(job.logsEnabled
? [
{
label: t("cronjobs.viewLogs"),
icon: <FileTextIcon className="h-3 w-3" />,
onClick: () => onViewLogs(job),
},
]
: []),
{
label: job.paused
? t("cronjobs.resumeCronJob")
: t("cronjobs.pauseCronJob"),
icon: job.paused ? (
<PlayIcon className="h-3 w-3" />
) : (
<PauseIcon className="h-3 w-3" />
),
onClick: () => (job.paused ? onResume(job.id) : onPause(job.id)),
},
{
label: t("cronjobs.cloneCronJob"),
icon: <FilesIcon className="h-3 w-3" />,
onClick: () => onClone(job),
},
{
label: t("cronjobs.backupJob"),
icon: <DownloadIcon className="h-3 w-3" />,
onClick: () => onBackup(job.id),
},
{
label: t("cronjobs.deleteCronJob"),
icon: <TrashIcon className="h-3 w-3" />,
onClick: () => onDelete(job),
variant: "destructive" as const,
disabled: deletingId === job.id,
},
];
return (
<div
key={job.id}
className={`border border-border lg:tui-card p-4 terminal-font transition-colors ${isDropdownOpen ? "relative z-10" : ""
}`}
>
<div className="flex flex-col sm:flex-row sm:items-start gap-4">
<div className="flex-1 min-w-0">
<div className="flex items-center gap-3 mb-2">
{(scheduleDisplayMode === "cron" ||
scheduleDisplayMode === "both") && (
<code className="text-sm bg-background0 text-status-warning px-2 py-1 terminal-font ascii-border">
{job.schedule}
</code>
)}
{scheduleDisplayMode === "human" && cronExplanation?.isValid && (
<div className="flex items-start gap-1.5 ascii-border bg-background2 px-2 py-0.5">
<InfoIcon className="h-3 w-3 mt-0.5 flex-shrink-0" />
<p className="text-sm italic">
{cronExplanation.humanReadable}
</p>
</div>
)}
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2 min-w-0 w-full">
{commandCopied === job.id && (
<CheckIcon className="h-3 w-3 text-status-success" />
)}
<pre
onClick={(e) => {
e.stopPropagation();
copyToClipboard(unwrapCommand(job.command));
setCommandCopied(job.id);
setTimeout(() => setCommandCopied(null), 3000);
}}
className="w-full cursor-pointer overflow-x-auto text-sm font-medium terminal-font bg-background1 px-2 py-1 ascii-border hide-scrollbar"
>
{unwrapCommand(displayCommand)}
</pre>
</div>
</div>
</div>
<div className="flex items-center gap-2 pb-2 pt-4">
{scheduleDisplayMode === "both" && cronExplanation?.isValid && (
<div className="flex items-start gap-1.5 ascii-border bg-background2 px-2 py-0.5">
<InfoIcon className="h-3 w-3 mt-0.5 flex-shrink-0" />
<p className="text-xs italic">
{cronExplanation.humanReadable}
</p>
</div>
)}
{job.comment && (
<p
className="text-xs italic truncate"
title={job.comment}
>
{job.comment}
</p>
)}
</div>
<div className="flex flex-wrap items-center gap-2 py-3">
<div className="flex items-center gap-1 text-xs bg-background0 px-2 py-0.5 ascii-border cursor-pointer hover:bg-background2 transition-colors relative terminal-font">
<UserIcon className="h-3 w-3" />
<span>{job.user}</span>
</div>
<div
className="flex items-center gap-1 text-xs bg-background0 px-2 py-0.5 ascii-border cursor-pointer hover:bg-background2 transition-colors relative terminal-font"
title="Click to copy Job UUID"
onClick={async () => {
const success = await copyToClipboard(job.id);
if (success) {
setShowCopyConfirmation(true);
setTimeout(() => setShowCopyConfirmation(false), 3000);
}
}}
>
{showCopyConfirmation ? (
<CheckIcon className="h-3 w-3 text-status-success" />
) : (
<HashIcon className="h-3 w-3" />
)}
<span className="font-mono">{job.id}</span>
</div>
{job.paused && (
<span className="text-xs bg-background2 px-2 py-0.5 ascii-border terminal-font">
<span className="text-status-warning">{t("cronjobs.paused")}</span>
</span>
)}
{job.logsEnabled && (
<span className="text-xs bg-background0 px-2 py-0.5 ascii-border terminal-font">
<span className="text-status-info">{t("cronjobs.logged")}</span>
</span>
)}
{job.logsEnabled && job.logError?.hasError && (
<button
onClick={(e) => {
e.stopPropagation();
onViewLogs(job);
}}
className="flex items-center gap-1 text-xs bg-background0 px-2 py-0.5 ascii-border hover:bg-background1 transition-colors cursor-pointer terminal-font"
title="Latest execution failed - Click to view error log"
>
<WarningCircleIcon className="h-3 w-3 text-status-error" />
<span className="text-status-error">
{t("cronjobs.failed", {
exitCode: job.logError?.exitCode?.toString() ?? "",
})}
</span>
</button>
)}
{job.logsEnabled &&
!job.logError?.hasError &&
job.logError?.hasHistoricalFailures && (
<button
onClick={(e) => {
e.stopPropagation();
onViewLogs(job);
}}
className="flex items-center gap-1 text-xs bg-background0 px-2 py-0.5 ascii-border hover:bg-background1 transition-colors cursor-pointer terminal-font"
title="Latest execution succeeded, but has historical failures - Click to view logs"
>
<CheckCircleIcon className="h-3 w-3 text-status-success" />
<span className="text-status-warning">{t("cronjobs.healthy")}</span>
<WarningIcon className="h-3 w-3 text-status-warning" />
</button>
)}
{job.logsEnabled &&
!job.logError?.hasError &&
!job.logError?.hasHistoricalFailures &&
job.logError?.latestExitCode === 0 && (
<div className="flex items-center gap-1 text-xs bg-background0 px-2 py-0.5 ascii-border terminal-font">
<CheckCircleIcon className="h-3 w-3 text-status-success" />
<span className="text-status-success">{t("cronjobs.healthy")}</span>
</div>
)}
{!job.logsEnabled && (
<ErrorBadge
errors={errors}
onErrorClick={onErrorClick}
onErrorDismiss={onErrorDismiss}
/>
)}
</div>
</div>
<div className="flex items-center gap-2 justify-between sm:justify-end">
<div className="flex items-center gap-2 flex-shrink-0">
<Button
variant="outline"
size="sm"
onClick={() => onRun(job.id)}
disabled={runningJobId === job.id || job.paused}
className="btn-outline h-8 px-3"
title={t("cronjobs.runCronManually")}
aria-label={t("cronjobs.runCronManually")}
>
{runningJobId === job.id ? (
<div className="h-3 w-3 animate-spin rounded-full border-2 border-current border-t-transparent" />
) : (
<CodeIcon className="h-3 w-3" />
)}
</Button>
<Button
variant="outline"
size="sm"
onClick={() => {
if (job.paused) {
onResume(job.id);
} else {
onPause(job.id);
}
}}
className="btn-outline h-8 px-3"
title={t("cronjobs.pauseCronJob")}
aria-label={t("cronjobs.pauseCronJob")}
>
{job.paused ? (
<PlayIcon className="h-3 w-3" />
) : (
<PauseIcon className="h-3 w-3" />
)}
</Button>
<Button
variant="outline"
size="sm"
onClick={() => {
if (job.logsEnabled) {
onViewLogs(job);
} else {
onToggleLogging(job.id);
}
}}
className="btn-outline h-8 px-3"
title={
job.logsEnabled
? t("cronjobs.viewLogs")
: t("cronjobs.enableLogging")
}
aria-label={
job.logsEnabled
? t("cronjobs.viewLogs")
: t("cronjobs.enableLogging")
}
>
{job.logsEnabled ? (
<FileTextIcon className="h-3 w-3" />
) : (
<FileArrowDownIcon className="h-3 w-3" />
)}
</Button>
</div>
<DropdownMenu
items={dropdownMenuItems}
onOpenChange={setIsDropdownOpen}
/>
</div>
</div>
</div>
);
};

View File

@@ -0,0 +1,310 @@
"use client";
import { useState, useEffect } from "react";
import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { DropdownMenu } from "@/app/_components/GlobalComponents/UIElements/DropdownMenu";
import {
TrashIcon,
PencilSimpleIcon,
FilesIcon,
PlayIcon,
PauseIcon,
CodeIcon,
InfoIcon,
DownloadIcon,
CheckIcon,
FileXIcon,
FileTextIcon,
FileArrowDownIcon,
} from "@phosphor-icons/react";
import { CronJob } from "@/app/_utils/cronjob-utils";
import { JobError } from "@/app/_utils/error-utils";
import {
parseCronExpression,
type CronExplanation,
} from "@/app/_utils/parser-utils";
import { unwrapCommand } from "@/app/_utils/wrapper-utils-client";
import { useLocale } from "next-intl";
import { useTranslations } from "next-intl";
import { copyToClipboard } from "@/app/_utils/global-utils";
interface MinimalCronJobItemProps {
job: CronJob;
errors: JobError[];
runningJobId: string | null;
deletingId: string | null;
scheduleDisplayMode: "cron" | "human" | "both";
onRun: (id: string) => void;
onEdit: (job: CronJob) => void;
onClone: (job: CronJob) => void;
onResume: (id: string) => void;
onPause: (id: string) => void;
onDelete: (job: CronJob) => void;
onToggleLogging: (id: string) => void;
onViewLogs: (job: CronJob) => void;
onBackup: (id: string) => void;
onErrorClick: (error: JobError) => void;
}
export const MinimalCronJobItem = ({
job,
errors,
runningJobId,
deletingId,
scheduleDisplayMode,
onRun,
onEdit,
onClone,
onResume,
onPause,
onDelete,
onToggleLogging,
onViewLogs,
onBackup,
onErrorClick,
}: MinimalCronJobItemProps) => {
const [cronExplanation, setCronExplanation] =
useState<CronExplanation | null>(null);
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
const [commandCopied, setCommandCopied] = useState<string | null>(null);
const locale = useLocale();
const t = useTranslations();
const displayCommand = unwrapCommand(job.command);
useEffect(() => {
if (job.schedule) {
const explanation = parseCronExpression(job.schedule, locale);
setCronExplanation(explanation);
} else {
setCronExplanation(null);
}
}, [job.schedule]);
const dropdownMenuItems = [
{
label: t("cronjobs.editCronJob"),
icon: <PencilSimpleIcon className="h-3 w-3" />,
onClick: () => onEdit(job),
},
{
label: job.logsEnabled
? t("cronjobs.disableLogging")
: t("cronjobs.enableLogging"),
icon: job.logsEnabled ? (
<FileXIcon className="h-3 w-3" />
) : (
<CodeIcon className="h-3 w-3" />
),
onClick: () => onToggleLogging(job.id),
},
...(job.logsEnabled
? [
{
label: t("cronjobs.viewLogs"),
icon: <CodeIcon className="h-3 w-3" />,
onClick: () => onViewLogs(job),
},
]
: []),
{
label: job.paused
? t("cronjobs.resumeCronJob")
: t("cronjobs.pauseCronJob"),
icon: job.paused ? (
<PlayIcon className="h-3 w-3" />
) : (
<PauseIcon className="h-3 w-3" />
),
onClick: () => (job.paused ? onResume(job.id) : onPause(job.id)),
},
{
label: t("cronjobs.cloneCronJob"),
icon: <FilesIcon className="h-3 w-3" />,
onClick: () => onClone(job),
},
{
label: t("cronjobs.backupJob"),
icon: <DownloadIcon className="h-3 w-3" />,
onClick: () => onBackup(job.id),
},
{
label: t("cronjobs.deleteCronJob"),
icon: <TrashIcon className="h-3 w-3" />,
onClick: () => onDelete(job),
variant: "destructive" as const,
disabled: deletingId === job.id,
},
];
return (
<div
key={job.id}
className={`border border-border lg:tui-card p-3 terminal-font transition-colors ${isDropdownOpen ? "relative z-10" : ""
}`}
>
<div className="flex items-center gap-3">
<div className="flex items-center gap-1 flex-shrink-0">
{scheduleDisplayMode === "cron" && (
<code className="text-xs bg-background0 text-status-warning px-1.5 py-0.5 terminal-font ascii-border">
{job.schedule}
</code>
)}
{scheduleDisplayMode === "human" && cronExplanation?.isValid && (
<div className="flex items-center gap-1 ascii-border bg-background2 px-1.5 py-0.5">
<InfoIcon className="h-3 w-3 flex-shrink-0" />
<span className="text-xs italic truncate max-w-32">
{cronExplanation.humanReadable}
</span>
</div>
)}
{scheduleDisplayMode === "both" && (
<div className="flex items-center gap-1">
<code className="text-xs bg-background0 text-status-warning px-1 py-0.5 terminal-font ascii-border">
{job.schedule}
</code>
{cronExplanation?.isValid && (
<div
className="flex items-center gap-1 ascii-border bg-background0 px-1 py-0.5 cursor-help"
title={cronExplanation.humanReadable}
>
<InfoIcon className="h-2.5 w-2.5 flex-shrink-0" />
</div>
)}
</div>
)}
</div>
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2">
{commandCopied === job.id && (
<CheckIcon className="h-3 w-3 text-status-success flex-shrink-0" />
)}
<pre
onClick={(e) => {
e.stopPropagation();
copyToClipboard(unwrapCommand(job.command));
setCommandCopied(job.id);
setTimeout(() => setCommandCopied(null), 3000);
}}
className="flex-1 cursor-pointer overflow-hidden text-sm font-medium terminal-font bg-background1 px-2 py-1 ascii-border truncate"
title={unwrapCommand(job.command)}
>
{unwrapCommand(displayCommand)}
</pre>
</div>
</div>
<div className="flex items-center gap-1 flex-shrink-0">
{job.logsEnabled && (
<div
className="w-2 h-2 bg-status-info ascii-border"
title={t("cronjobs.logged")}
/>
)}
{job.paused && (
<div
className="w-2 h-2 bg-status-warning ascii-border"
title={t("cronjobs.paused")}
/>
)}
{!job.logError?.hasError && job.logsEnabled && (
<div
className="w-2 h-2 bg-status-success ascii-border"
title={t("cronjobs.healthy")}
/>
)}
{job.logsEnabled && job.logError?.hasError && (
<div
className="w-2 h-2 bg-status-error ascii-border cursor-pointer"
title="Latest execution failed - Click to view error log"
onClick={(e) => {
e.stopPropagation();
onViewLogs(job);
}}
/>
)}
{!job.logsEnabled && errors.length > 0 && (
<div
className="w-2 h-2 bg-status-warning ascii-border cursor-pointer"
title={`${errors.length} error(s)`}
onClick={(e) => onErrorClick(errors[0])}
/>
)}
</div>
<div className="flex items-center gap-2 flex-shrink-0">
<Button
variant="outline"
size="sm"
onClick={() => onRun(job.id)}
disabled={runningJobId === job.id || job.paused}
className="btn-outline h-8 px-3 hidden md:flex"
title={t("cronjobs.runCronManually")}
aria-label={t("cronjobs.runCronManually")}
>
{runningJobId === job.id ? (
<div className="h-3 w-3 animate-spin rounded-full border-2 border-current border-t-transparent" />
) : (
<CodeIcon className="h-3 w-3" />
)}
</Button>
<Button
variant="outline"
size="sm"
onClick={() => {
if (job.paused) {
onResume(job.id);
} else {
onPause(job.id);
}
}}
className="btn-outline h-8 px-3 hidden md:flex"
title={t("cronjobs.pauseCronJob")}
aria-label={t("cronjobs.pauseCronJob")}
>
{job.paused ? (
<PlayIcon className="h-3 w-3" />
) : (
<PauseIcon className="h-3 w-3" />
)}
</Button>
<Button
variant="outline"
size="sm"
onClick={() => {
if (job.logsEnabled) {
onViewLogs(job);
} else {
onToggleLogging(job.id);
}
}}
className="btn-outline h-8 px-3 hidden md:flex"
title={
job.logsEnabled
? t("cronjobs.viewLogs")
: t("cronjobs.enableLogging")
}
aria-label={
job.logsEnabled
? t("cronjobs.viewLogs")
: t("cronjobs.enableLogging")
}
>
{job.logsEnabled ? (
<FileTextIcon className="h-3 w-3" />
) : (
<FileArrowDownIcon className="h-3 w-3" />
)}
</Button>
<DropdownMenu
items={dropdownMenuItems}
onOpenChange={setIsDropdownOpen}
/>
</div>
</div>
</div>
);
};

View File

@@ -0,0 +1,441 @@
import { JobError, setJobError } from "@/app/_utils/error-utils";
import { showToast } from "@/app/_components/GlobalComponents/UIElements/Toast";
import {
removeCronJob,
editCronJob,
createCronJob,
cloneCronJob,
pauseCronJobAction,
resumeCronJobAction,
runCronJob,
toggleCronJobLogging,
backupCronJob,
} from "@/app/_server/actions/cronjobs";
import { CronJob } from "@/app/_utils/cronjob-utils";
interface HandlerProps {
setDeletingId: (id: string | null) => void;
setIsDeleteModalOpen: (open: boolean) => void;
setJobToDelete: (job: CronJob | null) => void;
setIsCloneModalOpen: (open: boolean) => void;
setJobToClone: (job: CronJob | null) => void;
setIsCloning: (cloning: boolean) => void;
setIsEditModalOpen: (open: boolean) => void;
setEditingJob: (job: CronJob | null) => void;
setIsNewCronModalOpen: (open: boolean) => void;
setNewCronForm: (form: any) => void;
setRunningJobId: (id: string | null) => void;
refreshJobErrors: () => void;
setIsLiveLogModalOpen?: (open: boolean) => void;
setLiveLogRunId?: (runId: string) => void;
setLiveLogJobId?: (jobId: string) => void;
setLiveLogJobComment?: (comment: string) => void;
jobToClone: CronJob | null;
editingJob: CronJob | null;
editForm: {
schedule: string;
command: string;
comment: string;
logsEnabled: boolean;
};
newCronForm: {
schedule: string;
command: string;
comment: string;
selectedScriptId: string | null;
user: string;
logsEnabled: boolean;
};
}
export const handleErrorClick = (
error: JobError,
setSelectedError: (error: JobError | null) => void,
setErrorModalOpen: (open: boolean) => void
) => {
setSelectedError(error);
setErrorModalOpen(true);
};
export const refreshJobErrors = (
filteredJobs: CronJob[],
getJobErrorsByJobId: (jobId: string) => JobError[],
setJobErrors: (errors: Record<string, JobError[]>) => void
) => {
const errors: Record<string, JobError[]> = {};
filteredJobs.forEach((job) => {
errors[job.id] = getJobErrorsByJobId(job.id);
});
setJobErrors(errors);
};
export const handleDelete = async (job: CronJob, props: HandlerProps) => {
const {
setDeletingId,
setIsDeleteModalOpen,
setJobToDelete,
refreshJobErrors,
} = props;
setDeletingId(job.id);
try {
const result = await removeCronJob({
id: job.id,
schedule: job.schedule,
command: job.command,
comment: job.comment,
user: job.user,
});
if (result.success) {
showToast("success", "Cron job deleted successfully");
} else {
const errorId = `delete-${job.id}-${Date.now()}`;
const jobError: JobError = {
id: errorId,
title: "Failed to delete cron job",
message: result.message,
timestamp: new Date().toISOString(),
jobId: job.id,
};
setJobError(jobError);
refreshJobErrors();
showToast(
"error",
"Failed to delete cron job",
result.message,
undefined,
{
title: jobError.title,
message: jobError.message,
timestamp: jobError.timestamp,
jobId: jobError.jobId,
}
);
}
} catch (error: any) {
const errorId = `delete-${job.id}-${Date.now()}`;
const jobError: JobError = {
id: errorId,
title: "Failed to delete cron job",
message: error.message || "Please try again later.",
details: error.stack,
timestamp: new Date().toISOString(),
jobId: job.id,
};
setJobError(jobError);
showToast(
"error",
"Failed to delete cron job",
"Please try again later.",
undefined,
{
title: jobError.title,
message: jobError.message,
details: jobError.details,
timestamp: jobError.timestamp,
jobId: jobError.jobId,
}
);
} finally {
setDeletingId(null);
setIsDeleteModalOpen(false);
setJobToDelete(null);
}
};
export const handleClone = async (newComment: string, props: HandlerProps) => {
const { jobToClone, setIsCloneModalOpen, setJobToClone, setIsCloning } =
props;
if (!jobToClone) return;
setIsCloning(true);
try {
const result = await cloneCronJob(jobToClone.id, newComment);
if (result.success) {
setIsCloneModalOpen(false);
setJobToClone(null);
showToast("success", "Cron job cloned successfully");
} else {
showToast("error", "Failed to clone cron job", result.message);
}
} finally {
setIsCloning(false);
}
};
export const handlePause = async (job: any) => {
try {
const result = await pauseCronJobAction({
id: job.id,
schedule: job.schedule,
command: job.command,
comment: job.comment,
user: job.user,
});
if (result.success) {
showToast("success", "Cron job paused successfully");
} else {
showToast("error", "Failed to pause cron job", result.message);
}
} catch (error) {
showToast("error", "Failed to pause cron job", "Please try again later.");
}
};
export const handleToggleLogging = async (job: any) => {
try {
const result = await toggleCronJobLogging({
id: job.id,
schedule: job.schedule,
command: job.command,
comment: job.comment,
user: job.user,
logsEnabled: job.logsEnabled,
});
if (result.success) {
showToast("success", result.message);
} else {
showToast("error", "Failed to toggle logging", result.message);
}
} catch (error: any) {
console.error("Error toggling logging:", error);
showToast("error", "Error toggling logging", error.message);
}
};
export const handleResume = async (job: any) => {
try {
const result = await resumeCronJobAction({
id: job.id,
schedule: job.schedule,
command: job.command,
comment: job.comment,
user: job.user,
});
if (result.success) {
showToast("success", "Cron job resumed successfully");
} else {
showToast("error", "Failed to resume cron job", result.message);
}
} catch (error) {
showToast("error", "Failed to resume cron job", "Please try again later.");
}
};
export const handleRun = async (id: string, props: HandlerProps, job: CronJob) => {
const {
setRunningJobId,
refreshJobErrors,
setIsLiveLogModalOpen,
setLiveLogRunId,
setLiveLogJobId,
setLiveLogJobComment,
} = props;
setRunningJobId(id);
try {
const result = await runCronJob(id);
if (result.success) {
if (result.mode === "async" && result.runId) {
if (setIsLiveLogModalOpen && setLiveLogRunId && setLiveLogJobId) {
setLiveLogRunId(result.runId);
setLiveLogJobId(id);
if (setLiveLogJobComment) {
setLiveLogJobComment(job.comment || "");
}
setIsLiveLogModalOpen(true);
}
} else {
showToast("success", "Cron job executed successfully");
}
} else {
const errorId = `run-${id}-${Date.now()}`;
const jobError: JobError = {
id: errorId,
title: "Failed to execute cron job",
message: result.message,
output: result.output,
timestamp: new Date().toISOString(),
jobId: id,
};
setJobError(jobError);
refreshJobErrors();
showToast(
"error",
"Failed to execute cron job",
result.message,
undefined,
{
title: jobError.title,
message: jobError.message,
output: jobError.output,
timestamp: jobError.timestamp,
jobId: jobError.jobId,
}
);
}
} catch (error: any) {
const errorId = `run-${id}-${Date.now()}`;
const jobError: JobError = {
id: errorId,
title: "Failed to execute cron job",
message: error.message || "Please try again later.",
details: error.stack,
timestamp: new Date().toISOString(),
jobId: id,
};
setJobError(jobError);
refreshJobErrors();
showToast(
"error",
"Failed to execute cron job",
"Please try again later.",
undefined,
{
title: jobError.title,
message: jobError.message,
details: jobError.details,
timestamp: jobError.timestamp,
jobId: jobError.jobId,
}
);
} finally {
setRunningJobId(null);
}
};
export const handleEditSubmit = async (
e: React.FormEvent,
props: HandlerProps
) => {
const {
editingJob,
editForm,
setIsEditModalOpen,
setEditingJob,
refreshJobErrors,
} = props;
e.preventDefault();
if (!editingJob) return;
try {
const formData = new FormData();
formData.append("id", editingJob.id);
formData.append("schedule", editForm.schedule);
formData.append("command", editForm.command);
formData.append("comment", editForm.comment);
formData.append("logsEnabled", editForm.logsEnabled.toString());
const result = await editCronJob(formData);
if (result.success) {
setIsEditModalOpen(false);
setEditingJob(null);
showToast("success", "Cron job updated successfully");
} else {
const errorId = `edit-${editingJob.id}-${Date.now()}`;
const jobError: JobError = {
id: errorId,
title: "Failed to update cron job",
message: result.message,
details: result.details,
timestamp: new Date().toISOString(),
jobId: editingJob.id,
};
setJobError(jobError);
refreshJobErrors();
showToast(
"error",
"Failed to update cron job",
result.message,
undefined,
{
title: jobError.title,
message: jobError.message,
details: jobError.details,
timestamp: jobError.timestamp,
jobId: jobError.jobId,
}
);
}
} catch (error: any) {
const errorId = `edit-${editingJob?.id || "unknown"}-${Date.now()}`;
const jobError: JobError = {
id: errorId,
title: "Failed to update cron job",
message: error.message || "Please try again later.",
details: error.stack,
timestamp: new Date().toISOString(),
jobId: editingJob?.id || "unknown",
};
setJobError(jobError);
refreshJobErrors();
showToast(
"error",
"Failed to update cron job",
"Please try again later.",
undefined,
{
title: jobError.title,
message: jobError.message,
details: jobError.details,
timestamp: jobError.timestamp,
jobId: jobError.jobId,
}
);
}
};
export const handleNewCronSubmit = async (
e: React.FormEvent,
props: HandlerProps
) => {
const { newCronForm, setIsNewCronModalOpen, setNewCronForm } = props;
e.preventDefault();
try {
const formData = new FormData();
formData.append("schedule", newCronForm.schedule);
formData.append("command", newCronForm.command);
formData.append("comment", newCronForm.comment);
formData.append("user", newCronForm.user);
formData.append("logsEnabled", newCronForm.logsEnabled.toString());
if (newCronForm.selectedScriptId) {
formData.append("selectedScriptId", newCronForm.selectedScriptId);
}
const result = await createCronJob(formData);
if (result.success) {
setIsNewCronModalOpen(false);
setNewCronForm({
schedule: "",
command: "",
comment: "",
selectedScriptId: null,
user: "",
logsEnabled: false,
});
showToast("success", "Cron job created successfully");
} else {
showToast("error", "Failed to create cron job", result.message);
}
} catch (error) {
showToast("error", "Failed to create cron job", "Please try again later.");
}
};
export const handleBackup = async (job: any) => {
try {
const result = await backupCronJob(job);
if (result.success) {
showToast("success", "Job backed up successfully");
} else {
showToast("error", "Failed to backup job", result.message);
}
} catch (error: any) {
console.error("Error backing up job:", error);
showToast("error", "Error backing up job", error.message);
}
};

View File

@@ -0,0 +1,431 @@
"use client";
import { useEffect, useRef, useState, useCallback } from "react";
import { useTranslations } from "next-intl";
import { ArrowUpIcon, ArrowDownIcon, ArrowLeftIcon, ArrowRightIcon, ArrowClockwiseIcon, PlayIcon, PauseIcon } from "@phosphor-icons/react";
interface Position {
x: number;
y: number;
}
type Direction = "UP" | "DOWN" | "LEFT" | "RIGHT";
const GRID_SIZE = 20;
const INITIAL_SNAKE: Position[] = [
{ x: 10, y: 10 },
{ x: 9, y: 10 },
{ x: 8, y: 10 },
];
const INITIAL_DIRECTION: Direction = "RIGHT";
const GAME_SPEED = 150;
export const SnakeGame = () => {
const t = useTranslations("notFound");
const canvasRef = useRef<HTMLCanvasElement>(null);
const containerRef = useRef<HTMLDivElement>(null);
const [snake, setSnake] = useState<Position[]>(INITIAL_SNAKE);
const [direction, setDirection] = useState<Direction>(INITIAL_DIRECTION);
const [food, setFood] = useState<Position>({ x: 15, y: 15 });
const [gameOver, setGameOver] = useState(false);
const [gameStarted, setGameStarted] = useState(false);
const [score, setScore] = useState(0);
const [highScore, setHighScore] = useState(0);
const [isPaused, setIsPaused] = useState(false);
const [colors, setColors] = useState({ snake: "#00ff00", food: "#ff0000", grid: "#333333" });
const [cellSize, setCellSize] = useState(20);
const directionRef = useRef<Direction>(INITIAL_DIRECTION);
const gameLoopRef = useRef<NodeJS.Timeout | null>(null);
useEffect(() => {
const savedHighScore = localStorage.getItem("snakeHighScore");
if (savedHighScore) {
setHighScore(parseInt(savedHighScore));
}
const updateColors = () => {
const theme = document.documentElement.getAttribute("data-webtui-theme");
if (theme === "catppuccin-mocha") {
setColors({
snake: "#9ca0b0",
food: "#f38ba8",
grid: "#313244",
});
} else {
setColors({
snake: "#313244",
food: "#d20f39",
grid: "#9ca0b0",
});
}
};
updateColors();
const observer = new MutationObserver(updateColors);
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: ["data-webtui-theme"],
});
const updateCellSize = () => {
if (containerRef.current) {
const containerWidth = containerRef.current.offsetWidth;
const maxCanvasSize = Math.min(containerWidth - 32, 400);
const newCellSize = Math.floor(maxCanvasSize / GRID_SIZE);
setCellSize(newCellSize);
}
};
updateCellSize();
window.addEventListener("resize", updateCellSize);
return () => {
observer.disconnect();
window.removeEventListener("resize", updateCellSize);
};
}, []);
const generateFood = useCallback((): Position => {
let newFood: Position;
do {
newFood = {
x: Math.floor(Math.random() * GRID_SIZE),
y: Math.floor(Math.random() * GRID_SIZE),
};
} while (snake.some((segment) => segment.x === newFood.x && segment.y === newFood.y));
return newFood;
}, [snake]);
const resetGame = useCallback(() => {
setSnake(INITIAL_SNAKE);
setDirection(INITIAL_DIRECTION);
directionRef.current = INITIAL_DIRECTION;
setFood(generateFood());
setGameOver(false);
setGameStarted(true);
setScore(0);
setIsPaused(false);
}, [generateFood]);
const draw = useCallback(() => {
const canvas = canvasRef.current;
if (!canvas) return;
const ctx = canvas.getContext("2d");
if (!ctx) return;
const theme = document.documentElement.getAttribute("data-webtui-theme");
const bgColor = theme === "catppuccin-mocha" ? "#1e1e2e" : "#eff1f5";
ctx.fillStyle = bgColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = colors.grid;
ctx.lineWidth = 1;
for (let i = 0; i <= GRID_SIZE; i++) {
ctx.beginPath();
ctx.moveTo(i * cellSize, 0);
ctx.lineTo(i * cellSize, GRID_SIZE * cellSize);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, i * cellSize);
ctx.lineTo(GRID_SIZE * cellSize, i * cellSize);
ctx.stroke();
}
snake.forEach((segment) => {
ctx.fillStyle = colors.snake;
ctx.fillRect(
segment.x * cellSize + 1,
segment.y * cellSize + 1,
cellSize - 2,
cellSize - 2
);
});
ctx.fillStyle = colors.food;
ctx.fillRect(
food.x * cellSize + 1,
food.y * cellSize + 1,
cellSize - 2,
cellSize - 2
);
}, [snake, food, colors, cellSize]);
useEffect(() => {
draw();
}, [draw]);
const moveSnake = useCallback(() => {
if (gameOver || !gameStarted || isPaused) return;
setSnake((prevSnake) => {
const head = prevSnake[0];
const newHead: Position = { ...head };
switch (directionRef.current) {
case "UP":
newHead.y -= 1;
break;
case "DOWN":
newHead.y += 1;
break;
case "LEFT":
newHead.x -= 1;
break;
case "RIGHT":
newHead.x += 1;
break;
}
if (
newHead.x < 0 ||
newHead.x >= GRID_SIZE ||
newHead.y < 0 ||
newHead.y >= GRID_SIZE ||
prevSnake.some((segment) => segment.x === newHead.x && segment.y === newHead.y)
) {
setGameOver(true);
setGameStarted(false);
return prevSnake;
}
const newSnake = [newHead, ...prevSnake];
if (newHead.x === food.x && newHead.y === food.y) {
setScore((prev) => {
const newScore = prev + 10;
if (newScore > highScore) {
setHighScore(newScore);
localStorage.setItem("snakeHighScore", newScore.toString());
}
return newScore;
});
setFood(generateFood());
} else {
newSnake.pop();
}
return newSnake;
});
}, [gameOver, gameStarted, isPaused, food, highScore, generateFood]);
useEffect(() => {
if (gameStarted && !gameOver && !isPaused) {
gameLoopRef.current = setInterval(moveSnake, GAME_SPEED);
}
return () => {
if (gameLoopRef.current) {
clearInterval(gameLoopRef.current);
}
};
}, [gameStarted, gameOver, isPaused, moveSnake]);
useEffect(() => {
const handleKeyPress = (e: KeyboardEvent) => {
if (e.code === "Space") {
e.preventDefault();
if (gameOver) {
resetGame();
} else if (!gameStarted) {
setGameStarted(true);
}
return;
}
if (e.code === "KeyP") {
e.preventDefault();
if (gameStarted && !gameOver) {
setIsPaused((prev) => !prev);
}
return;
}
if (!gameStarted || gameOver || isPaused) return;
let newDirection: Direction | null = null;
switch (e.key) {
case "ArrowUp":
if (directionRef.current !== "DOWN") {
newDirection = "UP";
}
break;
case "ArrowDown":
if (directionRef.current !== "UP") {
newDirection = "DOWN";
}
break;
case "ArrowLeft":
if (directionRef.current !== "RIGHT") {
newDirection = "LEFT";
}
break;
case "ArrowRight":
if (directionRef.current !== "LEFT") {
newDirection = "RIGHT";
}
break;
}
if (newDirection) {
e.preventDefault();
directionRef.current = newDirection;
setDirection(newDirection);
}
};
window.addEventListener("keydown", handleKeyPress);
return () => window.removeEventListener("keydown", handleKeyPress);
}, [gameOver, gameStarted, isPaused, resetGame]);
const handleTouchMove = (dir: Direction) => {
if (!gameStarted) {
setGameStarted(true);
directionRef.current = dir;
setDirection(dir);
return;
}
if (gameOver || isPaused) return;
let canMove = false;
switch (dir) {
case "UP":
canMove = directionRef.current !== "DOWN";
break;
case "DOWN":
canMove = directionRef.current !== "UP";
break;
case "LEFT":
canMove = directionRef.current !== "RIGHT";
break;
case "RIGHT":
canMove = directionRef.current !== "LEFT";
break;
}
if (canMove) {
directionRef.current = dir;
setDirection(dir);
}
};
const handleCanvasClick = () => {
if (gameOver) {
resetGame();
} else if (!gameStarted) {
setGameStarted(true);
}
};
return (
<div ref={containerRef} className="flex flex-col items-center gap-4 w-full">
<div className="tui-card p-4 w-full max-w-md">
<div className="flex justify-between items-center mb-4 terminal-font">
<div className="text-sm">
<span className="text-status-success">{t("score")}:</span> {score}
</div>
<div className="text-sm">
<span className="text-status-info">{t("highScore")}:</span> {highScore}
</div>
</div>
<div className="relative">
<canvas
ref={canvasRef}
width={GRID_SIZE * cellSize}
height={GRID_SIZE * cellSize}
className="ascii-border mx-auto cursor-pointer bg-background0"
onClick={handleCanvasClick}
/>
{!gameStarted && !gameOver && (
<div
className="absolute inset-0 flex items-center justify-center bg-background0 bg-opacity-90 ascii-border cursor-pointer"
onClick={handleCanvasClick}
>
<div className="text-center terminal-font">
<p className="text-lg font-bold uppercase mb-2">{t("pressToStart")}</p>
<p className="text-xs text-foreground0 opacity-70">{t("pauseGame")}</p>
</div>
</div>
)}
{gameOver && (
<div
className="absolute inset-0 flex items-center justify-center bg-background0 bg-opacity-90 ascii-border cursor-pointer"
onClick={handleCanvasClick}
>
<div className="text-center terminal-font">
<p className="text-2xl font-bold uppercase text-status-error mb-2">
{t("gameOver")}
</p>
<p className="text-lg mb-1">
{t("score")}: {score}
</p>
<p className="text-sm text-foreground0 opacity-70">{t("pressToRestart")}</p>
</div>
</div>
)}
{isPaused && (
<div className="absolute inset-0 flex items-center justify-center bg-background0 bg-opacity-90 ascii-border">
<div className="text-center terminal-font">
<p className="text-2xl font-bold uppercase text-status-warning">
{t("paused")}
</p>
</div>
</div>
)}
</div>
<div className="mt-4 grid grid-cols-3 gap-2 md:hidden">
<div></div>
<button
onClick={() => handleTouchMove("UP")}
className="ascii-border bg-background1 p-3 active:bg-background2 terminal-font flex items-center justify-center"
>
<ArrowUpIcon size={20} weight="bold" />
</button>
<div></div>
<button
onClick={() => handleTouchMove("LEFT")}
className="ascii-border bg-background1 p-3 active:bg-background2 terminal-font flex items-center justify-center"
>
<ArrowLeftIcon size={20} weight="bold" />
</button>
<button
onClick={() => (gameStarted && !gameOver ? setIsPaused(!isPaused) : resetGame())}
className="ascii-border bg-background1 p-3 active:bg-background2 terminal-font flex items-center justify-center"
>
{gameOver ? <ArrowClockwiseIcon size={20} weight="bold" /> : isPaused ? <PlayIcon size={20} weight="fill" /> : <PauseIcon size={20} weight="fill" />}
</button>
<button
onClick={() => handleTouchMove("RIGHT")}
className="ascii-border bg-background1 p-3 active:bg-background2 terminal-font flex items-center justify-center"
>
<ArrowRightIcon size={20} weight="bold" />
</button>
<div></div>
<button
onClick={() => handleTouchMove("DOWN")}
className="ascii-border bg-background1 p-3 active:bg-background2 terminal-font flex items-center justify-center"
>
<ArrowDownIcon size={20} weight="bold" />
</button>
<div></div>
</div>
<div className="mt-4 terminal-font text-xs text-center text-foreground0 opacity-70">
<p className="hidden md:block">{t("useArrowKeys")}</p>
<p className="md:hidden">{t("tapToMove")}</p>
</div>
</div>
</div>
);
};

View File

@@ -1,20 +1,20 @@
import { cn } from "@/app/_utils/cn"; import { cn } from "@/app/_utils/global-utils";
import { HTMLAttributes, forwardRef, useState, useEffect } from "react"; import { HTMLAttributes, forwardRef, useState, useEffect } from "react";
import React from "react"; import React from "react";
import { import {
ChevronLeft, CaretLeftIcon,
ChevronRight, CaretRightIcon,
Server, HardDrivesIcon,
Menu, ListIcon,
X, XIcon,
Cpu, CpuIcon,
HardDrive, HardDriveIcon,
Wifi, WifiHighIcon,
} from "lucide-react"; } from "@phosphor-icons/react";
import { useTranslations } from "next-intl";
export interface SidebarProps extends HTMLAttributes<HTMLDivElement> { export interface SidebarProps extends HTMLAttributes<HTMLDivElement> {
children: React.ReactNode; children: React.ReactNode;
title?: string;
defaultCollapsed?: boolean; defaultCollapsed?: boolean;
quickStats?: { quickStats?: {
cpu: number; cpu: number;
@@ -23,18 +23,18 @@ export interface SidebarProps extends HTMLAttributes<HTMLDivElement> {
}; };
} }
const Sidebar = forwardRef<HTMLDivElement, SidebarProps>( export const Sidebar = forwardRef<HTMLDivElement, SidebarProps>(
( (
{ {
className, className,
children, children,
title = "System Overview",
defaultCollapsed = false, defaultCollapsed = false,
quickStats, quickStats,
...props ...props
}, },
ref ref
) => { ) => {
const t = useTranslations();
const [isCollapsed, setIsCollapsed] = useState(defaultCollapsed); const [isCollapsed, setIsCollapsed] = useState(defaultCollapsed);
const [isMobileOpen, setIsMobileOpen] = useState(false); const [isMobileOpen, setIsMobileOpen] = useState(false);
@@ -54,18 +54,18 @@ const Sidebar = forwardRef<HTMLDivElement, SidebarProps>(
<> <>
<button <button
onClick={() => setIsMobileOpen(!isMobileOpen)} onClick={() => setIsMobileOpen(!isMobileOpen)}
className="fixed bottom-4 right-4 z-50 lg:hidden p-2 bg-background/80 backdrop-blur-md border border-border/50 rounded-lg hover:bg-accent transition-colors" className="fixed bottom-4 right-4 z-50 lg:hidden p-2 bg-background0 ascii-border transition-colors terminal-font"
> >
{isMobileOpen ? ( {isMobileOpen ? (
<X className="h-5 w-5" /> <XIcon className="h-5 w-5" />
) : ( ) : (
<Menu className="h-5 w-5" /> <ListIcon className="h-5 w-5" />
)} )}
</button> </button>
<div <div
className={cn( className={cn(
"fixed inset-0 bg-black/50 z-20 lg:hidden transition-opacity duration-300", "fixed inset-0 bg-background0 z-20 lg:hidden transition-opacity duration-300",
isMobileOpen ? "opacity-100" : "opacity-0 pointer-events-none" isMobileOpen ? "opacity-100" : "opacity-0 pointer-events-none"
)} )}
onClick={() => setIsMobileOpen(false)} onClick={() => setIsMobileOpen(false)}
@@ -74,7 +74,7 @@ const Sidebar = forwardRef<HTMLDivElement, SidebarProps>(
<div <div
ref={ref} ref={ref}
className={cn( className={cn(
"bg-background/95 backdrop-blur-md border-r border-border/50 transition-all duration-300 ease-in-out glass-card", "bg-background0 ascii-border transition-all duration-300 ease-in-out terminal-font",
isMobileOpen isMobileOpen
? "fixed left-0 top-0 h-full w-80 z-30 translate-x-0" ? "fixed left-0 top-0 h-full w-80 z-30 translate-x-0"
: "fixed left-0 top-0 h-full w-80 z-30 -translate-x-full lg:translate-x-0", : "fixed left-0 top-0 h-full w-80 z-30 -translate-x-full lg:translate-x-0",
@@ -92,38 +92,20 @@ const Sidebar = forwardRef<HTMLDivElement, SidebarProps>(
> >
<button <button
onClick={() => setIsCollapsed(!isCollapsed)} onClick={() => setIsCollapsed(!isCollapsed)}
className="absolute -right-3 top-6 w-6 h-6 bg-background border border-border rounded-full items-center justify-center hover:bg-accent transition-colors z-40 hidden lg:flex" className="absolute -right-3 top-[21.5vh] w-6 h-6 bg-background0 ascii-border items-center justify-center transition-colors z-40 hidden lg:flex"
> >
{isCollapsed ? ( {isCollapsed ? (
<ChevronRight className="h-3 w-3" /> <CaretRightIcon className="h-3 w-3" />
) : ( ) : (
<ChevronLeft className="h-3 w-3" /> <CaretLeftIcon className="h-3 w-3" />
)} )}
</button> </button>
<div className="p-4 border-b border-border/50 bg-background/95 backdrop-blur-md">
<div
className={cn(
"flex items-center gap-3",
isCollapsed && "lg:justify-center"
)}
>
<div className="p-2 bg-gradient-to-br from-cyan-500/20 to-blue-500/20 rounded-lg flex-shrink-0">
<Server className="h-4 w-4 text-cyan-500" />
</div>
{(!isCollapsed || !isCollapsed) && (
<h2 className="text-sm font-semibold text-foreground truncate">
{title}
</h2>
)}
</div>
</div>
<div <div
className={cn( className={cn(
"overflow-y-auto custom-scrollbar", "overflow-y-auto tui-scrollbar",
isCollapsed ? "lg:p-2" : "p-4", isCollapsed ? "lg:p-2" : "p-4",
"h-full lg:h-[calc(100vh-88px-80px)]" "h-full lg:h-[calc(100vh-88px)]"
)} )}
> >
{isCollapsed ? ( {isCollapsed ? (
@@ -131,22 +113,22 @@ const Sidebar = forwardRef<HTMLDivElement, SidebarProps>(
<div className="flex flex-col items-center space-y-4"> <div className="flex flex-col items-center space-y-4">
{quickStats ? ( {quickStats ? (
<> <>
<div className="w-12 h-12 bg-card/50 border border-border/30 rounded-lg flex flex-col items-center justify-center p-1"> <div className="w-12 h-12 bg-background0 ascii-border flex flex-col items-center justify-center p-1">
<Cpu className="h-3 w-3 text-pink-500 mb-1" /> <CpuIcon className="h-3 w-3 mb-1" />
<span className="text-xs font-bold text-foreground"> <span className="text-xs font-bold text-foreground">
{quickStats.cpu}% {quickStats.cpu}%
</span> </span>
</div> </div>
<div className="w-12 h-12 bg-card/50 border border-border/30 rounded-lg flex flex-col items-center justify-center p-1"> <div className="w-12 h-12 bg-background0 ascii-border flex flex-col items-center justify-center p-1">
<HardDrive className="h-3 w-3 text-cyan-500 mb-1" /> <HardDriveIcon className="h-3 w-3 mb-1" />
<span className="text-xs font-bold text-foreground"> <span className="text-xs font-bold text-foreground">
{quickStats.memory}% {quickStats.memory}%
</span> </span>
</div> </div>
<div className="w-12 h-12 bg-card/50 border border-border/30 rounded-lg flex flex-col items-center justify-center p-1"> <div className="w-12 h-12 bg-background0 ascii-border flex flex-col items-center justify-center p-1">
<Wifi className="h-3 w-3 text-teal-500 mb-1" /> <WifiHighIcon className="h-3 w-3 mb-1" />
<div className="flex flex-col items-center"> <div className="flex flex-col items-center">
<span className="text-xs font-bold text-foreground leading-none"> <span className="text-xs font-bold text-foreground leading-none">
{quickStats.network} {quickStats.network}
@@ -163,9 +145,9 @@ const Sidebar = forwardRef<HTMLDivElement, SidebarProps>(
return ( return (
<div <div
key={index} key={index}
className="w-8 h-8 bg-card/50 border border-border/30 rounded-lg flex items-center justify-center" className="w-8 h-8 bg-background2 ascii-border flex items-center justify-center"
> >
<Server className="h-4 w-4 text-muted-foreground" /> <HardDrivesIcon className="h-4 w-4 text-muted-foreground" />
</div> </div>
); );
} }
@@ -185,5 +167,3 @@ const Sidebar = forwardRef<HTMLDivElement, SidebarProps>(
); );
Sidebar.displayName = "Sidebar"; Sidebar.displayName = "Sidebar";
export { Sidebar };

View File

@@ -0,0 +1,67 @@
"use client";
import { useState } from "react";
import { CronJobList } from "@/app/_components/FeatureComponents/Cronjobs/CronJobList";
import { ScriptsManager } from "@/app/_components/FeatureComponents/Scripts/ScriptsManager";
import { CronJob } from "@/app/_utils/cronjob-utils";
import { Script } from "@/app/_utils/scripts-utils";
import { ClockIcon, FileTextIcon } from "@phosphor-icons/react";
import { useTranslations } from "next-intl";
interface TabbedInterfaceProps {
cronJobs: CronJob[];
scripts: Script[];
}
export const TabbedInterface = ({
cronJobs,
scripts,
}: TabbedInterfaceProps) => {
const [activeTab, setActiveTab] = useState<"cronjobs" | "scripts">(
"cronjobs"
);
const t = useTranslations();
return (
<div className="space-y-6">
<div className="tui-card p-1 terminal-font">
<div className="flex gap-2">
<button
onClick={() => setActiveTab("cronjobs")}
className={`flex items-center gap-2 px-4 py-2 border border-transparent text-sm font-medium flex-1 justify-center terminal-font ${activeTab === "cronjobs"
? "bg-background0 ascii-border"
: "hover:ascii-border"
}`}
>
<ClockIcon className="h-4 w-4" />
{t("cronjobs.cronJobs")}
<span className="ml-1 text-xs bg-background0 px-2 py-0.5 ascii-border font-medium">
{cronJobs.length}
</span>
</button>
<button
onClick={() => setActiveTab("scripts")}
className={`flex items-center gap-2 px-4 py-2 border border-transparent text-sm font-medium flex-1 justify-center terminal-font ${activeTab === "scripts"
? "bg-background0 ascii-border"
: "hover:ascii-border"
}`}
>
<FileTextIcon className="h-4 w-4" />
{t("scripts.scripts")}
<span className="ml-1 text-xs bg-background0 px-2 py-0.5 ascii-border font-medium">
{scripts.length}
</span>
</button>
</div>
</div>
<div className="min-h-[60vh]">
{activeTab === "cronjobs" ? (
<CronJobList cronJobs={cronJobs} scripts={scripts} />
) : (
<ScriptsManager scripts={scripts} />
)}
</div>
</div>
);
};

View File

@@ -0,0 +1,215 @@
"use client";
import { useState, useEffect } from "react";
import { useRouter, useSearchParams } from "next/navigation";
import { useTranslations } from "next-intl";
import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { Input } from "@/app/_components/GlobalComponents/FormElements/Input";
import {
Card,
CardHeader,
CardTitle,
CardDescription,
CardContent,
} from "@/app/_components/GlobalComponents/Cards/Card";
import { LockIcon, EyeIcon, EyeSlashIcon, ShieldIcon, WarningIcon, CircleNotchIcon } from "@phosphor-icons/react";
interface LoginFormProps {
hasPassword?: boolean;
hasOIDC?: boolean;
oidcAutoRedirect?: boolean;
version?: string;
}
export const LoginForm = ({
hasPassword = false,
hasOIDC = false,
oidcAutoRedirect = false,
version,
}: LoginFormProps) => {
const [password, setPassword] = useState("");
const [showPassword, setShowPassword] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState("");
const [isRedirecting, setIsRedirecting] = useState(false);
const router = useRouter();
const searchParams = useSearchParams();
const t = useTranslations();
useEffect(() => {
const errorParam = searchParams.get("error");
if (errorParam) {
setError(decodeURIComponent(errorParam));
return;
}
if (oidcAutoRedirect && !hasPassword && hasOIDC) {
setIsRedirecting(true);
window.location.href = "/api/oidc/login";
}
}, [oidcAutoRedirect, hasPassword, hasOIDC, searchParams]);
const handlePasswordSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setIsLoading(true);
setError("");
try {
const response = await fetch("/api/auth/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ password }),
});
const result = await response.json();
if (result.success) {
router.push("/");
} else {
setError(result.message || t("login.loginFailed"));
}
} catch (error) {
setError(t("login.genericError"));
} finally {
setIsLoading(false);
}
};
const handleOIDCLogin = () => {
setIsLoading(true);
window.location.href = "/api/oidc/login";
};
if (isRedirecting) {
return (
<Card className="w-full max-w-md shadow-xl">
<CardContent className="pt-6">
<div className="flex flex-col items-center justify-center py-8 space-y-4">
<CircleNotchIcon className="w-12 h-12 text-primary animate-spin" />
<div className="text-center">
<p className="text-lg font-medium">{t("login.redirectingToOIDC")}</p>
<p className="text-sm text-muted-foreground mt-1">
{t("login.pleaseWait")}
</p>
</div>
</div>
</CardContent>
</Card>
);
}
return (
<Card className="w-full max-w-md shadow-xl">
<CardHeader className="text-center">
<div className="mx-auto w-16 h-16 bg-primary/10 rounded-full flex items-center justify-center mb-4">
<LockIcon className="w-8 h-8 text-primary" />
</div>
<CardTitle>{t("login.welcomeTitle")}</CardTitle>
<CardDescription>
{hasPassword && hasOIDC
? t("login.signInWithPasswordOrSSO")
: hasOIDC
? t("login.signInWithSSO")
: t("login.enterPasswordToContinue")}
</CardDescription>
</CardHeader>
<CardContent>
{!hasPassword && !hasOIDC && (
<div className="mb-4 p-3 bg-amber-500/10 border border-amber-500/20 rounded-md">
<div className="flex items-start space-x-2">
<WarningIcon className="w-4 h-4 text-amber-500 mt-0.5 flex-shrink-0" />
<div className="text-sm text-amber-700 dark:text-amber-400">
<div className="font-medium">
{t("login.authenticationNotConfigured")}
</div>
<div className="mt-1">{t("login.noAuthMethodsEnabled")}</div>
</div>
</div>
</div>
)}
<div className="space-y-4">
{hasPassword && (
<form onSubmit={handlePasswordSubmit} className="space-y-4">
<div className="relative">
<Input
type={showPassword ? "text" : "password"}
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder={t("login.enterPassword")}
className="pr-10"
required
disabled={isLoading}
/>
<button
type="button"
onClick={() => setShowPassword(!showPassword)}
className="absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground transition-colors"
disabled={isLoading}
>
{showPassword ? (
<EyeSlashIcon className="w-4 h-4" />
) : (
<EyeIcon className="w-4 h-4" />
)}
</button>
</div>
<Button
type="submit"
className="w-full"
disabled={isLoading || !password.trim()}
>
{isLoading ? t("login.signingIn") : t("login.signIn")}
</Button>
</form>
)}
{hasPassword && hasOIDC && (
<div className="relative">
<div className="absolute inset-0 flex items-center">
<span className="w-full border-t" />
</div>
<div className="relative flex justify-center text-xs uppercase">
<span className="bg-background0 px-2 text-muted-foreground">
{t("login.orContinueWith")}
</span>
</div>
</div>
)}
{hasOIDC && (
<Button
type="button"
variant="outline"
className="w-full"
onClick={handleOIDCLogin}
disabled={isLoading}
>
<ShieldIcon className="w-4 h-4 mr-2" />
{isLoading ? t("login.redirecting") : t("login.signInWithSSO")}
</Button>
)}
{error && (
<div className="text-sm text-red-500 bg-red-500/10 border border-red-500/20 rounded-md p-3">
{error}
</div>
)}
</div>
{version && (
<div className="mt-6 pt-4 border-t border-border">
<div className="text-center text-xs text-muted-foreground">
Cr*nMaster {t("common.version", { version })}
</div>
</div>
)}
</CardContent>
</Card>
);
};

View File

@@ -0,0 +1,42 @@
"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { SignOutIcon } from "@phosphor-icons/react";
export const LogoutButton = () => {
const [isLoading, setIsLoading] = useState(false);
const router = useRouter();
const handleLogout = async () => {
setIsLoading(true);
try {
const response = await fetch("/api/auth/logout", {
method: "POST",
});
if (response.ok) {
router.push("/login");
router.refresh();
}
} catch (error) {
console.error("Logout error:", error);
} finally {
setIsLoading(false);
}
};
return (
<Button
variant="ghost"
size="icon"
onClick={handleLogout}
disabled={isLoading}
title="Logout"
>
<SignOutIcon className="h-[1.2rem] w-[1.2rem]" />
<span className="sr-only">Logout</span>
</Button>
);
};

View File

@@ -1,11 +1,11 @@
"use client"; "use client";
import { useState } from "react"; import { useState } from "react";
import { Copy, FileText } from "lucide-react"; import { CopyIcon } from "@phosphor-icons/react";
import { Button } from "../ui/Button"; import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { Modal } from "../ui/Modal"; import { Modal } from "@/app/_components/GlobalComponents/UIElements/Modal";
import { Input } from "../ui/Input"; import { Input } from "@/app/_components/GlobalComponents/FormElements/Input";
import { type Script } from "@/app/_server/actions/scripts"; import { Script } from "@/app/_utils/scripts-utils";
interface CloneScriptModalProps { interface CloneScriptModalProps {
script: Script | null; script: Script | null;
@@ -15,18 +15,18 @@ interface CloneScriptModalProps {
isCloning: boolean; isCloning: boolean;
} }
export function CloneScriptModal({ export const CloneScriptModal = ({
script, script,
isOpen, isOpen,
onClose, onClose,
onConfirm, onConfirm,
isCloning, isCloning,
}: CloneScriptModalProps) { }: CloneScriptModalProps) => {
const [newName, setNewName] = useState(""); const [newName, setNewName] = useState("");
if (!isOpen || !script) return null; if (!isOpen || !script) return null;
const handleSubmit = (e: React.FormEvent) => { const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault(); e.preventDefault();
if (newName.trim()) { if (newName.trim()) {
onConfirm(newName.trim()); onConfirm(newName.trim());
@@ -89,7 +89,7 @@ export function CloneScriptModal({
</> </>
) : ( ) : (
<> <>
<Copy className="h-4 w-4 mr-2" /> <CopyIcon className="h-4 w-4 mr-2" />
Clone Script Clone Script
</> </>
)} )}

View File

@@ -1,11 +1,11 @@
"use client"; "use client";
import { useState } from "react"; import { useState } from "react";
import { Copy } from "lucide-react"; import { CopyIcon } from "@phosphor-icons/react";
import { Button } from "../ui/Button"; import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { Modal } from "../ui/Modal"; import { Modal } from "@/app/_components/GlobalComponents/UIElements/Modal";
import { Input } from "../ui/Input"; import { Input } from "@/app/_components/GlobalComponents/FormElements/Input";
import { type CronJob } from "@/app/_utils/system"; import { type CronJob } from "@/app/_utils/cronjob-utils";
interface CloneTaskModalProps { interface CloneTaskModalProps {
cronJob: CronJob | null; cronJob: CronJob | null;
@@ -15,18 +15,18 @@ interface CloneTaskModalProps {
isCloning: boolean; isCloning: boolean;
} }
export function CloneTaskModal({ export const CloneTaskModal = ({
cronJob, cronJob,
isOpen, isOpen,
onClose, onClose,
onConfirm, onConfirm,
isCloning, isCloning,
}: CloneTaskModalProps) { }: CloneTaskModalProps) => {
const [newComment, setNewComment] = useState(""); const [newComment, setNewComment] = useState("");
if (!isOpen || !cronJob) return null; if (!isOpen || !cronJob) return null;
const handleSubmit = (e: React.FormEvent) => { const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault(); e.preventDefault();
if (newComment.trim()) { if (newComment.trim()) {
onConfirm(newComment.trim()); onConfirm(newComment.trim());
@@ -89,7 +89,7 @@ export function CloneTaskModal({
</> </>
) : ( ) : (
<> <>
<Copy className="h-4 w-4 mr-2" /> <CopyIcon className="h-4 w-4 mr-2" />
Clone Cron Job Clone Cron Job
</> </>
)} )}

View File

@@ -1,7 +1,7 @@
"use client"; "use client";
import { Plus } from "lucide-react"; import { PlusIcon } from "@phosphor-icons/react";
import { ScriptModal } from "./ScriptModal"; import { ScriptModal } from "@/app/_components/FeatureComponents/Modals/ScriptModal";
interface CreateScriptModalProps { interface CreateScriptModalProps {
isOpen: boolean; isOpen: boolean;
@@ -15,15 +15,19 @@ interface CreateScriptModalProps {
content: string; content: string;
}; };
onFormChange: (updates: Partial<CreateScriptModalProps["form"]>) => void; onFormChange: (updates: Partial<CreateScriptModalProps["form"]>) => void;
isDraft?: boolean;
onClearDraft?: () => void;
} }
export function CreateScriptModal({ export const CreateScriptModal = ({
isOpen, isOpen,
onClose, onClose,
onSubmit, onSubmit,
form, form,
onFormChange, onFormChange,
}: CreateScriptModalProps) { isDraft,
onClearDraft,
}: CreateScriptModalProps) => {
return ( return (
<ScriptModal <ScriptModal
isOpen={isOpen} isOpen={isOpen}
@@ -31,9 +35,11 @@ export function CreateScriptModal({
onSubmit={onSubmit} onSubmit={onSubmit}
title="Create New Script" title="Create New Script"
submitButtonText="Create Script" submitButtonText="Create Script"
submitButtonIcon={<Plus className="h-4 w-4 mr-2" />} submitButtonIcon={<PlusIcon className="h-4 w-4 mr-2" />}
form={form} form={form}
onFormChange={onFormChange} onFormChange={onFormChange}
isDraft={isDraft}
onClearDraft={onClearDraft}
/> />
); );
} }

View File

@@ -1,14 +1,17 @@
"use client"; "use client";
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { Modal } from "../ui/Modal"; import { Modal } from "@/app/_components/GlobalComponents/UIElements/Modal";
import { Button } from "../ui/Button"; import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { Input } from "../ui/Input"; import { Input } from "@/app/_components/GlobalComponents/FormElements/Input";
import { CronExpressionHelper } from "../CronExpressionHelper"; import { Switch } from "@/app/_components/GlobalComponents/UIElements/Switch";
import { SelectScriptModal } from "./SelectScriptModal"; import { CronExpressionHelper } from "@/app/_components/FeatureComponents/Scripts/CronExpressionHelper";
import { Plus, Terminal, FileText, X } from "lucide-react"; import { SelectScriptModal } from "@/app/_components/FeatureComponents/Modals/SelectScriptModal";
import { UserSwitcher } from "@/app/_components/FeatureComponents/User/UserSwitcher";
import { PlusIcon, TerminalIcon, FileTextIcon, XIcon, FileArrowDownIcon } from "@phosphor-icons/react";
import { getScriptContent } from "@/app/_server/actions/scripts"; import { getScriptContent } from "@/app/_server/actions/scripts";
import { getHostScriptPath } from "@/app/_utils/scripts"; import { getHostScriptPath } from "@/app/_server/actions/scripts";
import { useTranslations } from "next-intl";
interface Script { interface Script {
id: string; id: string;
@@ -21,29 +24,32 @@ interface Script {
interface CreateTaskModalProps { interface CreateTaskModalProps {
isOpen: boolean; isOpen: boolean;
onClose: () => void; onClose: () => void;
onSubmit: (e: React.FormEvent) => void; onSubmit: (e: React.FormEvent<HTMLFormElement>) => void;
scripts: Script[]; scripts: Script[];
form: { form: {
schedule: string; schedule: string;
command: string; command: string;
comment: string; comment: string;
selectedScriptId: string | null; selectedScriptId: string | null;
user: string;
logsEnabled: boolean;
}; };
onFormChange: (updates: Partial<CreateTaskModalProps["form"]>) => void; onFormChange: (updates: Partial<CreateTaskModalProps["form"]>) => void;
} }
export function CreateTaskModal({ export const CreateTaskModal = ({
isOpen, isOpen,
onClose, onClose,
onSubmit, onSubmit,
scripts, scripts,
form, form,
onFormChange, onFormChange,
}: CreateTaskModalProps) { }: CreateTaskModalProps) => {
const [selectedScriptContent, setSelectedScriptContent] = const [selectedScriptContent, setSelectedScriptContent] =
useState<string>(""); useState<string>("");
const [isSelectScriptModalOpen, setIsSelectScriptModalOpen] = useState(false); const [isSelectScriptModalOpen, setIsSelectScriptModalOpen] = useState(false);
const selectedScript = scripts.find((s) => s.id === form.selectedScriptId); const selectedScript = scripts.find((s) => s.id === form.selectedScriptId);
const t = useTranslations();
useEffect(() => { useEffect(() => {
const loadScriptContent = async () => { const loadScriptContent = async () => {
@@ -58,10 +64,11 @@ export function CreateTaskModal({
loadScriptContent(); loadScriptContent();
}, [selectedScript]); }, [selectedScript]);
const handleScriptSelect = (script: Script) => { const handleScriptSelect = async (script: Script) => {
const scriptPath = await getHostScriptPath(script.filename);
onFormChange({ onFormChange({
selectedScriptId: script.id, selectedScriptId: script.id,
command: getHostScriptPath(script.filename), command: scriptPath,
}); });
}; };
@@ -84,13 +91,23 @@ export function CreateTaskModal({
<Modal <Modal
isOpen={isOpen} isOpen={isOpen}
onClose={onClose} onClose={onClose}
title="Create New Scheduled Task" title={t("cronjobs.createNewScheduledTask")}
size="lg" size="lg"
> >
<form onSubmit={onSubmit} className="space-y-4"> <form onSubmit={onSubmit} className="space-y-4">
<div> <div>
<label className="block text-sm font-medium text-foreground mb-1"> <label className="block text-sm font-medium text-foreground mb-1">
Schedule {t("common.user")}
</label>
<UserSwitcher
selectedUser={form.user}
onUserChange={(user: string) => onFormChange({ user })}
/>
</div>
<div>
<label className="block text-sm font-medium text-foreground mb-1">
{t("cronjobs.schedule")}
</label> </label>
<CronExpressionHelper <CronExpressionHelper
value={form.schedule} value={form.schedule}
@@ -102,23 +119,26 @@ export function CreateTaskModal({
<div> <div>
<label className="block text-sm font-medium text-foreground mb-2"> <label className="block text-sm font-medium text-foreground mb-2">
Task Type {t("cronjobs.taskType")}
</label> </label>
<div className="grid grid-cols-2 gap-3"> <div className="grid grid-cols-2 gap-3">
<button <button
type="button" type="button"
onClick={handleCustomCommand} onClick={handleCustomCommand}
className={`p-4 rounded-lg border-2 transition-all ${ className={`p-4 rounded-lg transition-all ${!form.selectedScriptId
!form.selectedScriptId ? "border-border border-2"
? "border-primary bg-primary/5 text-primary" : "border-border border"
: "border-border bg-muted/30 text-muted-foreground hover:border-border/60" }`}
}`}
> >
<div className="flex items-center gap-3"> <div className="flex items-center gap-3">
<Terminal className="h-5 w-5" /> <TerminalIcon className="h-5 w-5" />
<div className="text-left"> <div className="text-left">
<div className="font-medium">Custom Command</div> <div className="font-medium">
<div className="text-xs opacity-70">Single command</div> {t("cronjobs.customCommand")}
</div>
<div className="text-xs opacity-70">
{t("cronjobs.singleCommand")}
</div>
</div> </div>
</div> </div>
</button> </button>
@@ -126,18 +146,19 @@ export function CreateTaskModal({
<button <button
type="button" type="button"
onClick={() => setIsSelectScriptModalOpen(true)} onClick={() => setIsSelectScriptModalOpen(true)}
className={`p-4 rounded-lg border-2 transition-all ${ className={`p-4 rounded-lg transition-all ${form.selectedScriptId
form.selectedScriptId ? "border-border border-2"
? "border-primary bg-primary/5 text-primary" : "border-border border"
: "border-border bg-muted/30 text-muted-foreground hover:border-border/60" }`}
}`}
> >
<div className="flex items-center gap-3"> <div className="flex items-center gap-3">
<FileText className="h-5 w-5" /> <FileTextIcon className="h-5 w-5" />
<div className="text-left"> <div className="text-left">
<div className="font-medium">Saved Script</div> <div className="font-medium">
{t("scripts.savedScript")}
</div>
<div className="text-xs opacity-70"> <div className="text-xs opacity-70">
Select from library {t("scripts.selectFromLibrary")}
</div> </div>
</div> </div>
</div> </div>
@@ -150,7 +171,7 @@ export function CreateTaskModal({
<div className="flex items-start justify-between"> <div className="flex items-start justify-between">
<div className="flex-1"> <div className="flex-1">
<div className="flex items-center gap-2 mb-1"> <div className="flex items-center gap-2 mb-1">
<FileText className="h-4 w-4 text-primary" /> <FileTextIcon className="h-4 w-4 text-primary" />
<h4 className="font-medium text-foreground"> <h4 className="font-medium text-foreground">
{selectedScript.name} {selectedScript.name}
</h4> </h4>
@@ -158,7 +179,7 @@ export function CreateTaskModal({
<p className="text-sm text-muted-foreground mb-2"> <p className="text-sm text-muted-foreground mb-2">
{selectedScript.description} {selectedScript.description}
</p> </p>
<div className="bg-muted/30 p-2 rounded border border-border/30"> <div className="bg-muted/30 p-2 rounded border border-border">
<code className="text-xs font-mono text-foreground break-all"> <code className="text-xs font-mono text-foreground break-all">
{form.command} {form.command}
</code> </code>
@@ -172,7 +193,7 @@ export function CreateTaskModal({
onClick={() => setIsSelectScriptModalOpen(true)} onClick={() => setIsSelectScriptModalOpen(true)}
className="h-8 px-2 text-xs" className="h-8 px-2 text-xs"
> >
Change {t("common.change")}
</Button> </Button>
<Button <Button
type="button" type="button"
@@ -181,7 +202,7 @@ export function CreateTaskModal({
onClick={handleClearScript} onClick={handleClearScript}
className="h-8 w-8 p-0 text-muted-foreground hover:text-foreground" className="h-8 w-8 p-0 text-muted-foreground hover:text-foreground"
> >
<X className="h-4 w-4" /> <XIcon className="h-4 w-4" />
</Button> </Button>
</div> </div>
</div> </div>
@@ -191,7 +212,7 @@ export function CreateTaskModal({
{!form.selectedScriptId && !selectedScript && ( {!form.selectedScriptId && !selectedScript && (
<div> <div>
<label className="block text-sm font-medium text-foreground mb-1"> <label className="block text-sm font-medium text-foreground mb-1">
Command {t("cronjobs.command")}
</label> </label>
<div className="relative"> <div className="relative">
<textarea <textarea
@@ -202,18 +223,17 @@ export function CreateTaskModal({
? "/app/scripts/script_name.sh" ? "/app/scripts/script_name.sh"
: "/usr/bin/command" : "/usr/bin/command"
} }
className="w-full h-24 p-2 border border-border rounded bg-background text-foreground font-mono text-sm resize-none focus:outline-none focus:ring-1 focus:ring-primary/20" className="w-full h-24 p-2 border border-border rounded bg-background0 text-foreground font-mono text-sm resize-none focus:outline-none focus:ring-1 focus:ring-primary/20"
required required
readOnly={!!form.selectedScriptId} readOnly={!!form.selectedScriptId}
/> />
<div className="absolute right-3 top-2"> <div className="absolute right-3 top-2">
<Terminal className="h-4 w-4 text-muted-foreground" /> <TerminalIcon className="h-4 w-4 text-muted-foreground" />
</div> </div>
</div> </div>
{form.selectedScriptId && ( {form.selectedScriptId && (
<p className="text-xs text-muted-foreground mt-1"> <p className="text-xs text-muted-foreground mt-1">
Script path is read-only. Edit the script in the Scripts {t("scripts.scriptPathReadOnly")}
Library.
</p> </p>
)} )}
</div> </div>
@@ -221,29 +241,57 @@ export function CreateTaskModal({
<div> <div>
<label className="block text-sm font-medium text-foreground mb-1"> <label className="block text-sm font-medium text-foreground mb-1">
Description{" "} {t("common.description")}
<span className="text-muted-foreground">(Optional)</span> <span className="text-muted-foreground">
({t("common.optional")})
</span>
</label> </label>
<Input <Input
value={form.comment} value={form.comment}
onChange={(e) => onFormChange({ comment: e.target.value })} onChange={(e) => onFormChange({ comment: e.target.value })}
placeholder="What does this task do?" placeholder={t("cronjobs.whatDoesThisTaskDo")}
className="bg-muted/30 border-border/50 focus:border-primary/50" className="bg-muted/30 border-border focus:border-primary/50"
/> />
</div> </div>
<div className="flex justify-end gap-2 pt-3 border-t border-border/50">
<div className="border border-border bg-muted/10 rounded-lg p-4">
<div
className="flex items-start gap-3 cursor-pointer"
onClick={() => onFormChange({ logsEnabled: !form.logsEnabled })}
>
<Switch
checked={form.logsEnabled}
onCheckedChange={(checked) =>
onFormChange({ logsEnabled: checked })
}
className="mt-1"
/>
<div className="flex-1">
<div className="flex items-center gap-2 text-sm font-medium text-foreground">
<FileArrowDownIcon className="h-4 w-4 text-primary" />
{t("cronjobs.enableLogging")}
</div>
<p className="text-xs text-muted-foreground mt-1">
{t("cronjobs.loggingDescription")}
</p>
</div>
</div>
</div>
<div className="flex justify-end gap-2 pt-3 border-t border-border">
<Button <Button
type="button" type="button"
variant="outline" variant="outline"
onClick={onClose} onClick={onClose}
className="btn-outline" className="btn-outline"
> >
Cancel {t("common.cancel")}
</Button> </Button>
<Button type="submit" className="btn-primary glow-primary"> <Button type="submit" className="btn-primary glow-primary">
<Plus className="h-4 w-4 mr-2" /> <PlusIcon className="h-4 w-4 mr-2" />
Create Task {t("cronjobs.createTask")}
</Button> </Button>
</div> </div>
</form> </form>
@@ -258,4 +306,4 @@ export function CreateTaskModal({
/> />
</> </>
); );
} };

View File

@@ -0,0 +1,121 @@
"use client";
import { CreateTaskModal } from "@/app/_components/FeatureComponents/Modals/CreateTaskModal";
import { EditTaskModal } from "@/app/_components/FeatureComponents/Modals/EditTaskModal";
import { DeleteTaskModal } from "@/app/_components/FeatureComponents/Modals/DeleteTaskModal";
import { CloneTaskModal } from "@/app/_components/FeatureComponents/Modals/CloneTaskModal";
import { ErrorDetailsModal } from "@/app/_components/FeatureComponents/Modals/ErrorDetailsModal";
import { CronJob } from "@/app/_utils/cronjob-utils";
import { Script } from "@/app/_utils/scripts-utils";
import { JobError } from "@/app/_utils/error-utils";
interface CronJobListModalsProps {
cronJobs: CronJob[];
scripts: Script[];
isNewCronModalOpen: boolean;
onNewCronModalClose: () => void;
onNewCronSubmit: (e: React.FormEvent) => Promise<void>;
newCronForm: any;
onNewCronFormChange: (updates: any) => void;
isEditModalOpen: boolean;
onEditModalClose: () => void;
onEditSubmit: (e: React.FormEvent) => Promise<void>;
editForm: any;
onEditFormChange: (updates: any) => void;
isDeleteModalOpen: boolean;
onDeleteModalClose: () => void;
onDeleteConfirm: () => void;
jobToDelete: CronJob | null;
isCloneModalOpen: boolean;
onCloneModalClose: () => void;
onCloneConfirm: (newComment: string) => Promise<void>;
jobToClone: CronJob | null;
isCloning: boolean;
isErrorModalOpen: boolean;
onErrorModalClose: () => void;
selectedError: JobError | null;
}
export const CronJobListModals = ({
scripts,
isNewCronModalOpen,
onNewCronModalClose,
onNewCronSubmit,
newCronForm,
onNewCronFormChange,
isEditModalOpen,
onEditModalClose,
onEditSubmit,
editForm,
onEditFormChange,
isDeleteModalOpen,
onDeleteModalClose,
onDeleteConfirm,
jobToDelete,
isCloneModalOpen,
onCloneModalClose,
onCloneConfirm,
jobToClone,
isCloning,
isErrorModalOpen,
onErrorModalClose,
selectedError,
}: CronJobListModalsProps) => {
return (
<>
<CreateTaskModal
isOpen={isNewCronModalOpen}
onClose={onNewCronModalClose}
onSubmit={onNewCronSubmit}
scripts={scripts}
form={newCronForm}
onFormChange={onNewCronFormChange}
/>
<EditTaskModal
isOpen={isEditModalOpen}
onClose={onEditModalClose}
onSubmit={onEditSubmit}
form={editForm}
onFormChange={onEditFormChange}
/>
<DeleteTaskModal
isOpen={isDeleteModalOpen}
onClose={onDeleteModalClose}
onConfirm={onDeleteConfirm}
job={jobToDelete}
/>
<CloneTaskModal
cronJob={jobToClone}
isOpen={isCloneModalOpen}
onClose={onCloneModalClose}
onConfirm={onCloneConfirm}
isCloning={isCloning}
/>
{isErrorModalOpen && selectedError && (
<ErrorDetailsModal
isOpen={isErrorModalOpen}
onClose={onErrorModalClose}
error={{
title: selectedError.title,
message: selectedError.message,
details: selectedError.details,
command: selectedError.command,
output: selectedError.output,
stderr: selectedError.stderr,
timestamp: selectedError.timestamp,
jobId: selectedError.jobId,
}}
/>
)}
</>
);
};

View File

@@ -1,9 +1,9 @@
"use client"; "use client";
import { Modal } from "../ui/Modal"; import { Modal } from "@/app/_components/GlobalComponents/UIElements/Modal";
import { Button } from "../ui/Button"; import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { FileText, AlertCircle, Trash2 } from "lucide-react"; import { FileTextIcon, WarningCircleIcon, TrashIcon } from "@phosphor-icons/react";
import { type Script } from "@/app/_server/actions/scripts"; import { Script } from "@/app/_utils/scripts-utils";
interface DeleteScriptModalProps { interface DeleteScriptModalProps {
script: Script | null; script: Script | null;
@@ -13,22 +13,22 @@ interface DeleteScriptModalProps {
isDeleting: boolean; isDeleting: boolean;
} }
export function DeleteScriptModal({ export const DeleteScriptModal = ({
script, script,
isOpen, isOpen,
onClose, onClose,
onConfirm, onConfirm,
isDeleting, isDeleting,
}: DeleteScriptModalProps) { }: DeleteScriptModalProps) => {
if (!isOpen || !script) return null; if (!isOpen || !script) return null;
return ( return (
<Modal isOpen={isOpen} onClose={onClose} title="Delete Script" size="sm"> <Modal isOpen={isOpen} onClose={onClose} title="Delete Script" size="sm">
<div className="space-y-3"> <div className="space-y-3">
<div className="bg-muted/30 rounded p-2 border border-border/50"> <div className="bg-muted/30 rounded p-2 border border-border">
<div className="space-y-1"> <div className="space-y-1">
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<FileText className="h-3 w-3 text-muted-foreground" /> <FileTextIcon className="h-3 w-3 text-muted-foreground" />
<span className="text-xs font-medium text-foreground"> <span className="text-xs font-medium text-foreground">
{script.name} {script.name}
</span> </span>
@@ -36,7 +36,7 @@ export function DeleteScriptModal({
{script.description && ( {script.description && (
<div className="flex items-start gap-2"> <div className="flex items-start gap-2">
<FileText className="h-3 w-3 text-muted-foreground mt-0.5 flex-shrink-0" /> <FileTextIcon className="h-3 w-3 text-muted-foreground mt-0.5 flex-shrink-0" />
<p className="text-xs text-muted-foreground break-words italic"> <p className="text-xs text-muted-foreground break-words italic">
{script.description} {script.description}
</p> </p>
@@ -44,8 +44,8 @@ export function DeleteScriptModal({
)} )}
<div className="flex items-start gap-2"> <div className="flex items-start gap-2">
<FileText className="h-3 w-3 text-muted-foreground mt-0.5 flex-shrink-0" /> <FileTextIcon className="h-3 w-3 text-muted-foreground mt-0.5 flex-shrink-0" />
<code className="text-xs font-mono bg-muted/30 px-1 py-0.5 rounded border border-border/30"> <code className="text-xs font-mono bg-muted/30 px-1 py-0.5 rounded border border-border">
{script.filename} {script.filename}
</code> </code>
</div> </div>
@@ -54,7 +54,7 @@ export function DeleteScriptModal({
<div className="bg-destructive/5 border border-destructive/20 rounded p-2"> <div className="bg-destructive/5 border border-destructive/20 rounded p-2">
<div className="flex items-start gap-2"> <div className="flex items-start gap-2">
<AlertCircle className="h-4 w-4 text-destructive mt-0.5 flex-shrink-0" /> <WarningCircleIcon className="h-4 w-4 text-destructive mt-0.5 flex-shrink-0" />
<div> <div>
<p className="text-xs font-medium text-destructive mb-0.5"> <p className="text-xs font-medium text-destructive mb-0.5">
This action cannot be undone This action cannot be undone
@@ -66,7 +66,7 @@ export function DeleteScriptModal({
</div> </div>
</div> </div>
<div className="flex justify-end gap-2 pt-2 border-t border-border/50"> <div className="flex justify-end gap-2 pt-2 border-t border-border">
<Button <Button
variant="outline" variant="outline"
onClick={onClose} onClick={onClose}
@@ -78,7 +78,6 @@ export function DeleteScriptModal({
<Button <Button
variant="destructive" variant="destructive"
onClick={onConfirm} onClick={onConfirm}
className="btn-destructive"
disabled={isDeleting} disabled={isDeleting}
> >
{isDeleting ? ( {isDeleting ? (
@@ -88,7 +87,7 @@ export function DeleteScriptModal({
</> </>
) : ( ) : (
<> <>
<Trash2 className="h-4 w-4 mr-2" /> <TrashIcon className="h-4 w-4 mr-2" />
Delete Script Delete Script
</> </>
)} )}

View File

@@ -1,15 +1,15 @@
"use client"; "use client";
import { Modal } from "../ui/Modal"; import { Modal } from "@/app/_components/GlobalComponents/UIElements/Modal";
import { Button } from "../ui/Button"; import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { import {
Calendar, Calendar,
Terminal, TerminalIcon,
MessageSquare, ChatTextIcon,
AlertCircle, WarningCircleIcon,
Trash2, TrashIcon,
} from "lucide-react"; } from "@phosphor-icons/react";
import { CronJob } from "@/app/_utils/system"; import { CronJob } from "@/app/_utils/cronjob-utils";
interface DeleteTaskModalProps { interface DeleteTaskModalProps {
isOpen: boolean; isOpen: boolean;
@@ -18,12 +18,12 @@ interface DeleteTaskModalProps {
job: CronJob | null; job: CronJob | null;
} }
export function DeleteTaskModal({ export const DeleteTaskModal = ({
isOpen, isOpen,
onClose, onClose,
onConfirm, onConfirm,
job, job,
}: DeleteTaskModalProps) { }: DeleteTaskModalProps) => {
if (!job) return null; if (!job) return null;
return ( return (
@@ -34,7 +34,7 @@ export function DeleteTaskModal({
size="sm" size="sm"
> >
<div className="space-y-3"> <div className="space-y-3">
<div className="bg-muted/30 rounded p-2 border border-border/50"> <div className="bg-muted/30 rounded p-2 border border-border">
<div className="space-y-1"> <div className="space-y-1">
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<Calendar className="h-3 w-3 text-muted-foreground" /> <Calendar className="h-3 w-3 text-muted-foreground" />
@@ -44,15 +44,15 @@ export function DeleteTaskModal({
</div> </div>
<div className="flex items-start gap-2"> <div className="flex items-start gap-2">
<Terminal className="h-3 w-3 text-muted-foreground mt-0.5 flex-shrink-0" /> <TerminalIcon className="h-3 w-3 text-muted-foreground mt-0.5 flex-shrink-0" />
<pre className="text-xs font-medium text-foreground break-words bg-muted/30 px-1 py-0.5 rounded border border-border/30 flex-1"> <pre className="max-w-full overflow-x-auto text-xs font-medium text-foreground break-words bg-muted/30 px-1 py-0.5 rounded border border-border flex-1 hide-scrollbar">
{job.command} {job.command}
</pre> </pre>
</div> </div>
{job.comment && ( {job.comment && (
<div className="flex items-start gap-2"> <div className="flex items-start gap-2">
<MessageSquare className="h-3 w-3 text-muted-foreground mt-0.5 flex-shrink-0" /> <ChatTextIcon className="h-3 w-3 text-muted-foreground mt-0.5 flex-shrink-0" />
<p className="text-xs text-muted-foreground break-words italic"> <p className="text-xs text-muted-foreground break-words italic">
{job.comment} {job.comment}
</p> </p>
@@ -63,7 +63,7 @@ export function DeleteTaskModal({
<div className="bg-destructive/5 border border-destructive/20 rounded p-2"> <div className="bg-destructive/5 border border-destructive/20 rounded p-2">
<div className="flex items-start gap-2"> <div className="flex items-start gap-2">
<AlertCircle className="h-4 w-4 text-destructive mt-0.5 flex-shrink-0" /> <WarningCircleIcon className="h-4 w-4 text-destructive mt-0.5 flex-shrink-0" />
<div> <div>
<p className="text-xs font-medium text-destructive mb-0.5"> <p className="text-xs font-medium text-destructive mb-0.5">
This action cannot be undone This action cannot be undone
@@ -75,20 +75,19 @@ export function DeleteTaskModal({
</div> </div>
</div> </div>
<div className="flex justify-end gap-2 pt-2 border-t border-border/50"> <div className="flex justify-end gap-2 pt-2 border-t border-border">
<Button variant="outline" onClick={onClose} className="btn-outline"> <Button variant="outline" onClick={onClose} className="btn-outline">
Cancel Cancel
</Button> </Button>
<Button <Button
variant="destructive" variant="destructive"
onClick={onConfirm} onClick={onConfirm}
className="btn-destructive"
> >
<Trash2 className="h-4 w-4 mr-2" /> <TrashIcon className="h-4 w-4 mr-2" />
Delete Task Delete Task
</Button> </Button>
</div> </div>
</div> </div>
</Modal> </Modal>
); );
} };

View File

@@ -1,8 +1,8 @@
"use client"; "use client";
import { Edit } from "lucide-react"; import { PencilSimpleIcon } from "@phosphor-icons/react";
import { type Script } from "@/app/_server/actions/scripts"; import { Script } from "@/app/_utils/scripts-utils";
import { ScriptModal } from "./ScriptModal"; import { ScriptModal } from "@/app/_components/FeatureComponents/Modals/ScriptModal";
interface EditScriptModalProps { interface EditScriptModalProps {
isOpen: boolean; isOpen: boolean;
@@ -19,14 +19,14 @@ interface EditScriptModalProps {
onFormChange: (updates: Partial<EditScriptModalProps["form"]>) => void; onFormChange: (updates: Partial<EditScriptModalProps["form"]>) => void;
} }
export function EditScriptModal({ export const EditScriptModal = ({
isOpen, isOpen,
onClose, onClose,
onSubmit, onSubmit,
script, script,
form, form,
onFormChange, onFormChange,
}: EditScriptModalProps) { }: EditScriptModalProps) => {
if (!script) return null; if (!script) return null;
return ( return (
@@ -36,7 +36,7 @@ export function EditScriptModal({
onSubmit={onSubmit} onSubmit={onSubmit}
title="Edit Script" title="Edit Script"
submitButtonText="Update Script" submitButtonText="Update Script"
submitButtonIcon={<Edit className="h-4 w-4 mr-2" />} submitButtonIcon={<PencilSimpleIcon className="h-4 w-4 mr-2" />}
form={form} form={form}
onFormChange={onFormChange} onFormChange={onFormChange}
additionalFormData={{ id: script.id }} additionalFormData={{ id: script.id }}

View File

@@ -0,0 +1,129 @@
"use client";
import { Modal } from "@/app/_components/GlobalComponents/UIElements/Modal";
import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { Input } from "@/app/_components/GlobalComponents/FormElements/Input";
import { Switch } from "@/app/_components/GlobalComponents/UIElements/Switch";
import { CronExpressionHelper } from "@/app/_components/FeatureComponents/Scripts/CronExpressionHelper";
import { PencilSimpleIcon, TerminalIcon, FileArrowDownIcon } from "@phosphor-icons/react";
import { useTranslations } from "next-intl";
interface EditTaskModalProps {
isOpen: boolean;
onClose: () => void;
onSubmit: (e: React.FormEvent<HTMLFormElement>) => void;
form: {
schedule: string;
command: string;
comment: string;
logsEnabled: boolean;
};
onFormChange: (updates: Partial<EditTaskModalProps["form"]>) => void;
}
export const EditTaskModal = ({
isOpen,
onClose,
onSubmit,
form,
onFormChange,
}: EditTaskModalProps) => {
const t = useTranslations();
return (
<Modal
isOpen={isOpen}
onClose={onClose}
title={t("cronjobs.editScheduledTask")}
size="xl"
>
<form onSubmit={onSubmit} className="space-y-4">
<div>
<label className="block text-sm font-medium text-foreground mb-1">
Schedule
</label>
<CronExpressionHelper
value={form.schedule}
onChange={(value) => onFormChange({ schedule: value })}
placeholder="* * * * *"
showPatterns={true}
/>
</div>
<div>
<label className="block text-sm font-medium text-foreground mb-1">
Command
</label>
<div className="space-y-3">
<div className="relative">
<Input
value={form.command}
onChange={(e) => onFormChange({ command: e.target.value })}
placeholder="/usr/bin/command"
className="font-mono bg-muted/30 border-border focus:border-primary/50"
required
/>
<div className="absolute right-3 top-1/2 -translate-y-1/2">
<TerminalIcon className="h-4 w-4 text-muted-foreground" />
</div>
</div>
</div>
</div>
<div>
<label className="block text-sm font-medium text-foreground mb-1">
{t("common.description")}{" "}
<span className="text-muted-foreground">
({t("common.optional")})
</span>
</label>
<Input
value={form.comment}
onChange={(e) => onFormChange({ comment: e.target.value })}
placeholder={t("cronjobs.whatDoesThisTaskDo")}
className="bg-muted/30 border-border focus:border-primary/50"
/>
</div>
<div className="border border-border bg-muted/10 rounded-lg p-4">
<div
className="flex items-start gap-3 cursor-pointer"
onClick={() => onFormChange({ logsEnabled: !form.logsEnabled })}
>
<Switch
checked={form.logsEnabled}
onCheckedChange={(checked) => onFormChange({ logsEnabled: checked })}
className="mt-1"
/>
<div className="flex-1">
<div className="flex items-center gap-2 text-sm font-medium text-foreground">
<FileArrowDownIcon className="h-4 w-4 text-primary" />
{t("cronjobs.enableLogging")}
</div>
<p className="text-xs text-muted-foreground mt-1">
{t("cronjobs.loggingDescription")}
</p>
</div>
</div>
</div>
<div className="flex justify-end gap-2 pt-3 border-t border-border">
<Button
type="button"
variant="outline"
onClick={onClose}
className="btn-outline"
>
Cancel
</Button>
<Button type="submit" className="btn-primary glow-primary">
<PencilSimpleIcon className="h-4 w-4 mr-2" />
Update Task
</Button>
</div>
</form>
</Modal>
);
};

View File

@@ -0,0 +1,137 @@
"use client";
import { Modal } from "@/app/_components/GlobalComponents/UIElements/Modal";
import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { WarningCircleIcon, CopyIcon, XIcon } from "@phosphor-icons/react";
import { showToast } from "@/app/_components/GlobalComponents/UIElements/Toast";
interface ErrorDetails {
title: string;
message: string;
details?: string;
command?: string;
output?: string;
stderr?: string;
timestamp: string;
jobId?: string;
}
interface ErrorDetailsModalProps {
isOpen: boolean;
onClose: () => void;
error: ErrorDetails | null;
}
export const ErrorDetailsModal = ({
isOpen,
onClose,
error,
}: ErrorDetailsModalProps) => {
if (!isOpen || !error) return null;
const handleCopyDetails = async () => {
const detailsText = `
Error Details:
Title: ${error.title}
Message: ${error.message}
${error.details ? `Details: ${error.details}` : ""}
${error.command ? `Command: ${error.command}` : ""}
${error.output ? `Output: ${error.output}` : ""}
${error.stderr ? `Stderr: ${error.stderr}` : ""}
Timestamp: ${error.timestamp}
`.trim();
try {
await navigator.clipboard.writeText(detailsText);
showToast("success", "Error details copied to clipboard");
} catch (err) {
showToast("error", "Failed to copy error details");
}
};
return (
<Modal isOpen={isOpen} onClose={onClose} title="Error Details" size="xl">
<div className="space-y-4">
<div className="bg-destructive/5 border border-destructive/20 rounded-lg p-4">
<div className="flex items-start gap-3">
<WarningCircleIcon className="h-5 w-5 text-destructive mt-0.5 flex-shrink-0" />
<div className="flex-1">
<h3 className="font-medium text-destructive mb-1">
{error.title}
</h3>
<p className="text-sm text-muted-foreground">{error.message}</p>
</div>
</div>
</div>
{error.details && (
<div>
<h4 className="text-sm font-medium text-foreground mb-2">
Details
</h4>
<div className="bg-muted/30 p-3 rounded border border-border">
<pre className="text-sm font-mono text-foreground whitespace-pre-wrap break-words">
{error.details}
</pre>
</div>
</div>
)}
{error.command && (
<div>
<h4 className="text-sm font-medium text-foreground mb-2">
Command
</h4>
<div className="bg-muted/30 p-3 rounded border border-border">
<code className="text-sm font-mono text-foreground break-all">
{error.command}
</code>
</div>
</div>
)}
{error.output && (
<div>
<h4 className="text-sm font-medium text-foreground mb-2">Output</h4>
<div className="bg-muted/30 p-3 rounded border border-border max-h-32 overflow-y-auto tui-scrollbar">
<pre className="text-sm font-mono text-foreground whitespace-pre-wrap">
{error.output}
</pre>
</div>
</div>
)}
{error.stderr && (
<div>
<h4 className="text-sm font-medium text-foreground mb-2">
Error Output
</h4>
<div className="bg-destructive/5 p-3 rounded border border-destructive/20 max-h-32 overflow-y-auto tui-scrollbar">
<pre className="text-sm font-mono text-destructive whitespace-pre-wrap">
{error.stderr}
</pre>
</div>
</div>
)}
<div className="text-xs text-muted-foreground">
Timestamp: {error.timestamp}
</div>
<div className="flex justify-end gap-2 pt-3 border-t border-border">
<Button
variant="outline"
onClick={handleCopyDetails}
className="btn-outline"
>
<CopyIcon className="h-4 w-4 mr-2" />
CopyIcon Details
</Button>
<Button onClick={onClose} className="btn-primary">
Close
</Button>
</div>
</div>
</Modal>
);
};

View File

@@ -0,0 +1,155 @@
"use client";
import { useState, useEffect } from "react";
import { Modal } from "@/app/_components/GlobalComponents/UIElements/Modal";
import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { CaretDownIcon, CodeIcon, ChatTextIcon } from "@phosphor-icons/react";
import { UserFilter } from "@/app/_components/FeatureComponents/User/UserFilter";
import { useTranslations } from "next-intl";
interface FiltersModalProps {
isOpen: boolean;
onClose: () => void;
selectedUser: string | null;
onUserChange: (user: string | null) => void;
scheduleDisplayMode: "cron" | "human" | "both";
onScheduleDisplayModeChange: (mode: "cron" | "human" | "both") => void;
}
export const FiltersModal = ({
isOpen,
onClose,
selectedUser,
onUserChange,
scheduleDisplayMode,
onScheduleDisplayModeChange,
}: FiltersModalProps) => {
const t = useTranslations();
const [localScheduleMode, setLocalScheduleMode] =
useState(scheduleDisplayMode);
const [isScheduleDropdownOpen, setIsScheduleDropdownOpen] = useState(false);
useEffect(() => {
setLocalScheduleMode(scheduleDisplayMode);
}, [scheduleDisplayMode]);
const handleSave = () => {
onScheduleDisplayModeChange(localScheduleMode);
onClose();
};
return (
<Modal
isOpen={isOpen}
onClose={onClose}
title={t("cronjobs.filtersAndDisplay")}
size="md"
>
<div className="space-y-6">
<div className="space-y-4 min-h-[200px]">
<div>
<label className="text-sm font-medium text-foreground mb-2 block">
{t("cronjobs.filterByUser")}
</label>
<UserFilter
selectedUser={selectedUser}
onUserChange={onUserChange}
className="w-full"
/>
</div>
<div>
<label className="text-sm font-medium text-foreground mb-2 block">
{t("cronjobs.scheduleDisplay")}
</label>
<div className="relative">
<Button
variant="outline"
onClick={() =>
setIsScheduleDropdownOpen(!isScheduleDropdownOpen)
}
className="btn-outline w-full justify-between"
>
<div className="flex items-center">
{localScheduleMode === "cron" && (
<CodeIcon className="h-4 w-4 mr-2" />
)}
{localScheduleMode === "human" && (
<ChatTextIcon className="h-4 w-4 mr-2" />
)}
{localScheduleMode === "both" && (
<>
<CodeIcon className="h-4 w-4 mr-1" />
<ChatTextIcon className="h-4 w-4 mr-2" />
</>
)}
<span>
{localScheduleMode === "cron" && t("cronjobs.cronSyntax")}
{localScheduleMode === "human" &&
t("cronjobs.humanReadable")}
{localScheduleMode === "both" && t("cronjobs.both")}
</span>
</div>
<CaretDownIcon className="h-4 w-4 ml-2" />
</Button>
{isScheduleDropdownOpen && (
<div className="absolute top-full left-0 p-1 right-0 mt-1 bg-background0 border border-border rounded-md shadow-lg z-50 min-w-[140px]">
<button
onClick={() => {
setLocalScheduleMode("cron");
setIsScheduleDropdownOpen(false);
}}
className={`w-full text-left px-3 border py-2 text-sm hover:border-border border-transparent transition-colors flex items-center gap-2 ${localScheduleMode === "cron"
? "border-border"
: ""
}`}
>
<CodeIcon className="h-3 w-3" />
{t("cronjobs.cronSyntax")}
</button>
<button
onClick={() => {
setLocalScheduleMode("human");
setIsScheduleDropdownOpen(false);
}}
className={`w-full text-left px-3 py-2 border text-sm hover:border-border border-transparent transition-colors flex items-center gap-2 ${localScheduleMode === "human"
? "border-border"
: ""
}`}
>
<ChatTextIcon className="h-3 w-3" />
{t("cronjobs.humanReadable")}
</button>
<button
onClick={() => {
setLocalScheduleMode("both");
setIsScheduleDropdownOpen(false);
}}
className={`w-full text-left px-3 py-2 border text-sm hover:border-border border-transparent transition-colors flex items-center gap-2 ${localScheduleMode === "both"
? "border-border"
: ""
}`}
>
<CodeIcon className="h-3 w-3" />
<ChatTextIcon className="h-3 w-3" />
{t("cronjobs.both")}
</button>
</div>
)}
</div>
</div>
</div>
<div className="flex justify-end gap-2 pt-4">
<Button variant="outline" onClick={onClose}>
{t("common.cancel")}
</Button>
<Button className="btn-primary" onClick={handleSave}>
{t("cronjobs.applyFilters")}
</Button>
</div>
</div>
</Modal>
);
};

View File

@@ -0,0 +1,366 @@
"use client";
import { useState, useEffect, useRef, useCallback } from "react";
import { CircleNotchIcon, CheckCircleIcon, XCircleIcon, WarningIcon, ArrowsInIcon, ArrowsOutIcon } from "@phosphor-icons/react";
import { Modal } from "@/app/_components/GlobalComponents/UIElements/Modal";
import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { useSSEContext } from "@/app/_contexts/SSEContext";
import { SSEEvent } from "@/app/_utils/sse-events";
import { usePageVisibility } from "@/app/_hooks/usePageVisibility";
import { useTranslations } from "next-intl";
interface LiveLogModalProps {
isOpen: boolean;
onClose: () => void;
runId: string;
jobId: string;
jobComment?: string;
}
const MAX_LINES_FULL_RENDER = 10000;
const TAIL_LINES = 5000;
export const LiveLogModal = ({
isOpen,
onClose,
runId,
jobId,
jobComment,
}: LiveLogModalProps) => {
const t = useTranslations();
const [logContent, setLogContent] = useState<string>("");
const [status, setStatus] = useState<"running" | "completed" | "failed">(
"running"
);
const [exitCode, setExitCode] = useState<number | null>(null);
const [tailMode, setTailMode] = useState<boolean>(false);
const [showSizeWarning, setShowSizeWarning] = useState<boolean>(false);
const logEndRef = useRef<HTMLDivElement>(null);
const { subscribe } = useSSEContext();
const isPageVisible = usePageVisibility();
const lastOffsetRef = useRef<number>(0);
const abortControllerRef = useRef<AbortController | null>(null);
const [fileSize, setFileSize] = useState<number>(0);
const [lineCount, setLineCount] = useState<number>(0);
const [maxLines, setMaxLines] = useState<number>(500);
const [totalLines, setTotalLines] = useState<number>(0);
const [truncated, setTruncated] = useState<boolean>(false);
const [showFullLog, setShowFullLog] = useState<boolean>(false);
const [isJobComplete, setIsJobComplete] = useState<boolean>(false);
useEffect(() => {
if (isOpen) {
lastOffsetRef.current = 0;
setLogContent("");
setTailMode(false);
setShowSizeWarning(false);
setFileSize(0);
setLineCount(0);
setShowFullLog(false);
setIsJobComplete(false);
}
}, [isOpen, runId]);
useEffect(() => {
if (isOpen && runId && !isJobComplete) {
lastOffsetRef.current = 0;
setLogContent("");
fetchLogs();
}
}, [maxLines]);
const fetchLogs = useCallback(async () => {
if (abortControllerRef.current) {
abortControllerRef.current.abort();
}
const abortController = new AbortController();
abortControllerRef.current = abortController;
try {
const url = `/api/logs/stream?runId=${runId}&offset=${lastOffsetRef.current}&maxLines=${maxLines}`;
const response = await fetch(url, {
signal: abortController.signal,
});
const data = await response.json();
if (data.fileSize !== undefined) {
lastOffsetRef.current = data.fileSize;
setFileSize(data.fileSize);
if (data.fileSize > 10 * 1024 * 1024) {
setShowSizeWarning(true);
}
}
if (data.totalLines !== undefined) {
setTotalLines(data.totalLines);
}
setLineCount(data.displayedLines || 0);
if (data.truncated !== undefined) {
setTruncated(data.truncated);
}
if (lastOffsetRef.current === 0 && data.content) {
setLogContent(data.content);
if (data.truncated) {
setTailMode(true);
}
} else if (data.newContent) {
setLogContent((prev) => {
const combined = prev + data.newContent;
const lines = combined.split("\n");
if (lines.length > maxLines) {
return lines.slice(-maxLines).join("\n");
}
return combined;
});
}
const jobStatus = data.status || "running";
setStatus(jobStatus);
if (jobStatus === "completed" || jobStatus === "failed") {
setIsJobComplete(true);
}
if (data.exitCode !== undefined) {
setExitCode(data.exitCode);
}
} catch (error: any) {
if (error.name !== "AbortError") {
console.error("Failed to fetch logs:", error);
}
}
}, [runId, maxLines]);
useEffect(() => {
if (!isOpen || !runId || !isPageVisible) return;
fetchLogs();
let interval: NodeJS.Timeout | null = null;
if (isPageVisible && !isJobComplete) {
interval = setInterval(fetchLogs, 3000);
}
return () => {
if (interval) {
clearInterval(interval);
}
if (abortControllerRef.current) {
abortControllerRef.current.abort();
}
};
}, [isOpen, runId, isPageVisible, fetchLogs, isJobComplete]);
useEffect(() => {
if (!isOpen) return;
const unsubscribe = subscribe((event: SSEEvent) => {
if (event.type === "job-completed" && event.data.runId === runId) {
setStatus("completed");
setExitCode(event.data.exitCode);
fetch(`/api/logs/stream?runId=${runId}&offset=0`)
.then((res) => res.json())
.then((data) => {
if (data.content) {
const lines = data.content.split("\n");
setLineCount(lines.length);
if (tailMode && lines.length > TAIL_LINES) {
setLogContent(lines.slice(-TAIL_LINES).join("\n"));
} else {
setLogContent(data.content);
}
}
});
} else if (event.type === "job-failed" && event.data.runId === runId) {
setStatus("failed");
setExitCode(event.data.exitCode);
fetch(`/api/logs/stream?runId=${runId}&offset=0`)
.then((res) => res.json())
.then((data) => {
if (data.content) {
const lines = data.content.split("\n");
setLineCount(lines.length);
if (tailMode && lines.length > TAIL_LINES) {
setLogContent(lines.slice(-TAIL_LINES).join("\n"));
} else {
setLogContent(data.content);
}
}
});
}
});
return unsubscribe;
}, [isOpen, runId, subscribe, tailMode]);
useEffect(() => {
if (logEndRef.current) {
logEndRef.current.scrollIntoView({ behavior: "instant" });
}
}, [logContent]);
const toggleTailMode = () => {
setTailMode(!tailMode);
if (!tailMode) {
const lines = logContent.split("\n");
if (lines.length > TAIL_LINES) {
setLogContent(lines.slice(-TAIL_LINES).join("\n"));
}
}
};
const formatFileSize = (bytes: number): string => {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
};
const titleWithStatus = (
<div className="flex items-center gap-3">
<span>{t("cronjobs.liveJobExecution")}{jobComment && `: ${jobComment}`}</span>
{status === "running" && (
<span className="flex items-center gap-1 text-sm text-status-info">
<CircleNotchIcon className="w-4 h-4 animate-spin" />
{t("cronjobs.running")}
</span>
)}
{status === "completed" && (
<span className="flex items-center gap-1 text-sm text-status-success">
<CheckCircleIcon className="w-4 h-4" />
{t("cronjobs.completed", { exitCode: exitCode ?? 0 })}
</span>
)}
{status === "failed" && (
<span className="flex items-center gap-1 text-sm text-status-error">
<XCircleIcon className="w-4 h-4" />
{t("cronjobs.jobFailed", { exitCode: exitCode ?? 1 })}
</span>
)}
</div>
);
return (
<Modal
isOpen={isOpen}
onClose={onClose}
title={titleWithStatus as any}
size="xl"
preventCloseOnClickOutside={status === "running"}
>
<div className="space-y-4">
<div className="flex items-center justify-between gap-4">
<div className="flex items-center gap-3">
{!showFullLog ? (
<>
<label htmlFor="maxLines" className="text-sm text-muted-foreground">
{t("cronjobs.showLast")}
</label>
<select
id="maxLines"
value={maxLines}
onChange={(e) => setMaxLines(parseInt(e.target.value, 10))}
className="bg-background0 border border-border rounded px-2 py-1 text-sm"
>
<option value="100">{t("cronjobs.nLines", { count: "100" })}</option>
<option value="500">{t("cronjobs.nLines", { count: "500" })}</option>
<option value="1000">{t("cronjobs.nLines", { count: "1,000" })}</option>
<option value="2000">{t("cronjobs.nLines", { count: "2,000" })}</option>
<option value="5000">{t("cronjobs.nLines", { count: "5,000" })}</option>
</select>
{truncated && (
<Button
type="button"
variant="outline"
size="sm"
onClick={() => {
setShowFullLog(true);
setMaxLines(50000);
}}
className="text-xs"
>
{totalLines > 0
? t("cronjobs.viewFullLog", { totalLines: totalLines.toLocaleString() })
: t("cronjobs.viewFullLogNoCount")}
</Button>
)}
</>
) : (
<div className="flex items-center gap-2">
<span className="text-sm text-muted-foreground">
{totalLines > 0
? t("cronjobs.viewingFullLog", { totalLines: totalLines.toLocaleString() })
: t("cronjobs.viewingFullLogNoCount")}
</span>
<Button
type="button"
variant="outline"
size="sm"
onClick={() => {
setShowFullLog(false);
setMaxLines(500);
}}
className="text-xs"
>
{t("cronjobs.backToWindowedView")}
</Button>
</div>
)}
</div>
{truncated && !showFullLog && (
<div className="text-sm text-status-warning flex items-center gap-1 terminal-font">
<WarningIcon className="h-4 w-4" />
{t("cronjobs.showingLastOf", {
lineCount: lineCount.toLocaleString(),
totalLines: totalLines.toLocaleString()
})}
</div>
)}
</div>
{showSizeWarning && (
<div className="bg-background2 ascii-border p-3 flex items-start gap-3 terminal-font">
<WarningIcon className="h-4 w-4 text-status-warning mt-0.5 flex-shrink-0" />
<div className="flex-1 min-w-0">
<p className="text-sm text-foreground">
<span className="font-medium">{t("cronjobs.largeLogFileDetected")}</span> ({formatFileSize(fileSize)})
{tailMode && ` - ${t("cronjobs.tailModeEnabled", { tailLines: TAIL_LINES.toLocaleString() })}`}
</p>
</div>
<Button
type="button"
variant="ghost"
size="sm"
onClick={toggleTailMode}
className="text-status-warning hover:text-status-warning hover:bg-background2 h-auto py-1 px-2 text-xs"
title={tailMode ? t("cronjobs.showAllLines") : t("cronjobs.enableTailMode")}
>
{tailMode ? <ArrowsOutIcon className="h-3 w-3" /> : <ArrowsInIcon className="h-3 w-3" />}
</Button>
</div>
)}
<div className="bg-background0 p-4 max-h-[60vh] overflow-auto terminal-font ascii-border">
<pre className="text-xs text-status-success whitespace-pre-wrap break-words">
{logContent || t("cronjobs.waitingForJobToStart")}
<div ref={logEndRef} />
</pre>
</div>
<div className="flex justify-between items-center text-xs text-muted-foreground">
<span>
{t("cronjobs.runIdJobId", { runId, jobId })}
</span>
</div>
</div>
</Modal>
);
};

View File

@@ -0,0 +1,298 @@
"use client";
import { useState, useEffect } from "react";
import { Modal } from "@/app/_components/GlobalComponents/UIElements/Modal";
import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { FileTextIcon, TrashIcon, EyeIcon, XIcon, ArrowsClockwiseIcon, WarningCircleIcon, CheckCircleIcon } from "@phosphor-icons/react";
import { useTranslations } from "next-intl";
import {
getJobLogs,
getLogContent,
deleteLogFile,
deleteAllJobLogs,
getJobLogStats,
} from "@/app/_server/actions/logs";
interface LogEntry {
filename: string;
timestamp: string;
fullPath: string;
size: number;
dateCreated: Date;
exitCode?: number;
hasError?: boolean;
}
interface LogsModalProps {
isOpen: boolean;
onClose: () => void;
jobId: string;
jobComment?: string;
preSelectedLog?: string;
}
export const LogsModal = ({
isOpen,
onClose,
jobId,
jobComment,
preSelectedLog,
}: LogsModalProps) => {
const t = useTranslations();
const [logs, setLogs] = useState<LogEntry[]>([]);
const [selectedLog, setSelectedLog] = useState<string | null>(null);
const [logContent, setLogContent] = useState<string>("");
const [isLoadingLogs, setIsLoadingLogs] = useState(false);
const [isLoadingContent, setIsLoadingContent] = useState(false);
const [stats, setStats] = useState<{
count: number;
totalSize: number;
totalSizeMB: number;
} | null>(null);
const loadLogs = async () => {
setIsLoadingLogs(true);
try {
const [logsData, statsData] = await Promise.all([
getJobLogs(jobId, false, true),
getJobLogStats(jobId),
]);
setLogs(logsData);
setStats(statsData);
} catch (error) {
console.error("Error loading logs:", error);
} finally {
setIsLoadingLogs(false);
}
};
useEffect(() => {
if (isOpen) {
loadLogs().then(() => {
if (preSelectedLog) {
handleViewLog(preSelectedLog);
}
});
if (!preSelectedLog) {
setSelectedLog(null);
setLogContent("");
}
}
}, [isOpen, jobId, preSelectedLog]);
const handleViewLog = async (filename: string) => {
setIsLoadingContent(true);
setSelectedLog(filename);
try {
const content = await getLogContent(jobId, filename);
setLogContent(content);
} catch (error) {
console.error("Error loading log content:", error);
setLogContent("Error loading log content");
} finally {
setIsLoadingContent(false);
}
};
const handleDeleteLog = async (filename: string) => {
if (!confirm(t("cronjobs.confirmDeleteLog"))) return;
try {
const result = await deleteLogFile(jobId, filename);
if (result.success) {
await loadLogs();
if (selectedLog === filename) {
setSelectedLog(null);
setLogContent("");
}
} else {
alert(result.message);
}
} catch (error) {
console.error("Error deleting log:", error);
alert("Error deleting log file");
}
};
const handleDeleteAllLogs = async () => {
if (!confirm(t("cronjobs.confirmDeleteAllLogs"))) return;
try {
const result = await deleteAllJobLogs(jobId);
if (result.success) {
await loadLogs();
setSelectedLog(null);
setLogContent("");
} else {
alert(result.message);
}
} catch (error) {
console.error("Error deleting all logs:", error);
alert("Error deleting all logs");
}
};
const formatFileSize = (bytes: number): string => {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(2)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
};
const formatTimestamp = (timestamp: string): string => {
const [datePart, timePart] = timestamp.split("_");
const [year, month, day] = datePart.split("-");
const [hour, minute, second] = timePart.split("-");
const date = new Date(
parseInt(year),
parseInt(month) - 1,
parseInt(day),
parseInt(hour),
parseInt(minute),
parseInt(second)
);
return date.toLocaleString();
};
return (
<Modal isOpen={isOpen} onClose={onClose} title={t("cronjobs.viewLogs")} size="xl">
<div className="flex flex-col h-[600px]">
<div className="flex items-center justify-between mb-4 pb-4 border-b border-border">
<div>
<h3 className="font-semibold text-lg">{jobComment || jobId}</h3>
{stats && (
<p className="text-sm text-muted-foreground">
{stats.count} {t("cronjobs.logs")} {stats.totalSizeMB} MB
</p>
)}
</div>
<div className="flex gap-2">
<Button
onClick={loadLogs}
disabled={isLoadingLogs}
className="btn-primary glow-primary"
size="sm"
>
<ArrowsClockwiseIcon
className={`w-4 h-4 mr-2 ${isLoadingLogs ? "animate-spin" : ""
}`}
/>
{t("common.refresh")}
</Button>
{logs.length > 0 && (
<Button
onClick={handleDeleteAllLogs}
variant="destructive"
size="sm"
>
<TrashIcon className="w-4 h-4 mr-2" />
{t("cronjobs.deleteAll")}
</Button>
)}
</div>
</div>
<div className="flex-1 flex gap-4 overflow-hidden">
<div className="w-1/3 flex flex-col border-r border-border pr-4 overflow-hidden">
<h4 className="font-semibold mb-2">{t("cronjobs.logFiles")}</h4>
<div className="flex-1 overflow-y-auto space-y-2">
{isLoadingLogs ? (
<div className="text-center py-8 text-muted-foreground">
{t("common.loading")}...
</div>
) : logs.length === 0 ? (
<div className="text-center py-8 text-muted-foreground">
{t("cronjobs.noLogsFound")}
</div>
) : (
logs.map((log) => (
<div
key={log.filename}
className={`p-3 ascii-border cursor-pointer transition-colors terminal-font ${selectedLog === log.filename
? "border-primary bg-background2"
: log.hasError
? "border-red-600 hover:border-red-600"
: "ascii-border hover:border-primary"
}`}
onClick={() => handleViewLog(log.filename)}
>
<div className="flex items-start justify-between gap-2">
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2 mb-1">
{log.hasError ? (
<WarningCircleIcon className="w-4 h-4 flex-shrink-0 text-status-error" />
) : log.exitCode === 0 ? (
<CheckCircleIcon className="w-4 h-4 flex-shrink-0 text-status-success" />
) : (
<FileTextIcon className="w-4 h-4 flex-shrink-0" />
)}
<span className="text-sm font-medium truncate">
{formatTimestamp(log.timestamp)}
</span>
</div>
<div className="flex items-center gap-2">
<p className="text-xs text-muted-foreground">
{formatFileSize(log.size)}
</p>
{log.exitCode !== undefined && (
<span
className={`text-xs px-1.5 py-0.5 ${log.hasError
? "bg-background2 text-status-error"
: "bg-background2 text-status-success"
}`}
>
Exit: {log.exitCode}
</span>
)}
</div>
</div>
<Button
onClick={(e) => {
e.stopPropagation();
handleDeleteLog(log.filename);
}}
variant="destructive"
size="sm"
>
<TrashIcon className="w-3 h-3" />
</Button>
</div>
</div>
))
)}
</div>
</div>
<div className="flex-1 flex flex-col overflow-hidden">
<h4 className="font-semibold mb-2">{t("cronjobs.logContent")}</h4>
<div className="flex-1 overflow-hidden">
{isLoadingContent ? (
<div className="h-full flex items-center justify-center text-muted-foreground">
{t("common.loading")}...
</div>
) : selectedLog ? (
<pre className="h-full overflow-auto bg-background0 tui-scrollbar p-4 ascii-border text-xs font-mono whitespace-pre-wrap terminal-font">
{logContent}
</pre>
) : (
<div className="h-full flex items-center justify-center text-muted-foreground">
<div className="text-center">
<EyeIcon className="w-12 h-12 mx-auto mb-2 opacity-50" />
<p>{t("cronjobs.selectLogToView")}</p>
</div>
</div>
)}
</div>
</div>
</div>
<div className="mt-4 pt-4 border-t border-border flex justify-end">
<Button onClick={onClose} className="btn-primary glow-primary">
<XIcon className="w-4 h-4 mr-2" />
{t("common.close")}
</Button>
</div>
</div>
</Modal>
);
};

View File

@@ -0,0 +1,286 @@
"use client";
import { useState, useEffect } from "react";
import { Modal } from "@/app/_components/GlobalComponents/UIElements/Modal";
import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import {
UploadIcon,
TrashIcon,
CalendarIcon,
UserIcon,
DownloadIcon,
ArrowsClockwiseIcon,
CheckIcon,
} from "@phosphor-icons/react";
import { useTranslations } from "next-intl";
import { CronJob } from "@/app/_utils/cronjob-utils";
import { unwrapCommand } from "@/app/_utils/wrapper-utils-client";
import { copyToClipboard } from "@/app/_utils/global-utils";
interface BackupFile {
filename: string;
job: CronJob;
backedUpAt: string;
}
interface RestoreBackupModalProps {
isOpen: boolean;
onClose: () => void;
backups: BackupFile[];
onRestore: (filename: string) => void;
onRestoreAll: () => void;
onBackupAll: () => void;
onDelete: (filename: string) => void;
onRefresh: () => void;
}
export const RestoreBackupModal = ({
isOpen,
onClose,
backups,
onRestore,
onRestoreAll,
onBackupAll,
onDelete,
onRefresh,
}: RestoreBackupModalProps) => {
const t = useTranslations();
const [deletingFilename, setDeletingFilename] = useState<string | null>(null);
const [commandCopied, setCommandCopied] = useState<string | null>(null);
useEffect(() => {
if (isOpen) {
onRefresh();
}
}, [isOpen]);
const handleRestoreAll = () => {
if (window.confirm(t("cronjobs.confirmRestoreAll"))) {
onRestoreAll();
}
};
const handleDelete = async (filename: string) => {
if (window.confirm(t("cronjobs.confirmDeleteBackup"))) {
setDeletingFilename(filename);
await onDelete(filename);
setDeletingFilename(null);
onRefresh();
}
};
const formatDate = (dateString: string) => {
try {
const date = new Date(dateString);
return date.toLocaleString();
} catch {
return dateString;
}
};
return (
<Modal
isOpen={isOpen}
onClose={onClose}
title={t("cronjobs.backups")}
size="xl"
>
<div className="space-y-4">
<div className="flex flex-col sm:flex-row gap-2">
<Button
variant="outline"
onClick={onBackupAll}
className="btn-outline flex-1"
>
<DownloadIcon className="h-4 w-4 sm:mr-2" />
<span className="hidden sm:inline">{t("cronjobs.backupAll")}</span>
<span className="sm:hidden">Backup</span>
</Button>
{backups.length > 0 && (
<Button
variant="outline"
onClick={handleRestoreAll}
className="btn-primary flex-1"
>
<UploadIcon className="h-4 w-4 sm:mr-2" />
<span className="hidden sm:inline">{t("cronjobs.restoreAll")}</span>
<span className="sm:hidden">Restore</span>
</Button>
)}
<Button
variant="outline"
onClick={onRefresh}
className="btn-outline sm:w-auto"
title={t("common.refresh")}
>
<ArrowsClockwiseIcon className="h-4 w-4" />
<span className="sm:hidden ml-2">Refresh</span>
</Button>
</div>
{backups.length === 0 ? (
<div className="text-center py-8 text-muted-foreground">
<p>{t("cronjobs.noBackupsFound")}</p>
</div>
) : (
<div className="space-y-3 max-h-[500px] overflow-y-auto tui-scrollbar pr-2 pb-2">
{backups.map((backup) => (
<div
key={backup.filename}
className="tui-card p-3 terminal-font"
>
<div className="flex flex-col gap-3 lg:hidden">
<div className="flex items-center justify-between">
<code className="text-xs bg-background0 text-status-warning px-1.5 py-0.5 terminal-font ascii-border">
{backup.job.schedule}
</code>
<div className="flex items-center gap-2">
<Button
variant="outline"
size="sm"
onClick={() => {
onRestore(backup.filename);
onClose();
}}
className="btn-outline h-8 px-3"
title={t("cronjobs.restoreThisBackup")}
>
<UploadIcon className="h-3 w-3" />
</Button>
<Button
variant="destructive"
size="sm"
onClick={() => handleDelete(backup.filename)}
disabled={deletingFilename === backup.filename}
className="h-8 px-3"
title={t("cronjobs.deleteBackup")}
>
{deletingFilename === backup.filename ? (
<div className="h-3 w-3 animate-spin rounded-full border-2 border-current border-t-transparent" />
) : (
<TrashIcon className="h-3 w-3" />
)}
</Button>
</div>
</div>
<div className="flex items-center gap-2">
{commandCopied === backup.filename && (
<CheckIcon className="h-3 w-3 text-status-success flex-shrink-0" />
)}
<pre
onClick={(e) => {
e.stopPropagation();
copyToClipboard(unwrapCommand(backup.job.command));
setCommandCopied(backup.filename);
setTimeout(() => setCommandCopied(null), 3000);
}}
className="max-w-full overflow-x-auto flex-1 cursor-pointer text-sm font-medium terminal-font bg-background1 px-2 py-1 ascii-border break-all"
title={unwrapCommand(backup.job.command)}
>
{unwrapCommand(backup.job.command)}
</pre>
</div>
<div className="flex flex-wrap items-center gap-3 text-xs text-muted-foreground">
<div className="flex items-center gap-1">
<UserIcon className="h-3 w-3" />
<span>{backup.job.user}</span>
</div>
<div className="flex items-center gap-1">
<CalendarIcon className="h-3 w-3" />
<span>{formatDate(backup.backedUpAt)}</span>
</div>
</div>
</div>
<div className="hidden lg:flex items-center gap-3">
<div className="flex-shrink-0">
<code className="text-xs bg-background0 text-status-warning px-1.5 py-0.5 terminal-font ascii-border">
{backup.job.schedule}
</code>
</div>
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2">
{commandCopied === backup.filename && (
<CheckIcon className="h-3 w-3 text-status-success flex-shrink-0" />
)}
<pre
onClick={(e) => {
e.stopPropagation();
copyToClipboard(unwrapCommand(backup.job.command));
setCommandCopied(backup.filename);
setTimeout(() => setCommandCopied(null), 3000);
}}
className="flex-1 cursor-pointer overflow-hidden text-sm font-medium terminal-font bg-background1 px-2 py-1 ascii-border truncate"
title={unwrapCommand(backup.job.command)}
>
{unwrapCommand(backup.job.command)}
</pre>
</div>
</div>
<div className="flex items-center gap-3 text-xs text-muted-foreground flex-shrink-0">
<div className="flex items-center gap-1">
<UserIcon className="h-3 w-3" />
<span>{backup.job.user}</span>
</div>
<div className="flex items-center gap-1">
<CalendarIcon className="h-3 w-3" />
<span>{formatDate(backup.backedUpAt)}</span>
</div>
</div>
<div className="flex items-center gap-2 flex-shrink-0">
<Button
variant="outline"
size="sm"
onClick={() => {
onRestore(backup.filename);
onClose();
}}
className="btn-outline h-8 px-3"
title={t("cronjobs.restoreThisBackup")}
>
<UploadIcon className="h-3 w-3" />
</Button>
<Button
variant="destructive"
size="sm"
onClick={() => handleDelete(backup.filename)}
disabled={deletingFilename === backup.filename}
className="h-8 px-3"
title={t("cronjobs.deleteBackup")}
>
{deletingFilename === backup.filename ? (
<div className="h-3 w-3 animate-spin rounded-full border-2 border-current border-t-transparent" />
) : (
<TrashIcon className="h-3 w-3" />
)}
</Button>
</div>
</div>
{backup.job.comment && (
<p className="text-xs text-muted-foreground italic mt-2">
{backup.job.comment}
</p>
)}
</div>
))}
</div>
)}
<div className="flex flex-col sm:flex-row sm:justify-between gap-2 pt-4 border-t border-border">
<p className="text-sm text-muted-foreground text-center sm:text-left">
{t("cronjobs.availableBackups")}: {backups.length}
</p>
<Button variant="outline" onClick={onClose} className="btn-outline w-full sm:w-auto">
{t("common.close")}
</Button>
</div>
</div>
</Modal>
);
};

View File

@@ -0,0 +1,180 @@
"use client";
import { Modal } from "@/app/_components/GlobalComponents/UIElements/Modal";
import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { Input } from "@/app/_components/GlobalComponents/FormElements/Input";
import { BashEditor } from "@/app/_components/FeatureComponents/Scripts/BashEditor";
import { BashSnippetHelper } from "@/app/_components/FeatureComponents/Scripts/BashSnippetHelper";
import { showToast } from "@/app/_components/GlobalComponents/UIElements/Toast";
import { FileTextIcon, CodeIcon, InfoIcon, TrashIcon } from "@phosphor-icons/react";
import { useTranslations } from "next-intl";
interface ScriptModalProps {
isOpen: boolean;
onClose: () => void;
onSubmit: (
formData: FormData
) => Promise<{ success: boolean; message: string }>;
title: string;
submitButtonText: string;
submitButtonIcon: React.ReactNode;
form: {
name: string;
description: string;
content: string;
};
onFormChange: (updates: Partial<ScriptModalProps["form"]>) => void;
additionalFormData?: Record<string, string>;
isDraft?: boolean;
onClearDraft?: () => void;
}
export const ScriptModal = ({
isOpen,
onClose,
onSubmit,
title,
submitButtonText,
submitButtonIcon,
form,
onFormChange,
additionalFormData = {},
isDraft = false,
onClearDraft,
}: ScriptModalProps) => {
const t = useTranslations();
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (!form.name.trim()) {
showToast("error", "Validation Error", "Script name is required");
return;
}
if (!form.content.trim()) {
showToast("error", "Validation Error", "Script content is required");
return;
}
const formData = new FormData();
formData.append("name", form.name.trim());
formData.append("description", form.description.trim());
formData.append("content", form.content.trim());
Object.entries(additionalFormData).forEach(([key, value]) => {
formData.append(key, value);
});
const result = await onSubmit(formData);
if (result.success) {
onClose();
} else {
showToast("error", `Failed to ${title.toLowerCase()}`, result.message);
}
};
const handleInsertSnippet = (snippet: string) => {
onFormChange({ content: snippet });
};
return (
<Modal isOpen={isOpen} onClose={onClose} title={title} size="2xl">
<form onSubmit={handleSubmit} className="space-y-6 terminal-font">
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium mb-2">
Script Name <span className="text-status-error">*</span>
</label>
<Input
value={form.name}
onChange={(e) => onFormChange({ name: e.target.value })}
placeholder="My Script"
required
className={
!form.name.trim()
? "border-status-error focus:border-status-error"
: ""
}
/>
</div>
<div>
<label className="block text-sm font-medium mb-2">
Description{" "}
<span className="text-xs opacity-60">(optional)</span>
</label>
<Input
value={form.description}
onChange={(e) => onFormChange({ description: e.target.value })}
placeholder="What does this script do?"
/>
</div>
</div>
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6 h-[500px]">
<div className="lg:col-span-1 bg-background0 ascii-border p-4 flex flex-col h-full overflow-hidden">
<div className="flex items-center gap-2 mb-4 flex-shrink-0">
<CodeIcon className="h-4 w-4" />
<h3 className="text-sm font-medium">Snippets</h3>
</div>
<div className="flex-1 overflow-y-auto min-h-0 !pr-0 tui-scrollbar">
<BashSnippetHelper onInsertSnippet={handleInsertSnippet} />
</div>
</div>
<div className="lg:col-span-2 flex flex-col h-full overflow-hidden">
<div className="flex items-center gap-2 mb-4 flex-shrink-0">
<FileTextIcon className="h-4 w-4" />
<h3 className="text-sm font-medium">
Script Content <span className="text-status-error">*</span>
</h3>
{isDraft && (
<span className="ml-auto px-2 py-0.5 text-xs font-medium bg-background0 text-status-info ascii-border">
{t("scripts.draft")}
</span>
)}
</div>
<div className="flex-1 min-h-0">
<BashEditor
value={form.content}
onChange={(value) => onFormChange({ content: value })}
placeholder="#!/bin/bash&#10;# Your script here&#10;echo 'Hello World'"
className="h-full"
/>
</div>
</div>
</div>
<div className="flex justify-between items-center gap-3 pt-4 border-border border-t">
<div>
{isDraft && onClearDraft && (
<Button
type="button"
variant="ghost"
onClick={onClearDraft}
className="opacity-60 hover:opacity-100"
>
<TrashIcon className="h-4 w-4 mr-2" />
{t("scripts.clearDraft")}
</Button>
)}
</div>
<div className="flex gap-3">
<Button
type="button"
variant="outline"
onClick={onClose}
className="btn-outline"
>
{t("scripts.close")}
</Button>
<Button type="submit" className="btn-primary glow-primary">
{submitButtonIcon}
{submitButtonText}
</Button>
</div>
</div>
</form>
</Modal>
);
};

View File

@@ -1,13 +1,14 @@
"use client"; "use client";
import { useState } from "react"; import { useEffect, useState } from "react";
import { Modal } from "../ui/Modal"; import { Modal } from "@/app/_components/GlobalComponents/UIElements/Modal";
import { Button } from "../ui/Button"; import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { Input } from "../ui/Input"; import { Input } from "@/app/_components/GlobalComponents/FormElements/Input";
import { FileText, Search, Check, Terminal } from "lucide-react"; import { FileTextIcon, MagnifyingGlassIcon, CheckIcon, TerminalIcon } from "@phosphor-icons/react";
import { type Script } from "@/app/_server/actions/scripts"; import { Script } from "@/app/_utils/scripts-utils";
import { getScriptContent } from "@/app/_server/actions/scripts"; import { getScriptContent } from "@/app/_server/actions/scripts";
import { getHostScriptPath } from "@/app/_utils/scripts"; import { getHostScriptPath } from "@/app/_server/actions/scripts";
import { useTranslations } from "next-intl";
interface SelectScriptModalProps { interface SelectScriptModalProps {
isOpen: boolean; isOpen: boolean;
@@ -17,16 +18,29 @@ interface SelectScriptModalProps {
selectedScriptId: string | null; selectedScriptId: string | null;
} }
export function SelectScriptModal({ export const SelectScriptModal = ({
isOpen, isOpen,
onClose, onClose,
scripts, scripts,
onScriptSelect, onScriptSelect,
selectedScriptId, selectedScriptId,
}: SelectScriptModalProps) { }: SelectScriptModalProps) => {
const t = useTranslations();
const [searchQuery, setSearchQuery] = useState(""); const [searchQuery, setSearchQuery] = useState("");
const [previewScript, setPreviewScript] = useState<Script | null>(null); const [previewScript, setPreviewScript] = useState<Script | null>(null);
const [previewContent, setPreviewContent] = useState<string>(""); const [previewContent, setPreviewContent] = useState<string>("");
const [hostScriptPath, setHostScriptPath] = useState<string>("");
useEffect(() => {
const fetchHostScriptPath = async () => {
const path = await getHostScriptPath(previewScript?.filename || "");
setHostScriptPath(path);
};
if (previewScript) {
fetchHostScriptPath();
}
}, [previewScript]);
const filteredScripts = scripts.filter( const filteredScripts = scripts.filter(
(script) => (script) =>
@@ -65,16 +79,16 @@ export function SelectScriptModal({
<Modal <Modal
isOpen={isOpen} isOpen={isOpen}
onClose={handleClose} onClose={handleClose}
title="Select Script" title={t("scripts.selectScript")}
size="xl" size="xl"
> >
<div className="space-y-4"> <div className="space-y-4">
<div className="relative"> <div className="relative">
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-muted-foreground" /> <MagnifyingGlassIcon className="absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-muted-foreground" />
<Input <Input
value={searchQuery} value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)} onChange={(e) => setSearchQuery(e.target.value)}
placeholder="Search scripts..." placeholder={t("scripts.searchScripts")}
className="pl-10" className="pl-10"
/> />
</div> </div>
@@ -83,13 +97,13 @@ export function SelectScriptModal({
<div className="border border-border rounded-lg overflow-hidden"> <div className="border border-border rounded-lg overflow-hidden">
<div className="bg-muted/30 px-4 py-2 border-b border-border"> <div className="bg-muted/30 px-4 py-2 border-b border-border">
<h3 className="text-sm font-medium text-foreground"> <h3 className="text-sm font-medium text-foreground">
Available Scripts ({filteredScripts.length}) {t("scripts.availableScripts", { count: filteredScripts.length })}
</h3> </h3>
</div> </div>
<div className="overflow-y-auto h-full max-h-80"> <div className="overflow-y-auto h-full max-h-80">
{filteredScripts.length === 0 ? ( {filteredScripts.length === 0 ? (
<div className="p-4 text-center text-muted-foreground"> <div className="p-4 text-center text-muted-foreground">
{searchQuery ? "No scripts found" : "No scripts available"} {searchQuery ? t("scripts.noScriptsFound") : t("scripts.noScriptsAvailable")}
</div> </div>
) : ( ) : (
<div className="divide-y divide-border"> <div className="divide-y divide-border">
@@ -97,21 +111,20 @@ export function SelectScriptModal({
<button <button
key={script.id} key={script.id}
onClick={() => handleScriptClick(script)} onClick={() => handleScriptClick(script)}
className={`w-full p-4 text-left hover:bg-accent/30 transition-colors ${ className={`w-full p-4 text-left hover:bg-accent/30 transition-colors ${previewScript?.id === script.id
previewScript?.id === script.id ? "bg-primary/5 border-r-2 border-primary"
? "bg-primary/5 border-r-2 border-primary" : ""
: "" }`}
}`}
> >
<div className="flex items-start justify-between"> <div className="flex items-start justify-between">
<div className="flex-1 min-w-0"> <div className="flex-1 min-w-0">
<div className="flex items-center gap-2 mb-1"> <div className="flex items-center gap-2 mb-1">
<FileText className="h-4 w-4 text-primary flex-shrink-0" /> <FileTextIcon className="h-4 w-4 text-primary flex-shrink-0" />
<h4 className="font-medium text-foreground truncate"> <h4 className="font-medium text-foreground truncate">
{script.name} {script.name}
</h4> </h4>
{selectedScriptId === script.id && ( {selectedScriptId === script.id && (
<Check className="h-4 w-4 text-green-500 flex-shrink-0" /> <CheckIcon className="h-4 w-4 text-status-success flex-shrink-0" />
)} )}
</div> </div>
<p className="text-sm text-muted-foreground line-clamp-2"> <p className="text-sm text-muted-foreground line-clamp-2">
@@ -132,10 +145,10 @@ export function SelectScriptModal({
<div className="border border-border rounded-lg overflow-hidden"> <div className="border border-border rounded-lg overflow-hidden">
<div className="bg-muted/30 px-4 py-2 border-b border-border"> <div className="bg-muted/30 px-4 py-2 border-b border-border">
<h3 className="text-sm font-medium text-foreground"> <h3 className="text-sm font-medium text-foreground">
Script Preview {t("scripts.scriptPreview")}
</h3> </h3>
</div> </div>
<div className="p-4 h-full max-h-80 overflow-y-auto"> <div className="p-4 h-full max-h-80 overflow-y-auto tui-scrollbar">
{previewScript ? ( {previewScript ? (
<div className="space-y-4"> <div className="space-y-4">
<div> <div>
@@ -149,23 +162,23 @@ export function SelectScriptModal({
<div> <div>
<div className="flex items-center gap-2 mb-2"> <div className="flex items-center gap-2 mb-2">
<Terminal className="h-4 w-4 text-primary" /> <TerminalIcon className="h-4 w-4 text-primary" />
<span className="text-sm font-medium text-foreground"> <span className="text-sm font-medium text-foreground">
Command Preview {t("scripts.commandPreview")}
</span> </span>
</div> </div>
<div className="bg-muted/30 p-3 rounded border border-border/30"> <div className="bg-muted/30 p-3 rounded border border-border">
<code className="text-sm font-mono text-foreground break-all"> <code className="text-sm font-mono text-foreground break-all">
{getHostScriptPath(previewScript.filename)} {hostScriptPath}
</code> </code>
</div> </div>
</div> </div>
<div> <div>
<span className="text-sm font-medium text-foreground"> <span className="text-sm font-medium text-foreground">
Script Content {t("scripts.scriptContent")}
</span> </span>
<div className="bg-muted/30 p-3 rounded border border-border/30 mt-2 max-h-32 overflow-auto"> <div className="bg-muted/30 p-3 rounded border border-border mt-2 max-h-32 overflow-auto tui-scrollbar">
<pre className="text-xs font-mono text-foreground whitespace-pre-wrap"> <pre className="text-xs font-mono text-foreground whitespace-pre-wrap">
{previewContent} {previewContent}
</pre> </pre>
@@ -174,22 +187,22 @@ export function SelectScriptModal({
</div> </div>
) : ( ) : (
<div className="text-center text-muted-foreground py-8"> <div className="text-center text-muted-foreground py-8">
<FileText className="h-12 w-12 mx-auto mb-4 opacity-50" /> <FileTextIcon className="h-12 w-12 mx-auto mb-4 opacity-50" />
<p>Select a script to preview</p> <p>{t("scripts.selectScriptToPreview")}</p>
</div> </div>
)} )}
</div> </div>
</div> </div>
</div> </div>
<div className="flex justify-end gap-2 pt-3 border-t border-border/50"> <div className="flex justify-end gap-2 pt-3 border-t border-border">
<Button <Button
type="button" type="button"
variant="outline" variant="outline"
onClick={handleClose} onClick={handleClose}
className="btn-outline" className="btn-outline"
> >
Cancel {t("common.cancel")}
</Button> </Button>
<Button <Button
type="button" type="button"
@@ -197,8 +210,8 @@ export function SelectScriptModal({
disabled={!previewScript} disabled={!previewScript}
className="btn-primary glow-primary" className="btn-primary glow-primary"
> >
<Check className="h-4 w-4 mr-2" /> <CheckIcon className="h-4 w-4 mr-2" />
Select Script {t("scripts.selectScript")}
</Button> </Button>
</div> </div>
</div> </div>

View File

@@ -0,0 +1,57 @@
"use client";
import { useCallback, useEffect, useState } from "react";
type BeforeInstallPromptEvent = Event & {
prompt: () => Promise<void>;
userChoice: Promise<{ outcome: "accepted" | "dismissed" }>;
};
export const PWAInstallPrompt = (): JSX.Element | null => {
const [deferred, setDeferred] = useState<BeforeInstallPromptEvent | null>(
null
);
const [isInstalled, setIsInstalled] = useState<boolean>(false);
useEffect(() => {
if (typeof window === "undefined") return;
const onBeforeInstallPrompt = (e: Event) => {
setDeferred(e as BeforeInstallPromptEvent);
};
const onAppInstalled = () => {
setDeferred(null);
setIsInstalled(true);
};
window.addEventListener("beforeinstallprompt", onBeforeInstallPrompt);
window.addEventListener("appinstalled", onAppInstalled);
if (window.matchMedia("(display-mode: standalone)").matches) {
setIsInstalled(true);
}
return () => {
window.removeEventListener("beforeinstallprompt", onBeforeInstallPrompt);
window.removeEventListener("appinstalled", onAppInstalled);
};
}, []);
const onInstall = useCallback(async () => {
if (!deferred) return;
try {
await deferred.prompt();
const choice = await deferred.userChoice;
if (choice.outcome === "accepted") {
setDeferred(null);
}
} catch (_err) { }
}, [deferred]);
if (isInstalled || !deferred) return null;
return (
<button
className="px-3 py-2 ascii-border bg-background0 hover:bg-background1 transition-colors terminal-font text-sm"
onClick={onInstall}
>
Install
</button>
);
};

View File

@@ -0,0 +1,23 @@
"use client";
import { useEffect } from "react";
export const ServiceWorkerRegister = (): null => {
useEffect(() => {
if (typeof window === "undefined") return;
if (!("serviceWorker" in navigator)) return;
const register = async () => {
try {
const registrations = await navigator.serviceWorker.getRegistrations();
const alreadyRegistered = registrations.some((r) =>
r.scope.endsWith("/")
);
if (alreadyRegistered) return;
await navigator.serviceWorker.register("/sw.js", { scope: "/" });
} catch (_err) {}
};
register();
}, []);
return null;
};

View File

@@ -0,0 +1,251 @@
"use client";
import { useEffect, useRef, useState } from "react";
import { EditorView, keymap } from "@codemirror/view";
import { EditorState, Transaction } from "@codemirror/state";
import { shell } from "@codemirror/legacy-modes/mode/shell";
import { StreamLanguage } from "@codemirror/language";
import { catppuccinMocha, catppuccinLatte } from './catppuccin-theme';
import { useTheme } from 'next-themes';
import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { TerminalIcon, CopyIcon, CheckIcon } from "@phosphor-icons/react";
interface BashEditorProps {
value: string;
onChange: (value: string) => void;
placeholder?: string;
className?: string;
label?: string;
}
export const BashEditor = ({
value,
onChange,
placeholder = "#!/bin/bash\n# Your bash script here\necho 'Hello World'",
className = "",
label,
}: BashEditorProps) => {
const [copied, setCopied] = useState(false);
const editorRef = useRef<HTMLDivElement>(null);
const editorViewRef = useRef<EditorView | null>(null);
const { theme } = useTheme();
const insertFourSpaces = ({
state,
dispatch,
}: {
state: EditorState;
dispatch: (tr: Transaction) => void;
}) => {
if (state.selection.ranges.some((range) => !range.empty)) {
const changes = state.selection.ranges
.map((range) => {
const fromLine = state.doc.lineAt(range.from).number;
const toLine = state.doc.lineAt(range.to).number;
const changes = [];
for (let line = fromLine; line <= toLine; line++) {
const lineObj = state.doc.line(line);
changes.push({ from: lineObj.from, insert: " " });
}
return changes;
})
.flat();
dispatch(state.update({ changes }));
} else {
dispatch(state.update(state.replaceSelection(" ")));
}
return true;
};
const removeFourSpaces = ({
state,
dispatch,
}: {
state: EditorState;
dispatch: (tr: Transaction) => void;
}) => {
if (state.selection.ranges.some((range) => !range.empty)) {
const changes = state.selection.ranges
.map((range) => {
const fromLine = state.doc.lineAt(range.from).number;
const toLine = state.doc.lineAt(range.to).number;
const changes = [];
for (let line = fromLine; line <= toLine; line++) {
const lineObj = state.doc.line(line);
const indent = lineObj.text.match(/^ /);
if (indent) {
changes.push({ from: lineObj.from, to: lineObj.from + 4 });
}
}
return changes;
})
.flat();
dispatch(state.update({ changes }));
} else {
const cursor = state.selection.main.head;
const line = state.doc.lineAt(cursor);
const beforeCursor = line.text.slice(0, cursor - line.from);
const spacesToRemove = beforeCursor.match(/ {1,4}$/);
if (spacesToRemove) {
const removeCount = spacesToRemove[0].length;
dispatch(
state.update({
changes: { from: cursor - removeCount, to: cursor },
})
);
}
}
return true;
};
useEffect(() => {
if (!editorRef.current) return;
const isDark = theme === 'catppuccin-mocha';
const bashLanguage = StreamLanguage.define(shell);
const getThemeColors = () => {
const root = document.documentElement;
const style = getComputedStyle(root);
return {
background: style.getPropertyValue('--base').trim() || (isDark ? '#1e1e2e' : '#eff1f5'),
foreground: style.getPropertyValue('--text').trim() || (isDark ? '#cdd6f4' : '#4c4f69'),
border: style.getPropertyValue('--box-border-color').trim() || (isDark ? '#313244' : '#9ca0b0'),
surface: style.getPropertyValue('--surface0').trim() || (isDark ? '#313244' : '#ccd0da'),
};
};
const colors = getThemeColors();
const customTheme = EditorView.theme({
"&": {
backgroundColor: colors.background,
color: colors.foreground,
border: `1px solid ${colors.border}`,
borderRadius: '0',
},
".cm-content": {
caretColor: colors.foreground,
padding: '12px'
},
".cm-gutters": {
backgroundColor: colors.surface,
color: colors.foreground,
borderRight: `1px solid ${colors.border}`,
opacity: '0.6',
},
".cm-activeLineGutter": {
backgroundColor: colors.surface,
opacity: '1',
},
".cm-scroller": {
fontFamily: 'JetBrains Mono, Fira CodeIcon, monospace',
},
}, { dark: isDark });
const state = EditorState.create({
doc: value || placeholder,
extensions: [
bashLanguage,
customTheme,
keymap.of([
{ key: "Tab", run: insertFourSpaces },
{ key: "Shift-Tab", run: removeFourSpaces },
]),
EditorView.updateListener.of((update: any) => {
if (update.docChanged) {
onChange(update.state.doc.toString());
}
}),
EditorView.theme({
"&": {
fontSize: "14px",
fontFamily:
'JetBrains Mono, Fira CodeIcon, ui-monospace, SFMono-Regular, "SF Mono", Consolas, "Liberation Mono", Menlo, monospace',
height: "100%",
maxHeight: "100%",
},
".cm-content": {
padding: "12px",
minHeight: "200px",
},
".cm-line": {
lineHeight: "1.4",
},
".cm-scroller": {
fontFamily:
'JetBrains Mono, Fira CodeIcon, ui-monospace, SFMono-Regular, "SF Mono", Consolas, "Liberation Mono", Menlo, monospace',
height: "100%",
maxHeight: "100%",
},
}),
],
});
const view = new EditorView({
state,
parent: editorRef.current,
});
editorViewRef.current = view;
return () => {
view.destroy();
};
}, [theme]);
useEffect(() => {
if (editorViewRef.current) {
const currentValue = editorViewRef.current.state.doc.toString();
if (currentValue !== value) {
editorViewRef.current.dispatch({
changes: {
from: 0,
to: editorViewRef.current.state.doc.length,
insert: value,
},
});
}
}
}, [value]);
const handleCopy = async () => {
if (editorViewRef.current) {
const text = editorViewRef.current.state.doc.toString();
await navigator.clipboard.writeText(text);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
}
};
return (
<div className={className}>
{label && (
<div className="flex items-center justify-between mb-2">
<div className="flex items-center gap-2">
<TerminalIcon className="h-4 w-4" />
<span className="text-sm font-medium">{label}</span>
</div>
<Button
type="button"
variant="outline"
size="sm"
onClick={handleCopy}
className="btn-outline h-7 px-2"
>
{copied ? (
<CheckIcon className="h-3 w-3 mr-1" />
) : (
<CopyIcon className="h-3 w-3 mr-1" />
)}
{copied ? "Copied!" : "CopyIcon"}
</Button>
</div>
)}
<div className="overflow-hidden h-full">
<div ref={editorRef} className="h-full" />
</div>
</div>
);
};

View File

@@ -1,40 +1,42 @@
"use client"; "use client";
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { Button } from "./ui/Button"; import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { Input } from "./ui/Input"; import { Input } from "@/app/_components/GlobalComponents/FormElements/Input";
import { import {
Search, MagnifyingGlassIcon,
FileText, FileTextIcon,
FolderOpen, FolderOpen,
Code, CodeIcon,
Settings, GearIcon,
Database, Database,
Copy, CopyIcon,
Check, CheckIcon,
} from "lucide-react"; } from "@phosphor-icons/react";
import { import {
fetchSnippets, fetchSnippets,
fetchSnippetCategories, fetchSnippetCategories,
searchSnippets, searchSnippets,
type BashSnippet, type BashSnippet,
} from "../_server/actions/snippets"; } from "@/app/_server/actions/snippets";
interface BashSnippetHelperProps { interface BashSnippetHelperProps {
onInsertSnippet: (snippet: string) => void; onInsertSnippet: (snippet: string) => void;
} }
const categoryIcons = { const categoryIcons = {
"File Operations": FileText, "File Operations": FileTextIcon,
Loops: Code, Loops: CodeIcon,
Conditionals: Code, Conditionals: CodeIcon,
"System Operations": Settings, "System Operations": GearIcon,
"Database Operations": Database, "Database Operations": Database,
"User Examples": FolderOpen, "UserIcon Examples": FolderOpen,
"Custom Scripts": Code, "Custom Scripts": CodeIcon,
}; };
export function BashSnippetHelper({ onInsertSnippet }: BashSnippetHelperProps) { export const BashSnippetHelper = ({
onInsertSnippet,
}: BashSnippetHelperProps) => {
const [searchQuery, setSearchQuery] = useState(""); const [searchQuery, setSearchQuery] = useState("");
const [selectedCategory, setSelectedCategory] = useState<string | null>(null); const [selectedCategory, setSelectedCategory] = useState<string | null>(null);
const [copiedId, setCopiedId] = useState<string | null>(null); const [copiedId, setCopiedId] = useState<string | null>(null);
@@ -107,7 +109,7 @@ export function BashSnippetHelper({ onInsertSnippet }: BashSnippetHelperProps) {
return ( return (
<div className="space-y-3"> <div className="space-y-3">
<div className="text-center py-8"> <div className="text-center py-8">
<Code className="h-8 w-8 text-muted-foreground mx-auto mb-2 animate-spin" /> <CodeIcon className="h-8 w-8 text-muted-foreground mx-auto mb-2 animate-spin" />
<p className="text-sm text-muted-foreground">Loading snippets...</p> <p className="text-sm text-muted-foreground">Loading snippets...</p>
</div> </div>
</div> </div>
@@ -117,7 +119,7 @@ export function BashSnippetHelper({ onInsertSnippet }: BashSnippetHelperProps) {
return ( return (
<div className="space-y-3"> <div className="space-y-3">
<div className="relative"> <div className="relative">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" /> <MagnifyingGlassIcon className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
<Input <Input
value={searchQuery} value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)} onChange={(e) => setSearchQuery(e.target.value)}
@@ -127,7 +129,7 @@ export function BashSnippetHelper({ onInsertSnippet }: BashSnippetHelperProps) {
</div> </div>
{!searchQuery && ( {!searchQuery && (
<div className="overflow-x-auto"> <div className="overflow-x-auto tui-scrollbar">
<div className="flex gap-1 pb-2 min-w-max"> <div className="flex gap-1 pb-2 min-w-max">
<Button <Button
type="button" type="button"
@@ -140,7 +142,7 @@ export function BashSnippetHelper({ onInsertSnippet }: BashSnippetHelperProps) {
</Button> </Button>
{categories.map((category) => { {categories.map((category) => {
const Icon = const Icon =
categoryIcons[category as keyof typeof categoryIcons] || Code; categoryIcons[category as keyof typeof categoryIcons] || CodeIcon;
return ( return (
<Button <Button
key={category} key={category}
@@ -161,15 +163,15 @@ export function BashSnippetHelper({ onInsertSnippet }: BashSnippetHelperProps) {
</div> </div>
)} )}
<div className="space-y-2 overflow-y-auto custom-scrollbar"> <div className="space-y-2 overflow-y-auto !pr-0 tui-scrollbar">
{filteredSnippets.map((snippet) => { {filteredSnippets.map((snippet) => {
const Icon = const Icon =
categoryIcons[snippet.category as keyof typeof categoryIcons] || categoryIcons[snippet.category as keyof typeof categoryIcons] ||
Code; CodeIcon;
return ( return (
<div <div
key={snippet.id} key={snippet.id}
className="bg-muted/30 rounded-lg border border-border/50 p-3 hover:bg-accent/30 transition-colors" className="bg-muted/30 rounded-lg border border-border p-3 hover:bg-accent/30 transition-colors"
> >
<div className="flex-1 min-w-0"> <div className="flex-1 min-w-0">
<div className="flex items-center gap-2 mb-1"> <div className="flex items-center gap-2 mb-1">
@@ -178,7 +180,7 @@ export function BashSnippetHelper({ onInsertSnippet }: BashSnippetHelperProps) {
{snippet.title} {snippet.title}
</h4> </h4>
{snippet.source === "user" && ( {snippet.source === "user" && (
<span className="inline-block px-1.5 py-0.5 text-xs bg-green-100 text-green-700 rounded border border-green-200"> <span className="inline-block px-1.5 py-0.5 text-xs text-status-success border border-border">
User User
</span> </span>
)} )}
@@ -190,7 +192,7 @@ export function BashSnippetHelper({ onInsertSnippet }: BashSnippetHelperProps) {
{snippet.tags.slice(0, 3).map((tag) => ( {snippet.tags.slice(0, 3).map((tag) => (
<span <span
key={tag} key={tag}
className="inline-block px-2 py-1 text-xs bg-primary/10 text-primary rounded border border-primary/20" className="inline-block px-2 py-1 text-xs bg-primary/10 text-primary border border-border"
> >
{tag} {tag}
</span> </span>
@@ -207,12 +209,11 @@ export function BashSnippetHelper({ onInsertSnippet }: BashSnippetHelperProps) {
variant="outline" variant="outline"
size="sm" size="sm"
onClick={() => handleCopy(snippet)} onClick={() => handleCopy(snippet)}
className="h-6 w-8 p-0 text-xs"
> >
{copiedId === snippet.id ? ( {copiedId === snippet.id ? (
<Check className="h-3 w-3" /> <CheckIcon className="h-3 w-3" />
) : ( ) : (
<Copy className="h-3 w-3" /> <CopyIcon className="h-3 w-3" />
)} )}
</Button> </Button>
<Button <Button
@@ -220,7 +221,7 @@ export function BashSnippetHelper({ onInsertSnippet }: BashSnippetHelperProps) {
variant="default" variant="default"
size="sm" size="sm"
onClick={() => handleInsert(snippet)} onClick={() => handleInsert(snippet)}
className="h-6 px-3 text-xs flex-1" className="flex-1"
> >
Insert Insert
</Button> </Button>
@@ -232,7 +233,7 @@ export function BashSnippetHelper({ onInsertSnippet }: BashSnippetHelperProps) {
{filteredSnippets.length === 0 && ( {filteredSnippets.length === 0 && (
<div className="text-center py-8"> <div className="text-center py-8">
<Code className="h-8 w-8 text-muted-foreground mx-auto mb-2" /> <CodeIcon className="h-8 w-8 text-muted-foreground mx-auto mb-2" />
<p className="text-sm text-muted-foreground"> <p className="text-sm text-muted-foreground">
{searchQuery {searchQuery
? `No snippets found for "${searchQuery}"` ? `No snippets found for "${searchQuery}"`
@@ -243,4 +244,4 @@ export function BashSnippetHelper({ onInsertSnippet }: BashSnippetHelperProps) {
</div> </div>
</div> </div>
); );
} };

View File

@@ -5,19 +5,20 @@ import {
parseCronExpression, parseCronExpression,
cronPatterns, cronPatterns,
type CronExplanation, type CronExplanation,
} from "../_utils/cronParser"; } from "@/app/_utils/parser-utils";
import { Button } from "./ui/Button"; import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { Input } from "./ui/Input"; import { Input } from "@/app/_components/GlobalComponents/FormElements/Input";
import { import {
Clock, ClockIcon,
Info, InfoIcon,
CheckCircle, CheckCircleIcon,
AlertCircle, WarningCircleIcon,
Calendar, Calendar,
ChevronDown, CaretDownIcon,
ChevronUp, CaretUpIcon,
Search, MagnifyingGlassIcon,
} from "lucide-react"; } from "@phosphor-icons/react";
import { useLocale } from "next-intl";
interface CronExpressionHelperProps { interface CronExpressionHelperProps {
value: string; value: string;
@@ -27,13 +28,14 @@ interface CronExpressionHelperProps {
showPatterns?: boolean; showPatterns?: boolean;
} }
export function CronExpressionHelper({ export const CronExpressionHelper = ({
value, value,
onChange, onChange,
placeholder = "* * * * *", placeholder = "* * * * *",
className = "", className = "",
showPatterns = true, showPatterns = true,
}: CronExpressionHelperProps) { }: CronExpressionHelperProps) => {
const locale = useLocale();
const [explanation, setExplanation] = useState<CronExplanation | null>(null); const [explanation, setExplanation] = useState<CronExplanation | null>(null);
const [showPatternsPanel, setShowPatternsPanel] = useState(false); const [showPatternsPanel, setShowPatternsPanel] = useState(false);
const [debouncedValue, setDebouncedValue] = useState(value); const [debouncedValue, setDebouncedValue] = useState(value);
@@ -49,7 +51,7 @@ export function CronExpressionHelper({
useEffect(() => { useEffect(() => {
if (debouncedValue) { if (debouncedValue) {
const result = parseCronExpression(debouncedValue); const result = parseCronExpression(debouncedValue, locale);
setExplanation(result); setExplanation(result);
} else { } else {
setExplanation(null); setExplanation(null);
@@ -85,20 +87,20 @@ export function CronExpressionHelper({
/> />
<div className="absolute right-3 top-1/2 -translate-y-1/2"> <div className="absolute right-3 top-1/2 -translate-y-1/2">
{explanation?.isValid ? ( {explanation?.isValid ? (
<CheckCircle className="h-4 w-4 text-green-500" /> <CheckCircleIcon className="h-4 w-4 text-status-success" />
) : value ? ( ) : value ? (
<AlertCircle className="h-4 w-4 text-red-500" /> <WarningCircleIcon className="h-4 w-4 text-status-error" />
) : ( ) : (
<Clock className="h-4 w-4 text-muted-foreground" /> <ClockIcon className="h-4 w-4 text-muted-foreground" />
)} )}
</div> </div>
</div> </div>
{explanation && ( {explanation && (
<div className="bg-muted/30 rounded p-2 border border-border/30"> <div className="bg-background0 p-2 text-status-warning ascii-border terminal-font">
<div className="space-y-1"> <div className="space-y-1">
<div className="flex items-start gap-2"> <div className="flex items-start gap-2">
<Info className="h-3 w-3 text-primary mt-0.5 flex-shrink-0" /> <InfoIcon className="h-3 w-3 text-primary mt-0.5 flex-shrink-0" />
<div className="flex-1"> <div className="flex-1">
<p className="text-xs italic text-muted-foreground"> <p className="text-xs italic text-muted-foreground">
{explanation.isValid {explanation.isValid
@@ -106,7 +108,7 @@ export function CronExpressionHelper({
: "Invalid Expression"} : "Invalid Expression"}
</p> </p>
{explanation.error && ( {explanation.error && (
<p className="text-xs text-red-600 dark:text-red-400 mt-0.5"> <p className="text-xs text-status-error mt-0.5">
{explanation.error} {explanation.error}
</p> </p>
)} )}
@@ -115,7 +117,7 @@ export function CronExpressionHelper({
{explanation.isValid && explanation.nextRuns.length > 0 && ( {explanation.isValid && explanation.nextRuns.length > 0 && (
<div className="flex items-start gap-2"> <div className="flex items-start gap-2">
<Calendar className="h-3 w-3 text-blue-500 mt-0.5 flex-shrink-0" /> <Calendar className="h-3 w-3 text-status-info mt-0.5 flex-shrink-0" />
<div className="flex-1"> <div className="flex-1">
<p className="text-xs text-muted-foreground mb-1"> <p className="text-xs text-muted-foreground mb-1">
Next executions: Next executions:
@@ -135,7 +137,7 @@ export function CronExpressionHelper({
)} )}
{showPatterns && ( {showPatterns && (
<div className="bg-muted/30 rounded-lg border border-border/50"> <div className="bg-background0 ascii-border terminal-font">
<button <button
type="button" type="button"
onClick={(e) => { onClick={(e) => {
@@ -143,24 +145,24 @@ export function CronExpressionHelper({
e.stopPropagation(); e.stopPropagation();
setShowPatternsPanel(!showPatternsPanel); setShowPatternsPanel(!showPatternsPanel);
}} }}
className="w-full text-left p-3 hover:bg-accent/30 transition-colors rounded-t-lg" className="w-full text-left p-3 hover:bg-background0 transition-colors"
> >
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
<span className="text-sm font-medium">Quick Patterns</span> <span className="text-sm font-medium">Quick Patterns</span>
<div className="p-1"> <div className="p-1">
{showPatternsPanel ? ( {showPatternsPanel ? (
<ChevronUp className="h-4 w-4" /> <CaretUpIcon className="h-4 w-4" />
) : ( ) : (
<ChevronDown className="h-4 w-4" /> <CaretDownIcon className="h-4 w-4" />
)} )}
</div> </div>
</div> </div>
</button> </button>
{showPatternsPanel && ( {showPatternsPanel && (
<div className="p-3 border-t border-border/50"> <div className="p-3 border-t border-border">
<div className="relative mb-3"> <div className="relative mb-3">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" /> <MagnifyingGlassIcon className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
<Input <Input
value={patternSearch} value={patternSearch}
onChange={(e) => setPatternSearch(e.target.value)} onChange={(e) => setPatternSearch(e.target.value)}
@@ -169,7 +171,7 @@ export function CronExpressionHelper({
/> />
</div> </div>
<div className="space-y-3 max-h-64 overflow-y-auto custom-scrollbar"> <div className="space-y-3 max-h-64 overflow-y-auto tui-scrollbar">
{filteredPatterns.map((category) => ( {filteredPatterns.map((category) => (
<div key={category.category} className="space-y-2"> <div key={category.category} className="space-y-2">
<h4 className="font-medium text-foreground text-sm"> <h4 className="font-medium text-foreground text-sm">

View File

@@ -1,19 +1,18 @@
"use client"; "use client";
import { useState } from "react"; import { useState, useEffect } from "react";
import { Card, CardContent, CardHeader, CardTitle } from "./ui/Card"; import { Card, CardContent, CardHeader, CardTitle } from "@/app/_components/GlobalComponents/Cards/Card";
import { Button } from "./ui/Button"; import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { import {
FileText, FileTextIcon,
Plus, PlusIcon,
Edit, PencilSimpleIcon,
Trash2, TrashIcon,
Copy, CopyIcon,
Copy as CopyIcon, CheckCircleIcon,
CheckCircle, FilesIcon,
Files, } from "@phosphor-icons/react";
} from "lucide-react"; import { Script } from "@/app/_utils/scripts-utils";
import { type Script } from "@/app/_server/actions/scripts";
import { import {
createScript, createScript,
updateScript, updateScript,
@@ -21,19 +20,22 @@ import {
cloneScript, cloneScript,
getScriptContent, getScriptContent,
} from "@/app/_server/actions/scripts"; } from "@/app/_server/actions/scripts";
import { CreateScriptModal } from "./modals/CreateScriptModal"; import { CreateScriptModal } from "@/app/_components/FeatureComponents/Modals/CreateScriptModal";
import { EditScriptModal } from "./modals/EditScriptModal"; import { EditScriptModal } from "@/app/_components/FeatureComponents/Modals/EditScriptModal";
import { DeleteScriptModal } from "./modals/DeleteScriptModal"; import { DeleteScriptModal } from "@/app/_components/FeatureComponents/Modals/DeleteScriptModal";
import { CloneScriptModal } from "./modals/CloneScriptModal"; import { CloneScriptModal } from "@/app/_components/FeatureComponents/Modals/CloneScriptModal";
import { showToast } from "./ui/Toast"; import { showToast } from "@/app/_components/GlobalComponents/UIElements/Toast";
import { useTranslations } from "next-intl";
interface ScriptsManagerProps { interface ScriptsManagerProps {
scripts: Script[]; scripts: Script[];
} }
export function ScriptsManager({ const DRAFT_STORAGE_KEY = "cronjob_script_draft";
export const ScriptsManager = ({
scripts: initialScripts, scripts: initialScripts,
}: ScriptsManagerProps) { }: ScriptsManagerProps) => {
const [scripts, setScripts] = useState<Script[]>(initialScripts); const [scripts, setScripts] = useState<Script[]>(initialScripts);
const [isCreateModalOpen, setIsCreateModalOpen] = useState(false); const [isCreateModalOpen, setIsCreateModalOpen] = useState(false);
const [isEditModalOpen, setIsEditModalOpen] = useState(false); const [isEditModalOpen, setIsEditModalOpen] = useState(false);
@@ -43,12 +45,15 @@ export function ScriptsManager({
const [copiedId, setCopiedId] = useState<string | null>(null); const [copiedId, setCopiedId] = useState<string | null>(null);
const [isDeleting, setIsDeleting] = useState(false); const [isDeleting, setIsDeleting] = useState(false);
const [isCloning, setIsCloning] = useState(false); const [isCloning, setIsCloning] = useState(false);
const t = useTranslations();
const [createForm, setCreateForm] = useState({ const defaultFormValues = {
name: "", name: "",
description: "", description: "",
content: "#!/bin/bash\n# Your script here\necho 'Hello World'", content: "#!/bin/bash\n# Your script here\necho 'Hello World'",
}); };
const [createForm, setCreateForm] = useState(defaultFormValues);
const [editForm, setEditForm] = useState({ const [editForm, setEditForm] = useState({
name: "", name: "",
@@ -56,6 +61,37 @@ export function ScriptsManager({
content: "", content: "",
}); });
useEffect(() => {
try {
const savedDraft = localStorage.getItem(DRAFT_STORAGE_KEY);
if (savedDraft) {
const parsedDraft = JSON.parse(savedDraft);
setCreateForm(parsedDraft);
}
} catch (error) {
console.error("Failed to load draft from localStorage:", error);
}
}, []);
useEffect(() => {
try {
localStorage.setItem(DRAFT_STORAGE_KEY, JSON.stringify(createForm));
} catch (error) {
console.error("Failed to save draft to localStorage:", error);
}
}, [createForm]);
const isDraft =
createForm.name.trim() !== "" ||
createForm.description.trim() !== "" ||
createForm.content !== defaultFormValues.content;
const handleClearDraft = () => {
setCreateForm(defaultFormValues);
localStorage.removeItem(DRAFT_STORAGE_KEY);
showToast("success", t("scripts.draftCleared"));
};
const refreshScripts = async () => { const refreshScripts = async () => {
try { try {
const { fetchScripts } = await import("@/app/_server/actions/scripts"); const { fetchScripts } = await import("@/app/_server/actions/scripts");
@@ -76,6 +112,8 @@ export function ScriptsManager({
if (result.success) { if (result.success) {
await refreshScripts(); await refreshScripts();
setIsCreateModalOpen(false); setIsCreateModalOpen(false);
setCreateForm(defaultFormValues);
localStorage.removeItem(DRAFT_STORAGE_KEY);
showToast("success", "Script created successfully"); showToast("success", "Script created successfully");
} else { } else {
showToast("error", "Failed to create script", result.message); showToast("error", "Failed to create script", result.message);
@@ -168,15 +206,15 @@ export function ScriptsManager({
<CardHeader> <CardHeader>
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4"> <div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
<div className="flex items-center gap-3"> <div className="flex items-center gap-3">
<div className="p-2 bg-primary/10 rounded-lg"> <div className="p-2 bg-background0 ascii-border">
<FileText className="h-5 w-5 text-primary" /> <FileTextIcon className="h-5 w-5 text-primary" />
</div> </div>
<div> <div>
<CardTitle className="text-xl brand-gradient"> <CardTitle className="text-xl brand-gradient">
Scripts Library {t("scripts.scriptsLibrary")}
</CardTitle> </CardTitle>
<p className="text-sm text-muted-foreground"> <p className="text-sm text-muted-foreground">
{scripts.length} saved script{scripts.length !== 1 ? "s" : ""} {t("scripts.nOfNSavedScripts", { count: scripts.length })}
</p> </p>
</div> </div>
</div> </div>
@@ -184,30 +222,30 @@ export function ScriptsManager({
onClick={() => setIsCreateModalOpen(true)} onClick={() => setIsCreateModalOpen(true)}
className="btn-primary glow-primary" className="btn-primary glow-primary"
> >
<Plus className="h-4 w-4 mr-2" /> <PlusIcon className="h-4 w-4 mr-2" />
New Script {t("scripts.newScript")}
</Button> </Button>
</div> </div>
</CardHeader> </CardHeader>
<CardContent> <CardContent>
{scripts.length === 0 ? ( {scripts.length === 0 ? (
<div className="text-center py-16"> <div className="text-center py-16 terminal-font">
<div className="mx-auto w-20 h-20 bg-gradient-to-br from-primary/20 to-blue-500/20 rounded-full flex items-center justify-center mb-6"> <div className="mx-auto w-20 h-20 bg-background2 ascii-border flex items-center justify-center mb-6">
<FileText className="h-10 w-10 text-primary" /> <FileTextIcon className="h-10 w-10 text-primary" />
</div> </div>
<h3 className="text-xl font-semibold mb-3 brand-gradient"> <h3 className="text-xl font-semibold mb-3 brand-gradient">
No scripts yet {t("scripts.noScriptsYet")}
</h3> </h3>
<p className="text-muted-foreground mb-6 max-w-md mx-auto"> <p className="text-muted-foreground mb-6 max-w-md mx-auto">
Create reusable bash scripts to use in your scheduled tasks. {t("scripts.createReusableBashScripts")}
</p> </p>
<Button <Button
onClick={() => setIsCreateModalOpen(true)} onClick={() => setIsCreateModalOpen(true)}
className="btn-primary glow-primary" className="btn-primary glow-primary"
size="lg" size="lg"
> >
<Plus className="h-5 w-5 mr-2" /> <PlusIcon className="h-5 w-5 mr-2" />
Create Your First Script {t("scripts.createYourFirstScript")}
</Button> </Button>
</div> </div>
) : ( ) : (
@@ -215,7 +253,7 @@ export function ScriptsManager({
{scripts.map((script) => ( {scripts.map((script) => (
<div <div
key={script.id} key={script.id}
className="glass-card p-4 border border-border/50 rounded-lg hover:bg-accent/30 transition-colors" className="glass-card p-4 ascii-border hover:bg-accent/30 transition-colors terminal-font"
> >
<div className="flex items-start justify-between gap-4"> <div className="flex items-start justify-between gap-4">
<div className="flex-1 min-w-0"> <div className="flex-1 min-w-0">
@@ -233,7 +271,7 @@ export function ScriptsManager({
</p> </p>
)} )}
<div className="text-xs text-muted-foreground"> <div className="text-xs text-muted-foreground">
File: {script.filename} {t("scripts.file")}: {script.filename}
</div> </div>
</div> </div>
@@ -243,11 +281,11 @@ export function ScriptsManager({
size="sm" size="sm"
onClick={() => handleCopy(script)} onClick={() => handleCopy(script)}
className="btn-outline h-8 px-3" className="btn-outline h-8 px-3"
title="Copy script content to clipboard" title="CopyIcon script content to clipboard"
aria-label="Copy script content to clipboard" aria-label="CopyIcon script content to clipboard"
> >
{copiedId === script.id ? ( {copiedId === script.id ? (
<CheckCircle className="h-3 w-3 text-green-500" /> <CheckCircleIcon className="h-3 w-3 text-status-success" />
) : ( ) : (
<CopyIcon className="h-3 w-3" /> <CopyIcon className="h-3 w-3" />
)} )}
@@ -263,7 +301,7 @@ export function ScriptsManager({
title="Clone script" title="Clone script"
aria-label="Clone script" aria-label="Clone script"
> >
<Files className="h-3 w-3" /> <FilesIcon className="h-3 w-3" />
</Button> </Button>
<Button <Button
variant="outline" variant="outline"
@@ -284,7 +322,7 @@ export function ScriptsManager({
title="Edit script" title="Edit script"
aria-label="Edit script" aria-label="Edit script"
> >
<Edit className="h-3 w-3" /> <PencilSimpleIcon className="h-3 w-3" />
</Button> </Button>
<Button <Button
variant="destructive" variant="destructive"
@@ -293,11 +331,11 @@ export function ScriptsManager({
setSelectedScript(script); setSelectedScript(script);
setIsDeleteModalOpen(true); setIsDeleteModalOpen(true);
}} }}
className="btn-destructive h-8 px-3" className="h-8 px-3"
title="Delete script" title="Delete script"
aria-label="Delete script" aria-label="Delete script"
> >
<Trash2 className="h-3 w-3" /> <TrashIcon className="h-3 w-3" />
</Button> </Button>
</div> </div>
</div> </div>
@@ -316,6 +354,8 @@ export function ScriptsManager({
onFormChange={(updates) => onFormChange={(updates) =>
setCreateForm((prev) => ({ ...prev, ...updates })) setCreateForm((prev) => ({ ...prev, ...updates }))
} }
isDraft={isDraft}
onClearDraft={handleClearDraft}
/> />
<EditScriptModal <EditScriptModal

View File

@@ -0,0 +1,36 @@
import { Extension } from '@codemirror/state';
import { EditorView } from '@codemirror/view';
export const catppuccinMocha: Extension = EditorView.theme({
'&': {
backgroundColor: '#1e1e2e',
color: '#cdd6f4',
fontSize: '14px',
fontFamily: 'JetBrains Mono, monospace',
border: '1px solid #45475a',
borderRadius: '0',
},
'.cm-content': { caretColor: '#f5e0dc', padding: '12px' },
'.cm-gutters': {
backgroundColor: '#181825',
color: '#6c7086',
borderRight: '1px solid #45475a',
},
}, { dark: true });
export const catppuccinLatte: Extension = EditorView.theme({
'&': {
backgroundColor: '#eff1f5',
color: '#4c4f69',
fontSize: '14px',
fontFamily: 'JetBrains Mono, monospace',
border: '1px solid #9ca0b0',
borderRadius: '0',
},
'.cm-content': { caretColor: '#dc8a78', padding: '12px' },
'.cm-gutters': {
backgroundColor: '#e6e9ef',
color: '#8c8fa1',
borderRight: '1px solid #9ca0b0',
},
}, { dark: false });

View File

@@ -1,7 +1,7 @@
import { cn } from "@/app/_utils/cn"; import { cn } from "@/app/_utils/global-utils";
import { HTMLAttributes, forwardRef } from "react"; import { HTMLAttributes, forwardRef } from "react";
import { Zap } from "lucide-react"; import { LightningIcon } from "@phosphor-icons/react";
import { StatusBadge } from "./StatusBadge"; import { StatusBadge } from "@/app/_components/GlobalComponents/Badges/StatusBadge";
export interface PerformanceMetric { export interface PerformanceMetric {
label: string; label: string;
@@ -14,20 +14,20 @@ export interface PerformanceSummaryProps
metrics: PerformanceMetric[]; metrics: PerformanceMetric[];
} }
const PerformanceSummary = forwardRef<HTMLDivElement, PerformanceSummaryProps>( export const PerformanceSummary = forwardRef<HTMLDivElement, PerformanceSummaryProps>(
({ className, metrics, ...props }, ref) => { ({ className, metrics, ...props }, ref) => {
return ( return (
<div <div
ref={ref} ref={ref}
className={cn( className={cn(
"p-3 bg-gradient-to-r from-purple-500/5 to-pink-500/5 border border-purple-500/20 rounded-lg glass-card", "p-3 bg-background0 ascii-border glass-card terminal-font",
className className
)} )}
{...props} {...props}
> >
<div className="flex items-center gap-2 mb-3"> <div className="flex items-center gap-2 mb-3">
<Zap className="h-4 w-4 text-purple-500" /> <LightningIcon className="h-4 w-4" />
<span className="text-sm font-medium text-purple-600 dark:text-purple-400"> <span className="text-sm font-medium">
Performance Summary Performance Summary
</span> </span>
</div> </div>
@@ -58,5 +58,3 @@ const PerformanceSummary = forwardRef<HTMLDivElement, PerformanceSummaryProps>(
); );
PerformanceSummary.displayName = "PerformanceSummary"; PerformanceSummary.displayName = "PerformanceSummary";
export { PerformanceSummary };

View File

@@ -1,16 +1,10 @@
"use client"; "use client";
import { MetricCard } from "./ui/MetricCard"; import { MetricCard } from "@/app/_components/GlobalComponents/Cards/MetricCard";
import { SystemStatus } from "./ui/SystemStatus"; import { SystemStatus } from "@/app/_components/FeatureComponents/System/SystemStatus";
import { PerformanceSummary } from "./ui/PerformanceSummary"; import { PerformanceSummary } from "@/app/_components/FeatureComponents/System/PerformanceSummary";
import { Sidebar } from "./ui/Sidebar"; import { Sidebar } from "@/app/_components/FeatureComponents/Layout/Sidebar";
import { import { ClockIcon, HardDriveIcon, CpuIcon, MonitorIcon, WifiHighIcon } from "@phosphor-icons/react";
Clock,
HardDrive,
Cpu,
Monitor,
Wifi,
} from "lucide-react";
interface SystemInfoType { interface SystemInfoType {
hostname: string; hostname: string;
@@ -54,68 +48,119 @@ interface SystemInfoType {
details: string; details: string;
}; };
} }
import { useState, useEffect } from "react"; import { useState, useEffect, useRef } from "react";
import { useTranslations } from "next-intl";
import { useSSEContext } from "@/app/_contexts/SSEContext";
import { SSEEvent } from "@/app/_utils/sse-events";
import { usePageVisibility } from "@/app/_hooks/usePageVisibility";
interface SystemInfoCardProps { interface SystemInfoCardProps {
systemInfo: SystemInfoType; systemInfo: SystemInfoType;
} }
export function SystemInfoCard({ export const SystemInfoCard = ({
systemInfo: initialSystemInfo, systemInfo: initialSystemInfo,
}: SystemInfoCardProps) { }: SystemInfoCardProps) => {
const [currentTime, setCurrentTime] = useState<string>(""); const [currentTime, setCurrentTime] = useState<string>("");
const [systemInfo, setSystemInfo] = const [systemInfo, setSystemInfo] =
useState<SystemInfoType>(initialSystemInfo); useState<SystemInfoType>(initialSystemInfo);
const [isUpdating, setIsUpdating] = useState(false); const [isUpdating, setIsUpdating] = useState(false);
const [isDisabled, setIsDisabled] = useState(false);
const t = useTranslations();
const { subscribe } = useSSEContext();
const isPageVisible = usePageVisibility();
const abortControllerRef = useRef<AbortController | null>(null);
const updateSystemInfo = async () => { const updateSystemInfo = async () => {
if (isDisabled) {
return;
}
if (abortControllerRef.current) {
abortControllerRef.current.abort();
}
const abortController = new AbortController();
abortControllerRef.current = abortController;
try { try {
setIsUpdating(true); setIsUpdating(true);
const response = await fetch('/api/system-stats'); const response = await fetch("/api/system-stats", {
signal: abortController.signal,
});
if (!response.ok) { if (!response.ok) {
throw new Error('Failed to fetch system stats'); throw new Error("Failed to fetch system stats");
} }
const freshData = await response.json(); const freshData = await response.json();
if (freshData === null) {
setIsDisabled(true);
return;
}
setSystemInfo(freshData); setSystemInfo(freshData);
} catch (error) { } catch (error: any) {
console.error("Failed to update system info:", error); if (error.name !== "AbortError") {
console.error("Failed to update system info:", error);
}
} finally { } finally {
setIsUpdating(false); if (!abortControllerRef.current?.signal.aborted) {
setIsUpdating(false);
}
} }
}; };
useEffect(() => {
const unsubscribe = subscribe((event: SSEEvent) => {
if (event.type === "system-stats" && event.data !== null) {
setSystemInfo(event.data);
}
});
return unsubscribe;
}, [subscribe]);
useEffect(() => { useEffect(() => {
const updateTime = () => { const updateTime = () => {
setCurrentTime(new Date().toLocaleTimeString()); setCurrentTime(new Date().toLocaleTimeString());
}; };
updateTime(); updateTime();
updateSystemInfo();
if (isPageVisible) {
updateSystemInfo();
}
const updateInterval = parseInt( const updateInterval = parseInt(
process.env.NEXT_PUBLIC_CLOCK_UPDATE_INTERVAL || "30000" process.env.NEXT_PUBLIC_CLOCK_UPDATE_INTERVAL || "30000"
); );
let mounted = true; let mounted = true;
let timeoutId: NodeJS.Timeout | null = null;
const doUpdate = () => { const doUpdate = () => {
if (!mounted) return; if (!mounted || !isPageVisible || isDisabled) return;
updateTime(); updateTime();
updateSystemInfo().finally(() => { updateSystemInfo().finally(() => {
if (mounted) { if (mounted && isPageVisible && !isDisabled) {
setTimeout(doUpdate, updateInterval); timeoutId = setTimeout(doUpdate, updateInterval);
} }
}); });
}; };
setTimeout(doUpdate, updateInterval); if (isPageVisible && !isDisabled) {
timeoutId = setTimeout(doUpdate, updateInterval);
}
return () => { return () => {
mounted = false; mounted = false;
if (timeoutId) {
clearTimeout(timeoutId);
}
if (abortControllerRef.current) {
abortControllerRef.current.abort();
}
}; };
}, []); }, [isPageVisible, isDisabled]);
const quickStats = { const quickStats = {
cpu: systemInfo.cpu.usage, cpu: systemInfo.cpu.usage,
@@ -125,78 +170,77 @@ export function SystemInfoCard({
const basicInfoItems = [ const basicInfoItems = [
{ {
icon: Clock, icon: ClockIcon,
label: "Uptime", label: t("sidebar.uptime"),
value: systemInfo.uptime, value: systemInfo.uptime,
color: "text-orange-500",
}, },
]; ];
const performanceItems = [ const performanceItems = [
{ {
icon: HardDrive, icon: HardDriveIcon,
label: "Memory", label: t("sidebar.memory"),
value: `${systemInfo.memory.used} / ${systemInfo.memory.total}`, value: `${systemInfo.memory.used} / ${systemInfo.memory.total}`,
detail: `${systemInfo.memory.free} free`, detail: `${systemInfo.memory.free} free`,
status: systemInfo.memory.status, status: systemInfo.memory.status,
color: "text-cyan-500",
showProgress: true, showProgress: true,
progressValue: systemInfo.memory.usage, progressValue: systemInfo.memory.usage,
}, },
{ {
icon: Cpu, icon: CpuIcon,
label: "CPU", label: t("sidebar.cpu"),
value: systemInfo.cpu.model, value: systemInfo.cpu.model,
detail: `${systemInfo.cpu.cores} cores`, detail: `${systemInfo.cpu.cores} cores`,
status: systemInfo.cpu.status, status: systemInfo.cpu.status,
color: "text-pink-500",
showProgress: true, showProgress: true,
progressValue: systemInfo.cpu.usage, progressValue: systemInfo.cpu.usage,
}, },
{ {
icon: Monitor, icon: MonitorIcon,
label: "GPU", label: t("sidebar.gpu"),
value: systemInfo.gpu.model, value: systemInfo.gpu.model,
detail: systemInfo.gpu.memory detail: systemInfo.gpu.memory
? `${systemInfo.gpu.memory} VRAM` ? `${systemInfo.gpu.memory} VRAM`
: systemInfo.gpu.status, : systemInfo.gpu.status,
status: systemInfo.gpu.status, status: systemInfo.gpu.status,
color: "text-indigo-500",
}, },
...(systemInfo.network ? [{ ...(systemInfo.network
icon: Wifi, ? [
label: "Network", {
value: `${systemInfo.network.latency}ms`, icon: WifiHighIcon,
detail: `${systemInfo.network.latency}ms latency • ${systemInfo.network.speed}`, label: t("sidebar.network"),
status: systemInfo.network.status, value: `${systemInfo.network.latency}ms`,
color: "text-teal-500", detail: `${systemInfo.network.latency}ms latency • ${systemInfo.network.speed}`,
}] : []), status: systemInfo.network.status,
},
]
: []),
]; ];
const performanceMetrics = [ const performanceMetrics = [
{ {
label: "CPU Usage", label: t("sidebar.cpuUsage"),
value: `${systemInfo.cpu.usage}%`, value: `${systemInfo.cpu.usage}%`,
status: systemInfo.cpu.status, status: systemInfo.cpu.status,
}, },
{ {
label: "Memory Usage", label: t("sidebar.memoryUsage"),
value: `${systemInfo.memory.usage}%`, value: `${systemInfo.memory.usage}%`,
status: systemInfo.memory.status, status: systemInfo.memory.status,
}, },
...(systemInfo.network ? [{ ...(systemInfo.network
label: "Network Latency", ? [
value: `${systemInfo.network.latency}ms`, {
status: systemInfo.network.status, label: t("sidebar.networkLatency"),
}] : []), value: `${systemInfo.network.latency}ms`,
status: systemInfo.network.status,
},
]
: []),
]; ];
return ( return (
<Sidebar <Sidebar defaultCollapsed={false} quickStats={quickStats}>
title="System Overview"
defaultCollapsed={false}
quickStats={quickStats}
>
<SystemStatus <SystemStatus
status={systemInfo.systemStatus.overall} status={systemInfo.systemStatus.overall}
details={systemInfo.systemStatus.details} details={systemInfo.systemStatus.details}
@@ -206,7 +250,7 @@ export function SystemInfoCard({
<div> <div>
<h3 className="text-xs font-semibold text-foreground mb-2 uppercase tracking-wide"> <h3 className="text-xs font-semibold text-foreground mb-2 uppercase tracking-wide">
System Information {t("sidebar.systemInformation")}
</h3> </h3>
<div className="space-y-2"> <div className="space-y-2">
{basicInfoItems.map((item) => ( {basicInfoItems.map((item) => (
@@ -215,7 +259,6 @@ export function SystemInfoCard({
icon={item.icon} icon={item.icon}
label={item.label} label={item.label}
value={item.value} value={item.value}
color={item.color}
variant="basic" variant="basic"
/> />
))} ))}
@@ -224,9 +267,9 @@ export function SystemInfoCard({
<div> <div>
<h3 className="text-xs font-semibold text-foreground mb-2 uppercase tracking-wide"> <h3 className="text-xs font-semibold text-foreground mb-2 uppercase tracking-wide">
Performance Metrics {t("sidebar.performanceMetrics")}
</h3> </h3>
<div className="space-y-2"> <div className="space-y-4">
{performanceItems.map((item) => ( {performanceItems.map((item) => (
<MetricCard <MetricCard
key={item.label} key={item.label}
@@ -235,7 +278,6 @@ export function SystemInfoCard({
value={item.value} value={item.value}
detail={item.detail} detail={item.detail}
status={item.status} status={item.status}
color={item.color}
variant="performance" variant="performance"
showProgress={item.showProgress} showProgress={item.showProgress}
progressValue={item.progressValue} progressValue={item.progressValue}
@@ -247,16 +289,16 @@ export function SystemInfoCard({
<PerformanceSummary metrics={performanceMetrics} /> <PerformanceSummary metrics={performanceMetrics} />
<div className="text-xs text-muted-foreground text-center p-2 bg-muted/20 rounded-lg"> <div className="text-xs text-muted-foreground text-center p-2 bg-muted/20 rounded-lg">
💡 Stats update every{" "} {t("sidebar.statsUpdateEvery")}{" "}
{Math.round( {Math.round(
parseInt(process.env.NEXT_PUBLIC_CLOCK_UPDATE_INTERVAL || "30000") / parseInt(process.env.NEXT_PUBLIC_CLOCK_UPDATE_INTERVAL || "30000") /
1000 1000
)} )}
s Network speed estimated from latency s {t("sidebar.networkSpeedEstimatedFromLatency")}
{isUpdating && ( {isUpdating && (
<span className="ml-2 animate-pulse">🔄 Updating...</span> <span className="ml-2 animate-pulse">{t("sidebar.updating")}...</span>
)} )}
</div> </div>
</Sidebar> </Sidebar>
); );
} };

View File

@@ -1,7 +1,7 @@
import { cn } from "@/app/_utils/cn"; import { cn } from "@/app/_utils/global-utils";
import { HTMLAttributes, forwardRef } from "react"; import { HTMLAttributes, forwardRef } from "react";
import { Activity } from "lucide-react"; import { PulseIcon } from "@phosphor-icons/react";
import { StatusBadge } from "./StatusBadge"; import { useTranslations } from "next-intl";
export interface SystemStatusProps extends HTMLAttributes<HTMLDivElement> { export interface SystemStatusProps extends HTMLAttributes<HTMLDivElement> {
status: string; status: string;
@@ -10,38 +10,39 @@ export interface SystemStatusProps extends HTMLAttributes<HTMLDivElement> {
isUpdating?: boolean; isUpdating?: boolean;
} }
const SystemStatus = forwardRef<HTMLDivElement, SystemStatusProps>( export const SystemStatus = forwardRef<HTMLDivElement, SystemStatusProps>(
( (
{ className, status, details, timestamp, isUpdating = false, ...props }, { className, status, details, timestamp, isUpdating = false, ...props },
ref ref
) => { ) => {
const t = useTranslations();
const getStatusConfig = (status: string) => { const getStatusConfig = (status: string) => {
const lowerStatus = status.toLowerCase(); const lowerStatus = status.toLowerCase();
switch (lowerStatus) { switch (lowerStatus) {
case "operational": case "operational":
return { return {
bgColor: "bg-emerald-500/10", bgColor: "bg-background0",
borderColor: "border-emerald-500/20", borderColor: "ascii-border",
dotColor: "bg-emerald-500", dotColor: "bg-status-success",
}; };
case "warning": case "warning":
return { return {
bgColor: "bg-yellow-500/10", bgColor: "bg-background0",
borderColor: "border-yellow-500/20", borderColor: "ascii-border",
dotColor: "bg-yellow-500", dotColor: "bg-status-warning",
}; };
case "critical": case "critical":
return { return {
bgColor: "bg-destructive/10", bgColor: "bg-background0",
borderColor: "border-destructive/20", borderColor: "ascii-border",
dotColor: "bg-destructive", dotColor: "bg-status-error",
}; };
default: default:
return { return {
bgColor: "bg-muted", bgColor: "bg-background0",
borderColor: "border-border", borderColor: "ascii-border",
dotColor: "bg-muted-foreground", dotColor: "bg-status-success",
}; };
} }
}; };
@@ -52,7 +53,7 @@ const SystemStatus = forwardRef<HTMLDivElement, SystemStatusProps>(
<div <div
ref={ref} ref={ref}
className={cn( className={cn(
"p-4 border border-border/50 rounded-lg glass-card", "p-4 glass-card terminal-font",
config.bgColor, config.bgColor,
config.borderColor, config.borderColor,
className className
@@ -60,16 +61,16 @@ const SystemStatus = forwardRef<HTMLDivElement, SystemStatusProps>(
{...props} {...props}
> >
<div className="flex items-center gap-3"> <div className="flex items-center gap-3">
<div className={cn("w-3 h-3 rounded-full", config.dotColor)} /> <div className={cn("w-3 h-3", config.dotColor)} />
<div className="flex-1"> <div className="flex-1">
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<Activity className="h-4 w-4 text-muted-foreground" /> <PulseIcon className="h-4 w-4 text-muted-foreground" />
<span className="text-sm font-medium text-foreground"> <span className="text-sm font-medium text-foreground">
System Status: {status} {t("system.systemStatus")}: {status}
</span> </span>
</div> </div>
<p className="text-xs text-muted-foreground mt-1"> <p className="text-xs text-muted-foreground mt-1">
{details} Last updated: {timestamp} {details} {t("system.lastUpdated")}: {timestamp}
{isUpdating && <span className="ml-2 animate-pulse">🔄</span>} {isUpdating && <span className="ml-2 animate-pulse">🔄</span>}
</p> </p>
</div> </div>
@@ -80,5 +81,3 @@ const SystemStatus = forwardRef<HTMLDivElement, SystemStatusProps>(
); );
SystemStatus.displayName = "SystemStatus"; SystemStatus.displayName = "SystemStatus";
export { SystemStatus };

View File

@@ -0,0 +1,69 @@
"use client";
import { useState, useEffect } from "react";
import { useTranslations } from "next-intl";
import { WarningIcon, XIcon } from "@phosphor-icons/react";
export const WrapperScriptWarning = () => {
const [isVisible, setIsVisible] = useState(false);
const [isLoading, setIsLoading] = useState(true);
const t = useTranslations();
useEffect(() => {
const dismissed = localStorage.getItem("wrapper-warning-dismissed");
if (dismissed === "true") {
setIsLoading(false);
return;
}
checkWrapperScriptModification();
}, []);
const checkWrapperScriptModification = async () => {
try {
const response = await fetch("/api/system/wrapper-check");
if (response.ok) {
const data = await response.json();
setIsVisible(data.modified);
}
} catch (error) {
console.error("Failed to check wrapper script:", error);
} finally {
setIsLoading(false);
}
};
const dismissWarning = () => {
setIsVisible(false);
localStorage.setItem("wrapper-warning-dismissed", "true");
};
if (isLoading || !isVisible) {
return null;
}
return (
<div className="bg-amber-500/10 border border-amber-500/20 rounded-lg p-4 mb-4">
<div className="flex items-start justify-between">
<div className="flex items-start space-x-3">
<WarningIcon className="w-5 h-5 text-amber-500 mt-0.5 flex-shrink-0" />
<div className="flex-1">
<h3 className="text-sm font-medium text-amber-800 dark:text-amber-400">
{t("warnings.wrapperScriptModified")}
</h3>
<p className="text-sm text-amber-700 dark:text-amber-500 mt-1">
{t("warnings.wrapperScriptModifiedDescription")}
</p>
</div>
</div>
<button
onClick={dismissWarning}
className="text-amber-600 dark:text-amber-400 hover:text-amber-800 dark:hover:text-amber-300 transition-colors ml-4"
aria-label="Dismiss warning"
>
<XIcon className="w-4 h-4" />
</button>
</div>
</div>
);
};

View File

@@ -0,0 +1,33 @@
'use client'
import { useTheme } from 'next-themes';
import { useEffect, useState } from 'react';
import { SunIcon, MoonIcon } from '@phosphor-icons/react';
export const ThemeToggle = () => {
const [mounted, setMounted] = useState(false);
const { theme, setTheme } = useTheme();
useEffect(() => {
setMounted(true);
}, []);
if (!mounted) return null;
const isDark = theme === 'dark';
return (
<button
onClick={() => setTheme(isDark ? 'light' : 'dark')}
className="p-2 ascii-border bg-background0 hover:bg-background1 transition-colors"
aria-label={isDark ? 'Switch to light mode' : 'Switch to dark mode'}
title={isDark ? 'Switch to light mode' : 'Switch to dark mode'}
>
{isDark ? (
<SunIcon size={20} weight="regular" className="text-foreground" />
) : (
<MoonIcon size={20} weight="regular" className="text-foreground" />
)}
</button>
);
};

View File

@@ -0,0 +1,110 @@
"use client";
import { useState, useEffect } from "react";
import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { CaretDownIcon, UserIcon, XIcon } from "@phosphor-icons/react";
import { fetchAvailableUsers } from "@/app/_server/actions/cronjobs";
import { useTranslations } from "next-intl";
interface UserFilterProps {
selectedUser: string | null;
onUserChange: (user: string | null) => void;
className?: string;
}
export const UserFilter = ({
selectedUser,
onUserChange,
className = "",
}: UserFilterProps) => {
const [users, setUsers] = useState<string[]>([]);
const [isOpen, setIsOpen] = useState(false);
const [isLoading, setIsLoading] = useState(true);
const t = useTranslations();
useEffect(() => {
const loadUsers = async () => {
try {
const availableUsers = await fetchAvailableUsers();
setUsers(availableUsers);
} catch (error) {
console.error("Error loading users:", error);
} finally {
setIsLoading(false);
}
};
loadUsers();
}, []);
if (isLoading) {
return (
<div
className={`flex items-center gap-2 px-3 py-2 bg-muted/50 rounded-md ${className}`}
>
<UserIcon className="h-4 w-4 text-muted-foreground" />
<span className="text-sm text-muted-foreground">Loading users...</span>
</div>
);
}
return (
<div className={`relative ${className}`}>
<div className="flex items-center gap-1">
<Button
variant="outline"
onClick={() => setIsOpen(!isOpen)}
className="flex-1 justify-between"
>
<div className="flex items-center gap-2">
<UserIcon className="h-4 w-4" />
<span className="text-sm">
{selectedUser
? `${t("common.userWithUsername", { user: selectedUser })}`
: t("common.allUsers")}
</span>
</div>
<CaretDownIcon className="h-4 w-4" />
</Button>
{selectedUser && (
<Button
variant="ghost"
size="sm"
onClick={() => onUserChange(null)}
className="p-2 h-8 w-8 flex-shrink-0"
>
<XIcon className="h-3 w-3" />
</Button>
)}
</div>
{isOpen && (
<div className="absolute top-full left-0 right-0 p-1 mt-1 bg-background0 border border-border rounded-md shadow-lg z-50 max-h-48 overflow-y-auto tui-scrollbar">
<button
onClick={() => {
onUserChange(null);
setIsOpen(false);
}}
className={`w-full text-left px-3 py-2 text-sm hover:border-border transition-colors ${!selectedUser ? "border border-border" : "border border-transparent"
}`}
>
{t("common.allUsers")}
</button>
{users.map((user) => (
<button
key={user}
onClick={() => {
onUserChange(user);
setIsOpen(false);
}}
className={`w-full text-left px-3 py-2 text-sm border border-transparent hover:border-border transition-colors ${selectedUser === user ? "border border-border" : "border border-transparent"
}`}
>
{user}
</button>
))}
</div>
)}
</div>
);
};

View File

@@ -0,0 +1,93 @@
"use client";
import { useState, useEffect } from "react";
import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { CaretDownIcon, UserIcon } from "@phosphor-icons/react";
import { fetchAvailableUsers } from "@/app/_server/actions/cronjobs";
interface UserSwitcherProps {
selectedUser: string;
onUserChange: (user: string) => void;
className?: string;
}
export const UserSwitcher = ({
selectedUser,
onUserChange,
className = "",
}: UserSwitcherProps) => {
const [users, setUsers] = useState<string[]>([]);
const [isOpen, setIsOpen] = useState(false);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
const loadUsers = async () => {
try {
const availableUsers = await fetchAvailableUsers();
setUsers(availableUsers);
if (availableUsers.length > 0 && !selectedUser) {
onUserChange(availableUsers[0]);
}
} catch (error) {
console.error("Error loading users:", error);
} finally {
setIsLoading(false);
}
};
loadUsers();
}, [selectedUser, onUserChange]);
if (isLoading) {
return (
<div
className={`flex items-center gap-2 px-3 py-2 bg-muted/50 rounded-md ${className}`}
>
<UserIcon className="h-4 w-4 text-muted-foreground" />
<span className="text-sm text-muted-foreground">Loading users...</span>
</div>
);
}
return (
<div className={`relative ${className}`}>
<Button
type="button"
variant="outline"
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
setIsOpen(!isOpen);
}}
className="w-full justify-between"
>
<div className="flex items-center gap-2">
<UserIcon className="h-4 w-4" />
<span className="text-sm">{selectedUser || "Select user"}</span>
</div>
<CaretDownIcon className="h-4 w-4" />
</Button>
{isOpen && (
<div className="absolute top-full left-0 right-0 mt-1 bg-background0 border border-border rounded-md shadow-lg z-50 max-h-48 overflow-y-auto tui-scrollbar">
{users.map((user) => (
<button
type="button"
key={user}
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
onUserChange(user);
setIsOpen(false);
}}
className={`w-full text-left px-3 py-2 text-sm hover:bg-accent transition-colors ${selectedUser === user ? "border border-border" : "border border-transparent"
}`}
>
{user}
</button>
))}
</div>
)}
</div>
);
};

View File

@@ -0,0 +1,47 @@
"use client";
import { WarningCircleIcon, XIcon } from "@phosphor-icons/react";
import { JobError, removeJobError } from "@/app/_utils/error-utils";
interface ErrorBadgeProps {
errors: JobError[];
onErrorClick: (error: JobError) => void;
onErrorDismiss?: () => void;
}
export const ErrorBadge = ({
errors,
onErrorClick,
onErrorDismiss,
}: ErrorBadgeProps) => {
if (errors.length === 0) return null;
const handleDismissError = (errorId: string) => {
removeJobError(errorId);
onErrorDismiss?.();
};
return (
<div className="flex items-center gap-1">
{errors.map((error) => (
<div key={error.id} className="flex items-center gap-1">
<button
onClick={() => onErrorClick(error)}
className="flex items-center gap-1 px-2 py-1 bg-destructive/10 text-destructive border border-destructive/20 rounded text-xs hover:bg-destructive/20 transition-colors"
title={error.message}
>
<WarningCircleIcon className="h-3 w-3" />
<span className="hidden sm:inline">Error</span>
</button>
<button
onClick={() => handleDismissError(error.id)}
className="p-1 text-destructive hover:bg-destructive/10 rounded transition-colors"
title="Dismiss error"
>
<XIcon className="h-3 w-3" />
</button>
</div>
))}
</div>
);
};

View File

@@ -1,6 +1,7 @@
import { cn } from "@/app/_utils/cn"; import { cn } from "@/app/_utils/global-utils";
import { HTMLAttributes, forwardRef } from "react"; import { HTMLAttributes, forwardRef } from "react";
import { CheckCircle, AlertTriangle, XCircle, Activity } from "lucide-react"; import { CheckCircleIcon, WarningIcon, XCircleIcon, PulseIcon } from "@phosphor-icons/react";
import { useTranslations } from "next-intl";
export interface StatusBadgeProps extends HTMLAttributes<HTMLDivElement> { export interface StatusBadgeProps extends HTMLAttributes<HTMLDivElement> {
status: string; status: string;
@@ -9,7 +10,7 @@ export interface StatusBadgeProps extends HTMLAttributes<HTMLDivElement> {
showText?: boolean; showText?: boolean;
} }
const StatusBadge = forwardRef<HTMLDivElement, StatusBadgeProps>( export const StatusBadge = forwardRef<HTMLDivElement, StatusBadgeProps>(
( (
{ {
className, className,
@@ -21,6 +22,7 @@ const StatusBadge = forwardRef<HTMLDivElement, StatusBadgeProps>(
}, },
ref ref
) => { ) => {
const t = useTranslations();
const getStatusConfig = (status: string) => { const getStatusConfig = (status: string) => {
const lowerStatus = status.toLowerCase(); const lowerStatus = status.toLowerCase();
@@ -29,47 +31,42 @@ const StatusBadge = forwardRef<HTMLDivElement, StatusBadgeProps>(
case "operational": case "operational":
case "stable": case "stable":
return { return {
color: "text-emerald-500", color: "text-status-success",
bgColor: "bg-emerald-500/10", bgColor: "bg-background0",
borderColor: "border-emerald-500/20", icon: CheckCircleIcon,
icon: CheckCircle, label: t("system.optimal"),
label: "Optimal",
}; };
case "moderate": case "moderate":
case "warning": case "warning":
return { return {
color: "text-yellow-500", color: "text-status-warning",
bgColor: "bg-yellow-500/10", bgColor: "bg-background0",
borderColor: "border-yellow-500/20", icon: WarningIcon,
icon: AlertTriangle, label: t("system.warning"),
label: "Warning",
}; };
case "high": case "high":
case "slow": case "slow":
return { return {
color: "text-orange-500", color: "text-status-warning",
bgColor: "bg-orange-500/10", bgColor: "bg-background0",
borderColor: "border-orange-500/20", icon: WarningIcon,
icon: AlertTriangle, label: t("system.high"),
label: "High",
}; };
case "critical": case "critical":
case "poor": case "poor":
case "offline": case "offline":
return { return {
color: "text-destructive", color: "text-status-error",
bgColor: "bg-destructive/10", bgColor: "bg-background0",
borderColor: "border-destructive/20", icon: XCircleIcon,
icon: XCircle, label: t("system.critical"),
label: "Critical",
}; };
default: default:
return { return {
color: "text-muted-foreground", color: "",
bgColor: "bg-muted", bgColor: "bg-background0",
borderColor: "border-border", icon: PulseIcon,
icon: Activity, label: t("system.unknown"),
label: "Unknown",
}; };
} }
}; };
@@ -81,9 +78,8 @@ const StatusBadge = forwardRef<HTMLDivElement, StatusBadgeProps>(
<div <div
ref={ref} ref={ref}
className={cn( className={cn(
"inline-flex items-center gap-1.5 rounded-full border px-2 py-1", "inline-flex items-center gap-1.5 ascii-border px-2 py-1 terminal-font",
config.bgColor, config.bgColor,
config.borderColor,
{ {
"text-xs": size === "sm", "text-xs": size === "sm",
"text-sm": size === "md", "text-sm": size === "md",
@@ -105,5 +101,3 @@ const StatusBadge = forwardRef<HTMLDivElement, StatusBadgeProps>(
); );
StatusBadge.displayName = "StatusBadge"; StatusBadge.displayName = "StatusBadge";
export { StatusBadge };

View File

@@ -0,0 +1,43 @@
import { HTMLAttributes, forwardRef } from 'react';
export const Card = forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>(
({ className = '', ...props }, ref) => (
<div ref={ref} className={`tui-card ${className}`} {...props} />
)
);
Card.displayName = 'Card';
export const CardHeader = forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>(
({ className = '', ...props }, ref) => (
<div ref={ref} className={`p-4 border-b border-foreground1 ${className}`} {...props} />
)
);
CardHeader.displayName = 'CardHeader';
export const CardTitle = forwardRef<HTMLHeadingElement, HTMLAttributes<HTMLHeadingElement>>(
({ className = '', ...props }, ref) => (
<h3 ref={ref} className={`terminal-font font-bold uppercase ${className}`} {...props} />
)
);
CardTitle.displayName = 'CardTitle';
export const CardDescription = forwardRef<HTMLParagraphElement, HTMLAttributes<HTMLParagraphElement>>(
({ className = '', ...props }, ref) => (
<p ref={ref} className={`terminal-font text-sm ${className}`} {...props} />
)
);
CardDescription.displayName = 'CardDescription';
export const CardContent = forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>(
({ className = '', ...props }, ref) => (
<div ref={ref} className={`p-4 ${className}`} {...props} />
)
);
CardContent.displayName = 'CardContent';
export const CardFooter = forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>(
({ className = '', ...props }, ref) => (
<div ref={ref} className={`flex items-center p-4 border-t border-foreground1 ${className}`} {...props} />
)
);
CardFooter.displayName = 'CardFooter';

View File

@@ -1,12 +1,12 @@
import { cn } from "@/app/_utils/cn"; import { cn } from "@/app/_utils/global-utils";
import { HTMLAttributes, forwardRef } from "react"; import { HTMLAttributes, forwardRef, ComponentType } from "react";
import { LucideIcon } from "lucide-react"; import { IconProps } from "@phosphor-icons/react";
import { StatusBadge } from "./StatusBadge"; import { StatusBadge } from "@/app/_components/GlobalComponents/Badges/StatusBadge";
import { ProgressBar } from "./ProgressBar"; import { ProgressBar } from "@/app/_components/GlobalComponents/UIElements/ProgressBar";
import { TruncatedText } from "./TruncatedText"; import { TruncatedText } from "@/app/_components/GlobalComponents/UIElements/TruncatedText";
export interface MetricCardProps extends HTMLAttributes<HTMLDivElement> { export interface MetricCardProps extends HTMLAttributes<HTMLDivElement> {
icon: LucideIcon; icon: ComponentType<IconProps>;
label: string; label: string;
value: string; value: string;
detail?: string; detail?: string;
@@ -18,7 +18,7 @@ export interface MetricCardProps extends HTMLAttributes<HTMLDivElement> {
progressMax?: number; progressMax?: number;
} }
const MetricCard = forwardRef<HTMLDivElement, MetricCardProps>( export const MetricCard = forwardRef<HTMLDivElement, MetricCardProps>(
( (
{ {
className, className,
@@ -27,7 +27,7 @@ const MetricCard = forwardRef<HTMLDivElement, MetricCardProps>(
value, value,
detail, detail,
status, status,
color = "text-blue-500", color,
variant = "basic", variant = "basic",
showProgress = false, showProgress = false,
progressValue = 0, progressValue = 0,
@@ -40,14 +40,14 @@ const MetricCard = forwardRef<HTMLDivElement, MetricCardProps>(
<div <div
ref={ref} ref={ref}
className={cn( className={cn(
"flex items-start gap-3 p-3 border border-border/50 rounded-lg hover:bg-accent/50 transition-colors duration-200 glass-card-hover", "flex items-start gap-3 p-3 tui-card-mini transition-colors duration-200 terminal-font",
className className
)} )}
{...props} {...props}
> >
<div <div
className={cn( className={cn(
"p-2 rounded-lg border border-border/50 flex-shrink-0 bg-card/50" "p-2 ascii-border flex-shrink-0 bg-background0"
)} )}
> >
<Icon className={cn("h-4 w-4", color)} /> <Icon className={cn("h-4 w-4", color)} />
@@ -55,7 +55,7 @@ const MetricCard = forwardRef<HTMLDivElement, MetricCardProps>(
<div className="flex-1 min-w-0"> <div className="flex-1 min-w-0">
<div className="flex items-center gap-2 mb-1"> <div className="flex items-center gap-2 mb-1">
<p className="text-xs font-medium text-muted-foreground uppercase tracking-wide"> <p className="text-xs font-medium uppercase tracking-wide terminal-font">
{label} {label}
</p> </p>
{status && variant === "performance" && ( {status && variant === "performance" && (
@@ -67,12 +67,12 @@ const MetricCard = forwardRef<HTMLDivElement, MetricCardProps>(
<TruncatedText <TruncatedText
text={value} text={value}
maxLength={40} maxLength={40}
className="text-sm font-medium text-foreground" className="text-sm font-medium terminal-font"
/> />
</div> </div>
{detail && ( {detail && (
<p className="text-xs text-muted-foreground mb-2">{detail}</p> <p className="text-xs mb-2 terminal-font">{detail}</p>
)} )}
{showProgress && ( {showProgress && (
@@ -99,5 +99,3 @@ const MetricCard = forwardRef<HTMLDivElement, MetricCardProps>(
); );
MetricCard.displayName = "MetricCard"; MetricCard.displayName = "MetricCard";
export { MetricCard };

View File

@@ -0,0 +1,17 @@
import { InputHTMLAttributes, forwardRef } from 'react';
export interface InputProps extends InputHTMLAttributes<HTMLInputElement> {}
export const Input = forwardRef<HTMLInputElement, InputProps>(
({ className = '', ...props }, ref) => {
return (
<input
className={`terminal-font ascii-border px-3 py-2 bg-background0 w-full ${className}`}
ref={ref}
{...props}
/>
);
}
);
Input.displayName = 'Input';

View File

@@ -0,0 +1,46 @@
"use client";
import { AsteriskIcon, TerminalIcon } from "@phosphor-icons/react";
interface LogoProps {
size?: number;
showGlow?: boolean;
}
export const Logo = ({ size = 48, showGlow = false }: LogoProps) => {
const iconSize = size * 0.8;
const asteriskSize = size * 0.4;
const asteriskOffset = size * 0.08;
return (
<div
className="relative flex items-center justify-center flex-shrink-0"
style={{ width: size, height: size }}
>
{showGlow && (
<div
className="absolute inset-0 bg-gradient-to-br from-primary/20 via-primary/10 to-transparent blur-xl"
style={{ width: size, height: size }}
/>
)}
<div className="relative z-10 flex items-center justify-center w-full h-full">
<TerminalIcon
className="text-primary drop-shadow-[0_0_10px_rgba(var(--primary-rgb),0.4)]"
weight="duotone"
style={{ width: iconSize, height: iconSize }}
/>
<AsteriskIcon
className="text-primary absolute drop-shadow-[0_0_8px_rgba(var(--primary-rgb),0.6)]"
weight="bold"
style={{
width: asteriskSize,
height: asteriskSize,
top: -asteriskOffset,
right: -asteriskOffset
}}
/>
</div>
</div>
);
};

View File

@@ -0,0 +1,38 @@
import { ButtonHTMLAttributes, forwardRef } from 'react';
export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
variant?: 'default' | 'destructive' | 'outline' | 'secondary' | 'ghost' | 'link';
size?: 'default' | 'sm' | 'lg' | 'icon';
}
export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
({ className = '', variant = 'default', size = 'default', children, ...props }, ref) => {
const baseClasses = 'terminal-font border border-border px-4 py-2 cursor-pointer inline-flex items-center justify-center disabled:opacity-50 disabled:cursor-not-allowed';
const variantClasses = {
default: 'bg-background1 hover:bg-background2',
destructive: 'text-status-error hover:bg-status-error hover:text-white',
outline: 'bg-background0 hover:bg-background1',
secondary: 'bg-background2 hover:bg-background1',
ghost: 'border-0 bg-transparent hover:bg-background1',
link: 'border-0 underline bg-transparent',
};
const sizeClasses = {
default: '',
sm: 'px-2 py-1 text-sm',
lg: 'px-6 py-3',
icon: 'p-2',
};
return (
<button
className={`${baseClasses} ${variantClasses[variant]} ${sizeClasses[size]} ${className}`}
ref={ref}
{...props}
>
{children}
</button>
);
}
);
Button.displayName = 'Button';

View File

@@ -0,0 +1,128 @@
"use client";
import { useState, useRef, useEffect, ReactNode } from "react";
import { Button } from "@/app/_components/GlobalComponents/UIElements/Button";
import { DotsThreeVerticalIcon } from "@phosphor-icons/react";
const DROPDOWN_HEIGHT = 200;
interface DropdownMenuItem {
label: string;
icon?: ReactNode;
onClick: () => void;
disabled?: boolean;
variant?: "default" | "destructive";
}
interface DropdownMenuProps {
items: DropdownMenuItem[];
triggerLabel?: string;
triggerIcon?: ReactNode;
triggerClassName?: string;
onOpenChange?: (isOpen: boolean) => void;
}
export const DropdownMenu = ({
items,
triggerLabel,
triggerIcon = <DotsThreeVerticalIcon className="h-3 w-3" />,
triggerClassName = "btn-outline h-8 px-3",
onOpenChange,
}: DropdownMenuProps) => {
const [isOpen, setIsOpen] = useState(false);
const [positionAbove, setPositionAbove] = useState(false);
const dropdownRef = useRef<HTMLDivElement>(null);
const triggerRef = useRef<HTMLButtonElement>(null);
const handleOpenChange = (open: boolean) => {
if (open && triggerRef.current) {
const rect = triggerRef.current.getBoundingClientRect();
const viewportHeight = window.innerHeight;
const spaceBelow = viewportHeight - rect.bottom;
const spaceAbove = rect.top;
setPositionAbove(spaceBelow < DROPDOWN_HEIGHT && spaceAbove > spaceBelow);
}
setIsOpen(open);
onOpenChange?.(open);
};
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (
dropdownRef.current &&
!dropdownRef.current.contains(event.target as Node)
) {
handleOpenChange(false);
}
};
const handleEscape = (event: KeyboardEvent) => {
if (event.key === "Escape") {
handleOpenChange(false);
}
};
if (isOpen) {
document.addEventListener("mousedown", handleClickOutside);
document.addEventListener("keydown", handleEscape);
}
return () => {
document.removeEventListener("mousedown", handleClickOutside);
document.removeEventListener("keydown", handleEscape);
};
}, [isOpen]);
const handleItemClick = (item: DropdownMenuItem) => {
if (!item.disabled) {
item.onClick();
handleOpenChange(false);
}
};
return (
<div className="relative inline-block" ref={dropdownRef}>
<Button
ref={triggerRef}
variant="outline"
size="sm"
onClick={() => handleOpenChange(!isOpen)}
className={triggerClassName}
aria-label={triggerLabel || "Open menu"}
title={triggerLabel || "Open menu"}
>
{triggerIcon}
{triggerLabel && <span className="ml-2">{triggerLabel}</span>}
</Button>
{isOpen && (
<div
className={`absolute right-0 w-56 ascii-border bg-background0 shadow-lg z-[9999] overflow-hidden terminal-font ${positionAbove ? "bottom-full mb-2" : "top-full mt-2"
}`}
>
<div className="p-1">
{items.map((item, index) => (
<button
key={index}
onClick={() => handleItemClick(item)}
disabled={item.disabled}
className={`w-full flex items-center border border-transparent gap-3 px-4 py-2 text-sm transition-colors ${item.disabled
? "opacity-50 cursor-not-allowed"
: item.variant === "destructive"
? "text-status-error hover:border hover:border-border"
: "hover:border-border"
}`}
>
{item.icon && (
<span className="flex-shrink-0">{item.icon}</span>
)}
<span className="flex-1 text-left">{item.label}</span>
</button>
))}
</div>
</div>
)}
</div>
);
};

View File

@@ -0,0 +1,75 @@
"use client";
import { useEffect, useRef } from "react";
import { XIcon } from "@phosphor-icons/react";
import { Button } from "./Button";
interface ModalProps {
isOpen: boolean;
onClose: () => void;
title: string;
children: React.ReactNode;
size?: "sm" | "md" | "lg" | "xl" | "2xl" | "3xl";
showCloseButton?: boolean;
preventCloseOnClickOutside?: boolean;
className?: string;
}
export const Modal = ({
isOpen,
onClose,
title,
children,
size = "md",
showCloseButton = true,
preventCloseOnClickOutside = false,
className = "",
}: ModalProps) => {
const dialogRef = useRef<HTMLDialogElement>(null);
useEffect(() => {
const dialog = dialogRef.current;
if (!dialog) return;
if (isOpen) {
dialog.showModal();
document.body.style.overflow = "hidden";
} else {
dialog.close();
document.body.style.overflow = "unset";
}
}, [isOpen]);
const sizeClasses = {
sm: "w-[600px]",
md: "w-[800px]",
lg: "w-[1000px]",
xl: "w-[1200px]",
"2xl": "w-[1400px]",
"3xl": "w-[90vw]",
};
return (
<dialog
ref={dialogRef}
className={`ascii-border terminal-font bg-background0 mobile-modal ${sizeClasses[size]} max-w-[95vw] ${className}`}
onClick={(e) => {
if (e.target === dialogRef.current && !preventCloseOnClickOutside) {
onClose();
}
}}
>
<div className="border-border border-b p-4 flex justify-between items-center bg-background0">
<h2 className="terminal-font font-bold uppercase">{title}</h2>
{showCloseButton && (
<Button variant="ghost" size="icon" onClick={onClose}>
<XIcon className="h-4 w-4" />
</Button>
)}
</div>
<div className="p-4 max-h-[70vh] overflow-y-auto tui-scrollbar bg-background0">
{children}
</div>
</dialog>
);
};

View File

@@ -1,4 +1,4 @@
import { cn } from "@/app/_utils/cn"; import { cn } from "@/app/_utils/global-utils";
import { HTMLAttributes, forwardRef } from "react"; import { HTMLAttributes, forwardRef } from "react";
export interface ProgressBarProps extends HTMLAttributes<HTMLDivElement> { export interface ProgressBarProps extends HTMLAttributes<HTMLDivElement> {
@@ -9,7 +9,7 @@ export interface ProgressBarProps extends HTMLAttributes<HTMLDivElement> {
variant?: "default" | "gradient"; variant?: "default" | "gradient";
} }
const ProgressBar = forwardRef<HTMLDivElement, ProgressBarProps>( export const ProgressBar = forwardRef<HTMLDivElement, ProgressBarProps>(
( (
{ {
className, className,
@@ -25,35 +25,29 @@ const ProgressBar = forwardRef<HTMLDivElement, ProgressBarProps>(
const percentage = Math.min(Math.max((value / max) * 100, 0), 100); const percentage = Math.min(Math.max((value / max) * 100, 0), 100);
const getColorClass = (percentage: number) => { const getColorClass = (percentage: number) => {
if (percentage >= 90) return "bg-destructive"; if (percentage >= 90) return "bg-red-600";
if (percentage >= 80) return "bg-orange-500"; if (percentage >= 80) return "bg-yellow-600";
if (percentage >= 70) return "bg-yellow-500"; if (percentage >= 70) return "bg-yellow-600";
return "bg-emerald-500"; return "bg-green-600";
}; };
const getGradientClass = (percentage: number) => { const getGradientClass = (percentage: number) => {
if (percentage >= 90) return getColorClass(percentage);
return "bg-gradient-to-r from-destructive to-red-600";
if (percentage >= 80)
return "bg-gradient-to-r from-orange-500 to-orange-600";
if (percentage >= 70)
return "bg-gradient-to-r from-yellow-500 to-yellow-600";
return "bg-gradient-to-r from-emerald-500 to-emerald-600";
}; };
return ( return (
<div ref={ref} className={cn("w-full", className)} {...props}> <div ref={ref} className={cn("w-full terminal-font", className)} {...props}>
{showLabel && ( {showLabel && (
<div className="flex justify-between items-center mb-1"> <div className="flex justify-between items-center mb-1">
<span className="text-xs text-muted-foreground">Usage</span> <span className="text-xs">Usage</span>
<span className="text-xs font-medium text-foreground"> <span className="text-xs font-medium">
{Math.round(percentage)}% {Math.round(percentage)}%
</span> </span>
</div> </div>
)} )}
<div <div
className={cn("w-full bg-muted rounded-full overflow-hidden", { className={cn("w-full bg-background2 ascii-border overflow-hidden", {
"h-1.5": size === "sm", "h-1.5": size === "sm",
"h-2": size === "md", "h-2": size === "md",
"h-3": size === "lg", "h-3": size === "lg",
@@ -75,5 +69,3 @@ const ProgressBar = forwardRef<HTMLDivElement, ProgressBarProps>(
); );
ProgressBar.displayName = "ProgressBar"; ProgressBar.displayName = "ProgressBar";
export { ProgressBar };

View File

@@ -0,0 +1,54 @@
"use client";
interface SwitchProps {
checked: boolean;
onCheckedChange: (checked: boolean) => void;
className?: string;
disabled?: boolean;
id?: string;
}
export const Switch = ({ checked, onCheckedChange, className = "", disabled = false, id }: SwitchProps) => {
const handleClick = () => {
if (!disabled) {
onCheckedChange(!checked);
}
};
return (
<div
className={`inline-flex items-center ${disabled ? 'opacity-50 cursor-not-allowed' : 'cursor-pointer'} ${className}`}
onClick={handleClick}
role="checkbox"
aria-checked={checked}
aria-labelledby={id}
tabIndex={disabled ? -1 : 0}
onKeyDown={(e) => {
if (!disabled && (e.key === ' ' || e.key === 'Enter')) {
e.preventDefault();
handleClick();
}
}}
>
<div className="relative w-5 h-5 ascii-border bg-background0 transition-all focus-within:ring-2 focus-within:ring-primary/20 flex items-center justify-center group">
{checked && (
<svg
className="w-3.5 h-3.5 text-primary transition-transform duration-200"
viewBox="0 0 12 12"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M10 3L4.5 8.5L2 6"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="square"
strokeLinejoin="miter"
/>
</svg>
)}
<div className="absolute inset-0 border border-primary/0 group-hover:border-primary/50 transition-colors pointer-events-none" />
</div>
</div>
);
};

View File

@@ -0,0 +1,163 @@
"use client";
import { useEffect, useState } from "react";
import { XIcon, CheckCircleIcon, WarningCircleIcon, InfoIcon, WarningIcon } from "@phosphor-icons/react";
import { cn } from "@/app/_utils/global-utils";
import { ErrorDetailsModal } from "@/app/_components/FeatureComponents/Modals/ErrorDetailsModal";
export interface Toast {
id: string;
type: "success" | "error" | "info" | "warning";
title: string;
message?: string;
duration?: number;
errorDetails?: {
title: string;
message: string;
details?: string;
command?: string;
output?: string;
stderr?: string;
timestamp: string;
jobId?: string;
};
}
interface ToastProps {
toast: Toast;
onRemove: (id: string) => void;
onErrorClick?: (errorDetails: Toast["errorDetails"]) => void;
}
const toastIcons = {
success: CheckCircleIcon,
error: WarningCircleIcon,
info: InfoIcon,
warning: WarningIcon,
};
const toastStyles = {
success: "ascii-border bg-background0 text-status-success",
error: "ascii-border bg-background0 text-status-error",
info: "ascii-border bg-background0 text-status-info",
warning: "ascii-border bg-background0 text-status-warning",
};
export const Toast = ({ toast, onRemove, onErrorClick }: ToastProps) => {
const [isVisible, setIsVisible] = useState(false);
const Icon = toastIcons[toast.type];
useEffect(() => {
setIsVisible(true);
const timer = setTimeout(() => {
setIsVisible(false);
setTimeout(() => onRemove(toast.id), 300);
}, toast.duration || 5000);
return () => clearTimeout(timer);
}, [toast.id, toast.duration, onRemove]);
return (
<div
className={cn(
"flex items-start gap-3 p-4 terminal-font transition-all duration-300 ease-in-out",
toastStyles[toast.type],
isVisible ? "translate-x-0 opacity-100" : "translate-x-full opacity-0"
)}
>
<Icon className="h-5 w-5 flex-shrink-0 mt-0.5" />
<div
className={`flex-1 min-w-0 ${toast.type === "error" && toast.errorDetails ? "cursor-pointer" : ""
}`}
onClick={() => {
if (toast.type === "error" && toast.errorDetails && onErrorClick) {
onErrorClick(toast.errorDetails);
}
}}
>
<h4 className="font-medium text-sm">{toast.title}</h4>
{toast.message && (
<p className="text-sm opacity-90 mt-1">{toast.message}</p>
)}
{toast.type === "error" && toast.errorDetails && (
<p className="text-xs opacity-70 mt-1">Click for details</p>
)}
</div>
<button
onClick={() => {
setIsVisible(false);
setTimeout(() => onRemove(toast.id), 300);
}}
className="flex-shrink-0 p-1 rounded-md hover:bg-black/10 transition-colors"
>
<XIcon className="h-4 w-4" />
</button>
</div>
);
};
export const ToastContainer = () => {
const [toasts, setToasts] = useState<Toast[]>([]);
const [errorModalOpen, setErrorModalOpen] = useState(false);
const [selectedError, setSelectedError] = useState<
Toast["errorDetails"] | null
>(null);
const addToast = (toast: Omit<Toast, "id">) => {
const id = Math.random().toString(36).substr(2, 9);
setToasts((prev) => [...prev, { ...toast, id }]);
};
const removeToast = (id: string) => {
setToasts((prev) => prev.filter((toast) => toast.id !== id));
};
const handleErrorClick = (errorDetails: Toast["errorDetails"]) => {
setSelectedError(errorDetails);
setErrorModalOpen(true);
};
useEffect(() => {
(window as any).showToast = addToast;
return () => {
delete (window as any).showToast;
};
}, []);
return (
<>
<div className="fixed bottom-4 right-4 z-50 space-y-2 max-w-sm">
{toasts.map((toast) => (
<Toast
key={toast.id}
toast={toast}
onRemove={removeToast}
onErrorClick={handleErrorClick}
/>
))}
</div>
{errorModalOpen && (
<ErrorDetailsModal
isOpen={errorModalOpen}
onClose={() => {
setErrorModalOpen(false);
setSelectedError(null);
}}
error={selectedError || null}
/>
)}
</>
);
};
export const showToast = (
type: Toast["type"],
title: string,
message?: string,
duration?: number,
errorDetails?: Toast["errorDetails"]
) => {
if (typeof window !== "undefined" && (window as any).showToast) {
(window as any).showToast({ type, title, message, duration, errorDetails });
}
};

View File

@@ -1,4 +1,4 @@
import { cn } from "@/app/_utils/cn"; import { cn } from "@/app/_utils/global-utils";
import { HTMLAttributes, forwardRef, useState } from "react"; import { HTMLAttributes, forwardRef, useState } from "react";
export interface TruncatedTextProps extends HTMLAttributes<HTMLDivElement> { export interface TruncatedTextProps extends HTMLAttributes<HTMLDivElement> {
@@ -8,7 +8,7 @@ export interface TruncatedTextProps extends HTMLAttributes<HTMLDivElement> {
className?: string; className?: string;
} }
const TruncatedText = forwardRef<HTMLDivElement, TruncatedTextProps>( export const TruncatedText = forwardRef<HTMLDivElement, TruncatedTextProps>(
({ className, text, maxLength = 50, showTooltip = true, ...props }, ref) => { ({ className, text, maxLength = 50, showTooltip = true, ...props }, ref) => {
const [showTooltipState, setShowTooltipState] = useState(false); const [showTooltipState, setShowTooltipState] = useState(false);
const shouldTruncate = text.length > maxLength; const shouldTruncate = text.length > maxLength;
@@ -42,5 +42,3 @@ const TruncatedText = forwardRef<HTMLDivElement, TruncatedTextProps>(
); );
TruncatedText.displayName = "TruncatedText"; TruncatedText.displayName = "TruncatedText";
export { TruncatedText };

View File

@@ -1,64 +0,0 @@
"use client";
import { useState } from "react";
import { CronJobList } from "./CronJobList";
import { ScriptsManager } from "./ScriptsManager";
import { CronJob } from "@/app/_utils/system";
import { type Script } from "@/app/_server/actions/scripts";
import { Clock, FileText } from "lucide-react";
interface TabbedInterfaceProps {
cronJobs: CronJob[];
scripts: Script[];
}
export function TabbedInterface({ cronJobs, scripts }: TabbedInterfaceProps) {
const [activeTab, setActiveTab] = useState<"cronjobs" | "scripts">(
"cronjobs"
);
return (
<div className="space-y-6">
<div className="bg-background/80 backdrop-blur-md border border-border/50 rounded-lg p-1 glass-card">
<div className="flex">
<button
onClick={() => setActiveTab("cronjobs")}
className={`flex items-center gap-2 px-4 py-2 text-sm font-medium transition-all duration-200 rounded-md flex-1 justify-center ${
activeTab === "cronjobs"
? "bg-primary/10 text-primary shadow-sm border border-primary/20"
: "text-muted-foreground hover:text-foreground hover:bg-muted/50"
}`}
>
<Clock className="h-4 w-4" />
Cron Jobs
<span className="ml-1 text-xs bg-primary/20 text-primary px-2 py-0.5 rounded-full font-medium">
{cronJobs.length}
</span>
</button>
<button
onClick={() => setActiveTab("scripts")}
className={`flex items-center gap-2 px-4 py-2 text-sm font-medium transition-all duration-200 rounded-md flex-1 justify-center ${
activeTab === "scripts"
? "bg-primary/10 text-primary shadow-sm border border-primary/20"
: "text-muted-foreground hover:text-foreground hover:bg-muted/50"
}`}
>
<FileText className="h-4 w-4" />
Scripts
<span className="ml-1 text-xs bg-primary/20 text-primary px-2 py-0.5 rounded-full font-medium">
{scripts.length}
</span>
</button>
</div>
</div>
<div className="min-h-[400px]">
{activeTab === "cronjobs" ? (
<CronJobList cronJobs={cronJobs} scripts={scripts} />
) : (
<ScriptsManager scripts={scripts} />
)}
</div>
</div>
);
}

View File

@@ -1,98 +0,0 @@
"use client";
import { Modal } from "../ui/Modal";
import { Button } from "../ui/Button";
import { Input } from "../ui/Input";
import { CronExpressionHelper } from "../CronExpressionHelper";
import { Edit, Terminal } from "lucide-react";
interface EditTaskModalProps {
isOpen: boolean;
onClose: () => void;
onSubmit: (e: React.FormEvent) => void;
form: {
schedule: string;
command: string;
comment: string;
};
onFormChange: (updates: Partial<EditTaskModalProps["form"]>) => void;
}
export function EditTaskModal({
isOpen,
onClose,
onSubmit,
form,
onFormChange,
}: EditTaskModalProps) {
return (
<Modal
isOpen={isOpen}
onClose={onClose}
title="Edit Scheduled Task"
size="xl"
>
<form onSubmit={onSubmit} className="space-y-4">
<div>
<label className="block text-sm font-medium text-foreground mb-1">
Schedule
</label>
<CronExpressionHelper
value={form.schedule}
onChange={(value) => onFormChange({ schedule: value })}
placeholder="* * * * *"
showPatterns={true}
/>
</div>
<div>
<label className="block text-sm font-medium text-foreground mb-1">
Command
</label>
<div className="space-y-3">
<div className="relative">
<Input
value={form.command}
onChange={(e) => onFormChange({ command: e.target.value })}
placeholder="/usr/bin/command"
className="font-mono bg-muted/30 border-border/50 focus:border-primary/50"
required
/>
<div className="absolute right-3 top-1/2 -translate-y-1/2">
<Terminal className="h-4 w-4 text-muted-foreground" />
</div>
</div>
</div>
</div>
<div>
<label className="block text-sm font-medium text-foreground mb-1">
Description{" "}
<span className="text-muted-foreground">(Optional)</span>
</label>
<Input
value={form.comment}
onChange={(e) => onFormChange({ comment: e.target.value })}
placeholder="What does this task do?"
className="bg-muted/30 border-border/50 focus:border-primary/50"
/>
</div>
<div className="flex justify-end gap-2 pt-3 border-t border-border/50">
<Button
type="button"
variant="outline"
onClick={onClose}
className="btn-outline"
>
Cancel
</Button>
<Button type="submit" className="btn-primary glow-primary">
<Edit className="h-4 w-4 mr-2" />
Update Task
</Button>
</div>
</form>
</Modal>
);
}

View File

@@ -1,136 +0,0 @@
"use client";
import { Modal } from "../ui/Modal";
import { Button } from "../ui/Button";
import { Input } from "../ui/Input";
import { BashEditor } from "../BashEditor";
import { BashSnippetHelper } from "../BashSnippetHelper";
import { FileText, Code, Plus, Edit } from "lucide-react";
import { showToast } from "../ui/Toast";
interface ScriptModalProps {
isOpen: boolean;
onClose: () => void;
onSubmit: (
formData: FormData
) => Promise<{ success: boolean; message: string }>;
title: string;
submitButtonText: string;
submitButtonIcon: React.ReactNode;
form: {
name: string;
description: string;
content: string;
};
onFormChange: (updates: Partial<ScriptModalProps["form"]>) => void;
additionalFormData?: Record<string, string>;
}
export function ScriptModal({
isOpen,
onClose,
onSubmit,
title,
submitButtonText,
submitButtonIcon,
form,
onFormChange,
additionalFormData = {},
}: ScriptModalProps) {
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
const formData = new FormData();
formData.append("name", form.name);
formData.append("description", form.description);
formData.append("content", form.content);
Object.entries(additionalFormData).forEach(([key, value]) => {
formData.append(key, value);
});
const result = await onSubmit(formData);
if (result.success) {
onClose();
} else {
showToast("error", `Failed to ${title.toLowerCase()}`, result.message);
}
};
const handleInsertSnippet = (snippet: string) => {
onFormChange({ content: snippet });
};
return (
<Modal isOpen={isOpen} onClose={onClose} title={title} size="xl">
<form onSubmit={handleSubmit} className="space-y-6">
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium text-foreground mb-2">
Script Name
</label>
<Input
value={form.name}
onChange={(e) => onFormChange({ name: e.target.value })}
placeholder="My Script"
required
/>
</div>
<div>
<label className="block text-sm font-medium text-foreground mb-2">
Description
</label>
<Input
value={form.description}
onChange={(e) => onFormChange({ description: e.target.value })}
placeholder="What does this script do?"
/>
</div>
</div>
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6 h-[500px]">
<div className="lg:col-span-1 bg-muted/20 rounded-lg p-4 flex flex-col h-full overflow-hidden">
<div className="flex items-center gap-2 mb-4 flex-shrink-0">
<Code className="h-4 w-4 text-primary" />
<h3 className="text-sm font-medium text-foreground">Snippets</h3>
</div>
<div className="flex-1 overflow-y-auto min-h-0">
<BashSnippetHelper onInsertSnippet={handleInsertSnippet} />
</div>
</div>
<div className="lg:col-span-2 flex flex-col h-full overflow-hidden">
<div className="flex items-center gap-2 mb-4 flex-shrink-0">
<FileText className="h-4 w-4 text-primary" />
<h3 className="text-sm font-medium text-foreground">
Script Content
</h3>
</div>
<div className="flex-1 min-h-0">
<BashEditor
value={form.content}
onChange={(value) => onFormChange({ content: value })}
placeholder="#!/bin/bash&#10;# Your script here&#10;echo 'Hello World'"
className="h-full"
/>
</div>
</div>
</div>
<div className="flex justify-end gap-3 pt-4 border-t border-border/30">
<Button
type="button"
variant="outline"
onClick={onClose}
className="btn-outline"
>
Cancel
</Button>
<Button type="submit" className="btn-primary glow-primary">
{submitButtonIcon}
{submitButtonText}
</Button>
</div>
</form>
</Modal>
);
}

View File

@@ -1,44 +0,0 @@
import { cn } from '@/app/_utils/cn';
import { ButtonHTMLAttributes, forwardRef } from 'react';
export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
variant?: 'default' | 'destructive' | 'outline' | 'secondary' | 'ghost' | 'link';
size?: 'default' | 'sm' | 'lg' | 'icon';
}
const Button = forwardRef<HTMLButtonElement, ButtonProps>(
({ className, variant = 'default', size = 'default', ...props }, ref) => {
return (
<button
className={cn(
'inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50',
{
'bg-primary text-primary-foreground hover:bg-primary/90': variant === 'default',
'bg-destructive text-destructive-foreground hover:bg-destructive/90': variant === 'destructive',
'border border-input bg-background hover:bg-accent hover:text-accent-foreground': variant === 'outline',
'bg-secondary text-secondary-foreground hover:bg-secondary/80': variant === 'secondary',
'hover:bg-accent hover:text-accent-foreground': variant === 'ghost',
'text-primary underline-offset-4 hover:underline': variant === 'link',
},
{
'h-10 px-4 py-2': size === 'default',
'h-9 rounded-md px-3': size === 'sm',
'h-11 rounded-md px-8': size === 'lg',
'h-10 w-10': size === 'icon',
},
className
)}
ref={ref}
{...props}
/>
);
}
);
Button.displayName = 'Button';
export { Button };

View File

@@ -1,72 +0,0 @@
import { cn } from '@/app/_utils/cn';
import { HTMLAttributes, forwardRef } from 'react';
const Card = forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>(
({ className, ...props }, ref) => (
<div
ref={ref}
className={cn(
'rounded-lg border bg-card text-card-foreground shadow-sm',
className
)}
{...props}
/>
)
);
Card.displayName = 'Card';
const CardHeader = forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>(
({ className, ...props }, ref) => (
<div
ref={ref}
className={cn('flex flex-col space-y-1.5 p-6', className)}
{...props}
/>
)
);
CardHeader.displayName = 'CardHeader';
const CardTitle = forwardRef<HTMLParagraphElement, HTMLAttributes<HTMLHeadingElement>>(
({ className, ...props }, ref) => (
<h3
ref={ref}
className={cn(
'text-2xl font-semibold leading-none tracking-tight',
className
)}
{...props}
/>
)
);
CardTitle.displayName = 'CardTitle';
const CardDescription = forwardRef<HTMLParagraphElement, HTMLAttributes<HTMLParagraphElement>>(
({ className, ...props }, ref) => (
<p
ref={ref}
className={cn('text-sm text-muted-foreground', className)}
{...props}
/>
)
);
CardDescription.displayName = 'CardDescription';
const CardContent = forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>(
({ className, ...props }, ref) => (
<div ref={ref} className={cn('p-4 lg:p-6 pt-0', className)} {...props} />
)
);
CardContent.displayName = 'CardContent';
const CardFooter = forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>(
({ className, ...props }, ref) => (
<div
ref={ref}
className={cn('flex items-center p-6 pt-0', className)}
{...props}
/>
)
);
CardFooter.displayName = 'CardFooter';
export { Card, CardHeader, CardFooter, CardTitle, CardDescription, CardContent };

View File

@@ -1,28 +0,0 @@
import { cn } from '@/app/_utils/cn';
import { InputHTMLAttributes, forwardRef } from 'react';
export interface InputProps extends InputHTMLAttributes<HTMLInputElement> { }
const Input = forwardRef<HTMLInputElement, InputProps>(
({ className, type, ...props }, ref) => {
return (
<input
type={type}
className={cn(
'flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50',
className
)}
ref={ref}
{...props}
/>
);
}
);
Input.displayName = 'Input';
export { Input };

View File

@@ -1,117 +0,0 @@
"use client";
import { useEffect, useRef } from "react";
import { X } from "lucide-react";
import { cn } from "@/app/_utils/cn";
import { Button } from "./Button";
interface ModalProps {
isOpen: boolean;
onClose: () => void;
title: string;
children: React.ReactNode;
size?: "sm" | "md" | "lg" | "xl";
showCloseButton?: boolean;
preventCloseOnClickOutside?: boolean;
}
export function Modal({
isOpen,
onClose,
title,
children,
size = "md",
showCloseButton = true,
preventCloseOnClickOutside = false,
}: ModalProps) {
const modalRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const handleEscape = (e: KeyboardEvent) => {
if (e.key === "Escape") {
onClose();
}
};
if (isOpen) {
document.addEventListener("keydown", handleEscape);
document.body.style.overflow = "hidden";
}
return () => {
document.removeEventListener("keydown", handleEscape);
document.body.style.overflow = "unset";
};
}, [isOpen, onClose]);
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (
modalRef.current &&
!modalRef.current.contains(event.target as Node) &&
!preventCloseOnClickOutside
) {
const target = event.target as Element;
const isClickingOnModal = target.closest('[data-modal="true"]');
const isClickingOnBackdrop =
target.classList.contains("modal-backdrop");
if (isClickingOnBackdrop) {
onClose();
}
}
};
if (isOpen) {
document.addEventListener("mousedown", handleClickOutside);
}
return () => {
document.removeEventListener("mousedown", handleClickOutside);
};
}, [isOpen, onClose, preventCloseOnClickOutside]);
if (!isOpen) return null;
const sizeClasses = {
sm: "max-w-md",
md: "max-w-lg",
lg: "max-w-2xl",
xl: "max-w-4xl",
};
return (
<div
className="fixed inset-0 z-50 flex items-end justify-center sm:items-center p-0 sm:p-4"
data-modal="true"
>
<div className="absolute inset-0 bg-black/50 backdrop-blur-sm modal-backdrop" />
<div
ref={modalRef}
className={cn(
"relative w-full bg-card border border-border shadow-lg overflow-y-auto",
"max-h-[85vh]",
"sm:rounded-lg sm:max-h-[90vh] sm:w-full",
sizeClasses[size]
)}
>
<div className="flex items-center justify-between p-4 sm:p-6 border-b border-border sticky top-0 bg-card z-10">
<h2 className="text-lg font-semibold text-foreground">{title}</h2>
{showCloseButton && (
<Button
variant="ghost"
size="icon"
onClick={onClose}
className="h-8 w-8"
>
<X className="h-4 w-4" />
</Button>
)}
</div>
<div className="p-4 sm:p-6">{children}</div>
</div>
</div>
);
}

View File

@@ -1,12 +0,0 @@
'use client'
import { ThemeProvider as NextThemesProvider } from 'next-themes';
import { type ThemeProviderProps } from 'next-themes/dist/types';
export function ThemeProvider({ children, ...props }: ThemeProviderProps) {
return <NextThemesProvider {...props}>{children}</NextThemesProvider>;
}

View File

@@ -1,35 +0,0 @@
'use client'
import { Moon, Sun } from 'lucide-react';
import { useTheme } from 'next-themes';
import { useEffect, useState } from 'react';
import { Button } from './Button';
export function ThemeToggle() {
const [mounted, setMounted] = useState(false);
const { theme, setTheme } = useTheme();
useEffect(() => {
setMounted(true);
}, []);
if (!mounted) {
return null;
}
return (
<Button
variant="ghost"
size="icon"
onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}
>
<Sun className="h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0" />
<Moon className="absolute h-[1.2rem] w-[1.2rem] rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100" />
<span className="sr-only">Toggle theme</span>
</Button>
);
}

View File

@@ -1,115 +0,0 @@
"use client";
import { useEffect, useState } from "react";
import { X, CheckCircle, AlertCircle, Info, AlertTriangle } from "lucide-react";
import { cn } from "@/app/_utils/cn";
export interface Toast {
id: string;
type: "success" | "error" | "info" | "warning";
title: string;
message?: string;
duration?: number;
}
interface ToastProps {
toast: Toast;
onRemove: (id: string) => void;
}
const toastIcons = {
success: CheckCircle,
error: AlertCircle,
info: Info,
warning: AlertTriangle,
};
const toastStyles = {
success:
"border-green-500/20 bg-green-500/10 text-green-700 dark:text-green-400",
error: "border-red-500/20 bg-red-500/10 text-red-700 dark:text-red-400",
info: "border-blue-500/20 bg-blue-500/10 text-blue-700 dark:text-blue-400",
warning:
"border-yellow-500/20 bg-yellow-500/10 text-yellow-700 dark:text-yellow-400",
};
export function Toast({ toast, onRemove }: ToastProps) {
const [isVisible, setIsVisible] = useState(false);
const Icon = toastIcons[toast.type];
useEffect(() => {
setIsVisible(true);
const timer = setTimeout(() => {
setIsVisible(false);
setTimeout(() => onRemove(toast.id), 300);
}, toast.duration || 5000);
return () => clearTimeout(timer);
}, [toast.id, toast.duration, onRemove]);
return (
<div
className={cn(
"flex items-start gap-3 p-4 rounded-lg border backdrop-blur-md transition-all duration-300 ease-in-out",
toastStyles[toast.type],
isVisible ? "translate-x-0 opacity-100" : "translate-x-full opacity-0"
)}
>
<Icon className="h-5 w-5 flex-shrink-0 mt-0.5" />
<div className="flex-1 min-w-0">
<h4 className="font-medium text-sm">{toast.title}</h4>
{toast.message && (
<p className="text-sm opacity-90 mt-1">{toast.message}</p>
)}
</div>
<button
onClick={() => {
setIsVisible(false);
setTimeout(() => onRemove(toast.id), 300);
}}
className="flex-shrink-0 p-1 rounded-md hover:bg-black/10 transition-colors"
>
<X className="h-4 w-4" />
</button>
</div>
);
}
export function ToastContainer() {
const [toasts, setToasts] = useState<Toast[]>([]);
const addToast = (toast: Omit<Toast, "id">) => {
const id = Math.random().toString(36).substr(2, 9);
setToasts((prev) => [...prev, { ...toast, id }]);
};
const removeToast = (id: string) => {
setToasts((prev) => prev.filter((toast) => toast.id !== id));
};
useEffect(() => {
(window as any).showToast = addToast;
return () => {
delete (window as any).showToast;
};
}, []);
return (
<div className="fixed bottom-4 right-4 z-50 space-y-2 max-w-sm">
{toasts.map((toast) => (
<Toast key={toast.id} toast={toast} onRemove={removeToast} />
))}
</div>
);
}
export function showToast(
type: Toast["type"],
title: string,
message?: string,
duration?: number
) {
if (typeof window !== "undefined" && (window as any).showToast) {
(window as any).showToast({ type, title, message, duration });
}
}

32
app/_consts/commands.ts Normal file
View File

@@ -0,0 +1,32 @@
export const WRITE_CRONTAB = (content: string, user: string) => {
return `crontab -u ${user} - << 'EOF'\n${content}\nEOF`;
};
export const READ_CRONTAB = (user: string) =>
`crontab -l -u ${user} 2>/dev/null || echo ""`;
export const READ_CRON_FILE = () => 'crontab -l 2>/dev/null || echo ""';
export const WRITE_CRON_FILE = (content: string) => {
return `crontab - << 'EOF'\n${content}\nEOF`;
};
export const WRITE_HOST_CRONTAB = (base64Content: string, user: string) => {
const escapedContent = base64Content.replace(/'/g, "'\\''");
return `echo '${escapedContent}' | base64 -d | crontab -u ${user} -`;
};
export const ID_U = (username: string) => `id -u ${username}`;
export const ID_G = (username: string) => `id -g ${username}`;
export const MAKE_SCRIPT_EXECUTABLE = (scriptPath: string) =>
`chmod +x "${scriptPath}"`;
export const RUN_SCRIPT = (scriptPath: string) => `bash "${scriptPath}"`;
export const GET_TARGET_USER = `getent passwd | grep ":/home/" | head -1 | cut -d: -f1`;
export const GET_DOCKER_SOCKET_OWNER = 'stat -c "%U" /var/run/docker.sock';
export const READ_CRONTABS_DIRECTORY = `ls /var/spool/cron/crontabs/ 2>/dev/null || echo ''`;

5
app/_consts/file.ts Normal file
View File

@@ -0,0 +1,5 @@
import path from "path";
export const SCRIPTS_DIR = path.join("scripts");
export const SNIPPETS_DIR = path.join("snippets");
export const DATA_DIR = path.join("data");

7
app/_consts/nsenter.ts Normal file
View File

@@ -0,0 +1,7 @@
export const NSENTER_RUN_JOB = (
executionUser: string,
escapedCommand: string
) => `nsenter -t 1 -m -u -i -n -p su - ${executionUser} -c '${escapedCommand}'`;
export const NSENTER_HOST_CRONTAB = (command: string) =>
`nsenter -t 1 -m -u -i -n -p sh -c "${command}"`;

View File

@@ -0,0 +1,96 @@
"use client";
import React, {
createContext,
useContext,
useEffect,
useRef,
useState,
} from "react";
import { SSEEvent } from "@/app/_utils/sse-events";
import { usePageVisibility } from "@/app/_hooks/usePageVisibility";
interface SSEContextType {
isConnected: boolean;
subscribe: (callback: (event: SSEEvent) => void) => () => void;
}
const SSEContext = createContext<SSEContextType | null>(null);
export const SSEProvider: React.FC<{
children: React.ReactNode;
liveUpdatesEnabled: boolean;
}> = ({ children, liveUpdatesEnabled }) => {
const [isConnected, setIsConnected] = useState(false);
const eventSourceRef = useRef<EventSource | null>(null);
const subscribersRef = useRef<Set<(event: SSEEvent) => void>>(new Set());
const isPageVisible = usePageVisibility();
useEffect(() => {
if (!liveUpdatesEnabled || !isPageVisible) {
if (eventSourceRef.current) {
eventSourceRef.current.close();
eventSourceRef.current = null;
setIsConnected(false);
}
return;
}
const eventSource = new EventSource("/api/events");
eventSource.onopen = () => {
setIsConnected(true);
};
eventSource.onerror = () => {
setIsConnected(false);
};
const eventTypes = [
"job-started",
"job-completed",
"job-failed",
"log-line",
"system-stats",
"heartbeat",
];
eventTypes.forEach((eventType) => {
eventSource.addEventListener(eventType, (event: MessageEvent) => {
try {
const data = JSON.parse(event.data) as SSEEvent;
subscribersRef.current.forEach((callback) => callback(data));
} catch (error) {
console.error(`[SSE] Failed to parse ${eventType} event:`, error);
}
});
});
eventSourceRef.current = eventSource;
return () => {
eventSource.close();
};
}, [liveUpdatesEnabled, isPageVisible]);
const subscribe = (callback: (event: SSEEvent) => void) => {
subscribersRef.current.add(callback);
return () => {
subscribersRef.current.delete(callback);
};
};
return (
<SSEContext.Provider value={{ isConnected, subscribe }}>
{children}
</SSEContext.Provider>
);
};
export const useSSEContext = () => {
const context = useContext(SSEContext);
if (!context) {
throw new Error("useSSEContext must be used within SSEProvider");
}
return context;
};

View File

@@ -0,0 +1,258 @@
"use client";
import { useState, useMemo, useEffect } from "react";
import { CronJob } from "@/app/_utils/cronjob-utils";
import { Script } from "@/app/_utils/scripts-utils";
import {
getJobErrorsByJobId,
JobError,
} from "@/app/_utils/error-utils";
import {
handleErrorClick,
handleDelete,
handleClone,
handlePause,
handleResume,
handleRun,
handleEditSubmit,
handleNewCronSubmit,
handleToggleLogging,
handleBackup,
} from "@/app/_components/FeatureComponents/Cronjobs/helpers";
interface CronJobListProps {
cronJobs: CronJob[];
scripts: Script[];
}
export const useCronJobState = ({ cronJobs, scripts }: CronJobListProps) => {
const [deletingId, setDeletingId] = useState<string | null>(null);
const [editingJob, setEditingJob] = useState<CronJob | null>(null);
const [isEditModalOpen, setIsEditModalOpen] = useState(false);
const [isNewCronModalOpen, setIsNewCronModalOpen] = useState(false);
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
const [isCloneModalOpen, setIsCloneModalOpen] = useState(false);
const [jobToDelete, setJobToDelete] = useState<CronJob | null>(null);
const [jobToClone, setJobToClone] = useState<CronJob | null>(null);
const [isCloning, setIsCloning] = useState(false);
const [runningJobId, setRunningJobId] = useState<string | null>(null);
const [selectedUser, setSelectedUser] = useState<string | null>(null);
const [jobErrors, setJobErrors] = useState<Record<string, JobError[]>>({});
const [errorModalOpen, setErrorModalOpen] = useState(false);
const [selectedError, setSelectedError] = useState<JobError | null>(null);
const [isLogsModalOpen, setIsLogsModalOpen] = useState(false);
const [jobForLogs, setJobForLogs] = useState<CronJob | null>(null);
const [isLiveLogModalOpen, setIsLiveLogModalOpen] = useState(false);
const [liveLogRunId, setLiveLogRunId] = useState<string>("");
const [liveLogJobId, setLiveLogJobId] = useState<string>("");
const [liveLogJobComment, setLiveLogJobComment] = useState<string>("");
const [editForm, setEditForm] = useState({
schedule: "",
command: "",
comment: "",
logsEnabled: false,
});
const [newCronForm, setNewCronForm] = useState({
schedule: "",
command: "",
comment: "",
selectedScriptId: null as string | null,
user: "",
logsEnabled: false,
});
useEffect(() => {
const savedUser = localStorage.getItem("selectedCronUser");
if (savedUser) {
setSelectedUser(savedUser);
}
}, []);
useEffect(() => {
if (selectedUser) {
localStorage.setItem("selectedCronUser", selectedUser);
} else {
localStorage.removeItem("selectedCronUser");
}
}, [selectedUser]);
const filteredJobs = useMemo(() => {
if (!selectedUser) return cronJobs;
return cronJobs.filter((job) => job.user === selectedUser);
}, [cronJobs, selectedUser]);
useEffect(() => {
const errors: Record<string, JobError[]> = {};
filteredJobs.forEach((job) => {
errors[job.id] = getJobErrorsByJobId(job.id);
});
setJobErrors(errors);
}, [filteredJobs]);
const refreshJobErrorsLocal = () => {
const errors: Record<string, JobError[]> = {};
filteredJobs.forEach((job) => {
errors[job.id] = getJobErrorsByJobId(job.id);
});
setJobErrors(errors);
};
const getHelperState = () => ({
setDeletingId,
setIsDeleteModalOpen,
setJobToDelete,
setIsCloneModalOpen,
setJobToClone,
setIsCloning,
setIsEditModalOpen,
setEditingJob,
setIsNewCronModalOpen,
setNewCronForm,
setRunningJobId,
refreshJobErrors: refreshJobErrorsLocal,
setIsLiveLogModalOpen,
setLiveLogRunId,
setLiveLogJobId,
setLiveLogJobComment,
jobToClone,
editingJob,
editForm,
newCronForm,
});
const handleErrorClickLocal = (error: JobError) => {
handleErrorClick(error, setSelectedError, setErrorModalOpen);
};
const handleDeleteLocal = async (id: string) => {
const job = cronJobs.find(j => j.id === id);
if (job) {
await handleDelete(job, getHelperState());
}
};
const handleCloneLocal = async (newComment: string) => {
await handleClone(newComment, getHelperState());
};
const handlePauseLocal = async (id: string) => {
const job = cronJobs.find(j => j.id === id);
if (job) {
await handlePause(job);
}
};
const handleResumeLocal = async (id: string) => {
const job = cronJobs.find(j => j.id === id);
if (job) {
await handleResume(job);
}
};
const handleRunLocal = async (id: string) => {
const job = cronJobs.find(j => j.id === id);
if (!job) return;
await handleRun(id, getHelperState(), job);
};
const handleToggleLoggingLocal = async (id: string) => {
const job = cronJobs.find(j => j.id === id);
if (job) {
await handleToggleLogging(job);
}
};
const handleViewLogs = (job: CronJob) => {
setJobForLogs(job);
setIsLogsModalOpen(true);
};
const confirmDelete = (job: CronJob) => {
setJobToDelete(job);
setIsDeleteModalOpen(true);
};
const confirmClone = (job: CronJob) => {
setJobToClone(job);
setIsCloneModalOpen(true);
};
const handleEdit = (job: CronJob) => {
setEditingJob(job);
setEditForm({
schedule: job.schedule,
command: job.command,
comment: job.comment || "",
logsEnabled: job.logsEnabled || false,
});
setIsEditModalOpen(true);
};
const handleEditSubmitLocal = async (e: React.FormEvent) => {
await handleEditSubmit(e, getHelperState());
};
const handleNewCronSubmitLocal = async (e: React.FormEvent) => {
await handleNewCronSubmit(e, getHelperState());
};
const handleBackupLocal = async (id: string) => {
const job = cronJobs.find(j => j.id === id);
if (job) {
await handleBackup(job);
}
};
return {
deletingId,
runningJobId,
selectedUser,
setSelectedUser,
jobErrors,
errorModalOpen,
setErrorModalOpen,
selectedError,
setSelectedError,
isLogsModalOpen,
setIsLogsModalOpen,
jobForLogs,
isLiveLogModalOpen,
setIsLiveLogModalOpen,
liveLogRunId,
liveLogJobId,
liveLogJobComment,
filteredJobs,
isNewCronModalOpen,
setIsNewCronModalOpen,
isEditModalOpen,
setIsEditModalOpen,
isDeleteModalOpen,
setIsDeleteModalOpen,
isCloneModalOpen,
setIsCloneModalOpen,
jobToDelete,
jobToClone,
isCloning,
editForm,
setEditForm,
newCronForm,
setNewCronForm,
handleErrorClickLocal,
refreshJobErrorsLocal,
handleDeleteLocal,
handleCloneLocal,
handlePauseLocal,
handleResumeLocal,
handleRunLocal,
handleToggleLoggingLocal,
handleViewLogs,
confirmDelete,
confirmClone,
handleEdit,
handleEditSubmitLocal,
handleNewCronSubmitLocal,
handleBackupLocal,
};
};

View File

@@ -0,0 +1,24 @@
"use client";
import { useEffect, useState } from "react";
export function usePageVisibility(): boolean {
const [isVisible, setIsVisible] = useState<boolean>(
typeof document !== "undefined" ? !document.hidden : true
);
useEffect(() => {
const handleVisibilityChange = () => {
setIsVisible(!document.hidden);
};
document.addEventListener("visibilitychange", handleVisibilityChange);
return () => {
document.removeEventListener("visibilitychange", handleVisibilityChange);
};
}, []);
return isVisible;
}

126
app/_hooks/useSSE.ts Normal file
View File

@@ -0,0 +1,126 @@
"use client";
import { useEffect, useRef, useCallback } from "react";
import { SSEEvent, SSEEventType } from "@/app/_utils/sse-events";
type SSEEventHandler = (event: SSEEvent) => void;
type SSEErrorHandler = (error: Event) => void;
interface UseSSEOptions {
enabled?: boolean;
onEvent?: SSEEventHandler;
onError?: SSEErrorHandler;
onConnect?: () => void;
onDisconnect?: () => void;
}
/**
* Custom hook for consuming Server-Sent Events
*
* @param options Configuration options
* @returns Object with connection status and manual control functions
*
* @example
* ```tsx
* const { isConnected } = useSSE({
* enabled: true,
* onEvent: (event) => {
* if (event.type === 'job-started') {
* console.log('Job started:', event.data);
* }
* },
* });
* ```
*/
export const useSSE = (options: UseSSEOptions = {}) => {
const { enabled = true, onEvent, onError, onConnect, onDisconnect } = options;
const eventSourceRef = useRef<EventSource | null>(null);
const isConnectedRef = useRef(false);
const onEventRef = useRef(onEvent);
const onErrorRef = useRef(onError);
const onConnectRef = useRef(onConnect);
const onDisconnectRef = useRef(onDisconnect);
useEffect(() => {
onEventRef.current = onEvent;
onErrorRef.current = onError;
onConnectRef.current = onConnect;
onDisconnectRef.current = onDisconnect;
});
const connect = useCallback(() => {
if (eventSourceRef.current || !enabled) {
return;
}
try {
const eventSource = new EventSource("/api/events");
eventSource.onopen = () => {
isConnectedRef.current = true;
onConnectRef.current?.();
};
eventSource.onerror = (error) => {
isConnectedRef.current = false;
onErrorRef.current?.(error);
if (eventSource.readyState === EventSource.CLOSED) {
onDisconnectRef.current?.();
}
};
const eventTypes: SSEEventType[] = [
"job-started",
"job-completed",
"job-failed",
"log-line",
"system-stats",
"heartbeat",
];
eventTypes.forEach((eventType) => {
eventSource.addEventListener(eventType, (event: MessageEvent) => {
try {
const data = JSON.parse(event.data) as SSEEvent;
onEventRef.current?.(data);
} catch (error) {
console.error(`[SSE] Failed to parse ${eventType} event:`, error);
}
});
});
eventSourceRef.current = eventSource;
} catch (error) {
console.error("[SSE] Failed to create EventSource:", error);
}
}, [enabled]);
const disconnect = useCallback(() => {
if (eventSourceRef.current) {
eventSourceRef.current.close();
eventSourceRef.current = null;
isConnectedRef.current = false;
onDisconnectRef.current?.();
}
}, []);
useEffect(() => {
if (enabled) {
connect();
} else {
disconnect();
}
return () => {
disconnect();
};
}, [enabled, connect, disconnect]);
return {
isConnected: isConnectedRef.current,
connect,
disconnect,
};
};

View File

@@ -0,0 +1,26 @@
'use client'
import { ThemeProvider as NextThemesProvider } from 'next-themes';
import { type ThemeProviderProps } from 'next-themes/dist/types';
export const ThemeProvider = ({ children, ...props }: ThemeProviderProps) => {
return (
<NextThemesProvider
attribute="data-webtui-theme"
defaultTheme="light"
themes={['light', 'dark']}
value={{
light: 'catppuccin-latte',
dark: 'catppuccin-mocha',
}}
disableTransitionOnChange
{...props}
>
{children}
</NextThemesProvider>
);
}

View File

@@ -0,0 +1,69 @@
#!/bin/bash
# Cr*nmaster Log Wrapper Script
# Captures stdout, stderr, exit code, and timestamps for cronjob executions
#
# Usage: cron-log-wrapper.sh <logFolderName> <command...>
# Example: cron-log-wrapper.sh "backup-database" bash /app/scripts/backup.sh
set -u
if [ $# -lt 2 ]; then
echo "ERROR: Usage: $0 <logFolderName> <command...>" >&2
exit 1
fi
LOG_FOLDER_NAME="$1"
shift
# Get the script's absolute directory path (e.g., ./data)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LOG_DIR="${SCRIPT_DIR}/logs/${LOG_FOLDER_NAME}"
# Ensure the log directory exists
mkdir -p "$LOG_DIR"
TIMESTAMP_FILE=$(date '+%Y-%m-%d_%H-%M-%S')
HUMAN_START_TIME=$(date '+%Y-%m-%d %H:%M:%S')
LOG_FILE="${LOG_DIR}/${TIMESTAMP_FILE}.log"
START_SECONDS=$SECONDS
{
echo "--- [ JOB START ] ----------------------------------------------------"
echo "Command : $*"
echo "Timestamp : ${HUMAN_START_TIME}"
echo "Host : $(hostname)"
echo "User : $(whoami)"
echo "--- [ JOB OUTPUT ] ---------------------------------------------------"
echo ""
# Execute the command, capturing its exit code
"$@"
EXIT_CODE=$?
DURATION=$((SECONDS - START_SECONDS))
HUMAN_END_TIME=$(date '+%Y-%m-%d %H:%M:%S')
if [ $EXIT_CODE -eq 0 ]; then
STATUS="SUCCESS"
else
STATUS="FAILED"
fi
echo ""
echo "--- [ JOB SUMMARY ] --------------------------------------------------"
echo "Timestamp : ${HUMAN_END_TIME}"
echo "Duration : ${DURATION}s"
# ⚠️ ATTENTION: DO NOT MODIFY THE EXIT CODE LINE ⚠️
# The UI reads this exact format to detect job failures. Keep it as: "Exit Code : ${EXIT_CODE}"
echo "Exit Code : ${EXIT_CODE}"
echo "Status : ${STATUS}"
echo "--- [ JOB END ] ------------------------------------------------------"
exit $EXIT_CODE
} >> "$LOG_FILE" 2>&1
# Pass the command's exit code back to cron
exit $?

View File

@@ -3,32 +3,47 @@
import { import {
getCronJobs, getCronJobs,
addCronJob, addCronJob,
deleteCronJob, cleanupCrontab,
readUserCrontab,
writeUserCrontab,
findJobIndex,
updateCronJob, updateCronJob,
type CronJob, type CronJob,
} from "@/app/_utils/system"; } from "@/app/_utils/cronjob-utils";
import { getAllTargetUsers } from "@/app/_utils/crontab-utils";
import { revalidatePath } from "next/cache"; import { revalidatePath } from "next/cache";
import { getScriptPath } from "@/app/_utils/scripts"; import { getScriptPathForCron } from "@/app/_server/actions/scripts";
import { isDocker } from "@/app/_server/actions/global";
import {
runJobSynchronously,
runJobInBackground,
} from "@/app/_utils/job-execution-utils";
import {
pauseJobInLines,
resumeJobInLines,
deleteJobInLines,
} from "@/app/_utils/line-manipulation-utils";
import { cleanCrontabContent } from "@/app/_utils/files-manipulation-utils";
export async function fetchCronJobs(): Promise<CronJob[]> { export const fetchCronJobs = async (): Promise<CronJob[]> => {
try { try {
return await getCronJobs(); return await getCronJobs();
} catch (error) { } catch (error) {
console.error("Error fetching cron jobs:", error); console.error("Error fetching cron jobs:", error);
return []; return [];
} }
} };
export const createCronJob = async (
export async function createCronJob(
formData: FormData formData: FormData
): Promise<{ success: boolean; message: string }> { ): Promise<{ success: boolean; message: string; details?: string }> => {
try { try {
const schedule = formData.get("schedule") as string; const schedule = formData.get("schedule") as string;
const command = formData.get("command") as string; const command = formData.get("command") as string;
const comment = formData.get("comment") as string; const comment = formData.get("comment") as string;
const selectedScriptId = formData.get("selectedScriptId") as string; const selectedScriptId = formData.get("selectedScriptId") as string;
const user = formData.get("user") as string;
const logsEnabled = formData.get("logsEnabled") === "true";
if (!schedule) { if (!schedule) {
return { success: false, message: "Schedule is required" }; return { success: false, message: "Schedule is required" };
@@ -37,12 +52,12 @@ export async function createCronJob(
let finalCommand = command; let finalCommand = command;
if (selectedScriptId) { if (selectedScriptId) {
const { fetchScripts } = await import("../scripts"); const { fetchScripts } = await import("@/app/_server/actions/scripts");
const scripts = await fetchScripts(); const scripts = await fetchScripts();
const selectedScript = scripts.find((s) => s.id === selectedScriptId); const selectedScript = scripts.find((s) => s.id === selectedScriptId);
if (selectedScript) { if (selectedScript) {
finalCommand = getScriptPath(selectedScript.filename); finalCommand = await getScriptPathForCron(selectedScript.filename);
} else { } else {
return { success: false, message: "Selected script not found" }; return { success: false, message: "Selected script not found" };
} }
@@ -53,68 +68,112 @@ export async function createCronJob(
}; };
} }
const success = await addCronJob(schedule, finalCommand, comment); const success = await addCronJob(
schedule,
finalCommand,
comment,
user,
logsEnabled
);
if (success) { if (success) {
revalidatePath("/"); revalidatePath("/");
return { success: true, message: "Cron job created successfully" }; return { success: true, message: "Cron job created successfully" };
} else { } else {
return { success: false, message: "Failed to create cron job" }; return { success: false, message: "Failed to create cron job" };
} }
} catch (error) { } catch (error: any) {
console.error("Error creating cron job:", error); console.error("Error creating cron job:", error);
return { success: false, message: "Error creating cron job" }; return {
success: false,
message: error.message || "Error creating cron job",
details: error.stack,
};
} }
} };
export async function removeCronJob( export const removeCronJob = async (
id: string jobData: { id: string; schedule: string; command: string; comment?: string; user: string }
): Promise<{ success: boolean; message: string }> { ): Promise<{ success: boolean; message: string; details?: string }> => {
try { try {
const success = await deleteCronJob(id); const cronContent = await readUserCrontab(jobData.user);
const lines = cronContent.split("\n");
const jobIndex = findJobIndex(jobData, lines, jobData.user);
if (jobIndex === -1) {
return { success: false, message: "Cron job not found in crontab" };
}
const newCronEntries = deleteJobInLines(lines, jobIndex);
const newCron = await cleanCrontabContent(newCronEntries.join("\n"));
const success = await writeUserCrontab(jobData.user, newCron);
if (success) { if (success) {
revalidatePath("/"); revalidatePath("/");
return { success: true, message: "Cron job deleted successfully" }; return { success: true, message: "Cron job deleted successfully" };
} else { } else {
return { success: false, message: "Failed to delete cron job" }; return { success: false, message: "Failed to delete cron job" };
} }
} catch (error) { } catch (error: any) {
console.error("Error deleting cron job:", error); console.error("Error deleting cron job:", error);
return { success: false, message: "Error deleting cron job" }; return {
success: false,
message: error.message || "Error deleting cron job",
details: error.stack,
};
} }
} };
export async function editCronJob( export const editCronJob = async (
formData: FormData formData: FormData
): Promise<{ success: boolean; message: string }> { ): Promise<{ success: boolean; message: string; details?: string }> => {
try { try {
const id = formData.get("id") as string; const id = formData.get("id") as string;
const schedule = formData.get("schedule") as string; const schedule = formData.get("schedule") as string;
const command = formData.get("command") as string; const command = formData.get("command") as string;
const comment = formData.get("comment") as string; const comment = formData.get("comment") as string;
const logsEnabled = formData.get("logsEnabled") === "true";
if (!id || !schedule || !command) { if (!id || !schedule || !command) {
return { success: false, message: "Missing required fields" }; return { success: false, message: "Missing required fields" };
} }
const success = await updateCronJob(id, schedule, command, comment); const cronJobs = await getCronJobs(false);
const job = cronJobs.find((j) => j.id === id);
if (!job) {
return { success: false, message: "Cron job not found" };
}
const success = await updateCronJob(
job,
schedule,
command,
comment,
logsEnabled
);
if (success) { if (success) {
revalidatePath("/"); revalidatePath("/");
return { success: true, message: "Cron job updated successfully" }; return { success: true, message: "Cron job updated successfully" };
} else { } else {
return { success: false, message: "Failed to update cron job" }; return { success: false, message: "Failed to update cron job" };
} }
} catch (error) { } catch (error: any) {
console.error("Error updating cron job:", error); console.error("Error updating cron job:", error);
return { success: false, message: "Error updating cron job" }; return {
success: false,
message: error.message || "Error updating cron job",
details: error.stack,
};
} }
} };
export async function cloneCronJob( export const cloneCronJob = async (
id: string, id: string,
newComment: string newComment: string
): Promise<{ success: boolean; message: string }> { ): Promise<{ success: boolean; message: string; details?: string }> => {
try { try {
const cronJobs = await getCronJobs(); const cronJobs = await getCronJobs(false);
const originalJob = cronJobs.find((job) => job.id === id); const originalJob = cronJobs.find((job) => job.id === id);
if (!originalJob) { if (!originalJob) {
@@ -124,7 +183,8 @@ export async function cloneCronJob(
const success = await addCronJob( const success = await addCronJob(
originalJob.schedule, originalJob.schedule,
originalJob.command, originalJob.command,
newComment newComment,
originalJob.user
); );
if (success) { if (success) {
@@ -133,8 +193,424 @@ export async function cloneCronJob(
} else { } else {
return { success: false, message: "Failed to clone cron job" }; return { success: false, message: "Failed to clone cron job" };
} }
} catch (error) { } catch (error: any) {
console.error("Error cloning cron job:", error); console.error("Error cloning cron job:", error);
return { success: false, message: "Error cloning cron job" }; return {
success: false,
message: error.message || "Error cloning cron job",
details: error.stack,
};
} }
} };
export const pauseCronJobAction = async (
jobData: { id: string; schedule: string; command: string; comment?: string; user: string }
): Promise<{ success: boolean; message: string; details?: string }> => {
try {
const cronContent = await readUserCrontab(jobData.user);
const lines = cronContent.split("\n");
const jobIndex = findJobIndex(jobData, lines, jobData.user);
if (jobIndex === -1) {
return { success: false, message: "Cron job not found in crontab" };
}
const newCronEntries = pauseJobInLines(lines, jobIndex, jobData.id);
const newCron = await cleanCrontabContent(newCronEntries.join("\n"));
const success = await writeUserCrontab(jobData.user, newCron);
if (success) {
revalidatePath("/");
return { success: true, message: "Cron job paused successfully" };
} else {
return { success: false, message: "Failed to pause cron job" };
}
} catch (error: any) {
console.error("Error pausing cron job:", error);
return {
success: false,
message: error.message || "Error pausing cron job",
details: error.stack,
};
}
};
export const resumeCronJobAction = async (
jobData: { id: string; schedule: string; command: string; comment?: string; user: string }
): Promise<{ success: boolean; message: string; details?: string }> => {
try {
const cronContent = await readUserCrontab(jobData.user);
const lines = cronContent.split("\n");
const jobIndex = findJobIndex(jobData, lines, jobData.user);
if (jobIndex === -1) {
return { success: false, message: "Cron job not found in crontab" };
}
const newCronEntries = resumeJobInLines(lines, jobIndex, jobData.id);
const newCron = await cleanCrontabContent(newCronEntries.join("\n"));
const success = await writeUserCrontab(jobData.user, newCron);
if (success) {
revalidatePath("/");
return { success: true, message: "Cron job resumed successfully" };
} else {
return { success: false, message: "Failed to resume cron job" };
}
} catch (error: any) {
console.error("Error resuming cron job:", error);
return {
success: false,
message: error.message || "Error resuming cron job",
details: error.stack,
};
}
};
export const fetchAvailableUsers = async (): Promise<string[]> => {
try {
return await getAllTargetUsers();
} catch (error) {
console.error("Error fetching available users:", error);
return [];
}
};
export const cleanupCrontabAction = async (): Promise<{
success: boolean;
message: string;
details?: string;
}> => {
try {
const success = await cleanupCrontab();
if (success) {
revalidatePath("/");
return { success: true, message: "Crontab cleaned successfully" };
} else {
return { success: false, message: "Failed to clean crontab" };
}
} catch (error: any) {
console.error("Error cleaning crontab:", error);
return {
success: false,
message: error.message || "Error cleaning crontab",
details: error.stack,
};
}
};
export const toggleCronJobLogging = async (
jobData: { id: string; schedule: string; command: string; comment?: string; user: string; logsEnabled?: boolean }
): Promise<{ success: boolean; message: string; details?: string }> => {
try {
const newLogsEnabled = !jobData.logsEnabled;
const success = await updateCronJob(
jobData,
jobData.schedule,
jobData.command,
jobData.comment || "",
newLogsEnabled
);
if (success) {
revalidatePath("/");
return {
success: true,
message: newLogsEnabled
? "Logging enabled successfully"
: "Logging disabled successfully",
};
} else {
return { success: false, message: "Failed to toggle logging" };
}
} catch (error: any) {
console.error("Error toggling logging:", error);
return {
success: false,
message: error.message || "Error toggling logging",
details: error.stack,
};
}
};
export const runCronJob = async (
id: string
): Promise<{
success: boolean;
message: string;
output?: string;
details?: string;
runId?: string;
mode?: "sync" | "async";
}> => {
try {
const cronJobs = await getCronJobs(false);
const job = cronJobs.find((j) => j.id === id);
if (!job) {
return { success: false, message: "Cron job not found" };
}
if (job.paused) {
return { success: false, message: "Cannot run paused cron job" };
}
const docker = await isDocker();
const liveUpdatesEnabled =
(typeof process.env.LIVE_UPDATES === "boolean" &&
process.env.LIVE_UPDATES === true) ||
process.env.LIVE_UPDATES !== "false";
if (job.logsEnabled && liveUpdatesEnabled) {
return runJobInBackground(job, docker);
}
return runJobSynchronously(job, docker);
} catch (error: any) {
console.error("Error running cron job:", error);
const errorMessage =
error.stderr || error.message || "Unknown error occurred";
return {
success: false,
message: "Failed to execute cron job",
output: errorMessage.trim(),
details: error.stack,
};
}
};
export const executeJob = async (
id: string,
runInBackground: boolean = true
): Promise<{
success: boolean;
message: string;
output?: string;
details?: string;
runId?: string;
mode?: "sync" | "async";
}> => {
try {
const cronJobs = await getCronJobs(false);
const job = cronJobs.find((j) => j.id === id);
if (!job) {
return { success: false, message: "Cron job not found" };
}
if (job.paused) {
return { success: false, message: "Cannot run paused cron job" };
}
const docker = await isDocker();
if (runInBackground) {
return runJobInBackground(job, docker);
}
return runJobSynchronously(job, docker);
} catch (error: any) {
console.error("Error executing cron job:", error);
const errorMessage =
error.stderr || error.message || "Unknown error occurred";
return {
success: false,
message: "Failed to execute cron job",
output: errorMessage.trim(),
details: error.stack,
};
}
};
export const backupCronJob = async (
job: CronJob
): Promise<{ success: boolean; message: string; details?: string }> => {
try {
const {
backupJobToFile,
} = await import("@/app/_utils/backup-utils");
const success = await backupJobToFile(job);
if (success) {
return { success: true, message: "Cron job backed up successfully" };
} else {
return { success: false, message: "Failed to backup cron job" };
}
} catch (error: any) {
console.error("Error backing up cron job:", error);
return {
success: false,
message: error.message || "Error backing up cron job",
details: error.stack,
};
}
};
export const backupAllCronJobs = async (): Promise<{
success: boolean;
message: string;
details?: string;
}> => {
try {
const {
backupAllJobsToFiles,
} = await import("@/app/_utils/backup-utils");
const result = await backupAllJobsToFiles();
if (result.success) {
return {
success: true,
message: `Backed up ${result.count} cron job(s) successfully`,
};
} else {
return { success: false, message: "Failed to backup cron jobs" };
}
} catch (error: any) {
console.error("Error backing up all cron jobs:", error);
return {
success: false,
message: error.message || "Error backing up all cron jobs",
details: error.stack,
};
}
};
export const fetchBackupFiles = async (): Promise<Array<{
filename: string;
job: CronJob;
backedUpAt: string;
}>> => {
try {
const {
getAllBackupFiles,
} = await import("@/app/_utils/backup-utils");
return await getAllBackupFiles();
} catch (error) {
console.error("Error fetching backup files:", error);
return [];
}
};
export const restoreCronJob = async (
filename: string
): Promise<{ success: boolean; message: string; details?: string }> => {
try {
const {
restoreJobFromBackup,
} = await import("@/app/_utils/backup-utils");
const result = await restoreJobFromBackup(filename);
if (!result.success || !result.job) {
return { success: false, message: "Failed to read backup file" };
}
const job = result.job;
const success = await addCronJob(
job.schedule,
job.command,
job.comment || "",
job.user,
job.logsEnabled || false
);
if (success) {
revalidatePath("/");
return { success: true, message: "Cron job restored successfully" };
} else {
return { success: false, message: "Failed to restore cron job" };
}
} catch (error: any) {
console.error("Error restoring cron job:", error);
return {
success: false,
message: error.message || "Error restoring cron job",
details: error.stack,
};
}
};
export const deleteBackup = async (
filename: string
): Promise<{ success: boolean; message: string; details?: string }> => {
try {
const {
deleteBackupFile,
} = await import("@/app/_utils/backup-utils");
const success = await deleteBackupFile(filename);
if (success) {
return { success: true, message: "Backup deleted successfully" };
} else {
return { success: false, message: "Failed to delete backup" };
}
} catch (error: any) {
console.error("Error deleting backup:", error);
return {
success: false,
message: error.message || "Error deleting backup",
details: error.stack,
};
}
};
export const restoreAllCronJobs = async (): Promise<{
success: boolean;
message: string;
details?: string;
}> => {
try {
const {
getAllBackupFiles,
} = await import("@/app/_utils/backup-utils");
const backups = await getAllBackupFiles();
if (backups.length === 0) {
return { success: false, message: "No backup files found" };
}
let successCount = 0;
let failedCount = 0;
for (const backup of backups) {
const job = backup.job;
const success = await addCronJob(
job.schedule,
job.command,
job.comment || "",
job.user,
job.logsEnabled || false
);
if (success) {
successCount++;
} else {
failedCount++;
}
}
revalidatePath("/");
if (failedCount === 0) {
return {
success: true,
message: `Successfully restored ${successCount} cron job(s)`,
};
} else {
return {
success: true,
message: `Restored ${successCount} job(s), ${failedCount} failed`,
};
}
} catch (error: any) {
console.error("Error restoring all cron jobs:", error);
return {
success: false,
message: error.message || "Error restoring all cron jobs",
details: error.stack,
};
}
};

View File

@@ -0,0 +1,86 @@
"use server";
import { existsSync, readFileSync } from "fs";
import { execSync } from "child_process";
export const isDocker = async (): Promise<boolean> => {
try {
if (existsSync("/.dockerenv")) {
return true;
}
if (existsSync("/proc/1/cgroup")) {
const cgroupContent = readFileSync("/proc/1/cgroup", "utf8");
return cgroupContent.includes("/docker/");
}
return false;
} catch (error) {
return false;
}
};
export const getContainerIdentifier = async (): Promise<string | null> => {
try {
const docker = await isDocker();
if (!docker) {
return null;
}
const containerId = execSync("hostname").toString().trim();
return containerId;
} catch (error) {
console.error("Failed to get container identifier:", error);
return null;
}
};
export const getHostDataPath = async (): Promise<string | null> => {
try {
const docker = await isDocker();
if (!docker) {
return null;
}
const containerId = await getContainerIdentifier();
if (!containerId) {
return null;
}
const stdout = execSync(
`docker inspect --format '{{range .Mounts}}{{if eq .Destination "/app/data"}}{{.Source}}{{end}}{{end}}' ${containerId}`,
{ encoding: "utf8" }
);
const hostPath = stdout.trim();
return hostPath || null;
} catch (error) {
console.error("Failed to get host data path:", error);
return null;
}
};
export const getHostScriptsPath = async (): Promise<string | null> => {
try {
const docker = await isDocker();
if (!docker) {
return null;
}
const containerId = await getContainerIdentifier();
if (!containerId) {
return null;
}
const stdout = execSync(
`docker inspect --format '{{range .Mounts}}{{if eq .Destination "/app/scripts"}}{{.Source}}{{end}}{{end}}' ${containerId}`,
{ encoding: "utf8" }
);
const hostPath = stdout.trim();
return hostPath || null;
} catch (error) {
console.error("Failed to get host scripts path:", error);
return null;
}
};

View File

@@ -0,0 +1,354 @@
"use server";
import { readdir, readFile, unlink, stat } from "fs/promises";
import path from "path";
import { existsSync } from "fs";
import { DATA_DIR } from "@/app/_consts/file";
export interface LogEntry {
filename: string;
timestamp: string;
fullPath: string;
size: number;
dateCreated: Date;
exitCode?: number;
hasError?: boolean;
}
export interface JobLogError {
hasError: boolean;
lastFailedLog?: string;
lastFailedTimestamp?: Date;
exitCode?: number;
latestExitCode?: number;
hasHistoricalFailures?: boolean;
}
const MAX_LOGS_PER_JOB = process.env.MAX_LOGS_PER_JOB
? parseInt(process.env.MAX_LOGS_PER_JOB)
: 50;
const MAX_LOG_AGE_DAYS = process.env.MAX_LOG_AGE_DAYS
? parseInt(process.env.MAX_LOG_AGE_DAYS)
: 30;
const getLogBasePath = async (): Promise<string> => {
return path.join(process.cwd(), DATA_DIR, "logs");
};
const getJobLogPath = async (jobId: string): Promise<string | null> => {
const basePath = await getLogBasePath();
if (!existsSync(basePath)) {
return null;
}
try {
const allFolders = await readdir(basePath);
const matchingFolder = allFolders.find(
(folder) => folder === jobId || folder.endsWith(`_${jobId}`)
);
if (matchingFolder) {
return path.join(basePath, matchingFolder);
}
return path.join(basePath, jobId);
} catch (error) {
console.error("Error finding log path:", error);
return path.join(basePath, jobId);
}
};
export const getJobLogs = async (
jobId: string,
skipCleanup: boolean = false,
includeExitCodes: boolean = false
): Promise<LogEntry[]> => {
try {
const logDir = await getJobLogPath(jobId);
if (!logDir || !existsSync(logDir)) {
return [];
}
if (!skipCleanup) {
await cleanupJobLogs(jobId);
}
const files = await readdir(logDir);
const logFiles = files.filter((f) => f.endsWith(".log"));
const entries: LogEntry[] = [];
for (const file of logFiles) {
const fullPath = path.join(logDir, file);
const stats = await stat(fullPath);
let exitCode: number | undefined;
let hasError: boolean | undefined;
if (includeExitCodes) {
const exitCodeValue = await getExitCodeForLog(fullPath);
if (exitCodeValue !== null) {
exitCode = exitCodeValue;
hasError = exitCode !== 0;
}
}
entries.push({
filename: file,
timestamp: file.replace(".log", ""),
fullPath,
size: stats.size,
dateCreated: stats.birthtime,
exitCode,
hasError,
});
}
return entries.sort(
(a, b) => b.dateCreated.getTime() - a.dateCreated.getTime()
);
} catch (error) {
console.error(`Error reading logs for job ${jobId}:`, error);
return [];
}
};
export const getLogContent = async (
jobId: string,
filename: string
): Promise<string> => {
try {
const logDir = await getJobLogPath(jobId);
if (!logDir) {
return "Log directory not found";
}
const logPath = path.join(logDir, filename);
const content = await readFile(logPath, "utf-8");
return content;
} catch (error) {
console.error(`Error reading log file ${filename}:`, error);
return "Error reading log file";
}
};
export const deleteLogFile = async (
jobId: string,
filename: string
): Promise<{ success: boolean; message: string }> => {
try {
const logDir = await getJobLogPath(jobId);
if (!logDir) {
return {
success: false,
message: "Log directory not found",
};
}
const logPath = path.join(logDir, filename);
await unlink(logPath);
return {
success: true,
message: "Log file deleted successfully",
};
} catch (error: any) {
console.error(`Error deleting log file ${filename}:`, error);
return {
success: false,
message: error.message || "Error deleting log file",
};
}
};
export const deleteAllJobLogs = async (
jobId: string
): Promise<{ success: boolean; message: string; deletedCount: number }> => {
try {
const logs = await getJobLogs(jobId);
let deletedCount = 0;
for (const log of logs) {
const result = await deleteLogFile(jobId, log.filename);
if (result.success) {
deletedCount++;
}
}
return {
success: true,
message: `Deleted ${deletedCount} log files`,
deletedCount,
};
} catch (error: any) {
console.error(`Error deleting all logs for job ${jobId}:`, error);
return {
success: false,
message: error.message || "Error deleting log files",
deletedCount: 0,
};
}
};
export const cleanupJobLogs = async (
jobId: string
): Promise<{ success: boolean; message: string; deletedCount: number }> => {
try {
const logs = await getJobLogs(jobId, true);
if (logs.length === 0) {
return {
success: true,
message: "No logs to clean up",
deletedCount: 0,
};
}
let deletedCount = 0;
const now = new Date();
const maxAgeMs = MAX_LOG_AGE_DAYS * 24 * 60 * 60 * 1000;
for (const log of logs) {
const ageMs = now.getTime() - log.dateCreated.getTime();
if (ageMs > maxAgeMs) {
const result = await deleteLogFile(jobId, log.filename);
if (result.success) {
deletedCount++;
}
}
}
const remainingLogs = await getJobLogs(jobId, true);
if (remainingLogs.length > MAX_LOGS_PER_JOB) {
const logsToDelete = remainingLogs.slice(MAX_LOGS_PER_JOB);
for (const log of logsToDelete) {
const result = await deleteLogFile(jobId, log.filename);
if (result.success) {
deletedCount++;
}
}
}
return {
success: true,
message: `Cleaned up ${deletedCount} log files`,
deletedCount,
};
} catch (error: any) {
console.error(`Error cleaning up logs for job ${jobId}:`, error);
return {
success: false,
message: error.message || "Error cleaning up log files",
deletedCount: 0,
};
}
};
export const getJobLogStats = async (
jobId: string
): Promise<{ count: number; totalSize: number; totalSizeMB: number }> => {
try {
const logs = await getJobLogs(jobId);
const totalSize = logs.reduce((sum, log) => sum + log.size, 0);
const totalSizeMB = totalSize / (1024 * 1024);
return {
count: logs.length,
totalSize,
totalSizeMB: Math.round(totalSizeMB * 100) / 100,
};
} catch (error) {
console.error(`Error getting log stats for job ${jobId}:`, error);
return {
count: 0,
totalSize: 0,
totalSizeMB: 0,
};
}
};
const getExitCodeForLog = async (logPath: string): Promise<number | null> => {
try {
const content = await readFile(logPath, "utf-8");
const exitCodeMatch = content.match(/Exit Code\s*:\s*(-?\d+)/i);
if (exitCodeMatch) {
return parseInt(exitCodeMatch[1]);
}
return null;
} catch (error) {
console.error(`Error getting exit code for ${logPath}:`, error);
return null;
}
};
export const getJobLogError = async (jobId: string): Promise<JobLogError> => {
try {
const logs = await getJobLogs(jobId);
if (logs.length === 0) {
return { hasError: false };
}
const latestLog = logs[0];
const latestExitCode = await getExitCodeForLog(latestLog.fullPath);
if (latestExitCode !== null && latestExitCode !== 0) {
return {
hasError: true,
lastFailedLog: latestLog.filename,
lastFailedTimestamp: latestLog.dateCreated,
exitCode: latestExitCode,
latestExitCode,
hasHistoricalFailures: false,
};
}
let hasHistoricalFailures = false;
let lastFailedLog: string | undefined;
let lastFailedTimestamp: Date | undefined;
let failedExitCode: number | undefined;
for (let i = 1; i < logs.length; i++) {
const exitCode = await getExitCodeForLog(logs[i].fullPath);
if (exitCode !== null && exitCode !== 0) {
hasHistoricalFailures = true;
lastFailedLog = logs[i].filename;
lastFailedTimestamp = logs[i].dateCreated;
failedExitCode = exitCode;
break;
}
}
return {
hasError: false,
latestExitCode: latestExitCode ?? undefined,
hasHistoricalFailures,
lastFailedLog,
lastFailedTimestamp,
exitCode: failedExitCode,
};
} catch (error) {
console.error(`Error checking log errors for job ${jobId}:`, error);
return { hasError: false };
}
};
export const getAllJobLogErrors = async (
jobIds: string[]
): Promise<Map<string, JobLogError>> => {
const errorMap = new Map<string, JobLogError>();
await Promise.all(
jobIds.map(async (jobId) => {
const error = await getJobLogError(jobId);
errorMap.set(jobId, error);
})
);
return errorMap;
};

View File

@@ -2,18 +2,42 @@
import { revalidatePath } from "next/cache"; import { revalidatePath } from "next/cache";
import { writeFile, readFile, unlink, mkdir } from "fs/promises"; import { writeFile, readFile, unlink, mkdir } from "fs/promises";
import { join } from "path"; import path from "path";
import { existsSync } from "fs"; import { existsSync } from "fs";
import { exec } from "child_process"; import { exec } from "child_process";
import { promisify } from "util"; import { promisify } from "util";
import { SCRIPTS_DIR } from "@/app/_utils/scripts"; import { SCRIPTS_DIR } from "@/app/_consts/file";
import { loadAllScripts, type Script } from "@/app/_utils/scriptScanner"; import { loadAllScripts, Script } from "@/app/_utils/scripts-utils";
import { MAKE_SCRIPT_EXECUTABLE, RUN_SCRIPT } from "@/app/_consts/commands";
import { isDocker, getHostScriptsPath } from "@/app/_server/actions/global";
const execAsync = promisify(exec); const execAsync = promisify(exec);
export type { Script } from "@/app/_utils/scriptScanner"; export const getScriptPathForCron = async (
filename: string
): Promise<string> => {
const docker = await isDocker();
function sanitizeScriptName(name: string): string { if (docker) {
const hostScriptsPath = await getHostScriptsPath();
if (hostScriptsPath) {
return `bash ${path.join(hostScriptsPath, filename)}`;
}
console.warn("Could not determine host scripts path, using container path");
}
return `bash ${path.join(process.cwd(), SCRIPTS_DIR, filename)}`;
};
export const getHostScriptPath = async (filename: string): Promise<string> => {
return `bash ${path.join(process.cwd(), SCRIPTS_DIR, filename)}`;
};
export const normalizeLineEndings = async (content: string): Promise<string> => {
return content.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
};
const sanitizeScriptName = (name: string): string => {
return name return name
.toLowerCase() .toLowerCase()
.replace(/[^a-z0-9\s-]/g, "") .replace(/[^a-z0-9\s-]/g, "")
@@ -21,9 +45,9 @@ function sanitizeScriptName(name: string): string {
.replace(/-+/g, "-") .replace(/-+/g, "-")
.replace(/^-|-$/g, "") .replace(/^-|-$/g, "")
.substring(0, 50); .substring(0, 50);
} };
async function generateUniqueFilename(baseName: string): Promise<string> { const generateUniqueFilename = async (baseName: string): Promise<string> => {
const scripts = await loadAllScripts(); const scripts = await loadAllScripts();
let filename = `${sanitizeScriptName(baseName)}.sh`; let filename = `${sanitizeScriptName(baseName)}.sh`;
let counter = 1; let counter = 1;
@@ -34,44 +58,49 @@ async function generateUniqueFilename(baseName: string): Promise<string> {
} }
return filename; return filename;
} };
async function ensureScriptsDirectory() { const ensureScriptsDirectory = async () => {
if (!existsSync(SCRIPTS_DIR)) { const scriptsDir = path.join(process.cwd(), SCRIPTS_DIR);
await mkdir(SCRIPTS_DIR, { recursive: true }); if (!existsSync(scriptsDir)) {
await mkdir(scriptsDir, { recursive: true });
} }
} };
async function ensureHostScriptsDirectory() { const ensureHostScriptsDirectory = async () => {
const isDocker = process.env.DOCKER === "true"; const hostScriptsDir = path.join(process.cwd(), SCRIPTS_DIR);
const hostScriptsDir = isDocker
? "/app/scripts"
: join(process.cwd(), "scripts");
if (!existsSync(hostScriptsDir)) { if (!existsSync(hostScriptsDir)) {
await mkdir(hostScriptsDir, { recursive: true }); await mkdir(hostScriptsDir, { recursive: true });
} }
} };
async function saveScriptFile(filename: string, content: string) { const saveScriptFile = async (filename: string, content: string) => {
await ensureScriptsDirectory(); await ensureScriptsDirectory();
const scriptPath = join(SCRIPTS_DIR, filename);
await writeFile(scriptPath, content, "utf8");
}
async function deleteScriptFile(filename: string) { const scriptPath = path.join(process.cwd(), SCRIPTS_DIR, filename);
const scriptPath = join(SCRIPTS_DIR, filename); await writeFile(scriptPath, content, "utf8");
try {
await execAsync(MAKE_SCRIPT_EXECUTABLE(scriptPath));
} catch (error) {
console.error(`Failed to set execute permissions on ${scriptPath}:`, error);
}
};
const deleteScriptFile = async (filename: string) => {
const scriptPath = path.join(process.cwd(), SCRIPTS_DIR, filename);
if (existsSync(scriptPath)) { if (existsSync(scriptPath)) {
await unlink(scriptPath); await unlink(scriptPath);
} }
} };
export async function fetchScripts(): Promise<Script[]> { export const fetchScripts = async (): Promise<Script[]> => {
return await loadAllScripts(); return await loadAllScripts();
} };
export async function createScript( export const createScript = async (
formData: FormData formData: FormData
): Promise<{ success: boolean; message: string; script?: Script }> { ): Promise<{ success: boolean; message: string; script?: Script }> => {
try { try {
const name = formData.get("name") as string; const name = formData.get("name") as string;
const description = formData.get("description") as string; const description = formData.get("description") as string;
@@ -92,7 +121,8 @@ export async function createScript(
`; `;
const fullContent = metadataHeader + content; const normalizedContent = await normalizeLineEndings(content);
const fullContent = metadataHeader + normalizedContent;
await saveScriptFile(filename, fullContent); await saveScriptFile(filename, fullContent);
revalidatePath("/"); revalidatePath("/");
@@ -114,11 +144,11 @@ export async function createScript(
console.error("Error creating script:", error); console.error("Error creating script:", error);
return { success: false, message: "Error creating script" }; return { success: false, message: "Error creating script" };
} }
} };
export async function updateScript( export const updateScript = async (
formData: FormData formData: FormData
): Promise<{ success: boolean; message: string }> { ): Promise<{ success: boolean; message: string }> => {
try { try {
const id = formData.get("id") as string; const id = formData.get("id") as string;
const name = formData.get("name") as string; const name = formData.get("name") as string;
@@ -142,7 +172,8 @@ export async function updateScript(
`; `;
const fullContent = metadataHeader + content; const normalizedContent = await normalizeLineEndings(content);
const fullContent = metadataHeader + normalizedContent;
await saveScriptFile(existingScript.filename, fullContent); await saveScriptFile(existingScript.filename, fullContent);
revalidatePath("/"); revalidatePath("/");
@@ -152,11 +183,11 @@ export async function updateScript(
console.error("Error updating script:", error); console.error("Error updating script:", error);
return { success: false, message: "Error updating script" }; return { success: false, message: "Error updating script" };
} }
} };
export async function deleteScript( export const deleteScript = async (
id: string id: string
): Promise<{ success: boolean; message: string }> { ): Promise<{ success: boolean; message: string }> => {
try { try {
const scripts = await loadAllScripts(); const scripts = await loadAllScripts();
const script = scripts.find((s) => s.id === id); const script = scripts.find((s) => s.id === id);
@@ -173,12 +204,12 @@ export async function deleteScript(
console.error("Error deleting script:", error); console.error("Error deleting script:", error);
return { success: false, message: "Error deleting script" }; return { success: false, message: "Error deleting script" };
} }
} };
export async function cloneScript( export const cloneScript = async (
id: string, id: string,
newName: string newName: string
): Promise<{ success: boolean; message: string; script?: Script }> { ): Promise<{ success: boolean; message: string; script?: Script }> => {
try { try {
const scripts = await loadAllScripts(); const scripts = await loadAllScripts();
const originalScript = scripts.find((s) => s.id === id); const originalScript = scripts.find((s) => s.id === id);
@@ -200,7 +231,8 @@ export async function cloneScript(
`; `;
const fullContent = metadataHeader + originalContent; const normalizedContent = await normalizeLineEndings(originalContent);
const fullContent = metadataHeader + normalizedContent;
await saveScriptFile(filename, fullContent); await saveScriptFile(filename, fullContent);
revalidatePath("/"); revalidatePath("/");
@@ -222,11 +254,12 @@ export async function cloneScript(
console.error("Error cloning script:", error); console.error("Error cloning script:", error);
return { success: false, message: "Error cloning script" }; return { success: false, message: "Error cloning script" };
} }
} };
export async function getScriptContent(filename: string): Promise<string> { export const getScriptContent = async (filename: string): Promise<string> => {
try { try {
const scriptPath = join(SCRIPTS_DIR, filename); const scriptPath = path.join(process.cwd(), SCRIPTS_DIR, filename);
if (existsSync(scriptPath)) { if (existsSync(scriptPath)) {
const content = await readFile(scriptPath, "utf8"); const content = await readFile(scriptPath, "utf8");
const lines = content.split("\n"); const lines = content.split("\n");
@@ -251,19 +284,18 @@ export async function getScriptContent(filename: string): Promise<string> {
console.error("Error reading script content:", error); console.error("Error reading script content:", error);
return ""; return "";
} }
} };
export async function executeScript(filename: string): Promise<{ export const executeScript = async (
filename: string
): Promise<{
success: boolean; success: boolean;
output: string; output: string;
error: string; error: string;
}> { }> => {
try { try {
await ensureHostScriptsDirectory(); await ensureHostScriptsDirectory();
const isDocker = process.env.DOCKER === "true"; const hostScriptPath = await getHostScriptPath(filename);
const hostScriptPath = isDocker
? join("/app/scripts", filename)
: join(process.cwd(), "scripts", filename);
if (!existsSync(hostScriptPath)) { if (!existsSync(hostScriptPath)) {
return { return {
@@ -273,7 +305,7 @@ export async function executeScript(filename: string): Promise<{
}; };
} }
const { stdout, stderr } = await execAsync(`bash "${hostScriptPath}"`, { const { stdout, stderr } = await execAsync(RUN_SCRIPT(hostScriptPath), {
timeout: 30000, timeout: 30000,
}); });
@@ -289,4 +321,4 @@ export async function executeScript(filename: string): Promise<{
error: error.message || "Unknown error", error: error.message || "Unknown error",
}; };
} }
} };

View File

@@ -1,17 +1,16 @@
"use server"; "use server";
import { revalidatePath } from "next/cache";
import { import {
loadAllSnippets, loadAllSnippets,
searchBashSnippets, searchBashSnippets,
getSnippetCategories, getSnippetCategories,
getSnippetById, getSnippetById,
type BashSnippet, type BashSnippet,
} from "@/app/_utils/snippetScanner"; } from "@/app/_utils/snippets-utils";
export { type BashSnippet } from "@/app/_utils/snippetScanner"; export { type BashSnippet } from "@/app/_utils/snippets-utils";
export async function fetchSnippets(): Promise<BashSnippet[]> { export const fetchSnippets = async (): Promise<BashSnippet[]> => {
try { try {
return await loadAllSnippets(); return await loadAllSnippets();
} catch (error) { } catch (error) {
@@ -20,7 +19,7 @@ export async function fetchSnippets(): Promise<BashSnippet[]> {
} }
} }
export async function searchSnippets(query: string): Promise<BashSnippet[]> { export const searchSnippets = async (query: string): Promise<BashSnippet[]> => {
try { try {
const snippets = await loadAllSnippets(); const snippets = await loadAllSnippets();
return searchBashSnippets(snippets, query); return searchBashSnippets(snippets, query);
@@ -30,7 +29,7 @@ export async function searchSnippets(query: string): Promise<BashSnippet[]> {
} }
} }
export async function fetchSnippetCategories(): Promise<string[]> { export const fetchSnippetCategories = async (): Promise<string[]> => {
try { try {
const snippets = await loadAllSnippets(); const snippets = await loadAllSnippets();
return getSnippetCategories(snippets); return getSnippetCategories(snippets);
@@ -40,9 +39,9 @@ export async function fetchSnippetCategories(): Promise<string[]> {
} }
} }
export async function fetchSnippetById( export const fetchSnippetById = async (
id: string id: string
): Promise<BashSnippet | undefined> { ): Promise<BashSnippet | undefined> => {
try { try {
const snippets = await loadAllSnippets(); const snippets = await loadAllSnippets();
return getSnippetById(snippets, id); return getSnippetById(snippets, id);
@@ -52,9 +51,9 @@ export async function fetchSnippetById(
} }
} }
export async function fetchSnippetsByCategory( export const fetchSnippetsByCategory = async (
category: string category: string
): Promise<BashSnippet[]> { ): Promise<BashSnippet[]> => {
try { try {
const snippets = await loadAllSnippets(); const snippets = await loadAllSnippets();
return snippets.filter((snippet) => snippet.category === category); return snippets.filter((snippet) => snippet.category === category);
@@ -64,9 +63,9 @@ export async function fetchSnippetsByCategory(
} }
} }
export async function fetchSnippetsBySource( export const fetchSnippetsBySource = async (
source: "builtin" | "user" source: "builtin" | "user"
): Promise<BashSnippet[]> { ): Promise<BashSnippet[]> => {
try { try {
const snippets = await loadAllSnippets(); const snippets = await loadAllSnippets();
return snippets.filter((snippet) => snippet.source === source); return snippets.filter((snippet) => snippet.source === source);

View File

@@ -0,0 +1,59 @@
import fs from "fs";
import path from "path";
import "server-only";
/**
* Load translation messages for a given locale.
* First checks for custom translations in ./data/translations/,
* then falls back to built-in translations in app/_translations/.
*
* This function is server-only and should only be called from server components
* or server actions.
*/
export const loadTranslationMessages = async (locale: string): Promise<any> => {
const customTranslationPath = path.join(
process.cwd(),
"data",
"translations",
`${locale}.json`
);
try {
if (fs.existsSync(customTranslationPath)) {
const customMessages = JSON.parse(
fs.readFileSync(customTranslationPath, "utf8")
);
return customMessages;
}
} catch (error) {
console.warn(`Failed to load custom translation for ${locale}:`, error);
}
try {
const messages = (await import(`../../../_translations/${locale}.json`))
.default;
return messages;
} catch (error) {
const fallbackMessages = (await import("../../../_translations/en.json"))
.default;
return fallbackMessages;
}
};
type TranslationFunction = (key: string) => string;
export const getTranslations = async (
locale: string = process.env.LOCALE || "en"
): Promise<TranslationFunction> => {
const messages = await loadTranslationMessages(locale);
return (key: string) => {
const keys = key.split(".");
let value: any = messages;
for (const k of keys) {
value = value?.[k];
}
return value || key;
};
};

208
app/_translations/en.json Normal file
View File

@@ -0,0 +1,208 @@
{
"common": {
"cronManagementMadeEasy": "Cron Management made easy",
"allUsers": "All users",
"userWithUsername": "User: {user}",
"user": "User",
"change": "Change",
"description": "Description",
"optional": "Optional",
"cancel": "Cancel",
"close": "Close",
"refresh": "Refresh",
"loading": "Loading",
"version": "Version {version}"
},
"cronjobs": {
"cronJobs": "Cron Jobs",
"cronJob": "Cron Job",
"scheduledTasks": "Scheduled Tasks",
"nOfNJObs": "{filtered} of {total} scheduled tasks",
"forUser": "for user {user}",
"newTask": "New Task",
"runCronManually": "Run cron job manually",
"editCronJob": "Edit cron job",
"cloneCronJob": "Clone cron job",
"deleteCronJob": "Delete cron job",
"pauseCronJob": "Pause cron job",
"resumeCronJob": "Resume cron job",
"runCronJob": "Run cron job",
"runCronJobSuccess": "Cron job executed successfully",
"runCronJobFailed": "Failed to execute cron job",
"paused": "Paused",
"createNewScheduledTask": "Create new scheduled task",
"schedule": "Schedule",
"taskType": "Task Type",
"customCommand": "Custom Command",
"singleCommand": "Single command",
"command": "Command",
"whatDoesThisTaskDo": "What does this task do?",
"createTask": "Create Task",
"editScheduledTask": "Edit Scheduled Task",
"enableLogging": "Enable Logging",
"disableLogging": "Disable Logging",
"loggingDescription": "Capture stdout, stderr, exit codes, and timestamps for job executions. Logs are stored in ./data/logs and automatically cleaned up (defaults to 50 logs per job and 30 days retention, you can change these values in the environment variables).",
"logged": "Logged",
"viewLogs": "View Logs",
"logs": "logs",
"logFiles": "Log Files",
"logContent": "Log Content",
"selectLogToView": "Select a log file to view its content",
"noLogsFound": "No logs found for this job",
"confirmDeleteLog": "Are you sure you want to delete this log file?",
"confirmDeleteAllLogs": "Are you sure you want to delete all log files for this job? This action cannot be undone.",
"deleteAll": "Delete All",
"refresh": "Refresh",
"loading": "Loading",
"close": "Close",
"healthy": "Healthy",
"failed": "Failed (Exit: {exitCode})",
"backupJob": "Backup job",
"restoreJob": "Restore job",
"backupAll": "Backup All",
"backups": "Backups",
"restoreAll": "Restore All",
"confirmRestoreAll": "Are you sure you want to restore all backed up jobs? This will add them to your crontab.",
"backupJobSuccess": "Job backed up successfully",
"backupJobFailed": "Failed to backup job",
"backupAllSuccess": "All jobs backed up successfully",
"backupAllFailed": "Failed to backup all jobs",
"restoreJobSuccess": "Job restored successfully",
"restoreJobFailed": "Failed to restore job",
"moreActions": "More actions",
"restoreBackups": "Restore Backups",
"availableBackups": "Available Backups",
"noBackupsFound": "No backup files found",
"backedUpAt": "Backed up at",
"restoreThisBackup": "Restore this backup",
"deleteBackup": "Delete backup",
"confirmDeleteBackup": "Are you sure you want to delete this backup? This action cannot be undone.",
"backupDeleted": "Backup deleted successfully",
"filters": "Filters",
"filtersAndDisplay": "Filters & Display Options",
"filterByUser": "Filter by User",
"scheduleDisplay": "Schedule Display",
"cronSyntax": "Cron Syntax",
"humanReadable": "Human Readable",
"both": "Both",
"minimalMode": "Minimal Mode",
"minimalModeDescription": "Show compact view with icons instead of full text",
"applyFilters": "Apply Filters",
"nLines": "{count} lines",
"liveJobExecution": "Live Job Execution",
"running": "Running...",
"completed": "Completed (Exit: {exitCode})",
"jobFailed": "Failed (Exit: {exitCode})",
"showLast": "Show last:",
"viewFullLog": "View Full Log ({totalLines} lines)",
"viewFullLogNoCount": "View Full Log",
"viewingFullLog": "Viewing full log ({totalLines} lines)",
"viewingFullLogNoCount": "Viewing full log",
"backToWindowedView": "Back to Windowed View",
"showingLastOf": "Showing last {lineCount} of {totalLines} lines",
"showingLastLines": "Showing last {lineCount} lines",
"largeLogFileDetected": "Large log file detected",
"tailModeEnabled": "Tail mode enabled, showing last {tailLines} lines",
"showAllLines": "Show all lines",
"enableTailMode": "Enable tail mode",
"waitingForJobToStart": "Waiting for job to start...\n\nLogs will appear here in real-time.",
"runIdJobId": "Run ID: {runId} | Job ID: {jobId}"
},
"scripts": {
"scripts": "Scripts",
"scriptsLibrary": "Scripts Library",
"file": "File",
"newScript": "New Script",
"noScriptsYet": "No scripts yet",
"createReusableBashScripts": "Create reusable bash scripts to use in your scheduled tasks.",
"createYourFirstScript": "Create Your First Script",
"nOfNSavedScripts": "{count} saved scripts",
"savedScript": "Saved Script",
"selectFromLibrary": "Select from library",
"scriptPathReadOnly": "Script path is read-only. Edit the script in the Scripts Library",
"selectScript": "Select Script",
"availableScripts": "{count} available scripts",
"noScriptsFound": "No scripts found",
"noScriptsAvailable": "No scripts available",
"scriptPreview": "Script Preview",
"commandPreview": "Command Preview",
"scriptContent": "Script Content",
"selectScriptToPreview": "Select a script to preview",
"searchScripts": "Search scripts...",
"draft": "Draft",
"clearDraft": "Clear Draft",
"close": "Close",
"draftCleared": "Draft cleared"
},
"sidebar": {
"systemOverview": "System Overview",
"uptime": "Uptime",
"memory": "Memory",
"cpu": "CPU",
"gpu": "GPU",
"network": "Network",
"networkLatency": "Network Latency",
"memoryUsage": "Memory Usage",
"cpuUsage": "CPU Usage",
"systemInformation": "System Information",
"performanceMetrics": "Performance Metrics",
"statsUpdateEvery": "Stats update every",
"updating": "Updating",
"networkSpeedEstimatedFromLatency": "Network speed estimated from latency"
},
"system": {
"optimal": "Optimal",
"critical": "Critical",
"high": "High",
"moderate": "Moderate",
"warning": "Warning",
"unknown": "Unknown",
"connected": "Connected",
"allSystemsRunningNormally": "All systems running normally",
"highResourceUsageDetectedImmediateAttentionRequired": "High resource usage detected - immediate attention required",
"moderateResourceUsageMonitoringRecommended": "Moderate resource usage - monitoring recommended",
"unknownGPU": "Unknown GPU",
"noGPUDetected": "No GPU detected",
"gpuDetectionFailed": "GPU detection failed",
"available": "Available",
"systemStatus": "System Status",
"lastUpdated": "Last updated"
},
"login": {
"welcomeTitle": "Welcome to Cr*nMaster",
"signInWithPasswordOrSSO": "Sign in with password or SSO",
"signInWithSSO": "Sign in with SSO",
"enterPasswordToContinue": "Enter your password to continue",
"authenticationNotConfigured": "Authentication Not Configured",
"noAuthMethodsEnabled": "Neither password authentication nor OIDC SSO is enabled. Please configure at least one authentication method in your environment variables to be able to log in.",
"enterPassword": "Enter password",
"signingIn": "Signing in...",
"signIn": "Sign In",
"redirecting": "Redirecting...",
"redirectingToOIDC": "Redirecting to OIDC provider",
"pleaseWait": "Please wait...",
"orContinueWith": "Or continue with",
"loginFailed": "Login failed",
"genericError": "An error occurred. Please try again."
},
"warnings": {
"wrapperScriptModified": "Wrapper Script Modified",
"wrapperScriptModifiedDescription": "Your cron-log-wrapper.sh script has been modified from the official version. This may affect logging functionality. Consider reverting to the official version or ensure your changes don't break the required format for log parsing."
},
"notFound": {
"title": "404 - Page Not Found",
"subtitle": "ERROR: The requested resource could not be located",
"message": "The page you're looking for doesn't exist. Want to play snake instead?",
"gameOver": "GAME OVER",
"score": "Score",
"highScore": "High Score",
"pressToStart": "Press SPACE or tap to start",
"pressToRestart": "Press SPACE or tap to restart",
"controls": "Controls",
"useArrowKeys": "Use arrow keys to move",
"tapToMove": "Tap screen edges to move",
"goHome": "Return to Dashboard",
"pauseGame": "Press P to pause",
"paused": "PAUSED"
}
}

205
app/_translations/it.json Normal file
View File

@@ -0,0 +1,205 @@
{
"common": {
"cronManagementMadeEasy": "Gestione Cron semplificata",
"allUsers": "Tutti gli utenti",
"userWithUsername": "Utente: {user}",
"user": "Utente",
"change": "Modifica",
"description": "Descrizione",
"optional": "Opzionale",
"cancel": "Annulla",
"refresh": "Aggiorna",
"close": "Chiudi",
"version": "Versione {version}"
},
"cronjobs": {
"cronJobs": "Operazioni Cron",
"cronJob": "Operazione Cron",
"scheduledTasks": "Operazioni Pianificate",
"nOfNJObs": "{filtered} di {total} operazioni pianificate",
"forUser": "per l'utente {user}",
"newTask": "Nuova Operazione",
"runCronManually": "Esegui operazione cron manualmente",
"editCronJob": "Modifica operazione cron",
"cloneCronJob": "Clona operazione cron",
"deleteCronJob": "Elimina operazione cron",
"pauseCronJob": "Pausa operazione cron",
"resumeCronJob": "Riprendi operazione cron",
"runCronJob": "Esegui operazione cron",
"runCronJobSuccess": "Operazione cron eseguita con successo",
"runCronJobFailed": "Esecuzione operazione cron fallita",
"paused": "In pausa",
"createNewScheduledTask": "Crea nuova operazione pianificata",
"schedule": "Pianificazione",
"taskType": "Tipo di Operazione",
"customCommand": "Comando Personalizzato",
"singleCommand": "Comando singolo",
"command": "Comando",
"whatDoesThisTaskDo": "Cosa fa questa operazione?",
"createTask": "Crea Operazione",
"editScheduledTask": "Modifica Operazione Pianificata",
"enableLogging": "Abilita Logging",
"disableLogging": "Disabilita Logging",
"loggingDescription": "Cattura stdout, stderr, codici di uscita e timestamp per le esecuzioni dei job. I log sono memorizzati in ./data/logs e automaticamente puliti (per impostazione predefinita 50 log per job e 30 giorni di conservazione, puoi modificare questi valori nelle env variables).",
"logged": "Loggato",
"viewLogs": "Visualizza Log",
"logs": "log",
"logFiles": "File",
"logContent": "Contenuto Log",
"selectLogToView": "Seleziona un file per visualizzarne il contenuto",
"noLogsFound": "Nessun log trovato per questa operazione",
"confirmDeleteLog": "Sei sicuro di voler eliminare questo file?",
"confirmDeleteAllLogs": "Sei sicuro di voler eliminare tutti i file per questa operazione? Questa azione non può essere annullata.",
"deleteAll": "Elimina Tutto",
"refresh": "Aggiorna",
"loading": "Caricamento",
"close": "Chiudi",
"healthy": "Sano",
"failed": "Fallito (Exit: {exitCode})",
"backupJob": "Backup operazione",
"restoreJob": "Ripristina operazione",
"backupAll": "Backup Tutti",
"backups": "Backups",
"restoreAll": "Ripristina Tutti",
"confirmRestoreAll": "Sei sicuro di voler ripristinare tutte le operazioni salvate? Verranno aggiunte al tuo crontab.",
"backupJobSuccess": "Backup operazione completato con successo",
"backupJobFailed": "Backup operazione fallito",
"backupAllSuccess": "Backup di tutte le operazioni completato con successo",
"backupAllFailed": "Backup di tutte le operazioni fallito",
"restoreJobSuccess": "Operazione ripristinata con successo",
"restoreJobFailed": "Ripristino operazione fallito",
"moreActions": "Altre azioni",
"restoreBackups": "Ripristina Backup",
"availableBackups": "Backup Disponibili",
"noBackupsFound": "Nessun file di backup trovato",
"backedUpAt": "Backup effettuato il",
"restoreThisBackup": "Ripristina questo backup",
"deleteBackup": "Elimina backup",
"confirmDeleteBackup": "Sei sicuro di voler eliminare questo backup? Questa azione non può essere annullata.",
"backupDeleted": "Backup eliminato con successo",
"filters": "Filtri",
"filtersAndDisplay": "Filtri e Opzioni di Visualizzazione",
"filterByUser": "Filtra per Utente",
"scheduleDisplay": "Visualizzazione Pianificazione",
"cronSyntax": "Sintassi Cron",
"humanReadable": "Comprensibile",
"both": "Entrambi",
"minimalMode": "Modalità Minima",
"minimalModeDescription": "Mostra vista compatta con icone invece del testo completo",
"applyFilters": "Applica Filtri",
"nLines": "{count} linee",
"liveJobExecution": "Esecuzione Lavoro Live",
"running": "In esecuzione...",
"completed": "Completato (Exit: {exitCode})",
"jobFailed": "Fallito (Exit: {exitCode})",
"showLast": "Mostra ultime:",
"viewFullLog": "Visualizza Log Completo ({totalLines} linee)",
"viewingFullLog": "Visualizzazione log completo ({totalLines} linee)",
"backToWindowedView": "Torna alla Vista Finestrata",
"showingLastOf": "Mostrando ultime {lineCount} di {totalLines} linee",
"largeLogFileDetected": "Rilevato file di log di grandi dimensioni",
"tailModeEnabled": "Modalità tail abilitata, mostrando ultime {tailLines} linee",
"showAllLines": "Mostra tutte le linee",
"enableTailMode": "Abilita modalità tail",
"waitingForJobToStart": "In attesa che il lavoro inizi...\n\nI log appariranno qui in tempo reale.",
"runIdJobId": "ID Esecuzione: {runId} | ID Lavoro: {jobId}"
},
"scripts": {
"scripts": "Script",
"scriptsLibrary": "Libreria Script",
"file": "File",
"newScript": "Nuovo Script",
"noScriptsYet": "Ancora nessuno script",
"createReusableBashScripts": "Crea script bash riutilizzabili da usare nelle tue operazioni pianificate.",
"createYourFirstScript": "Crea il tuo primo script",
"nOfNSavedScripts": "{count} script salvati",
"savedScript": "Script Salvato",
"selectFromLibrary": "Seleziona dalla libreria",
"scriptPathReadOnly": "Il percorso dello script è di sola lettura. Modifica lo script nella Libreria Script",
"selectScript": "Seleziona Script",
"availableScripts": "{count} script disponibili",
"noScriptsFound": "Nessuno script trovato",
"noScriptsAvailable": "Nessuno script disponibile",
"scriptPreview": "Anteprima Script",
"commandPreview": "Anteprima Comando",
"scriptContent": "Contenuto Script",
"selectScriptToPreview": "Seleziona uno script per l'anteprima",
"searchScripts": "Cerca script...",
"draft": "Bozza",
"clearDraft": "Cancella Bozza",
"close": "Chiudi",
"draftCleared": "Bozza cancellata"
},
"sidebar": {
"systemOverview": "Panoramica del Sistema",
"uptime": "Uptime",
"memory": "Memoria",
"cpu": "CPU",
"gpu": "GPU",
"network": "Rete",
"networkLatency": "Latenza di Rete",
"memoryUsage": "Utilizzo Memoria",
"cpuUsage": "Utilizzo CPU",
"systemInformation": "Informazioni di Sistema",
"performanceMetrics": "Metriche delle Prestazioni",
"statsUpdateEvery": "Statistiche aggiornate ogni",
"updating": "Aggiornamento",
"networkSpeedEstimatedFromLatency": "Velocità di rete stimata dalla latenza"
},
"system": {
"optimal": "Ottimale",
"critical": "Critico",
"high": "Alto",
"moderate": "Moderato",
"warning": "Avviso",
"unknown": "Sconosciuto",
"connected": "Connesso",
"allSystemsRunningNormally": "Tutti i sistemi funzionano normalmente",
"highResourceUsageDetectedImmediateAttentionRequired": "Rilevato utilizzo elevato delle risorse - richiesta attenzione immediata",
"moderateResourceUsageMonitoringRecommended": "Utilizzo moderato delle risorse - monitoraggio raccomandato",
"unknownGPU": "GPU Sconosciuta",
"noGPUDetected": "Nessuna GPU rilevata",
"gpuDetectionFailed": "Rilevamento GPU fallito",
"available": "Disponibile",
"systemStatus": "Stato del Sistema",
"lastUpdated": "Ultimo aggiornamento"
},
"login": {
"welcomeTitle": "Benvenuto in Cr*nMaster",
"signInWithPasswordOrSSO": "Accedi con password o SSO",
"signInWithSSO": "Accedi con SSO",
"enterPasswordToContinue": "Inserisci la tua password per continuare",
"authenticationNotConfigured": "Autenticazione Non Configurata",
"noAuthMethodsEnabled": "Né l'autenticazione password né l'OIDC SSO sono abilitati. Si prega di configurare almeno un metodo di autenticazione nelle variabili d'ambiente per poter effettuare il login.",
"enterPassword": "Inserisci password",
"signingIn": "Accesso in corso...",
"signIn": "Accedi",
"redirecting": "Reindirizzamento...",
"redirectingToOIDC": "Reindirizzamento al provider OIDC",
"pleaseWait": "Attendere prego...",
"orContinueWith": "Oppure continua con",
"loginFailed": "Accesso fallito",
"genericError": "Si è verificato un errore. Riprova."
},
"warnings": {
"wrapperScriptModified": "Script Wrapper Modificato",
"wrapperScriptModifiedDescription": "Il tuo script cron-log-wrapper.sh è stato modificato dalla versione ufficiale. Questo potrebbe influenzare la funzionalità di logging. Considera di ripristinare la versione ufficiale o assicurati che le tue modifiche non interrompano il formato richiesto per l'analisi dei log."
},
"notFound": {
"title": "404 - Pagina Non Trovata",
"subtitle": "ERRORE: La risorsa richiesta non è stata trovata",
"message": "La pagina che stai cercando non esiste. Partitella a snake?",
"playSnake": "Gioca a Snake mentre sei qui",
"gameOver": "GAME OVER",
"score": "Punteggio",
"highScore": "Punteggio Massimo",
"pressToStart": "Premi SPAZIO o tocca per iniziare",
"pressToRestart": "Premi SPAZIO o tocca per ricominciare",
"controls": "Controlli",
"useArrowKeys": "Usa i tasti freccia per muoverti",
"tapToMove": "Tocca i bordi dello schermo per muoverti",
"goHome": "Torna alla Dashboard",
"pauseGame": "Premi P per mettere in pausa",
"paused": "IN PAUSA"
}
}

View File

@@ -0,0 +1,97 @@
import { NextRequest, NextResponse } from "next/server";
import { validateSession, getSessionCookieName } from "./session-utils";
export function validateApiKey(request: NextRequest): boolean {
const apiKey = process.env.API_KEY;
if (!apiKey) {
return true;
}
const authHeader = request.headers.get("authorization");
if (!authHeader) {
return false;
}
const match = authHeader.match(/^Bearer\s+(.+)$/i);
if (!match) {
return false;
}
const token = match[1];
return token === apiKey;
}
export async function validateSessionRequest(
request: NextRequest
): Promise<boolean> {
const cookieName = getSessionCookieName();
const sessionId = request.cookies.get(cookieName)?.value;
if (!sessionId) {
return false;
}
return await validateSession(sessionId);
}
export function isAuthRequired(): boolean {
const hasPassword = !!process.env.AUTH_PASSWORD;
const hasSSO = process.env.SSO_MODE === "oidc";
const hasApiKey = !!process.env.API_KEY;
return hasPassword || hasSSO || hasApiKey;
}
export async function requireAuth(
request: NextRequest
): Promise<Response | null> {
if (!isAuthRequired()) {
return null;
}
const hasValidSession = await validateSessionRequest(request);
if (hasValidSession) {
return null;
}
const apiKey = process.env.API_KEY;
if (apiKey) {
const hasValidApiKey = validateApiKey(request);
if (hasValidApiKey) {
return null;
}
}
if (process.env.DEBUGGER) {
console.log("[API Auth] Unauthorized request:", {
path: request.nextUrl.pathname,
hasSession: hasValidSession,
apiKeyConfigured: !!process.env.API_KEY,
hasAuthHeader: !!request.headers.get("authorization"),
});
}
return NextResponse.json(
{
error: "Unauthorized",
message:
"Authentication required. Use session cookie or API key (Bearer token).",
},
{ status: 401 }
);
}
export function withAuth<T extends any[]>(
handler: (request: NextRequest, ...args: T) => Promise<Response>
) {
return async (request: NextRequest, ...args: T): Promise<Response> => {
const authError = await requireAuth(request);
if (authError) {
return authError;
}
return handler(request, ...args);
};
}

189
app/_utils/backup-utils.ts Normal file
View File

@@ -0,0 +1,189 @@
import { promises as fs } from "fs";
import path from "path";
import { getCronJobs, type CronJob } from "@/app/_utils/cronjob-utils";
const BACKUP_DIR = path.join(process.cwd(), "data", "backup");
const ensureBackupDirectoryExists = async (): Promise<void> => {
try {
await fs.mkdir(BACKUP_DIR, { recursive: true });
} catch (error) {
console.error("Error creating backup directory:", error);
throw error;
}
};
const sanitizeFilename = (id: string): string => {
return id.replace(/[^a-zA-Z0-9_-]/g, "_");
};
export const backupJobToFile = async (job: CronJob): Promise<boolean> => {
try {
await ensureBackupDirectoryExists();
const jobData = {
id: job.id,
schedule: job.schedule,
command: job.command,
comment: job.comment || "",
user: job.user,
paused: job.paused || false,
logsEnabled: job.logsEnabled || false,
backedUpAt: new Date().toISOString(),
};
const filename = `${sanitizeFilename(job.id)}.job`;
const filepath = path.join(BACKUP_DIR, filename);
await fs.writeFile(filepath, JSON.stringify(jobData, null, 2), "utf8");
return true;
} catch (error) {
console.error(`Error backing up job ${job.id}:`, error);
return false;
}
};
export const backupAllJobsToFiles = async (): Promise<{
success: boolean;
count: number;
}> => {
try {
await ensureBackupDirectoryExists();
const cronJobs = await getCronJobs(false);
let successCount = 0;
for (const job of cronJobs) {
const success = await backupJobToFile(job);
if (success) {
successCount++;
}
}
return {
success: successCount === cronJobs.length,
count: successCount,
};
} catch (error) {
console.error("Error backing up all jobs:", error);
return {
success: false,
count: 0,
};
}
};
export const listBackupFiles = async (): Promise<string[]> => {
try {
await ensureBackupDirectoryExists();
const files = await fs.readdir(BACKUP_DIR);
return files.filter((file) => file.endsWith(".job"));
} catch (error) {
console.error("Error listing backup files:", error);
return [];
}
};
export const readBackupFile = async (
filename: string
): Promise<CronJob | null> => {
try {
const filepath = path.join(BACKUP_DIR, filename);
const content = await fs.readFile(filepath, "utf8");
const jobData = JSON.parse(content);
return {
id: jobData.id,
schedule: jobData.schedule,
command: jobData.command,
comment: jobData.comment,
user: jobData.user,
paused: jobData.paused,
logsEnabled: jobData.logsEnabled,
};
} catch (error) {
console.error(`Error reading backup file ${filename}:`, error);
return null;
}
};
export const getAllBackupFiles = async (): Promise<
Array<{
filename: string;
job: CronJob;
backedUpAt: string;
}>
> => {
try {
await ensureBackupDirectoryExists();
const files = await fs.readdir(BACKUP_DIR);
const jobFiles = files.filter((file) => file.endsWith(".job"));
const backups = await Promise.all(
jobFiles.map(async (filename) => {
try {
const filepath = path.join(BACKUP_DIR, filename);
const content = await fs.readFile(filepath, "utf8");
const jobData = JSON.parse(content);
return {
filename,
job: {
id: jobData.id,
schedule: jobData.schedule,
command: jobData.command,
comment: jobData.comment,
user: jobData.user,
paused: jobData.paused,
logsEnabled: jobData.logsEnabled,
} as CronJob,
backedUpAt: jobData.backedUpAt,
};
} catch (error) {
console.error(`Error reading backup file ${filename}:`, error);
return null;
}
})
);
return backups.filter((backup) => backup !== null) as Array<{
filename: string;
job: CronJob;
backedUpAt: string;
}>;
} catch (error) {
console.error("Error getting all backup files:", error);
return [];
}
};
export const restoreJobFromBackup = async (
filename: string
): Promise<{ success: boolean; job?: CronJob }> => {
try {
const job = await readBackupFile(filename);
if (!job) {
return { success: false };
}
return { success: true, job };
} catch (error) {
console.error(`Error restoring job from backup ${filename}:`, error);
return { success: false };
}
};
export const deleteBackupFile = async (filename: string): Promise<boolean> => {
try {
const filepath = path.join(BACKUP_DIR, filename);
await fs.unlink(filepath);
return true;
} catch (error) {
console.error(`Error deleting backup file ${filename}:`, error);
return false;
}
};

View File

@@ -1,6 +0,0 @@
import { type ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}

429
app/_utils/cronjob-utils.ts Normal file
View File

@@ -0,0 +1,429 @@
import { exec } from "child_process";
import { promisify } from "util";
import {
readAllHostCrontabs,
writeHostCrontabForUser,
} from "@/app/_utils/crontab-utils";
import {
parseJobsFromLines,
deleteJobInLines,
updateJobInLines,
pauseJobInLines,
resumeJobInLines,
formatCommentWithMetadata,
} from "@/app/_utils/line-manipulation-utils";
import {
cleanCrontabContent,
readCronFiles,
writeCronFiles,
} from "@/app/_utils/files-manipulation-utils";
import { isDocker } from "@/app/_server/actions/global";
import { READ_CRONTAB, WRITE_CRONTAB } from "@/app/_consts/commands";
import {
wrapCommandWithLogger,
unwrapCommand,
isCommandWrapped,
} from "@/app/_utils/wrapper-utils";
import { generateShortUUID } from "@/app/_utils/uuid-utils";
const execAsync = promisify(exec);
export interface CronJob {
id: string;
schedule: string;
command: string;
comment?: string;
user: string;
paused?: boolean;
logsEnabled?: boolean;
logError?: {
hasError: boolean;
lastFailedLog?: string;
lastFailedTimestamp?: Date;
exitCode?: number;
latestExitCode?: number;
hasHistoricalFailures?: boolean;
};
}
export const readUserCrontab = async (user: string): Promise<string> => {
const docker = await isDocker();
if (docker) {
const userCrontabs = await readAllHostCrontabs();
const targetUserCrontab = userCrontabs.find((uc) => uc.user === user);
return targetUserCrontab?.content || "";
} else {
const { stdout } = await execAsync(READ_CRONTAB(user));
return stdout;
}
};
export const writeUserCrontab = async (
user: string,
content: string
): Promise<boolean> => {
const docker = await isDocker();
if (docker) {
return await writeHostCrontabForUser(user, content);
} else {
try {
await execAsync(WRITE_CRONTAB(content, user));
return true;
} catch (error) {
console.error(`Error writing crontab for user ${user}:`, error);
return false;
}
}
};
const getAllUsers = async (): Promise<{ user: string; content: string }[]> => {
const docker = await isDocker();
if (docker) {
return await readAllHostCrontabs();
} else {
const { getAllTargetUsers } = await import("@/app/_utils/crontab-utils");
const users = await getAllTargetUsers();
const results: { user: string; content: string }[] = [];
for (const user of users) {
try {
const { stdout } = await execAsync(READ_CRONTAB(user));
results.push({ user, content: stdout });
} catch (error) {
console.error(`Error reading crontab for user ${user}:`, error);
results.push({ user, content: "" });
}
}
return results;
}
};
export const getCronJobs = async (
includeLogErrors: boolean = true
): Promise<CronJob[]> => {
try {
const userCrontabs = await getAllUsers();
let allJobs: CronJob[] = [];
for (const { user, content } of userCrontabs) {
if (!content.trim()) continue;
const lines = content.split("\n");
const jobs = parseJobsFromLines(lines, user);
allJobs.push(...jobs);
}
if (includeLogErrors) {
const { getAllJobLogErrors } = await import("@/app/_server/actions/logs");
const jobIds = allJobs.map((job) => job.id);
const errorMap = await getAllJobLogErrors(jobIds);
allJobs = allJobs.map((job) => ({
...job,
logError: errorMap.get(job.id),
}));
}
return allJobs;
} catch (error) {
console.error("Error getting cron jobs:", error);
return [];
}
};
export const addCronJob = async (
schedule: string,
command: string,
comment: string = "",
user?: string,
logsEnabled: boolean = false
): Promise<boolean> => {
try {
const jobId = generateShortUUID();
if (user) {
const cronContent = await readUserCrontab(user);
let finalCommand = command;
if (logsEnabled && !isCommandWrapped(command)) {
const docker = await isDocker();
finalCommand = await wrapCommandWithLogger(
jobId,
command,
docker,
comment
);
} else if (logsEnabled && isCommandWrapped(command)) {
finalCommand = command;
}
const formattedComment = formatCommentWithMetadata(
comment,
logsEnabled,
jobId
);
const newEntry = `# ${formattedComment}\n${schedule} ${finalCommand}`;
let newCron;
if (cronContent.trim() === "") {
newCron = newEntry;
} else {
const existingContent = cronContent.trim();
newCron = await cleanCrontabContent(existingContent + "\n" + newEntry);
}
return await writeUserCrontab(user, newCron);
} else {
const cronContent = await readCronFiles();
let finalCommand = command;
if (logsEnabled && !isCommandWrapped(command)) {
const docker = await isDocker();
finalCommand = await wrapCommandWithLogger(
jobId,
command,
docker,
comment
);
} else if (logsEnabled && isCommandWrapped(command)) {
finalCommand = command;
}
const formattedComment = formatCommentWithMetadata(
comment,
logsEnabled,
jobId
);
const newEntry = `# ${formattedComment}\n${schedule} ${finalCommand}`;
let newCron;
if (cronContent.trim() === "") {
newCron = newEntry;
} else {
const existingContent = cronContent.trim();
newCron = await cleanCrontabContent(existingContent + "\n" + newEntry);
}
return await writeCronFiles(newCron);
}
} catch (error) {
console.error("Error adding cron job:", error);
return false;
}
};
export const deleteCronJob = async (id: string): Promise<boolean> => {
try {
const allJobs = await getCronJobs(false);
const targetJob = allJobs.find((j) => j.id === id);
if (!targetJob) {
console.error(`Job with id ${id} not found`);
return false;
}
const user = targetJob.user;
const cronContent = await readUserCrontab(user);
const lines = cronContent.split("\n");
const userJobs = parseJobsFromLines(lines, user);
const jobIndex = userJobs.findIndex((j) => j.id === id);
if (jobIndex === -1) {
console.error(`Job with id ${id} not found in parsed jobs`);
return false;
}
const newCronEntries = deleteJobInLines(lines, jobIndex);
const newCron = await cleanCrontabContent(newCronEntries.join("\n"));
return await writeUserCrontab(user, newCron);
} catch (error) {
console.error("Error deleting cron job:", error);
return false;
}
};
export const updateCronJob = async (
jobData: {
id: string;
schedule: string;
command: string;
comment?: string;
user: string;
},
schedule: string,
command: string,
comment: string = "",
logsEnabled: boolean = false
): Promise<boolean> => {
try {
const user = jobData.user;
const cronContent = await readUserCrontab(user);
const lines = cronContent.split("\n");
const jobIndex = findJobIndex(jobData, lines, user);
if (jobIndex === -1) {
console.error(`Job not found in crontab`);
return false;
}
const isWrapped = isCommandWrapped(command);
let finalCommand = command;
if (logsEnabled && !isWrapped) {
const docker = await isDocker();
finalCommand = await wrapCommandWithLogger(
jobData.id,
command,
docker,
comment
);
} else if (!logsEnabled && isWrapped) {
finalCommand = unwrapCommand(command);
} else if (logsEnabled && isWrapped) {
const unwrapped = unwrapCommand(command);
const docker = await isDocker();
finalCommand = await wrapCommandWithLogger(
jobData.id,
unwrapped,
docker,
comment
);
} else {
finalCommand = command;
}
const newCronEntries = updateJobInLines(
lines,
jobIndex,
schedule,
finalCommand,
comment,
logsEnabled,
jobData.id
);
const newCron = await cleanCrontabContent(newCronEntries.join("\n"));
return await writeUserCrontab(user, newCron);
} catch (error) {
console.error("Error updating cron job:", error);
return false;
}
};
export const pauseCronJob = async (id: string): Promise<boolean> => {
try {
const allJobs = await getCronJobs(false);
const targetJob = allJobs.find((j) => j.id === id);
if (!targetJob) {
console.error(`Job with id ${id} not found`);
return false;
}
const user = targetJob.user;
const cronContent = await readUserCrontab(user);
const lines = cronContent.split("\n");
const userJobs = parseJobsFromLines(lines, user);
const jobIndex = userJobs.findIndex((j) => j.id === id);
if (jobIndex === -1) {
console.error(`Job with id ${id} not found in parsed jobs`);
return false;
}
const newCronEntries = pauseJobInLines(lines, jobIndex, id);
const newCron = await cleanCrontabContent(newCronEntries.join("\n"));
return await writeUserCrontab(user, newCron);
} catch (error) {
console.error("Error pausing cron job:", error);
return false;
}
};
export const resumeCronJob = async (id: string): Promise<boolean> => {
try {
const allJobs = await getCronJobs(false);
const targetJob = allJobs.find((j) => j.id === id);
if (!targetJob) {
console.error(`Job with id ${id} not found`);
return false;
}
const user = targetJob.user;
const cronContent = await readUserCrontab(user);
const lines = cronContent.split("\n");
const userJobs = parseJobsFromLines(lines, user);
const jobIndex = userJobs.findIndex((j) => j.id === id);
if (jobIndex === -1) {
console.error(`Job with id ${id} not found in parsed jobs`);
return false;
}
const newCronEntries = resumeJobInLines(lines, jobIndex, id);
const newCron = await cleanCrontabContent(newCronEntries.join("\n"));
return await writeUserCrontab(user, newCron);
} catch (error) {
console.error("Error resuming cron job:", error);
return false;
}
};
export const cleanupCrontab = async (): Promise<boolean> => {
try {
const userCrontabs = await getAllUsers();
for (const { user, content } of userCrontabs) {
if (!content.trim()) continue;
const cleanedContent = await cleanCrontabContent(content);
await writeUserCrontab(user, cleanedContent);
}
return true;
} catch (error) {
console.error("Error cleaning crontab:", error);
return false;
}
};
export const findJobIndex = (
jobData: {
id: string;
schedule: string;
command: string;
comment?: string;
user: string;
paused?: boolean;
},
lines: string[],
user: string
): number => {
const cronContentStr = lines.join("\n");
const userJobs = parseJobsFromLines(lines, user);
if (cronContentStr.includes(`id: ${jobData.id}`)) {
return userJobs.findIndex((j) => j.id === jobData.id);
}
return userJobs.findIndex(
(j) =>
j.schedule === jobData.schedule &&
j.command === jobData.command &&
j.user === jobData.user &&
(j.comment || "") === (jobData.comment || "")
);
};

176
app/_utils/crontab-utils.ts Normal file
View File

@@ -0,0 +1,176 @@
import {
GET_DOCKER_SOCKET_OWNER,
GET_TARGET_USER,
ID_G,
ID_U,
READ_CRONTAB,
READ_CRONTABS_DIRECTORY,
WRITE_HOST_CRONTAB,
} from "@/app/_consts/commands";
import { NSENTER_HOST_CRONTAB } from "@/app/_consts/nsenter";
import { exec } from "child_process";
import { promisify } from "util";
const execAsync = promisify(exec);
export interface UserInfo {
username: string;
uid: number;
gid: number;
}
const execHostCrontab = async (command: string): Promise<string> => {
try {
const { stdout } = await execAsync(NSENTER_HOST_CRONTAB(command?.trim()));
return stdout;
} catch (error: any) {
console.error("Error executing host crontab command:", error);
throw error;
}
};
const getTargetUser = async (): Promise<string> => {
try {
if (process.env.HOST_CRONTAB_USER) {
return process.env.HOST_CRONTAB_USER;
}
const { stdout } = await execAsync(GET_DOCKER_SOCKET_OWNER);
const dockerSocketOwner = stdout.trim();
if (dockerSocketOwner === "root") {
try {
const targetUser = await execHostCrontab(GET_TARGET_USER);
if (targetUser) {
return targetUser.trim();
}
} catch (error) {
console.warn("Could not detect user from passwd:", error);
}
return "root";
}
return dockerSocketOwner;
} catch (error) {
console.error("Error detecting target user:", error);
return "root";
}
};
export const getAllTargetUsers = async (): Promise<string[]> => {
try {
if (process.env.HOST_CRONTAB_USER) {
return process.env.HOST_CRONTAB_USER.split(",").map((u) => u.trim());
}
try {
const stdout = await execHostCrontab(READ_CRONTABS_DIRECTORY);
const users = stdout
.trim()
.split("\n")
.filter((user) => user.trim());
return users.length > 0 ? users : ["root"];
} catch (error) {
console.error("Error detecting users from crontabs directory:", error);
return ["root"];
}
} catch (error) {
console.error("Error getting all target users:", error);
return ["root"];
}
};
export const readHostCrontab = async (): Promise<string> => {
try {
const user = await getTargetUser();
return await execHostCrontab(READ_CRONTAB(user));
} catch (error) {
console.error("Error reading host crontab:", error);
return "";
}
};
export const readAllHostCrontabs = async (): Promise<
{ user: string; content: string }[]
> => {
try {
const users = await getAllTargetUsers();
const results: { user: string; content: string }[] = [];
for (const user of users) {
try {
const content = await execHostCrontab(READ_CRONTAB(user));
results.push({ user, content });
} catch (error) {
console.warn(`Error reading crontab for user ${user}:`, error);
results.push({ user, content: "" });
}
}
return results;
} catch (error) {
console.error("Error reading all host crontabs:", error);
return [];
}
};
export const writeHostCrontab = async (content: string): Promise<boolean> => {
try {
const user = await getTargetUser();
let finalContent = content;
if (!finalContent.endsWith("\n")) {
finalContent += "\n";
}
const base64Content = Buffer.from(finalContent).toString("base64");
await execHostCrontab(WRITE_HOST_CRONTAB(base64Content, user));
return true;
} catch (error) {
console.error("Error writing host crontab:", error);
return false;
}
};
export const writeHostCrontabForUser = async (
user: string,
content: string
): Promise<boolean> => {
try {
let finalContent = content;
if (!finalContent.endsWith("\n")) {
finalContent += "\n";
}
const base64Content = Buffer.from(finalContent).toString("base64");
await execHostCrontab(WRITE_HOST_CRONTAB(base64Content, user));
return true;
} catch (error) {
console.error(`Error writing host crontab for user ${user}:`, error);
return false;
}
};
export const getUserInfo = async (
username: string
): Promise<UserInfo | null> => {
try {
const uidResult = await execHostCrontab(ID_U(username));
const gidResult = await execHostCrontab(ID_G(username));
const uid = parseInt(uidResult.trim());
const gid = parseInt(gidResult.trim());
if (isNaN(uid) || isNaN(gid)) {
console.error(`Invalid UID/GID for user ${username}`);
return null;
}
return { username, uid, gid };
} catch (error) {
console.error(`Error getting user info for ${username}:`, error);
return null;
}
};

94
app/_utils/error-utils.ts Normal file
View File

@@ -0,0 +1,94 @@
export interface JobError {
id: string;
title: string;
message: string;
details?: string;
command?: string;
output?: string;
stderr?: string;
timestamp: string;
jobId: string;
}
const STORAGE_KEY = "cronmaster-job-errors";
const MAX_LOG_AGE_DAYS = parseInt(
process.env.NEXT_PUBLIC_MAX_LOG_AGE_DAYS || "30",
10
);
/**
* Clean up old errors from localStorage based on MAX_LOG_AGE_DAYS.
* This is called automatically when getting errors.
*/
const cleanupOldErrors = (errors: JobError[]): JobError[] => {
const maxAgeMs = MAX_LOG_AGE_DAYS * 24 * 60 * 60 * 1000;
const now = Date.now();
return errors.filter((error) => {
try {
const errorTime = new Date(error.timestamp).getTime();
const age = now - errorTime;
return age < maxAgeMs;
} catch {
return true;
}
});
};
export const getJobErrors = (): JobError[] => {
if (typeof window === "undefined") return [];
try {
const stored = localStorage.getItem(STORAGE_KEY);
const errors = stored ? JSON.parse(stored) : [];
const cleanedErrors = cleanupOldErrors(errors);
if (cleanedErrors.length !== errors.length) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(cleanedErrors));
}
return cleanedErrors;
} catch {
return [];
}
};
export const setJobError = (error: JobError) => {
if (typeof window === "undefined") return;
try {
const errors = getJobErrors();
const existingIndex = errors.findIndex((e) => e.id === error.id);
if (existingIndex >= 0) {
errors[existingIndex] = error;
} else {
errors.push(error);
}
localStorage.setItem(STORAGE_KEY, JSON.stringify(errors));
} catch {}
};
export const removeJobError = (errorId: string) => {
if (typeof window === "undefined") return;
try {
const errors = getJobErrors();
const filtered = errors.filter((e) => e.id !== errorId);
localStorage.setItem(STORAGE_KEY, JSON.stringify(filtered));
} catch {}
};
export const getJobErrorsByJobId = (jobId: string): JobError[] => {
return getJobErrors().filter((error) => error.jobId === jobId);
};
export const clearAllJobErrors = () => {
if (typeof window === "undefined") return;
try {
localStorage.removeItem(STORAGE_KEY);
} catch {}
};

View File

@@ -0,0 +1,61 @@
"use server";
import { exec } from "child_process";
import { promisify } from "util";
import { readHostCrontab, writeHostCrontab } from "@/app/_utils/crontab-utils";
import { isDocker } from "@/app/_server/actions/global";
import { READ_CRON_FILE, WRITE_CRON_FILE } from "@/app/_consts/commands";
const execAsync = promisify(exec);
export const cleanCrontabContent = async (content: string): Promise<string> => {
const lines = content.split("\n");
const cleanedLines: string[] = [];
let consecutiveEmptyLines = 0;
for (const line of lines) {
if (line.trim() === "") {
consecutiveEmptyLines++;
if (consecutiveEmptyLines <= 1) {
cleanedLines.push("");
}
} else {
consecutiveEmptyLines = 0;
cleanedLines.push(line);
}
}
return cleanedLines.join("\n").trim();
}
export const readCronFiles = async (): Promise<string> => {
const docker = await isDocker();
if (!docker) {
try {
const { stdout } = await execAsync(READ_CRON_FILE());
return stdout;
} catch (error) {
console.error("Error reading crontab:", error);
return "";
}
}
return await readHostCrontab();
}
export const writeCronFiles = async (content: string): Promise<boolean> => {
const docker = await isDocker();
if (!docker) {
try {
await execAsync(WRITE_CRON_FILE(content));
return true;
} catch (error) {
console.error("Error writing crontab:", error);
return false;
}
}
return await writeHostCrontab(content);
}

View File

@@ -0,0 +1,30 @@
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
export const cn = (...inputs: ClassValue[]) => {
return twMerge(clsx(inputs));
};
export const copyToClipboard = async (text: string): Promise<boolean> => {
try {
if (navigator?.clipboard?.writeText) {
await navigator.clipboard.writeText(text);
return true;
} else {
const textArea = document.createElement("textarea");
textArea.value = text;
textArea.style.position = "fixed";
textArea.style.left = "-9999px";
textArea.style.top = "-9999px";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
const successful = document.execCommand("copy");
document.body.removeChild(textArea);
return successful;
}
} catch (err) {
console.error("Failed to copy to clipboard:", err);
return false;
}
};

View File

@@ -0,0 +1,236 @@
import { exec, spawn } from "child_process";
import { promisify } from "util";
import { CronJob } from "./cronjob-utils";
import { getUserInfo } from "./crontab-utils";
import { NSENTER_RUN_JOB } from "../_consts/nsenter";
import {
saveRunningJob,
updateRunningJob,
getRunningJob,
removeRunningJob,
} from "./running-jobs-utils";
import { sseBroadcaster } from "./sse-broadcaster";
import { generateLogFolderName, cleanupOldLogFiles } from "./wrapper-utils";
import { watchForLogFile } from "./log-watcher";
const execAsync = promisify(exec);
export const runJobSynchronously = async (
job: CronJob,
docker: boolean
): Promise<{
success: boolean;
message: string;
output?: string;
mode: "sync";
}> => {
let command: string;
if (docker) {
const userInfo = await getUserInfo(job.user);
const executionUser = userInfo ? userInfo.username : "root";
const escapedCommand = job.command.replace(/'/g, "'\\''");
command = NSENTER_RUN_JOB(executionUser, escapedCommand);
} else {
command = job.command;
}
const { stdout, stderr } = await execAsync(command, {
timeout: 300000,
cwd: process.env.HOME || "/home",
});
const output = stdout || stderr || "Command executed successfully";
return {
success: true,
message: "Cron job executed successfully",
output: output.trim(),
mode: "sync",
};
};
export const runJobInBackground = async (
job: CronJob,
docker: boolean
): Promise<{
success: boolean;
message: string;
runId: string;
mode: "async";
}> => {
const runId = `run-${job.id}-${Date.now()}`;
const logFolderName = generateLogFolderName(job.id, job.comment);
let command: string;
let shellArgs: string[];
if (docker) {
const userInfo = await getUserInfo(job.user);
const executionUser = userInfo ? userInfo.username : "root";
const escapedCommand = job.command.replace(/'/g, "'\\''");
const nsenterCmd = NSENTER_RUN_JOB(executionUser, escapedCommand);
command = "sh";
shellArgs = ["-c", nsenterCmd];
} else {
command = "sh";
shellArgs = ["-c", job.command];
}
const child = spawn(command, shellArgs, {
detached: true,
stdio: "ignore",
});
child.unref();
const jobStartTime = new Date();
saveRunningJob({
id: runId,
cronJobId: job.id,
pid: child.pid!,
startTime: jobStartTime.toISOString(),
status: "running",
logFolderName,
});
watchForLogFile(runId, logFolderName, jobStartTime, (logFileName) => {
try {
updateRunningJob(runId, { logFileName });
console.log(`[RunningJob] Cached logFileName for ${runId}: ${logFileName}`);
} catch (error) {
console.error(`[RunningJob] Failed to cache logFileName for ${runId}:`, error);
}
});
sseBroadcaster.broadcast({
type: "job-started",
timestamp: jobStartTime.toISOString(),
data: {
runId,
cronJobId: job.id,
hasLogging: true,
},
});
monitorRunningJob(runId, child.pid!);
return {
success: true,
message: "Job started in background",
runId,
mode: "async",
};
};
const monitorRunningJob = (runId: string, pid: number): void => {
const checkInterval = setInterval(async () => {
try {
const isRunning = await isProcessStillRunning(pid);
if (!isRunning) {
clearInterval(checkInterval);
const exitCode = await getExitCodeFromLog(runId);
updateRunningJob(runId, {
status: exitCode === 0 ? "completed" : "failed",
exitCode,
});
setTimeout(async () => {
try {
removeRunningJob(runId);
await cleanupOldLogFiles(runningJob?.cronJobId || "");
} catch (error) {
console.error(`Error cleaning up job ${runId}:`, error);
}
}, 5000);
const runningJob = getRunningJob(runId);
if (runningJob) {
if (exitCode === 0) {
sseBroadcaster.broadcast({
type: "job-completed",
timestamp: new Date().toISOString(),
data: {
runId,
cronJobId: runningJob.cronJobId,
exitCode,
},
});
} else {
sseBroadcaster.broadcast({
type: "job-failed",
timestamp: new Date().toISOString(),
data: {
runId,
cronJobId: runningJob.cronJobId,
exitCode: exitCode ?? -1,
},
});
}
}
}
} catch (error) {
console.error(`[Monitor] Error checking job ${runId}:`, error);
clearInterval(checkInterval);
}
}, 2000);
};
const isProcessStillRunning = async (pid: number): Promise<boolean> => {
try {
await execAsync(`kill -0 ${pid} 2>/dev/null`);
return true;
} catch {
return false;
}
};
const getExitCodeFromLog = async (
runId: string
): Promise<number | undefined> => {
try {
const { readdir, readFile, access } = await import("fs/promises");
const path = await import("path");
const job = getRunningJob(runId);
if (!job || !job.logFolderName) {
return undefined;
}
const logDir = path.join(process.cwd(), "data", "logs", job.logFolderName);
try {
await access(logDir);
} catch {
return undefined;
}
const files = await readdir(logDir);
const sortedFiles = files.sort().reverse();
if (sortedFiles.length === 0) {
return undefined;
}
const latestLog = await readFile(
path.join(logDir, sortedFiles[0]),
"utf-8"
);
const exitCodeMatch = latestLog.match(/Exit Code\s*:\s*(\d+)/);
if (exitCodeMatch) {
return parseInt(exitCodeMatch[1], 10);
}
return undefined;
} catch (error) {
console.error("Error reading exit code from log:", error);
return undefined;
}
};

View File

@@ -0,0 +1,603 @@
import { CronJob } from "@/app/_utils/cronjob-utils";
import { generateShortUUID } from "@/app/_utils/uuid-utils";
import { createHash } from "crypto";
const generateStableJobId = (
schedule: string,
command: string,
user: string,
comment?: string,
lineIndex?: number
): string => {
const content = `${schedule}|${command}|${user}|${comment || ""}|${
lineIndex || 0
}`;
const hash = createHash("md5").update(content).digest("hex");
return hash.substring(0, 8);
};
export const pauseJobInLines = (
lines: string[],
targetJobIndex: number,
uuid: string
): string[] => {
const newCronEntries: string[] = [];
let currentJobIndex = 0;
let i = 0;
while (i < lines.length) {
const line = lines[i];
const trimmedLine = line.trim();
if (!trimmedLine) {
if (
newCronEntries.length > 0 &&
newCronEntries[newCronEntries.length - 1] !== ""
) {
newCronEntries.push("");
}
i++;
continue;
}
if (
trimmedLine.startsWith("# User:") ||
trimmedLine.startsWith("# System Crontab")
) {
newCronEntries.push(line);
i++;
continue;
}
if (trimmedLine.startsWith("# PAUSED:")) {
newCronEntries.push(line);
if (i + 1 < lines.length && lines[i + 1].trim().startsWith("# ")) {
newCronEntries.push(lines[i + 1]);
i += 2;
} else {
i++;
}
currentJobIndex++;
continue;
}
if (trimmedLine.startsWith("#")) {
if (
i + 1 < lines.length &&
!lines[i + 1].trim().startsWith("#") &&
lines[i + 1].trim()
) {
if (currentJobIndex === targetJobIndex) {
const commentText = trimmedLine.substring(1).trim();
const { comment, logsEnabled } = parseCommentMetadata(commentText);
const formattedComment = formatCommentWithMetadata(
comment,
logsEnabled,
uuid
);
const nextLine = lines[i + 1].trim();
const pausedEntry = `# PAUSED: ${formattedComment}\n# ${nextLine}`;
newCronEntries.push(pausedEntry);
i += 2;
currentJobIndex++;
} else {
newCronEntries.push(line);
newCronEntries.push(lines[i + 1]);
i += 2;
currentJobIndex++;
}
} else {
newCronEntries.push(line);
i++;
}
continue;
}
if (currentJobIndex === targetJobIndex) {
const formattedComment = formatCommentWithMetadata("", false, uuid);
const pausedEntry = `# PAUSED: ${formattedComment}\n# ${trimmedLine}`;
newCronEntries.push(pausedEntry);
} else {
newCronEntries.push(line);
}
currentJobIndex++;
i++;
}
return newCronEntries;
};
export const resumeJobInLines = (
lines: string[],
targetJobIndex: number,
uuid: string
): string[] => {
const newCronEntries: string[] = [];
let currentJobIndex = 0;
let i = 0;
while (i < lines.length) {
const line = lines[i];
const trimmedLine = line.trim();
if (!trimmedLine) {
if (
newCronEntries.length > 0 &&
newCronEntries[newCronEntries.length - 1] !== ""
) {
newCronEntries.push("");
}
i++;
continue;
}
if (
trimmedLine.startsWith("# User:") ||
trimmedLine.startsWith("# System Crontab")
) {
newCronEntries.push(line);
i++;
continue;
}
if (trimmedLine.startsWith("# PAUSED:")) {
if (currentJobIndex === targetJobIndex) {
const commentText = trimmedLine.substring(9).trim();
const { comment, logsEnabled } = parseCommentMetadata(commentText);
if (i + 1 < lines.length && lines[i + 1].trim().startsWith("# ")) {
const cronLine = lines[i + 1].trim().substring(2);
const formattedComment = formatCommentWithMetadata(
comment,
logsEnabled,
uuid
);
const resumedEntry = formattedComment
? `# ${formattedComment}\n${cronLine}`
: cronLine;
newCronEntries.push(resumedEntry);
i += 2;
} else {
i++;
}
} else {
newCronEntries.push(line);
if (i + 1 < lines.length && lines[i + 1].trim().startsWith("# ")) {
newCronEntries.push(lines[i + 1]);
i += 2;
} else {
i++;
}
}
currentJobIndex++;
continue;
}
if (trimmedLine.startsWith("#")) {
newCronEntries.push(line);
i++;
continue;
}
newCronEntries.push(line);
currentJobIndex++;
i++;
}
return newCronEntries;
};
export const parseCommentMetadata = (
commentText: string
): { comment: string; logsEnabled: boolean; uuid?: string } => {
if (!commentText) {
return { comment: "", logsEnabled: false };
}
const parts = commentText.split("|").map((p) => p.trim());
let comment = "";
let logsEnabled = false;
let uuid: string | undefined;
if (parts.length > 1) {
const firstPartIsMetadata =
parts[0].match(/logsEnabled:\s*(true|false)/i) ||
parts[0].match(/id:\s*([a-z0-9]{8}|[a-z0-9]{4}-[a-z0-9]{4})/i);
if (firstPartIsMetadata) {
comment = "";
const metadata = parts.join("|").trim();
const logsMatch = metadata.match(/logsEnabled:\s*(true|false)/i);
if (logsMatch) {
logsEnabled = logsMatch[1].toLowerCase() === "true";
}
const uuidMatches = Array.from(
metadata.matchAll(/id:\s*([a-z0-9]{8}|[a-z0-9]{4}-[a-z0-9]{4})/gi)
);
if (uuidMatches.length > 0) {
uuid = uuidMatches[uuidMatches.length - 1][1].toLowerCase();
}
} else {
comment = parts[0] || "";
const metadata = parts.slice(1).join("|").trim();
const logsMatch = metadata.match(/logsEnabled:\s*(true|false)/i);
if (logsMatch) {
logsEnabled = logsMatch[1].toLowerCase() === "true";
}
const uuidMatches = Array.from(
metadata.matchAll(/id:\s*([a-z0-9]{8}|[a-z0-9]{4}-[a-z0-9]{4})/gi)
);
if (uuidMatches.length > 0) {
uuid = uuidMatches[uuidMatches.length - 1][1].toLowerCase();
}
}
} else {
const logsMatch = commentText.match(/logsEnabled:\s*(true|false)/i);
const uuidMatch = commentText.match(
/id:\s*([a-z0-9]{8}|[a-z0-9]{4}-[a-z0-9]{4})/i
);
if (logsMatch || uuidMatch) {
if (logsMatch) {
logsEnabled = logsMatch[1].toLowerCase() === "true";
}
if (uuidMatch) {
uuid = uuidMatch[1].toLowerCase();
}
comment = "";
} else {
comment = parts[0] || "";
}
}
return { comment, logsEnabled, uuid };
};
export const formatCommentWithMetadata = (
comment: string,
logsEnabled: boolean,
uuid: string
): string => {
const trimmedComment = comment.trim();
const metadataParts: string[] = [];
if (logsEnabled) {
metadataParts.push("logsEnabled: true");
}
metadataParts.push(`id: ${uuid}`);
const metadata = metadataParts.join(" | ");
if (trimmedComment) {
return `${trimmedComment} | ${metadata}`;
}
return metadata;
};
export const parseJobsFromLines = (
lines: string[],
user: string
): CronJob[] => {
const jobs: CronJob[] = [];
let currentComment = "";
let currentLogsEnabled = false;
let currentUuid: string | undefined;
let jobIndex = 0;
let i = 0;
while (i < lines.length) {
const line = lines[i];
const trimmedLine = line.trim();
if (!trimmedLine) {
i++;
continue;
}
if (
trimmedLine.startsWith("# User:") ||
trimmedLine.startsWith("# System Crontab")
) {
i++;
continue;
}
if (trimmedLine.startsWith("# PAUSED:")) {
const commentText = trimmedLine.substring(9).trim();
const { comment, logsEnabled, uuid } = parseCommentMetadata(commentText);
if (i + 1 < lines.length) {
const nextLine = lines[i + 1].trim();
if (nextLine.startsWith("# ")) {
const commentedCron = nextLine.substring(2);
const parts = commentedCron.split(/\s+/);
if (parts.length >= 6) {
const schedule = parts.slice(0, 5).join(" ");
const command = parts.slice(5).join(" ");
const jobId =
uuid || generateStableJobId(schedule, command, user, comment, i);
jobs.push({
id: jobId,
schedule,
command,
comment: comment || undefined,
user,
paused: true,
logsEnabled,
});
jobIndex++;
i += 2;
continue;
}
}
}
i++;
continue;
}
if (trimmedLine.startsWith("#")) {
if (
i + 1 < lines.length &&
!lines[i + 1].trim().startsWith("#") &&
lines[i + 1].trim()
) {
const commentText = trimmedLine.substring(1).trim();
const { comment, logsEnabled, uuid } =
parseCommentMetadata(commentText);
currentComment = comment;
currentLogsEnabled = logsEnabled;
currentUuid = uuid;
i++;
continue;
} else {
i++;
continue;
}
}
let schedule, command;
const parts = trimmedLine.split(/(?:\s|\t)+/);
if (parts[0].startsWith("@")) {
if (parts.length >= 2) {
schedule = parts[0];
command = trimmedLine.slice(trimmedLine.indexOf(parts[1]));
}
} else if (parts.length >= 6) {
schedule = parts.slice(0, 5).join(" ");
command = trimmedLine.slice(trimmedLine.indexOf(parts[5]));
}
if (schedule && command) {
const jobId =
currentUuid ||
generateStableJobId(schedule, command, user, currentComment, i);
jobs.push({
id: jobId,
schedule,
command,
comment: currentComment || undefined,
user,
paused: false,
logsEnabled: currentLogsEnabled,
});
jobIndex++;
currentComment = "";
currentLogsEnabled = false;
currentUuid = undefined;
}
i++;
}
return jobs;
};
export const deleteJobInLines = (
lines: string[],
targetJobIndex: number
): string[] => {
const newCronEntries: string[] = [];
let currentJobIndex = 0;
let i = 0;
while (i < lines.length) {
const line = lines[i];
const trimmedLine = line.trim();
if (!trimmedLine) {
if (
newCronEntries.length > 0 &&
newCronEntries[newCronEntries.length - 1] !== ""
) {
newCronEntries.push("");
}
i++;
continue;
}
if (
trimmedLine.startsWith("# User:") ||
trimmedLine.startsWith("# System Crontab")
) {
newCronEntries.push(line);
i++;
continue;
}
if (trimmedLine.startsWith("# PAUSED:")) {
if (currentJobIndex !== targetJobIndex) {
newCronEntries.push(line);
if (i + 1 < lines.length && lines[i + 1].trim().startsWith("# ")) {
newCronEntries.push(lines[i + 1]);
i += 2;
} else {
i++;
}
} else {
if (i + 1 < lines.length && lines[i + 1].trim().startsWith("# ")) {
i += 2;
} else {
i++;
}
}
currentJobIndex++;
continue;
}
if (trimmedLine.startsWith("#")) {
if (
i + 1 < lines.length &&
!lines[i + 1].trim().startsWith("#") &&
lines[i + 1].trim()
) {
if (currentJobIndex !== targetJobIndex) {
newCronEntries.push(line);
newCronEntries.push(lines[i + 1]);
}
i += 2;
currentJobIndex++;
} else {
newCronEntries.push(line);
i++;
}
continue;
}
if (currentJobIndex !== targetJobIndex) {
newCronEntries.push(line);
}
currentJobIndex++;
i++;
}
return newCronEntries;
};
export const updateJobInLines = (
lines: string[],
targetJobIndex: number,
schedule: string,
command: string,
comment: string = "",
logsEnabled: boolean = false,
uuid: string
): string[] => {
const newCronEntries: string[] = [];
let currentJobIndex = 0;
let i = 0;
while (i < lines.length) {
const line = lines[i];
const trimmedLine = line.trim();
if (!trimmedLine) {
if (
newCronEntries.length > 0 &&
newCronEntries[newCronEntries.length - 1] !== ""
) {
newCronEntries.push("");
}
i++;
continue;
}
if (
trimmedLine.startsWith("# User:") ||
trimmedLine.startsWith("# System Crontab")
) {
newCronEntries.push(line);
i++;
continue;
}
if (trimmedLine.startsWith("# PAUSED:")) {
if (currentJobIndex === targetJobIndex) {
const formattedComment = formatCommentWithMetadata(
comment,
logsEnabled,
uuid
);
const newEntry = formattedComment
? `# PAUSED: ${formattedComment}\n# ${schedule} ${command}`
: `# PAUSED:\n# ${schedule} ${command}`;
newCronEntries.push(newEntry);
if (i + 1 < lines.length && lines[i + 1].trim().startsWith("# ")) {
i += 2;
} else {
i++;
}
} else {
newCronEntries.push(line);
if (i + 1 < lines.length && lines[i + 1].trim().startsWith("# ")) {
newCronEntries.push(lines[i + 1]);
i += 2;
} else {
i++;
}
}
currentJobIndex++;
continue;
}
if (trimmedLine.startsWith("#")) {
if (
i + 1 < lines.length &&
!lines[i + 1].trim().startsWith("#") &&
lines[i + 1].trim()
) {
if (currentJobIndex === targetJobIndex) {
const formattedComment = formatCommentWithMetadata(
comment,
logsEnabled,
uuid
);
const newEntry = formattedComment
? `# ${formattedComment}\n${schedule} ${command}`
: `${schedule} ${command}`;
newCronEntries.push(newEntry);
i += 2;
} else {
newCronEntries.push(line);
newCronEntries.push(lines[i + 1]);
i += 2;
}
currentJobIndex++;
} else {
newCronEntries.push(line);
i++;
}
continue;
}
if (currentJobIndex === targetJobIndex) {
const formattedComment = formatCommentWithMetadata(
comment,
logsEnabled,
uuid
);
const newEntry = formattedComment
? `# ${formattedComment}\n${schedule} ${command}`
: `${schedule} ${command}`;
newCronEntries.push(newEntry);
} else {
newCronEntries.push(line);
}
currentJobIndex++;
i++;
}
return newCronEntries;
};

156
app/_utils/log-watcher.ts Normal file
View File

@@ -0,0 +1,156 @@
import { watch } from "fs";
import { existsSync, readFileSync, readdirSync, statSync } from "fs";
import path from "path";
import { sseBroadcaster } from "./sse-broadcaster";
import { getRunningJob } from "./running-jobs-utils";
const DATA_DIR = path.join(process.cwd(), "data");
const LOGS_DIR = path.join(DATA_DIR, "logs");
let watcher: ReturnType<typeof watch> | null = null;
const parseExitCodeFromLog = (content: string): number | null => {
const match = content.match(/Exit Code\s*:\s*(\d+)/);
return match ? parseInt(match[1], 10) : null;
};
const processLogFile = (logFilePath: string) => {
try {
const pathParts = logFilePath.split(path.sep);
const logsIndex = pathParts.indexOf("logs");
if (logsIndex === -1 || logsIndex >= pathParts.length - 2) {
return;
}
const jobFolderName = pathParts[logsIndex + 1];
if (!existsSync(logFilePath)) {
return;
}
const content = readFileSync(logFilePath, "utf-8");
const exitCode = parseExitCodeFromLog(content);
if (exitCode === null) {
return;
}
const runningJob = getRunningJob(`run-${jobFolderName}`);
if (exitCode === 0) {
sseBroadcaster.broadcast({
type: "job-completed",
timestamp: new Date().toISOString(),
data: {
runId: runningJob?.id || `run-${jobFolderName}`,
cronJobId: runningJob?.cronJobId || jobFolderName,
exitCode,
},
});
} else {
sseBroadcaster.broadcast({
type: "job-failed",
timestamp: new Date().toISOString(),
data: {
runId: runningJob?.id || `run-${jobFolderName}`,
cronJobId: runningJob?.cronJobId || jobFolderName,
exitCode,
},
});
}
} catch (error) {
console.error("[LogWatcher] Error processing log file:", error);
}
};
export const startLogWatcher = () => {
if (watcher) {
return;
}
if (!existsSync(LOGS_DIR)) {
return;
}
watcher = watch(LOGS_DIR, { recursive: true }, (eventType, filename) => {
if (!filename || !filename.endsWith(".log")) {
return;
}
const fullPath = path.join(LOGS_DIR, filename);
if (eventType === "change") {
setTimeout(() => {
processLogFile(fullPath);
}, 500);
}
});
};
export const stopLogWatcher = () => {
if (watcher) {
watcher.close();
watcher = null;
}
};
export const watchForLogFile = (
runId: string,
logFolderName: string,
jobStartTime: Date,
callback: (logFileName: string) => void
): NodeJS.Timeout => {
const logDir = path.join(LOGS_DIR, logFolderName);
const startTime = jobStartTime.getTime();
const maxAttempts = 30;
let attempts = 0;
const checkInterval = setInterval(() => {
attempts++;
if (attempts > maxAttempts) {
console.warn(`[LogWatcher] Timeout waiting for log file for ${runId}`);
clearInterval(checkInterval);
return;
}
try {
if (!existsSync(logDir)) {
return;
}
const files = readdirSync(logDir);
const logFiles = files
.filter((f) => f.endsWith(".log"))
.map((f) => {
const filePath = path.join(logDir, f);
try {
const stats = statSync(filePath);
return {
name: f,
birthtime: stats.birthtime || stats.mtime,
};
} catch {
return null;
}
})
.filter((f): f is { name: string; birthtime: Date } => f !== null);
const matchingFile = logFiles.find((f) => {
const fileTime = f.birthtime.getTime();
return fileTime >= startTime - 5000 && fileTime <= startTime + 30000;
});
if (matchingFile) {
clearInterval(checkInterval);
callback(matchingFile.name);
}
} catch (error) {
console.error(`[LogWatcher] Error watching for log file ${runId}:`, error);
}
}, 500);
return checkInterval;
};

Some files were not shown because too many files have changed in this diff Show More