UNPKG

@accounter/client

Version:
1 lines • 10.6 kB
import{s as e}from"./dist-BmACi7k_.js";import{i as t,o as n}from"./utils-BxkqV8PM.js";import{D as r,In as i,Q as a,Ro as o,Yn as s,ia as c,r as l,t as u,zo as d}from"./error-handling-sQ-z99nE.js";import{a as f,c as p,d as m,dt as h,f as g,ft as _,gt as v,i as ee,l as y,m as b,mt as x,n as S,o as C,p as w,r as T,t as E}from"./select-BJv1AsCh.js";import{t as D}from"./button-BZ6z_NAl.js";import{Kl as O,Pl as te,Uc as k,a as ne,c as A,el as j,i as M,m as N,n as P,nl as F,o as I,r as L,s as R,t as z,u as B}from"./table-4P1UQn5n.js";var V=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`}]]),H=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`}]]);function U(e){return O(e,`yyyy-MM-dd`)}var W=e(n(),1),G=e(te(),1);function K(e){let[t,n]=(0,W.useState)(e);return t!==e&&!(0,G.default)(t,e)?(n(e),e):t}var q=t();function J({transaction:e}){return(0,q.jsx)(`div`,{className:`flex flex-col justify-center`,children:e.eventDate?O(new Date(e.eventDate),`dd/MM/yyyy`):`-`})}function Y({transaction:e}){let{isInterest:t,amount:n}=e,r=(0,W.useMemo)(()=>Number(n.raw)<0,[n.raw]);return(0,q.jsx)(`div`,{className:`flex items-center gap-2`,children:t?(0,q.jsxs)(q.Fragment,{children:[(0,q.jsx)(V,{className:`size-4 text-red-600`}),(0,q.jsx)(k,{variant:`outline`,className:`bg-red-50 text-red-700`,children:`Interest`})]}):r?(0,q.jsxs)(q.Fragment,{children:[(0,q.jsx)(H,{className:`size-4 text-green-600`}),(0,q.jsx)(k,{variant:`outline`,className:`bg-green-50 text-green-700`,children:`Deposit`})]}):(0,q.jsxs)(q.Fragment,{children:[(0,q.jsx)(V,{className:`size-4 text-red-600`}),(0,q.jsx)(k,{variant:`outline`,className:`bg-red-50 text-red-700`,children:`Withdrawal`})]})})}function X({transaction:e}){return(0,q.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 re({transaction:e}){return(0,q.jsx)(`div`,{className:`flex flex-col justify-center font-mono font-semibold`,children:(0,W.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 ie({transaction:e}){return(0,q.jsx)(`div`,{className:`flex flex-col justify-center font-mono font-semibold`,children:(0,W.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 ae({transaction:e}){return(0,q.jsx)(`div`,{className:`flex flex-col justify-center font-mono text-muted-foreground`,children:(0,W.useMemo)(()=>new Intl.NumberFormat(`en-US`,{style:`currency`,currency:e.localCurrency,minimumFractionDigits:2,maximumFractionDigits:2}).format(e.localAmount),[e.localAmount,e.localCurrency])})}function oe({transaction:e}){return(0,q.jsx)(`div`,{className:`flex flex-col justify-center font-mono font-semibold text-muted-foreground`,children:(0,W.useMemo)(()=>new Intl.NumberFormat(`en-US`,{style:`currency`,currency:e.localCurrency,minimumFractionDigits:2,maximumFractionDigits:2}).format(e.localCumulativeBalance),[e.localCumulativeBalance,e.localCurrency])})}var Z=[{id:`date`,accessorKey:`eventDate`,sortingFn:e=>e.original.eventDate?new Date(e.original.eventDate).getTime():0,header:({column:e})=>(0,q.jsxs)(D,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Date`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,q.jsx)(_,{className:`ml-2 h-4 w-4`}):(0,q.jsx)(x,{className:`ml-2 h-4 w-4`}))]}),cell:({row:e})=>(0,q.jsx)(J,{transaction:e.original})},{id:`indicator`,accessorKey:`amount.raw`,header:`Type`,cell:({row:e})=>(0,q.jsx)(Y,{transaction:e.original})},{id:`amount`,accessorKey:`amount.raw`,header:({column:e})=>(0,q.jsxs)(D,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Origin Amount`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,q.jsx)(_,{className:`ml-2 h-4 w-4`}):(0,q.jsx)(x,{className:`ml-2 h-4 w-4`}))]}),cell:({row:e})=>(0,q.jsx)(X,{transaction:e.original})},{id:`cumulativeBalance`,accessorKey:`cumulativeBalance`,header:({column:e})=>(0,q.jsxs)(D,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Origin Balance`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,q.jsx)(_,{className:`ml-2 h-4 w-4`}):(0,q.jsx)(x,{className:`ml-2 h-4 w-4`}))]}),cell:({row:e})=>(0,q.jsx)(re,{transaction:e.original})},{id:`totalInterest`,accessorKey:`totalInterest`,header:({column:e})=>(0,q.jsxs)(D,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Interest`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,q.jsx)(_,{className:`ml-2 h-4 w-4`}):(0,q.jsx)(x,{className:`ml-2 h-4 w-4`}))]}),cell:({row:e})=>(0,q.jsx)(ie,{transaction:e.original})},{id:`localAmount`,accessorKey:`localAmount`,header:({column:e})=>(0,q.jsxs)(D,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Local Amount`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,q.jsx)(_,{className:`ml-2 h-4 w-4`}):(0,q.jsx)(x,{className:`ml-2 h-4 w-4`}))]}),cell:({row:e})=>(0,q.jsx)(ae,{transaction:e.original})},{id:`localCumulativeBalance`,accessorKey:`localCumulativeBalance`,header:({column:e})=>(0,q.jsxs)(D,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Local Balance`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,q.jsx)(_,{className:`ml-2 h-4 w-4`}):(0,q.jsx)(x,{className:`ml-2 h-4 w-4`}))]}),cell:({row:e})=>(0,q.jsx)(oe,{transaction:e.original})}],se=`assignChargeToDeposit`,Q=()=>{let[{fetching:e},t]=o(a);return{assigning:e,assignChargeToDeposit:(0,W.useCallback)(async e=>{let n=`Error assigning charge ${e.chargeId} to deposit ${e.depositId}`,r=`${se}-${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 ce({depositId:e,chargeId:t,refetch:n}){let[i,a]=(0,W.useState)(!1),[o,s]=(0,W.useState)(void 0),[{data:c}]=d({query:r,pause:!i}),{assigning:l,assignChargeToDeposit:u}=Q(),h=(0,W.useMemo)(()=>(c?.allDeposits??[]).filter(t=>t.isOpen&&t.id!==e),[c?.allDeposits,e]),_=(0,W.useCallback)(async()=>{o&&(await u({chargeId:t,depositId:o}),a(!1),s(void 0),n?.())},[u,o,n,t]);return(0,W.useEffect)(()=>{i&&s(void 0)},[i]),(0,q.jsxs)(p,{open:i,onOpenChange:a,children:[(0,q.jsx)(b,{children:(0,q.jsx)(D,{variant:`ghost`,size:`sm`,children:`Reassign`})}),(0,q.jsxs)(y,{children:[(0,q.jsx)(g,{children:(0,q.jsx)(w,{children:`Reassign Transaction`})}),(0,q.jsx)(`div`,{className:`space-y-4`,children:(0,q.jsxs)(`div`,{className:`flex flex-col gap-2`,children:[(0,q.jsx)(C,{htmlFor:`target-deposit-select`,children:`Target Deposit`}),(0,q.jsxs)(E,{value:o,onValueChange:e=>s(e),children:[(0,q.jsx)(ee,{className:`min-w-[220px]`,children:(0,q.jsx)(f,{id:`target-deposit-select`,placeholder:h.length?`Select deposit`:`No open deposits`})}),(0,q.jsx)(S,{children:h.map(e=>(0,q.jsxs)(T,{value:e.id,children:[e.name,e.currency?` (${e.currency})`:``]},e.id))})]})]})}),(0,q.jsx)(m,{children:(0,q.jsx)(D,{variant:`secondary`,disabled:!o||l,onClick:_,children:l?`Reassigning…`:`Reassign`})})]})]})}function le(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 $={[i.Eur]:`eur`,[i.Gbp]:`gbp`,[i.Cad]:`cad`,[i.Jpy]:`jpy`,[i.Aud]:`aud`,[i.Sek]:`sek`,[i.Usd]:`usd`};function ue({depositId:e,enableReassign:t=!1,refetch:n,reassignToken:r}){let{userContext:a}=(0,W.useContext)(F),[o,l]=(0,W.useState)([{id:`date`,desc:!1}]),[{data:u,fetching:f},p]=d({query:c,variables:{depositId:e}}),m=(0,W.useRef)(r);(0,W.useEffect)(()=>{m.current!==r&&(m.current=r,p({requestPolicy:`network-only`}))},[r,p]);let g=K((0,W.useMemo)(()=>{if(!u?.deposit?.metadata.transactions)return[];let e=u.deposit.metadata.transactions.map(e=>j(s,e)),t=le(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)),r=0,o=0,c=0;return n.map(e=>{let n=null,s=$[e.amount.currency];s&&(n=e.debitExchangeRates?.[s]??e.eventExchangeRates?.[s]??null);let l=Number(e.amount.raw),u=l*(n??1),d=a?.context.defaultLocalCurrency??i.Ils,f=t.has(e.id);return f?o+=l:(r+=l,c+=u),{...e,cumulativeBalance:r,localCumulativeBalance:c,localAmount:u,localCurrency:d,totalInterest:o,isInterest:f}})},[u?.deposit,a])),_=(0,W.useMemo)(()=>{let r=a?.context.defaultLocalCurrency??i.Ils,o=u?.deposit?.currency&&u.deposit.currency===r?Z.filter(e=>e.id!==`amount`&&e.id!==`cumulativeBalance`):Z,s=t?[{id:`actions`,header:`Actions`,cell:({row:t})=>(0,q.jsx)(ce,{depositId:e,chargeId:t.original.chargeId,refetch:n})}]:[];return[...o,...s]},[t,e,n,u?.deposit?.currency,a?.context.defaultLocalCurrency]),v=A({data:g,columns:_,getCoreRowModel:B(),onSortingChange:l,getSortedRowModel:N(),state:{sorting:o}});return f&&!u?(0,q.jsx)(`div`,{className:`flex h-64 w-full items-center justify-center`,children:(0,q.jsx)(h,{className:`size-10 animate-spin`})}):(0,q.jsxs)(z,{children:[(0,q.jsx)(ne,{children:v.getHeaderGroups().map(e=>(0,q.jsx)(I,{children:e.headers.map(e=>(0,q.jsx)(M,{colSpan:e.colSpan,children:e.isPlaceholder?null:R(e.column.columnDef.header,e.getContext())},e.id))},e.id))}),(0,q.jsx)(P,{children:v.getRowModel().rows?.length?v.getRowModel().rows.map(e=>(0,q.jsx)(I,{children:e.getVisibleCells().map(e=>(0,q.jsx)(L,{children:R(e.column.columnDef.cell,e.getContext())},e.id))},e.id)):(0,q.jsx)(I,{children:(0,q.jsx)(L,{colSpan:_.length,className:`h-24 text-center`,children:`No transactions in this deposit.`})})})]})}export{U as i,Q as n,K as r,ue as t};