import { useEffect, useState } from "react"; import { Button, Column, Grid, InlineNotification, Link, NumberInput, RadioButton, RadioButtonGroup, Select, SelectItem, Table, TableBody, TableCell, TableContainer, TableHead, TableHeader, TableRow, TextArea, TextInput, Tile, Toggle, } from "@carbon/react"; import { Add, Save, TrashCan } from "@carbon/icons-react"; import { confirm } from "@tauri-apps/plugin-dialog"; import { api } from "../lib/api"; import { INDIAN_STATES, type BankAccount, type GstRegistration, type Settings, type TaxType, } from "../lib/types"; import { emailError, ifscError, panError, phoneError, vendorGstinError } from "../lib/validators"; import { ImagePicker } from "../components/ImagePicker"; import { LogoBranding } from "../components/LogoBranding"; import DiagnosticsPanel from "../components/DiagnosticsPanel"; const blankBank = (): BankAccount => ({ id: null, label: "", bankName: "", accountName: "", accountNo: "", branch: "", ifsc: "", isDefault: false, }); export default function AppSettings({ settings, onSaved, }: { settings: Settings; onSaved: () => void; }) { const [form, setForm] = useState({ ...settings }); const [diagnosticsOpen, setDiagnosticsOpen] = useState(false); const [banks, setBanks] = useState([]); const [editing, setEditing] = useState(null); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [notice, setNotice] = useState(null); const loadBanks = async () => { try { setBanks(await api.listBanks()); } catch (e) { setError(String(e)); } }; useEffect(() => { void loadBanks(); }, []); const set = (key: K, value: Settings[K]) => setForm((prev) => ({ ...prev, [key]: value })); const setBank = (key: K, value: BankAccount[K]) => setEditing((prev) => (prev ? { ...prev, [key]: value } : prev)); const registered = form.gstRegistration !== "unregistered"; const gstinProblem = vendorGstinError( form.gstRegistration, form.vendorGstin, form.vendorPan, form.vendorStateCode, ); const saveAll = async () => { if (gstinProblem) { setError(gstinProblem); return; } setBusy(true); setError(null); setNotice(null); try { await api.saveSettings(form); setNotice("Settings saved."); onSaved(); } catch (e) { setError(String(e)); } finally { setBusy(false); } }; const saveBank = async () => { if (!editing) return; setBusy(true); setError(null); try { const saved = await api.saveBank(editing); if (saved.isDefault || form.defaultBankId === null) { setForm((prev) => ({ ...prev, defaultBankId: saved.id })); } setEditing(null); await loadBanks(); } catch (e) { setError(String(e)); } finally { setBusy(false); } }; const removeBank = async (bank: BankAccount) => { if (bank.id === null) return; const ok = await confirm(`Delete bank account “${bank.bankName}”?`, { title: "Delete bank account", kind: "warning", }); if (!ok) return; try { await api.deleteBank(bank.id); if (form.defaultBankId === bank.id) set("defaultBankId", null); await loadBanks(); } catch (e) { setError(String(e)); } }; return (

Settings

Vendor details, defaults and bank accounts.

{error ? ( ) : null} {notice ? ( setNotice(null)} /> ) : null}

Vendor details

{ set("gstRegistration", value as GstRegistration); if (value === "unregistered") set("defaultTaxType", "none"); else if (form.defaultTaxType === "none") set("defaultTaxType", "cgst_sgst"); }} > set("vendorName", e.target.value)} />