@@ -24,6 +24,11 @@ import {
2424 listDraftsForUser ,
2525 type StoredDraft ,
2626} from "@/lib/offlineDraftDB" ;
27+ import SplitCalculator from "@/components/SplitCalculator" ;
28+ import {
29+ calculateSplit ,
30+ type SplitMeta ,
31+ } from "@/hooks/useSplitCalculator" ;
2732
2833const RecipientForm = dynamic ( ( ) => import ( "@/components/RecipientForm" ) , { ssr : false } ) ;
2934const TemplateManager = dynamic ( ( ) => import ( "@/components/TemplateManager" ) , { ssr : false } ) ;
@@ -99,6 +104,7 @@ function NewInvoiceForm() {
99104 const [ recurring , setRecurring ] = useState ( false ) ;
100105 const [ intervalDays , setIntervalDays ] = useState < 7 | 30 > ( 7 ) ;
101106 const [ submitting , setSubmitting ] = useState ( false ) ;
107+ const [ splitMeta , setSplitMeta ] = useState < SplitMeta | null > ( null ) ;
102108
103109 const fromId = searchParams . get ( "from" ) ;
104110 const deadlineParam = searchParams . get ( "deadline" ) ;
@@ -142,6 +148,7 @@ function NewInvoiceForm() {
142148 deadlineDays,
143149 recurring,
144150 intervalDays,
151+ splitMeta,
145152 } ;
146153
147154 const { isOffline : draftOffline , discardDraft } = useOfflineDraftAutosave (
@@ -157,6 +164,9 @@ function NewInvoiceForm() {
157164 setDeadlineDays ( recoveredDraft . data . deadlineDays ) ;
158165 setRecurring ( recoveredDraft . data . recurring ) ;
159166 setIntervalDays ( recoveredDraft . data . intervalDays ) ;
167+ if ( ( recoveredDraft . data as any ) . splitMeta ) {
168+ setSplitMeta ( ( recoveredDraft . data as any ) . splitMeta ) ;
169+ }
160170 if ( draftUserId ) {
161171 import ( "@/lib/offlineDraftDB" ) . then ( ( { deleteDraft } ) =>
162172 deleteDraft ( draftUserId , recoveredDraft . draftId )
@@ -363,6 +373,16 @@ function NewInvoiceForm() {
363373 setStepErrors ( ( prev ) => ( { ...prev , [ s ] : "Each recipient needs a valid G... address and positive amount" } ) ) ;
364374 return false ;
365375 }
376+ if ( splitMeta && splitMeta . recipients . length > 0 ) {
377+ const splitResult = calculateSplit ( splitMeta . totalAmount , splitMeta . recipients , splitMeta . assetCode ) ;
378+ if ( ! splitResult . validation . isValid ) {
379+ setStepErrors ( ( prev ) => ( {
380+ ...prev ,
381+ [ s ] : splitResult . validation . errorMessage ?? "Split calculator has invalid configuration" ,
382+ } ) ) ;
383+ return false ;
384+ }
385+ }
366386 setStepErrors ( ( prev ) => ( { ...prev , [ s ] : null } ) ) ;
367387 return true ;
368388 }
@@ -421,6 +441,13 @@ function NewInvoiceForm() {
421441 recipients . map ( ( r ) => ( { address : r . address , amount : r . amount } ) )
422442 ) ;
423443 addToast ( `Invoice #${ invoiceId } created` , "success" ) ;
444+ if ( splitMeta && splitMeta . recipients . length > 0 ) {
445+ fetch ( `/api/invoices/${ invoiceId } ` , {
446+ method : "PATCH" ,
447+ headers : { "Content-Type" : "application/json" } ,
448+ body : JSON . stringify ( { splitMeta } ) ,
449+ } ) . catch ( ( ) => null ) ;
450+ }
424451 setTxModal ( { txHash, invoiceId } ) ;
425452 } else {
426453 const { invoiceId, txHash } = await splitClient . createInvoice ( {
@@ -440,6 +467,13 @@ function NewInvoiceForm() {
440467 amount : equalSplit ? ( perRecipientAmount ?? "0" ) : r . amount ,
441468 } ) )
442469 ) ;
470+ if ( splitMeta && splitMeta . recipients . length > 0 ) {
471+ fetch ( `/api/invoices/${ invoiceId } ` , {
472+ method : "PATCH" ,
473+ headers : { "Content-Type" : "application/json" } ,
474+ body : JSON . stringify ( { splitMeta } ) ,
475+ } ) . catch ( ( ) => null ) ;
476+ }
443477 setTxModal ( { txHash, invoiceId } ) ;
444478 }
445479 discardDraft ( ) ;
@@ -680,6 +714,11 @@ function NewInvoiceForm() {
680714 />
681715 </ ChangedField >
682716 </ div >
717+
718+ < SplitCalculator
719+ initialTotal = { equalSplit ? totalAmount : recipients . reduce ( ( s , r ) => s + parseFloat ( r . amount || "0" ) , 0 ) . toFixed ( 7 ) }
720+ onSplitMetaChange = { setSplitMeta }
721+ />
683722 </ div >
684723 ) ;
685724
0 commit comments