@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{t as a}from"./createLucideIcon-BK8Z-P8J.js";import{a as o,c as s,d as c,dt as l,f as u,ft as d,i as f,l as p,m,n as h,o as g,p as _,r as v,t as y}from"./select-DrAHsHyM.js";import{t as b}from"./loader-circle-CZACSY-b.js";import{t as x}from"./button-DUNsVBiu.js";import{Bc as S,Kl as C,Pl as w,a as T,c as E,d as D,el as O,i as ee,n as te,nl as k,o as A,r as j,s as M,t as N}from"./table-Dje35oxp.js";import{Ea as P,In as F,X as I,Yn as L,ls as R,us as z,w as B}from"./graphql-BIob9ctl.js";var V=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`}]]),H=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`}]]),U=e(n(),1),W=e(w(),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)(V,{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)(H,{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)(V,{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 Z({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 ne({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 re({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)(x,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Date`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,K.jsx)(l,{className:`ml-2 h-4 w-4`}):(0,K.jsx)(d,{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)(x,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Origin Amount`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,K.jsx)(l,{className:`ml-2 h-4 w-4`}):(0,K.jsx)(d,{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)(x,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Origin Balance`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,K.jsx)(l,{className:`ml-2 h-4 w-4`}):(0,K.jsx)(d,{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)(x,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Interest`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,K.jsx)(l,{className:`ml-2 h-4 w-4`}):(0,K.jsx)(d,{className:`ml-2 h-4 w-4`}))]}),cell:({row:e})=>(0,K.jsx)(Z,{transaction:e.original})},{id:`localAmount`,accessorKey:`localAmount`,header:({column:e})=>(0,K.jsxs)(x,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Local Amount`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,K.jsx)(l,{className:`ml-2 h-4 w-4`}):(0,K.jsx)(d,{className:`ml-2 h-4 w-4`}))]}),cell:({row:e})=>(0,K.jsx)(ne,{transaction:e.original})},{id:`localCumulativeBalance`,accessorKey:`localCumulativeBalance`,header:({column:e})=>(0,K.jsxs)(x,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Local Balance`,e.getIsSorted()&&(e.getIsSorted()===`asc`?(0,K.jsx)(l,{className:`ml-2 h-4 w-4`}):(0,K.jsx)(d,{className:`ml-2 h-4 w-4`}))]}),cell:({row:e})=>(0,K.jsx)(re,{transaction:e.original})}],ie=`assignChargeToDeposit`,$=()=>{let[{fetching:e},t]=R(I);return{assigning:e,assignChargeToDeposit:(0,U.useCallback)(async e=>{let n=`Error assigning charge ${e.chargeId} to deposit ${e.depositId}`,a=`${ie}-${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 ae({depositId:e,chargeId:t,refetch:n}){let[r,i]=(0,U.useState)(!1),[a,l]=(0,U.useState)(void 0),[{data:d}]=z({query:B,pause:!r}),{assigning:b,assignChargeToDeposit:S}=$(),C=(0,U.useMemo)(()=>(d?.allDeposits??[]).filter(t=>t.isOpen&&t.id!==e),[d?.allDeposits,e]),w=(0,U.useCallback)(async()=>{a&&(await S({chargeId:t,depositId:a}),i(!1),l(void 0),n?.())},[S,a,n,t]);return(0,U.useEffect)(()=>{r&&l(void 0)},[r]),(0,K.jsxs)(s,{open:r,onOpenChange:i,children:[(0,K.jsx)(m,{children:(0,K.jsx)(x,{variant:`ghost`,size:`sm`,children:`Reassign`})}),(0,K.jsxs)(p,{children:[(0,K.jsx)(u,{children:(0,K.jsx)(_,{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)(g,{htmlFor:`target-deposit-select`,children:`Target Deposit`}),(0,K.jsxs)(y,{value:a,onValueChange:e=>l(e),children:[(0,K.jsx)(f,{className:`min-w-[220px]`,children:(0,K.jsx)(o,{id:`target-deposit-select`,placeholder:C.length?`Select deposit`:`No open deposits`})}),(0,K.jsx)(h,{children:C.map(e=>(0,K.jsxs)(v,{value:e.id,children:[e.name,e.currency?` (${e.currency})`:``]},e.id))})]})]})}),(0,K.jsx)(c,{children:(0,K.jsx)(x,{variant:`secondary`,disabled:!a||b,onClick:w,children:b?`Reassigning…`:`Reassign`})})]})]})}function oe(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 se={[F.Eur]:`eur`,[F.Gbp]:`gbp`,[F.Cad]:`cad`,[F.Jpy]:`jpy`,[F.Aud]:`aud`,[F.Sek]:`sek`,[F.Usd]:`usd`};function ce({depositId:e,enableReassign:t=!1,refetch:n,reassignToken:r}){let{userContext:i}=(0,U.useContext)(k),[a,o]=(0,U.useState)([{id:`date`,desc:!1}]),[{data:s,fetching:c},l]=z({query:P,variables:{depositId:e}}),u=(0,U.useRef)(r);(0,U.useEffect)(()=>{u.current!==r&&(u.current=r,l({requestPolicy:`network-only`}))},[r,l]);let d=G((0,U.useMemo)(()=>{if(!s?.deposit?.metadata.transactions)return[];let e=s.deposit.metadata.transactions.map(e=>O(L,e)),t=oe(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,c=[];for(let e of n){let n=null,s=se[e.amount.currency];s&&(n=e.debitExchangeRates?.[s]??e.eventExchangeRates?.[s]??null);let l=Number(e.amount.raw),u=l*(n??1),d=i?.context.defaultLocalCurrency??F.Ils,f=t.has(e.id);f?a+=l:(r+=l,o+=u),c.push({...e,cumulativeBalance:r,localCumulativeBalance:o,localAmount:u,localCurrency:d,totalInterest:a,isInterest:f})}return c},[s,i?.context.defaultLocalCurrency])),f=(0,U.useMemo)(()=>{let r=i?.context.defaultLocalCurrency??F.Ils,a=s?.deposit?.currency&&s.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)(ae,{depositId:e,chargeId:t.original.chargeId,refetch:n})}]:[];return[...a,...o]},[t,e,n,s,i?.context.defaultLocalCurrency]),p=E({features:M,data:d,columns:f,onSortingChange:o,state:{sorting:a}});return c&&!s?(0,K.jsx)(`div`,{className:`flex h-64 w-full items-center justify-center`,children:(0,K.jsx)(b,{className:`size-10 animate-spin`})}):(0,K.jsxs)(N,{children:[(0,K.jsx)(T,{children:p.getHeaderGroups().map(e=>(0,K.jsx)(A,{children:e.headers.map(e=>(0,K.jsx)(ee,{colSpan:e.colSpan,children:e.isPlaceholder?null:D(e.column.columnDef.header,e.getContext())},e.id))},e.id))}),(0,K.jsx)(te,{children:p.getRowModel().rows?.length?p.getRowModel().rows.map(e=>(0,K.jsx)(A,{children:e.getVisibleCells().map(e=>(0,K.jsx)(j,{children:D(e.column.columnDef.cell,e.getContext())},e.id))},e.id)):(0,K.jsx)(A,{children:(0,K.jsx)(j,{colSpan:f.length,className:`h-24 text-center`,children:`No transactions in this deposit.`})})})]})}export{$ as n,G as r,ce as t};