UNPKG

@accounter/client

Version:
1 lines • 10.3 kB
import{s as e}from"./dist-BBBZL-zT.js";import{i as t,o as n}from"./utils-C9b7NsUD.js";import{D as r,In as i,Jn as a,Ki as o,Q as s,So as c,r as l,t as u,xo as d}from"./error-handling-DIXN-FX1.js";import{a as f,c as p,d as m,dt as h,f as g,ft as _,gt as v,i as y,mt as b,n as x,o as S,p as ee,r as C,s as w,t as T,u as E}from"./select-D5MmcncR.js";import{t as D}from"./button-DATCl8sW.js";import{a as O,c as te,g as k,h as A,i as j,it as M,m as N,n as P,o as F,r as I,s as L,t as R,u as z,v as B}from"./table-x-G-XYlj.js";var V=v(`banknote-arrow-up`,[[`path`,{d:`M12 18H4a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2v5`,key:`x6cv4u`}],[`path`,{d:`M18 12h.01`,key:`yjnet6`}],[`path`,{d:`M19 22v-6`,key:`qhmiwi`}],[`path`,{d:`m22 19-3-3-3 3`,key:`rn6bg2`}],[`path`,{d:`M6 12h.01`,key:`c2rlol`}],[`circle`,{cx:`12`,cy:`12`,r:`2`,key:`1c9p78`}]]),H=v(`banknote-arrow-down`,[[`path`,{d:`M12 18H4a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2v5`,key:`x6cv4u`}],[`path`,{d:`m16 19 3 3 3-3`,key:`1ibux0`}],[`path`,{d:`M18 12h.01`,key:`yjnet6`}],[`path`,{d:`M19 16v6`,key:`tddt3s`}],[`path`,{d:`M6 12h.01`,key:`c2rlol`}],[`circle`,{cx:`12`,cy:`12`,r:`2`,key:`1c9p78`}]]),U=e(n(),1),W=t();function G({transaction:e}){return(0,W.jsx)(`div`,{className:`flex flex-col justify-center`,children:e.eventDate?M(new Date(e.eventDate),`dd/MM/yyyy`):`-`})}function K({transaction:e}){let{isInterest:t,amount:n}=e,r=(0,U.useMemo)(()=>Number(n.raw)<0,[n.raw]);return(0,W.jsx)(`div`,{className:`flex items-center gap-2`,children:t?(0,W.jsxs)(W.Fragment,{children:[(0,W.jsx)(H,{className:`size-4 text-red-600`}),(0,W.jsx)(A,{variant:`outline`,className:`bg-red-50 text-red-700`,children:`Interest`})]}):r?(0,W.jsxs)(W.Fragment,{children:[(0,W.jsx)(V,{className:`size-4 text-green-600`}),(0,W.jsx)(A,{variant:`outline`,className:`bg-green-50 text-green-700`,children:`Deposit`})]}):(0,W.jsxs)(W.Fragment,{children:[(0,W.jsx)(H,{className:`size-4 text-red-600`}),(0,W.jsx)(A,{variant:`outline`,className:`bg-red-50 text-red-700`,children:`Withdrawal`})]})})}function q({transaction:e}){return(0,W.jsx)(`div`,{className:`flex flex-col justify-center font-mono ${Number(e.amount.raw)<0?`text-green-700`:`text-red-500`}`,children:e.amount.formatted})}function J({transaction:e}){return(0,W.jsx)(`div`,{className:`flex flex-col justify-center font-mono font-semibold`,children:(0,U.useMemo)(()=>{let{currency:t}=e.amount;return new Intl.NumberFormat(`en-US`,{style:`currency`,currency:t,minimumFractionDigits:2,maximumFractionDigits:2}).format(e.cumulativeBalance)},[e.cumulativeBalance,e.amount])})}function Y({transaction:e}){return(0,W.jsx)(`div`,{className:`flex flex-col justify-center font-mono font-semibold`,children:(0,U.useMemo)(()=>{let{currency:t}=e.amount;return new Intl.NumberFormat(`en-US`,{style:`currency`,currency:t,minimumFractionDigits:2,maximumFractionDigits:2}).format(e.totalInterest)},[e.totalInterest,e.amount])})}function X({transaction:e}){return(0,W.jsx)(`div`,{className:`flex flex-col justify-center font-mono text-muted-foreground`,children:(0,U.useMemo)(()=>new Intl.NumberFormat(`en-US`,{style:`currency`,currency:e.localCurrency,minimumFractionDigits:2,maximumFractionDigits:2}).format(e.localAmount),[e.localAmount,e.localCurrency])})}function Z({transaction:e}){return(0,W.jsx)(`div`,{className:`flex flex-col justify-center font-mono font-semibold text-muted-foreground`,children:(0,U.useMemo)(()=>new Intl.NumberFormat(`en-US`,{style:`currency`,currency:e.localCurrency,minimumFractionDigits:2,maximumFractionDigits:2}).format(e.localCumulativeBalance),[e.localCumulativeBalance,e.localCurrency])})}var Q=[{id:`date`,accessorKey:`eventDate`,sortingFn:e=>e.original.eventDate?new Date(e.original.eventDate).getTime():0,header:({column:e})=>(0,W.jsxs)(D,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Date`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,W.jsx)(_,{className:`ml-2 h-4 w-4`}):(0,W.jsx)(b,{className:`ml-2 h-4 w-4`}))]}),cell:({row:e})=>(0,W.jsx)(G,{transaction:e.original})},{id:`indicator`,accessorKey:`amount.raw`,header:`Type`,cell:({row:e})=>(0,W.jsx)(K,{transaction:e.original})},{id:`amount`,accessorKey:`amount.raw`,header:({column:e})=>(0,W.jsxs)(D,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Origin Amount`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,W.jsx)(_,{className:`ml-2 h-4 w-4`}):(0,W.jsx)(b,{className:`ml-2 h-4 w-4`}))]}),cell:({row:e})=>(0,W.jsx)(q,{transaction:e.original})},{id:`cumulativeBalance`,accessorKey:`cumulativeBalance`,header:({column:e})=>(0,W.jsxs)(D,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Origin Balance`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,W.jsx)(_,{className:`ml-2 h-4 w-4`}):(0,W.jsx)(b,{className:`ml-2 h-4 w-4`}))]}),cell:({row:e})=>(0,W.jsx)(J,{transaction:e.original})},{id:`totalInterest`,accessorKey:`totalInterest`,header:({column:e})=>(0,W.jsxs)(D,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Interest`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,W.jsx)(_,{className:`ml-2 h-4 w-4`}):(0,W.jsx)(b,{className:`ml-2 h-4 w-4`}))]}),cell:({row:e})=>(0,W.jsx)(Y,{transaction:e.original})},{id:`localAmount`,accessorKey:`localAmount`,header:({column:e})=>(0,W.jsxs)(D,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Local Amount`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,W.jsx)(_,{className:`ml-2 h-4 w-4`}):(0,W.jsx)(b,{className:`ml-2 h-4 w-4`}))]}),cell:({row:e})=>(0,W.jsx)(X,{transaction:e.original})},{id:`localCumulativeBalance`,accessorKey:`localCumulativeBalance`,header:({column:e})=>(0,W.jsxs)(D,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Local Balance`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,W.jsx)(_,{className:`ml-2 h-4 w-4`}):(0,W.jsx)(b,{className:`ml-2 h-4 w-4`}))]}),cell:({row:e})=>(0,W.jsx)(Z,{transaction:e.original})}],ne=`assignChargeToDeposit`,$=()=>{let[{fetching:e},t]=d(s);return{assigning:e,assignChargeToDeposit:(0,U.useCallback)(async e=>{let n=`Error assigning charge ${e.chargeId} to deposit ${e.depositId}`,r=`${ne}-${e.chargeId}`;l.loading(`Assigning to deposit`,{id:r});try{let i=u(await t(e),n,r);i&&l.success(`Success`,{id:r,description:`Charge assigned to deposit ${i.assignChargeToDeposit.id}`})}catch(e){console.error(`${n}: ${e}`),l.error(`Error`,{id:r,description:n,duration:1e5,closeButton:!0})}},[t])}};function re({depositId:e,chargeId:t,refetch:n}){let[i,a]=(0,U.useState)(!1),[o,s]=(0,U.useState)(void 0),[{data:l}]=c({query:r,pause:!i}),{assigning:u,assignChargeToDeposit:d}=$(),h=(0,U.useMemo)(()=>(l?.allDeposits??[]).filter(t=>t.isOpen&&t.id!==e),[l?.allDeposits,e]),_=(0,U.useCallback)(async()=>{o&&(await d({chargeId:t,depositId:o}),a(!1),s(void 0),n?.())},[d,o,n,t]);return(0,U.useEffect)(()=>{i&&s(void 0)},[i]),(0,W.jsxs)(w,{open:i,onOpenChange:a,children:[(0,W.jsx)(ee,{children:(0,W.jsx)(D,{variant:`ghost`,size:`sm`,children:`Reassign`})}),(0,W.jsxs)(p,{children:[(0,W.jsx)(m,{children:(0,W.jsx)(g,{children:`Reassign Transaction`})}),(0,W.jsx)(`div`,{className:`space-y-4`,children:(0,W.jsxs)(`div`,{className:`flex flex-col gap-2`,children:[(0,W.jsx)(S,{htmlFor:`target-deposit-select`,children:`Target Deposit`}),(0,W.jsxs)(T,{value:o,onValueChange:e=>s(e),children:[(0,W.jsx)(y,{className:`min-w-[220px]`,children:(0,W.jsx)(f,{id:`target-deposit-select`,placeholder:h.length?`Select deposit`:`No open deposits`})}),(0,W.jsx)(x,{children:h.map(e=>(0,W.jsxs)(C,{value:e.id,children:[e.id,` (`,e.currency,`)`]},e.id))})]})]})}),(0,W.jsx)(E,{children:(0,W.jsx)(D,{variant:`secondary`,disabled:!o||u,onClick:_,children:u?`Reassigning…`:`Reassign`})})]})]})}function ie(e,t){let{getId:n,getChargeId:r,getAmount:i}=t,a=new Map;for(let t of e){let e=r(t);if(!e)continue;let n=a.get(e)??[];n.push(t),a.has(e)||a.set(e,n)}let o=new Set;for(let[,e]of a)if(e.length>1){let t=[...e].sort((e,t)=>Math.abs(i(t))-Math.abs(i(e)));for(let e=1;e<t.length;e++)o.add(n(t[e]))}return o}var ae={[i.Eur]:`eur`,[i.Gbp]:`gbp`,[i.Cad]:`cad`,[i.Jpy]:`jpy`,[i.Aud]:`aud`,[i.Sek]:`sek`,[i.Usd]:`usd`};function oe({depositId:e,enableReassign:t=!1,refetch:n}){let{userContext:r}=(0,U.useContext)(B),[s,l]=(0,U.useState)([{id:`date`,desc:!1}]),[{data:u,fetching:d}]=c({query:o,variables:{depositId:e}}),f=(0,U.useMemo)(()=>{if(!u?.deposit?.metadata.transactions)return[];let e=u.deposit.metadata.transactions.map(e=>k(a,e)),t=ie(e,{getId:e=>e.id,getChargeId:e=>e.chargeId,getAmount:e=>Number(e.amount.raw??0)}),n=[...e].sort((e,t)=>(e.eventDate?new Date(e.eventDate).getTime():0)-(t.eventDate?new Date(t.eventDate).getTime():0)),o=0,s=0,c=0;return n.map(e=>{let n=null,a=ae[e.amount.currency];a&&(n=e.debitExchangeRates?.[a]??e.eventExchangeRates?.[a]??null);let l=Number(e.amount.raw),u=l*(n??1),d=r?.context.defaultLocalCurrency??i.Ils,f=t.has(e.id);return f?s+=l:(o+=l,c+=u),{...e,cumulativeBalance:o,localCumulativeBalance:c,localAmount:u,localCurrency:d,totalInterest:s,isInterest:f}})},[u?.deposit,r]),p=(0,U.useMemo)(()=>{let a=r?.context.defaultLocalCurrency??i.Ils,o=u?.deposit?.currency&&u.deposit.currency===a?Q.filter(e=>e.id!==`amount`&&e.id!==`cumulativeBalance`):Q,s=t?[{id:`actions`,header:`Actions`,cell:({row:t})=>(0,W.jsx)(re,{depositId:e,chargeId:t.original.chargeId,refetch:n})}]:[];return[...o,...s]},[t,e,n,u?.deposit?.currency,r?.context.defaultLocalCurrency]),m=te({data:f,columns:p,getCoreRowModel:z(),onSortingChange:l,getSortedRowModel:N(),state:{sorting:s}});return d?(0,W.jsx)(`div`,{className:`flex h-64 w-full items-center justify-center`,children:(0,W.jsx)(h,{className:`size-10 animate-spin`})}):(0,W.jsxs)(R,{children:[(0,W.jsx)(O,{children:m.getHeaderGroups().map(e=>(0,W.jsx)(F,{children:e.headers.map(e=>(0,W.jsx)(j,{colSpan:e.colSpan,children:e.isPlaceholder?null:L(e.column.columnDef.header,e.getContext())},e.id))},e.id))}),(0,W.jsx)(P,{children:m.getRowModel().rows?.length?m.getRowModel().rows.map(e=>(0,W.jsx)(F,{children:e.getVisibleCells().map(e=>(0,W.jsx)(I,{children:L(e.column.columnDef.cell,e.getContext())},e.id))},e.id)):(0,W.jsx)(F,{children:(0,W.jsx)(I,{colSpan:p.length,className:`h-24 text-center`,children:`No transactions in this deposit.`})})})]})}export{$ as n,oe as t};