@accounter/client
Version:
Accounter client application
1 lines • 10.7 kB
JavaScript
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 h,o as g,p as _,pt as v,r as y,t as b}from"./select-Zdatwv1b.js";import{t as x}from"./button-DUNsVBiu.js";import{$c as S,Bc as C,Ul as w,Zc as T,a as E,c as D,d as O,i as k,jl as ee,n as te,o as A,r as j,s as M,t as N}from"./table-AJA7Uyxy.js";import{Pn as P,Qo as F,Y as I,Zo as L,ma as R,qn as z,w as B}from"./graphql-DC5GYCFE.js";var 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`}]]),H=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`}]]);function U(e){return w(e,`yyyy-MM-dd`)}var W=e(n(),1),G=e(ee(),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?w(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)(H,{className:`size-4 text-red-600`}),(0,q.jsx)(C,{variant:`outline`,className:`bg-red-50 text-red-700`,children:`Interest`})]}):r?(0,q.jsxs)(q.Fragment,{children:[(0,q.jsx)(V,{className:`size-4 text-green-600`}),(0,q.jsx)(C,{variant:`outline`,className:`bg-green-50 text-green-700`,children:`Deposit`})]}):(0,q.jsxs)(q.Fragment,{children:[(0,q.jsx)(H,{className:`size-4 text-red-600`}),(0,q.jsx)(C,{variant:`outline`,className:`bg-red-50 text-red-700`,children:`Withdrawal`})]})})}function X({transaction:e}){let t=Number(e.amount.raw)<0?`text-green-700`:`text-red-500`;return(0,q.jsx)(`div`,{className:`flex flex-col justify-center font-mono ${t}`,children:e.amount.formatted})}function Z({transaction:e}){let t=(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]);return(0,q.jsx)(`div`,{className:`flex flex-col justify-center font-mono font-semibold`,children:t})}function ne({transaction:e}){let t=(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]);return(0,q.jsx)(`div`,{className:`flex flex-col justify-center font-mono font-semibold`,children:t})}function re({transaction:e}){let t=(0,W.useMemo)(()=>new Intl.NumberFormat(`en-US`,{style:`currency`,currency:e.localCurrency,minimumFractionDigits:2,maximumFractionDigits:2}).format(e.localAmount),[e.localAmount,e.localCurrency]);return(0,q.jsx)(`div`,{className:`flex flex-col justify-center font-mono text-muted-foreground`,children:t})}function ie({transaction:e}){let t=(0,W.useMemo)(()=>new Intl.NumberFormat(`en-US`,{style:`currency`,currency:e.localCurrency,minimumFractionDigits:2,maximumFractionDigits:2}).format(e.localCumulativeBalance),[e.localCumulativeBalance,e.localCurrency]);return(0,q.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,q.jsxs)(x,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Date`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,q.jsx)(u,{className:`ml-2 h-4 w-4`}):(0,q.jsx)(v,{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)(x,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Origin Amount`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,q.jsx)(u,{className:`ml-2 h-4 w-4`}):(0,q.jsx)(v,{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)(x,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Origin Balance`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,q.jsx)(u,{className:`ml-2 h-4 w-4`}):(0,q.jsx)(v,{className:`ml-2 h-4 w-4`}))]}),cell:({row:e})=>(0,q.jsx)(Z,{transaction:e.original})},{id:`totalInterest`,accessorKey:`totalInterest`,header:({column:e})=>(0,q.jsxs)(x,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Interest`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,q.jsx)(u,{className:`ml-2 h-4 w-4`}):(0,q.jsx)(v,{className:`ml-2 h-4 w-4`}))]}),cell:({row:e})=>(0,q.jsx)(ne,{transaction:e.original})},{id:`localAmount`,accessorKey:`localAmount`,header:({column:e})=>(0,q.jsxs)(x,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Local Amount`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,q.jsx)(u,{className:`ml-2 h-4 w-4`}):(0,q.jsx)(v,{className:`ml-2 h-4 w-4`}))]}),cell:({row:e})=>(0,q.jsx)(re,{transaction:e.original})},{id:`localCumulativeBalance`,accessorKey:`localCumulativeBalance`,header:({column:e})=>(0,q.jsxs)(x,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Local Balance`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,q.jsx)(u,{className:`ml-2 h-4 w-4`}):(0,q.jsx)(v,{className:`ml-2 h-4 w-4`}))]}),cell:({row:e})=>(0,q.jsx)(ie,{transaction:e.original})}],ae=`assignChargeToDeposit`,$=()=>{let[{fetching:e},t]=L(I);return{assigning:e,assignChargeToDeposit:(0,W.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,W.useState)(!1),[a,o]=(0,W.useState)(void 0),[{data:u}]=F({query:B,pause:!r}),{assigning:v,assignChargeToDeposit:S}=$(),C=(0,W.useMemo)(()=>(u?.allDeposits??[]).filter(t=>t.isOpen&&t.id!==e),[u?.allDeposits,e]),w=(0,W.useCallback)(async()=>{a&&(await S({chargeId:t,depositId:a}),i(!1),o(void 0),n?.())},[S,a,n,t]);return(0,W.useEffect)(()=>{r&&o(void 0)},[r]),(0,q.jsxs)(c,{open:r,onOpenChange:i,children:[(0,q.jsx)(m,{children:(0,q.jsx)(x,{variant:`ghost`,size:`sm`,children:`Reassign`})}),(0,q.jsxs)(p,{children:[(0,q.jsx)(d,{children:(0,q.jsx)(_,{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)(g,{htmlFor:`target-deposit-select`,children:`Target Deposit`}),(0,q.jsxs)(b,{value:a,onValueChange:e=>o(e),children:[(0,q.jsx)(f,{className:`min-w-[220px]`,children:(0,q.jsx)(s,{id:`target-deposit-select`,placeholder:C.length?`Select deposit`:`No open deposits`})}),(0,q.jsx)(h,{children:C.map(e=>(0,q.jsxs)(y,{value:e.id,children:[e.name,e.currency?` (${e.currency})`:``]},e.id))})]})]})}),(0,q.jsx)(l,{children:(0,q.jsx)(x,{variant:`secondary`,disabled:!a||v,onClick:w,children:v?`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={[P.Eur]:`eur`,[P.Gbp]:`gbp`,[P.Cad]:`cad`,[P.Jpy]:`jpy`,[P.Aud]:`aud`,[P.Sek]:`sek`,[P.Usd]:`usd`};function le({depositId:e,enableReassign:t=!1,refetch:n,reassignToken:r}){let{userContext:i}=(0,W.useContext)(S),[a,s]=(0,W.useState)([{id:`date`,desc:!1}]),[{data:c,fetching:l},u]=F({query:R,variables:{depositId:e}}),d=(0,W.useRef)(r);(0,W.useEffect)(()=>{d.current!==r&&(d.current=r,u({requestPolicy:`network-only`}))},[r,u]);let f=K((0,W.useMemo)(()=>{if(!c?.deposit?.metadata.transactions)return[];let e=c.deposit.metadata.transactions.map(e=>T(z,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??P.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,W.useMemo)(()=>{let r=i?.context.defaultLocalCurrency??P.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,q.jsx)(oe,{depositId:e,chargeId:t.original.chargeId,refetch:n})}]:[];return[...a,...o]},[t,e,n,c,i?.context.defaultLocalCurrency]),m=D({features:M,data:f,columns:p,onSortingChange:s,state:{sorting:a}});return l&&!c?(0,q.jsx)(`div`,{className:`flex h-64 w-full items-center justify-center`,children:(0,q.jsx)(o,{className:`size-10 animate-spin`})}):(0,q.jsxs)(N,{children:[(0,q.jsx)(E,{children:m.getHeaderGroups().map(e=>(0,q.jsx)(A,{children:e.headers.map(e=>(0,q.jsx)(k,{colSpan:e.colSpan,children:e.isPlaceholder?null:O(e.column.columnDef.header,e.getContext())},e.id))},e.id))}),(0,q.jsx)(te,{children:m.getRowModel().rows?.length?m.getRowModel().rows.map(e=>(0,q.jsx)(A,{children:e.getVisibleCells().map(e=>(0,q.jsx)(j,{children:O(e.column.columnDef.cell,e.getContext())},e.id))},e.id)):(0,q.jsx)(A,{children:(0,q.jsx)(j,{colSpan:p.length,className:`h-24 text-center`,children:`No transactions in this deposit.`})})})]})}export{U as i,$ as n,K as r,le as t};