(squash) init

feat: filters, settings, backups

fix: ts compile errors

feat: new dashboard, webp previews and settings

feat: use webp for pdfs

feat: use webp

fix: analyze resets old data

fix: switch to corsproxy

fix: switch to free cors

fix: max upload limit

fix: currency conversion

feat: transaction export

fix: currency conversion

feat: refactor settings actions

feat: new loader

feat: README + LICENSE

doc: update readme

doc: update readme

doc: update readme

doc: update screenshots

ci: bump prisma
This commit is contained in:
Vasily Zubarev
2025-03-13 00:30:47 +01:00
commit 0b98a2c307
153 changed files with 17271 additions and 0 deletions

131
app/settings/actions.ts Normal file
View File

@@ -0,0 +1,131 @@
"use server"
import { createCategory, deleteCategory, updateCategory } from "@/data/categories"
import { createCurrency, deleteCurrency, updateCurrency } from "@/data/currencies"
import { createField, deleteField, updateField } from "@/data/fields"
import { createProject, deleteProject, updateProject } from "@/data/projects"
import { updateSettings } from "@/data/settings"
import { settingsFormSchema } from "@/forms/settings"
import { codeFromName } from "@/lib/utils"
import { revalidatePath } from "next/cache"
import { redirect } from "next/navigation"
export async function saveSettingsAction(prevState: any, formData: FormData) {
const validatedForm = settingsFormSchema.safeParse(Object.fromEntries(formData))
if (!validatedForm.success) {
return { success: false, error: validatedForm.error.message }
}
for (const key in validatedForm.data) {
await updateSettings(key, validatedForm.data[key as keyof typeof validatedForm.data] || "")
}
revalidatePath("/settings")
redirect("/settings")
// return { success: true }
}
export async function addProjectAction(data: { name: string; llm_prompt?: string; color?: string }) {
const project = await createProject({
code: codeFromName(data.name),
name: data.name,
llm_prompt: data.llm_prompt || null,
color: data.color || "#000000",
})
revalidatePath("/settings/projects")
return project
}
export async function editProjectAction(code: string, data: { name: string; llm_prompt?: string; color?: string }) {
const project = await updateProject(code, {
name: data.name,
llm_prompt: data.llm_prompt,
color: data.color,
})
revalidatePath("/settings/projects")
return project
}
export async function deleteProjectAction(code: string) {
await deleteProject(code)
revalidatePath("/settings/projects")
}
export async function addCurrencyAction(data: { code: string; name: string }) {
const currency = await createCurrency({
code: data.code,
name: data.name,
})
revalidatePath("/settings/currencies")
return currency
}
export async function editCurrencyAction(code: string, data: { name: string }) {
const currency = await updateCurrency(code, { name: data.name })
revalidatePath("/settings/currencies")
return currency
}
export async function deleteCurrencyAction(code: string) {
await deleteCurrency(code)
revalidatePath("/settings/currencies")
}
export async function addCategoryAction(data: { name: string; llm_prompt?: string; color?: string }) {
const category = await createCategory({
code: codeFromName(data.name),
name: data.name,
llm_prompt: data.llm_prompt,
color: data.color,
})
revalidatePath("/settings/categories")
return category
}
export async function editCategoryAction(code: string, data: { name: string; llm_prompt?: string; color?: string }) {
const category = await updateCategory(code, {
name: data.name,
llm_prompt: data.llm_prompt,
color: data.color,
})
revalidatePath("/settings/categories")
return category
}
export async function deleteCategoryAction(code: string) {
await deleteCategory(code)
revalidatePath("/settings/categories")
}
export async function addFieldAction(data: { name: string; type: string; llm_prompt?: string; isRequired?: boolean }) {
const field = await createField({
code: codeFromName(data.name),
name: data.name,
type: data.type,
llm_prompt: data.llm_prompt,
isRequired: data.isRequired,
isExtra: true,
})
revalidatePath("/settings/fields")
return field
}
export async function editFieldAction(
code: string,
data: { name: string; type: string; llm_prompt?: string; isRequired?: boolean }
) {
const field = await updateField(code, {
name: data.name,
type: data.type,
llm_prompt: data.llm_prompt,
isRequired: data.isRequired,
})
revalidatePath("/settings/fields")
return field
}
export async function deleteFieldAction(code: string) {
await deleteField(code)
revalidatePath("/settings/fields")
}

View File

@@ -0,0 +1,21 @@
"use server"
import { DATABASE_FILE } from "@/lib/db"
import fs from "fs"
export async function restoreBackupAction(prevState: any, formData: FormData) {
const file = formData.get("file") as File
if (!file) {
return { success: false, error: "No file provided" }
}
try {
const fileBuffer = await file.arrayBuffer()
const fileData = Buffer.from(fileBuffer)
fs.writeFileSync(DATABASE_FILE, fileData)
} catch (error) {
return { success: false, error: "Failed to restore backup" }
}
return { success: true }
}

View File

@@ -0,0 +1,18 @@
import { DATABASE_FILE } from "@/lib/db"
import fs from "fs"
import { NextResponse } from "next/server"
export async function GET(request: Request) {
try {
const file = fs.readFileSync(DATABASE_FILE)
return new NextResponse(file, {
headers: {
"Content-Type": "application/octet-stream",
"Content-Disposition": `attachment; filename="database.sqlite"`,
},
})
} catch (error) {
console.error("Error exporting database:", error)
return new NextResponse("Internal Server Error", { status: 500 })
}
}

View File

@@ -0,0 +1,52 @@
import { FILE_UPLOAD_PATH } from "@/lib/files"
import fs, { readdirSync } from "fs"
import JSZip from "jszip"
import { NextResponse } from "next/server"
import path from "path"
export async function GET(request: Request) {
try {
const zip = new JSZip()
const folder = zip.folder("uploads")
if (!folder) {
console.error("Failed to create zip folder")
return new NextResponse("Internal Server Error", { status: 500 })
}
const files = getAllFilePaths(FILE_UPLOAD_PATH)
files.forEach((file) => {
folder.file(file.replace(FILE_UPLOAD_PATH, ""), fs.readFileSync(file))
})
const archive = await zip.generateAsync({ type: "blob" })
return new NextResponse(archive, {
headers: {
"Content-Type": "application/octet-stream",
"Content-Disposition": `attachment; filename="uploads.zip"`,
},
})
} catch (error) {
console.error("Error exporting database:", error)
return new NextResponse("Internal Server Error", { status: 500 })
}
}
function getAllFilePaths(dirPath: string): string[] {
let filePaths: string[] = []
function readDirectory(currentPath: string) {
const entries = readdirSync(currentPath, { withFileTypes: true })
for (const entry of entries) {
const fullPath = path.join(currentPath, entry.name)
if (entry.isDirectory()) {
readDirectory(fullPath)
} else {
filePaths.push(fullPath)
}
}
}
readDirectory(dirPath)
return filePaths
}

View File

@@ -0,0 +1,58 @@
"use client"
import { Button } from "@/components/ui/button"
import { Card } from "@/components/ui/card"
import { Download, Loader2 } from "lucide-react"
import Link from "next/link"
import { useActionState } from "react"
import { restoreBackupAction } from "./actions"
export default function BackupSettingsPage() {
const [restoreState, restoreBackup, restorePending] = useActionState(restoreBackupAction, null)
return (
<div className="container flex flex-col gap-4">
<div className="flex flex-col gap-4">
<h1 className="text-2xl font-bold">Download backup</h1>
<div className="flex flex-row gap-4">
<Link href="/settings/backups/database">
<Button>
<Download /> Download database.sqlite
</Button>
</Link>
<Link href="/settings/backups/files">
<Button>
<Download /> Download files archive
</Button>
</Link>
</div>
<div className="text-sm text-muted-foreground">
You can use any SQLite client to view the database.sqlite file contents
</div>
</div>
<Card className="flex flex-col gap-4 mt-24 p-5 bg-red-100 max-w-xl">
<h2 className="text-xl font-semibold">Restore database from backup</h2>
<div className="text-sm text-muted-foreground">
Warning: This will overwrite your current database and destroy all the data! Don't forget to download backup
first.
</div>
<form action={restoreBackup}>
<label>
<input type="file" name="file" />
</label>
<Button type="submit" variant="destructive" disabled={restorePending}>
{restorePending ? (
<>
<Loader2 className="animate-spin" /> Uploading new database...
</>
) : (
"Restore"
)}
</Button>
</form>
{restoreState?.error && <p className="text-red-500">{restoreState.error}</p>}
</Card>
</div>
)
}

View File

@@ -0,0 +1,24 @@
import { DATABASE_FILE } from "@/lib/db"
import fs from "fs"
import { NextResponse } from "next/server"
export async function POST(request: Request) {
try {
const formData = await request.formData()
const file = formData.get("file") as File
if (!file) {
return new NextResponse("No file provided", { status: 400 })
}
const fileBuffer = await file.arrayBuffer()
const fileData = Buffer.from(fileBuffer)
fs.writeFileSync(DATABASE_FILE, fileData)
return new NextResponse("File restored", { status: 200 })
} catch (error) {
console.error("Error restoring from backup:", error)
return new NextResponse("Internal Server Error", { status: 500 })
}
}

View File

@@ -0,0 +1,52 @@
import { addCategoryAction, deleteCategoryAction, editCategoryAction } from "@/app/settings/actions"
import { CrudTable } from "@/components/settings/crud"
import { getCategories } from "@/data/categories"
export default async function CategoriesSettingsPage() {
const categories = await getCategories()
const categoriesWithActions = categories.map((category) => ({
...category,
isEditable: true,
isDeletable: true,
}))
return (
<div className="container">
<h1 className="text-2xl font-bold mb-6">Categories</h1>
<CrudTable
items={categoriesWithActions}
columns={[
{ key: "name", label: "Name", editable: true },
{ key: "llm_prompt", label: "LLM Prompt", editable: true },
{ key: "color", label: "Color", editable: true },
]}
onDelete={async (code) => {
"use server"
await deleteCategoryAction(code)
}}
onAdd={async (data) => {
"use server"
await addCategoryAction(
data as {
code: string
name: string
llm_prompt?: string
color: string
}
)
}}
onEdit={async (code, data) => {
"use server"
await editCategoryAction(
code,
data as {
name: string
llm_prompt?: string
color?: string
}
)
}}
/>
</div>
)
}

View File

@@ -0,0 +1,37 @@
import { addCurrencyAction, deleteCurrencyAction, editCurrencyAction } from "@/app/settings/actions"
import { CrudTable } from "@/components/settings/crud"
import { getCurrencies } from "@/data/currencies"
export default async function CurrenciesSettingsPage() {
const currencies = await getCurrencies()
const currenciesWithActions = currencies.map((currency) => ({
...currency,
isEditable: true,
isDeletable: true,
}))
return (
<div className="container">
<h1 className="text-2xl font-bold mb-6">Currencies</h1>
<CrudTable
items={currenciesWithActions}
columns={[
{ key: "code", label: "Code" },
{ key: "name", label: "Name", editable: true },
]}
onDelete={async (code) => {
"use server"
await deleteCurrencyAction(code)
}}
onAdd={async (data) => {
"use server"
await addCurrencyAction(data as { code: string; name: string })
}}
onEdit={async (code, data) => {
"use server"
await editCurrencyAction(code, data as { name: string })
}}
/>
</div>
)
}

View File

@@ -0,0 +1,51 @@
import { addFieldAction, deleteFieldAction, editFieldAction } from "@/app/settings/actions"
import { CrudTable } from "@/components/settings/crud"
import { getFields } from "@/data/fields"
export default async function FieldsSettingsPage() {
const fields = await getFields()
const fieldsWithActions = fields.map((field) => ({
...field,
isEditable: true,
isDeletable: field.isExtra,
}))
return (
<div className="container">
<h1 className="text-2xl font-bold mb-6">Custom Fields</h1>
<CrudTable
items={fieldsWithActions}
columns={[
{ key: "name", label: "Name", editable: true },
{ key: "type", label: "Type", editable: true },
{ key: "llm_prompt", label: "LLM Prompt", editable: true },
]}
onDelete={async (code) => {
"use server"
await deleteFieldAction(code)
}}
onAdd={async (data) => {
"use server"
await addFieldAction(
data as {
name: string
type: string
llm_prompt?: string
}
)
}}
onEdit={async (code, data) => {
"use server"
await editFieldAction(
code,
data as {
name: string
type: string
llm_prompt?: string
}
)
}}
/>
</div>
)
}

59
app/settings/layout.tsx Normal file
View File

@@ -0,0 +1,59 @@
import { SideNav } from "@/components/settings/side-nav"
import { Separator } from "@/components/ui/separator"
import { Metadata } from "next"
export const metadata: Metadata = {
title: "Settings",
description: "Customize your settings here",
}
const settingsCategories = [
{
title: "General",
href: "/settings",
},
{
title: "LLM settings",
href: "/settings/llm",
},
{
title: "Fields",
href: "/settings/fields",
},
{
title: "Categories",
href: "/settings/categories",
},
{
title: "Projects",
href: "/settings/projects",
},
{
title: "Currencies",
href: "/settings/currencies",
},
{
title: "Backups",
href: "/settings/backups",
},
]
export default function SettingsLayout({ children }: { children: React.ReactNode }) {
return (
<>
<div className="hidden space-y-6 p-10 pb-16 md:block">
<div className="space-y-0.5">
<h2 className="text-2xl font-bold tracking-tight">Settings</h2>
<p className="text-muted-foreground">Customize your settings here</p>
</div>
<Separator className="my-6" />
<div className="flex flex-col space-y-8 lg:flex-row lg:space-x-12 lg:space-y-0">
<aside className="-mx-4 lg:w-1/5">
<SideNav items={settingsCategories} />
</aside>
<div className="flex w-full">{children}</div>
</div>
</div>
</>
)
}

14
app/settings/llm/page.tsx Normal file
View File

@@ -0,0 +1,14 @@
import LLMSettingsForm from "@/components/settings/llm-settings-form"
import { getSettings } from "@/data/settings"
export default async function LlmSettingsPage() {
const settings = await getSettings()
return (
<>
<div className="w-full max-w-2xl">
<LLMSettingsForm settings={settings} />
</div>
</>
)
}

18
app/settings/page.tsx Normal file
View File

@@ -0,0 +1,18 @@
import GlobalSettingsForm from "@/components/settings/global-settings-form"
import { getCategories } from "@/data/categories"
import { getCurrencies } from "@/data/currencies"
import { getSettings } from "@/data/settings"
export default async function SettingsPage() {
const settings = await getSettings()
const currencies = await getCurrencies()
const categories = await getCategories()
return (
<>
<div className="w-full max-w-2xl">
<GlobalSettingsForm settings={settings} currencies={currencies} categories={categories} />
</div>
</>
)
}

View File

@@ -0,0 +1,38 @@
import { addProjectAction, deleteProjectAction, editProjectAction } from "@/app/settings/actions"
import { CrudTable } from "@/components/settings/crud"
import { getProjects } from "@/data/projects"
export default async function ProjectsSettingsPage() {
const projects = await getProjects()
const projectsWithActions = projects.map((project) => ({
...project,
isEditable: true,
isDeletable: true,
}))
return (
<div className="container">
<h1 className="text-2xl font-bold mb-6">Projects</h1>
<CrudTable
items={projectsWithActions}
columns={[
{ key: "name", label: "Name", editable: true },
{ key: "llm_prompt", label: "LLM Prompt", editable: true },
{ key: "color", label: "Color", editable: true },
]}
onDelete={async (code) => {
"use server"
await deleteProjectAction(code)
}}
onAdd={async (data) => {
"use server"
await addProjectAction(data as { code: string; name: string; llm_prompt: string; color: string })
}}
onEdit={async (code, data) => {
"use server"
await editProjectAction(code, data as { name: string; llm_prompt: string; color: string })
}}
/>
</div>
)
}