UNPKG

@accounter/client

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