import { useCollection, useIdentity } from '@deplixo/sdk';
import Dashboard from './components/Dashboard.jsx';
import CustomerDetail from './components/CustomerDetail.jsx';
import Subscription from './components/Subscription.jsx';
import Reports from './components/Reports.jsx';
import AdminPanel from './components/AdminPanel.jsx';
function formatINR(n) {
const val = Math.abs(Number(n) || 0);
return '₹' + val.toLocaleString('en-IN', { maximumFractionDigits: 2 });
}
function App() {
const { user, loading: idLoading, login, logout } = useIdentity();
const { items: allCustomers, loading: cLoading, add: addCustomer, remove: removeCustomer } =
useCollection('customers');
const { items: allTxns, loading: tLoading, add: addTxn, remove: removeTxn } =
useCollection('transactions');
const {
items: allSubs,
loading: sLoading,
add: addSub,
update: updateSub,
} = useCollection('subscriptions');
const [selectedId, setSelectedId] = useState(null);
if (idLoading) {
return (
Dukaan Khata
Loading your khata…
);
}
if (!user) {
return (
Dukaan Khata
Your digital udhaar bahi. Sign in to manage your customers and
payments securely.
);
}
if (cLoading || tLoading || sLoading) {
return (
Dukaan Khata
Loading your khata…
);
}
const customers = (allCustomers || []).filter(
(c) => c.author?.id === user?.id
);
const txns = (allTxns || []).filter((t) => t.author?.id === user?.id);
const mySub = (allSubs || []).find((s) => s.author?.id === user?.id);
const currentPlanId = mySub?.value?.planId || 'free';
const selectPlan = (planId) => {
if (mySub) {
updateSub && updateSub(mySub.id, { planId });
} else {
addSub && addSub({ planId });
}
};
// Compute balance per customer
const balanceOf = (customerId) => {
let udhaar = 0;
let paid = 0;
for (const t of txns) {
if (t.value?.customerId !== customerId) continue;
if (t.value?.type === 'udhaar') udhaar += Number(t.value.amount) || 0;
else if (t.value?.type === 'payment') paid += Number(t.value.amount) || 0;
}
return { udhaar, paid, balance: udhaar - paid };
};
const selected = customers.find((c) => c.id === selectedId);
const selectedTxns = selected
? txns
.filter((t) => t.value?.customerId === selected.id)
.sort(
(a, b) =>
new Date(b.createdAt || 0).getTime() -
new Date(a.createdAt || 0).getTime()
)
: [];
const FREE_LIMIT = 5;
const isFree = currentPlanId === 'free';
const atFreeLimit = isFree && customers.length >= FREE_LIMIT;
const handleAddCustomer = (name, phone) => {
if (atFreeLimit) return;
addCustomer({ name, phone });
};
// Admin actions
const settleCustomer = (customerId, dueAmount) => {
if (!dueAmount || dueAmount <= 0) return;
addTxn({
type: 'payment',
amount: dueAmount,
note: 'Settled via admin panel',
customerId,
});
};
const deleteCustomerTxns = (customerId) => {
for (const t of txns) {
if (t.value?.customerId === customerId) removeTxn(t.id);
}
};
const deleteCustomer = (customerId) => {
deleteCustomerTxns(customerId);
removeCustomer && removeCustomer(customerId);
if (selectedId === customerId) setSelectedId(null);
};
const clearAll = () => {
for (const t of txns) removeTxn(t.id);
for (const c of customers) removeCustomer && removeCustomer(c.id);
setSelectedId(null);
};
return (
{selected ? (
) : (
)}
{selected ? selected.value.name : 'Dukaan Khata'}
{selected
? selected.value.phone || 'No phone'
: 'Your digital udhaar bahi'}
{!selected && (
)}
{selected ? (
addTxn({
type: 'udhaar',
amount,
note,
customerId: selected.id,
})
}
onAddPayment={(amount, note) =>
addTxn({
type: 'payment',
amount,
note,
customerId: selected.id,
})
}
onDeleteTxn={(id) => removeTxn(id)}
/>
) : (
<>
{isFree && customers.length > 0 && (
Free plan: {customers.length} of{' '}
{FREE_LIMIT} customers used
{atFreeLimit ? ' — upgrade to add more.' : '.'}
)}
setSelectedId(id)}
onAddCustomer={handleAddCustomer}
addDisabled={atFreeLimit}
/>
>
)}
);
}
ReactDOM.createRoot(document.getElementById('root')).render();