@ -1,12 +1,12 @@
< script lang = "ts" >
import { untrack } from 'svelte';
import { tick, untrack } from 'svelte';
import {
CATEGORIES, exponent, isValidRate, owedOriginal, parseAmount, validateExpense,
type Category, type Expense, type Freq, type Photo, type SplitMode,
} from '@oweme/core';
import { serverConfig } from '../lib/api.ts';
import { randomId } from '../lib/crypto.ts';
import { amountInput , CATEGORY_ICONS , fmtDate , money , today } from '../lib/format.ts';
import { amountInput , CATEGORY_COLORS , CATEGORY_ICONS , fmtDate , money , today } from '../lib/format.ts';
import { processPhoto , type ProcessedPhoto } from '../lib/photo.ts';
import { suggestRate } from '../lib/rates.ts';
import { router } from '../lib/router.svelte.ts';
@ -15,6 +15,7 @@
import type { MsgKey } from '../lib/i18n.ts';
import Avatar from './Avatar.svelte';
import CurrencySelect from './CurrencySelect.svelte';
import Icon from './Icon.svelte';
import PhotoView from './PhotoView.svelte';
import Sheet from './Sheet.svelte';
@ -78,6 +79,12 @@
let newPhotoUrl = $state< string | null > (null);
let photoBusy = $state(false);
let repeat = $state< 'none' | Freq>('none');
// Note and repeat are folded into small buttons until used.
let showNote = $state(!!existing?.note);
let showRepeat = $state(false);
let noteEl = $state< HTMLTextAreaElement > ();
/** "Paid by" value that switches to the several-payers list. */
const SEVERAL = '*several';
let error = $state('');
let saving = $state(false);
@ -193,6 +200,12 @@
}
}
async function openNote() {
showNote = true;
await tick();
noteEl?.focus();
}
function removePhoto() {
photo = null;
newPhoto = null;
@ -252,78 +265,91 @@
< / script >
< Sheet title = { heading } onclose= { close } >
< form class = " stack " onsubmit = { save } novalidate >
< form class = " editor " onsubmit = { save } novalidate >
{ #if ! existing }
< div class = "seg " role = "group" >
< div class = "seg kind " role = "group" >
< button type = "button" aria-pressed = { kind === 'expense' } onclick= {() => ( kind = 'expense' )} > { t ( 'kind_expense' )} </ button >
< button type = "button" aria-pressed = { kind === 'transfer' } onclick= {() => ( kind = 'transfer' )} > { t ( 'kind_transfer' )} </ button >
< / div >
{ /if }
< div class = "card stack" style:gap = "4px" >
<!-- amount, with the currency as a pill; the real <select> sits invisibly on top of it -->
< div class = "card amount-card" >
< div class = "amount-row" >
< span class = "cur-pill" >
< span aria-hidden = "true" > { currency } </ span >< Icon name = "down" size = { 16 } / >
< label class = "sr-only" for = "currency" > { t ( 'currency' )} </ label >
< CurrencySelect id = "currency" bind:value = { currency } / >
< / span >
< label class = "sr-only" for = "amount" > { t ( 'amount' )} </ label >
< input id = "amount" class = "amount-input num" bind:value = { amountText } inputmode="decimal" placeholder = { exp === 0 ? '0' : `0$ { settings . lang === 'cs' ? ',' : '.' } $ { '0' . repeat ( exp )} ` } autocomplete="off" />
< div class = "row" style:justify-content = "center" >
< label class = "sr-only" for = "currency" > { t ( 'currency' )} </ label >
< div style:max-width = "260px" style:width = "100%" >< CurrencySelect id = "currency" bind:value = { currency } / ></ div >
< / div >
{ #if currency !== base }
< div class = "row small" style:justify-content = "center" style:margin-top = "6px" >
< div class = "rate" >
< span class = "row small" >
< label for = "rate" class = "muted" > 1 { currency } =</ label >
< input id = "rate" class = "num" style:width = "120px" style:min-height = "36px" inputmode = "decimal" bind:value = { rateText }
oninput={() => ( rateSource = 'manual' )} />
< input id = "rate" class = "num" inputmode = "decimal" bind:value = { rateText } oninput= {() => ( rateSource = 'manual' )} />
< span class = "muted" > { base } </ span >
< / div >
< / span >
<!-- xr: rates come from ExchangeRate - API's free tier, which requires the attribution link. -->
< p class = "small muted" style:text-align = "center" >
< span class = "small muted" >
{ #if rateMissing && rateSource !== 'manual' }{ t ( 'rate_unavailable' )}
{ :else if rateSource . startsWith ( 'ecb:' )}{ t ( 'rate_source_ecb' , { date : fmtDate ( rateSource . slice ( 4 )) })}
{ :else if rateSource . startsWith ( 'xr:' )} < a href = "https://www.exchangerate-api.com" target = "_blank" rel = "noopener noreferrer" > { t ( 'rate_source_xr' , { date : fmtDate ( rateSource . slice ( 3 )) })} </ a >
{ : else }{ t ( 'rate_source_manual' )}{ /if }
{ #if amount && isValidRate ( rateText . trim (). replace ( ',' , '.' ))} · ≈ { money ( Math . round ( amount * Number ( rateText . replace ( ',' , '.' )) * 10 ** ( exponent ( base ) - exp )), base )}{ /if }
< / p >
< / span >
< / div >
{ /if }
< / div >
{ #if kind === 'expense' }
< label class = "field" > { t ( 'title' )} < input bind:value = { title } placeholder= { t ( 'title_ph' )} maxlength = "200" /></ label >
< label class = "sr-only" for = "title" > { t ( 'title' )} </ label >
< input id = "title" bind:value = { title } placeholder= { t ( 'title' )} maxlength = "200" />
<!-- 12 equal tiles: 6 × 2 on phones, one row of 12 on wider screens -->
< div class = "field" >
< span class = "small muted" > { t ( 'category' )} </ span >
< div class = "chips" >
< span class = " lbl"> { t ( 'category' )} · < span class = "cat-name" > { t ( `cat_$ { category } ` as MsgKey) } </ span > </span >
< div class = "c ats" role = "group" aria-label = { t ( 'category' )} >
{ #each CATEGORIES as c }
< button type = "button" class = "chip" aria-pressed = { category === c } onclick= {() => ( category = c )} >
< span aria-hidden = "true" > { CATEGORY_ICONS [ c ]} </ span > { t ( `cat_$ { c } ` as MsgKey) }
< button type = "button" class = "cat" style:--cat = { CATEGORY_COLORS [ c ]} aria-pressed= { category === c }
aria-label={ t ( `cat_$ { c } ` as MsgKey)} title={t(` cat_$ { c } ` as MsgKey) } onclick={() => ( category = c )} >
< span aria-hidden = "true" > { CATEGORY_ICONS [ c ]} </ span >
< / button >
{ /each }
< / div >
< / div >
{ /if }
< label class = "field" > { t ( 'date' )} < input type = "date" bind:value = { date } required /></ label >
<!-- who paid -->
{ #if kind === 'transfer' }
< div class = " row ">
< label class = "field" style:flex = "1" > { t ( 'from_label' )}
< div class = "pair" >
< label class = "field" > { t ( 'from_label' )}
< select bind:value = { payer } > { #each members as m } <option value = { m . id } > { m . name } </option > { /each } </ select >
< / label >
< label class = "field" style:flex = "1" > { t ( 'to_label' )}
< label class = "field" > { t ( 'to_label' )}
< select bind:value = { transferTo } > { #each members . filter (( m ) => m . id !== payer ) as m } <option value = { m . id } > { m . name } </option > { /each } </ select >
< / label >
< / div >
< label class = "field" > { t ( 'date' )} < input type = "date" bind:value = { date } required /></ label >
{ : else }
< div class = "field" >
< span class = "row small muted" >
< span class = "grow" style:flex = "1" > { t ( 'paid_by_label' )} </ span >
< label class = "row" style:gap = "6px" >< input type = "checkbox" bind:checked = { multiPayer } / > { t ( 'multiple_payers' )} </ label >
< / span >
< div class = "pair" >
< label class = "field" > { t ( 'date' )} < input type = "date" bind:value = { date } required /></ label >
< label class = "field" > { t ( 'paid_by_label' )}
< select aria-label = { t ( 'paid_by_label' )} bind:value= {() => ( multiPayer ? SEVERAL : payer ), ( v ) => { multiPayer = v === SEVERAL ; if ( v !== SEVERAL ) payer = v ; }} >
{ #each members as m } < option value = { m . id } > { m . name }{ m . id === me ? ` ($ { t ( 'you' )}) ` : '' } </option > { /each }
{ #if members . length > 1 } < option value = { SEVERAL } > { t ( 'multiple_payers' )} </option > { /if }
< / select >
< / label >
< / div >
{ #if multiPayer }
< div class = "field" >
< div class = "list" >
{ #each members as m ( m . id )}
< div class = "item" >
< Avatar member = { m } index= { memberIndex ( m . id )} size = { 30 } / >
< span class = "grow "> { m . name } </ span >
< input class = "num " style:width = "120px " inputmode = "decimal" bind:value = { payerAmounts [ m . id ]} aria-label=" { t ( 'paid_by_label' )} : { m . name } " />
< div class = "item member ">
< Avatar member = { m } index= { memberIndex ( m . id )} size = { 28 } / >
< span class = "grow name "> { m . name } </ span >
< input class = "num value " inputmode = "decimal" bind:value = { payerAmounts [ m . id ]} aria-label=" { t ( 'paid_by_label' )} : { m . name } " />
< / div >
{ /each }
< / div >
@ -332,37 +358,36 @@
{ payersRemaining > 0 ? t ( 'remaining' , { amount : money ( payersRemaining , currency ) }) : t ( 'over_by' , { amount : money ( - payersRemaining , currency ) })}
< / span >
{ /if }
{ : else }
< select bind:value = { payer } aria-label= { t ( 'paid_by_label' )} >
{ #each members as m } < option value = { m . id } > { m . name }{ m . id === me ? ` ($ { t ( 'you' )}) ` : '' } </option > { /each }
< / select >
{ /if }
< / div >
{ /if }
<!-- split -->
< div class = "field" >
< span class = "small muted" > { t ( 'split' )} </ span >
< div class = "seg" role = "group" aria-label = { t ( 'split' )} >
< div class = "lbl-row" >
< span class = "lbl" > { t ( 'split' )} </ span >
< button type = "button" class = "link" onclick = {() => members . forEach (( m ) => ( included [ m . id ] = true ))} > { t ( 'select_all' )} </button >
< button type = "button" class = "link" onclick = {() => members . forEach (( m ) => ( included [ m . id ] = false ))} > { t ( 'select_none' )} </button >
< / div >
< div class = "seg modes" role = "group" aria-label = { t ( 'split' )} >
{ #each [[ 'equal' , 'split_equal' ], [ 'shares' , 'split_shares' ], [ 'exact' , 'split_exact' ], [ 'percent' , 'split_percent' ]] as const as [ m , label ]}
< button type = "button" aria-pressed = { mode === m } onclick= {() => setMode ( m )} > { t ( label )} </ button >
{ /each }
< / div >
< div class = "row small" style:justify-content = "flex-end" >
< button type = "button" class = "btn ghost small" onclick = {() => members . forEach (( m ) => ( included [ m . id ] = true ))} > { t ( 'select_all' )} </button >
< button type = "button" class = "btn ghost small" onclick = {() => members . forEach (( m ) => ( included [ m . id ] = false ))} > { t ( 'select_none' )} </button >
< / div >
< div class = "list" >
{ #each members as m ( m . id )}
< div class = "item" >
< input type = "checkbox" bind:checked = { included [ m . id ]} aria-label= { m . name } />
< Avatar member = { m } index= { memberIndex ( m . id )} size = { 30 } / >
< span class = "grow" >
< span style:display = "block" > { m . name } </ span >
{ #if preview [ m . id ]} < span class = "small muted num" > { money ( preview [ m . id ], currency )} </ span > { /if }
< / span >
< div class = "item member" class:off = { ! included [ m . id ]} >
< input id = "inc- { m . id } " type = "checkbox" bind:checked = { included [ m . id ]} aria-label= { m . name } />
< label for = "inc- { m . id } " class = "grow who" >
< Avatar member = { m } index= { memberIndex ( m . id )} size = { 28 } / >
< span class = "name" > { m . name } </ span >
< / label >
<!-- what each person owes; in "Amounts" mode the input already says it -->
{ #if mode !== 'exact' && preview [ m . id ]}
< span class = "num share" class:small = { mode !== 'equal' } class:muted= { mode !== 'equal' } > { money ( preview [ m . id ], currency )} </ span >
{ /if }
{ #if mode !== 'equal' }
< span class = "row" style:gap = "4px" >
< input class = "num" style:width = { mode === 'exact' ? '110px' : '72px' } inputmode= { mode === 'shares' ? 'numeric' : 'decimal' }
< span class = "row value-wrap ">
< input class = "num value" class:narrow = { mode !== 'exact '} inputmode= { mode === 'shares' ? 'numeric' : 'decimal' }
bind:value={ values [ m . id ]} disabled={ ! included [ m . id ]} aria-label="{ t ( 'split' )} : { m . name } " />
{ #if mode === 'percent' } < span class = "muted" > %</ span > { /if }
< / span >
@ -382,29 +407,38 @@
< / div >
{ /if }
<!-- photo -->
<!-- extras: photo, note and repeat stay folded away until wanted -->
< div class = "extras" >
{ #if repeat === 'none' }
< div class = "field" >
< span class = "small muted" > { t ( 'photo' )} </ span >
< label class = "btn small" >
< Icon name = "camera" size = { 16 } / > { photo || newPhoto ? t ( 'replace_photo' ) : t ( 'photo' )}
< input class = "sr-only" type = "file" accept = "image/*" onchange = { pickPhoto } disabled= { photoBusy } />
< / label >
{ /if }
{ #if ! showNote }
< button type = "button" class = "btn small" onclick = { openNote } > <Icon name = "edit" size = { 16 } / > { t ( 'note' )} </ button >
{ /if }
{ #if ! existing && kind === 'expense' && ! showRepeat }
< button type = "button" class = "btn small" onclick = {() => ( showRepeat = true )} > <Icon name = "repeat" size = { 16 } / > { t ( 'repeat' )} </ button >
{ /if }
< / div >
{ #if repeat === 'none' && ( photo || newPhoto || photoBusy )}
< div class = "photo" >
{ #if newPhotoUrl }
< img class = "photo-thumb" src = { newPhotoUrl } alt= { t ( 'photo' )} />
{ :else if photo }
< PhotoView { session } { photo } />
{ /if }
< div class = "row" >
< label class = "btn small" >
{ photo || newPhoto ? t ( 'replace_photo' ) : t ( 'add_photo' )}
< input class = "sr-only" type = "file" accept = "image/*" onchange = { pickPhoto } disabled= { photoBusy } />
< / label >
{ #if photo || newPhoto } < button type = "button" class = "btn small ghost" onclick = { removePhoto } > { t ( 'remove_photo' )} </button > { /if }
{ #if photoBusy } < span class = "small muted" > { t ( 'processing_photo' )} </ span > { /if }
< / div >
< / div >
{ /if }
< label class = "field" > { t ( 'note' )} < textarea bind: value= { note } maxlength="2000 "></ textarea ></ label >
{ #if ! existing && kind === 'expense' }
{ #if showNote }
< label class = "field" > { t ( 'note' )} < textarea bind: this= { noteEl } bind:value= { note } maxlength = "2000" rows = "2 "></ textarea ></ label >
{ /if }
{ #if showRepeat && ! existing && kind === 'expense' }
< label class = "field" > { t ( 'repeat' )}
< select bind:value = { repeat } >
< option value = "none" > { t ( 'repeat_none' )} </ option >
@ -415,10 +449,75 @@
< / label >
{ /if }
< div class = "save-bar" >
{ #if error } < p class = "neg" role = "alert" > { error } </ p > { /if }
< div class = "row" >
< button type = "button" class = "btn" style:flex = "1" onclick = { close } > { t ( 'cancel' )} </button >
< button type = "submit" class = "btn primary" style:flex = "2" disabled = { saving || photoBusy } > { t ( 'save' )} </button >
< button type = "submit" class = "btn primary" disabled = { saving || photoBusy } > { t ( 'save' )} </button >
< / div >
< / form >
< / Sheet >
< style >
.editor { display : flex ; flex - direction : column ; gap : 12px ; }
/* On phones the sheet is full screen: keep Save at the bottom edge. */
@media (max-width: 759px) {
.editor { min - height : calc ( 100 dvh - 88 px - env ( safe - area - inset - top ) - env ( safe - area - inset - bottom )); }
}
.seg.kind button, .seg.modes button { min - height : 32px ; }
.seg.modes button { font - size : 0.86rem ; padding : 0 6 px ; }
.lbl { font - size : 0.85rem ; color : var ( -- muted ); }
.cat-name { color : var ( -- text ); font - weight : 600 ; }
.field { display : flex ; flex - direction : column ; gap : 6px ; }
.amount-card { display : flex ; flex - direction : column ; gap : 8px ; padding : 8px 12 px ; }
.amount-row { display : flex ; align - items : center ; gap : 12px ; }
.amount-row .amount-input { flex : 1 ; min - width : 0 ; text - align : right ; font - size : 2.2rem ; min - height : 50px ; padding : 0 ; }
.cur-pill {
position: relative; flex-shrink: 0; display: inline-flex; align-items: center; gap: 4px;
height: 40px; padding: 0 10px 0 14px; border-radius: 999px; background: var(--surface-2); font-weight: 700;
}
.cur-pill:focus-within { outline : 2px solid var ( -- accent ); outline - offset : 1px ; }
.cur-pill :global(select) { position : absolute ; inset : 0 ; opacity : 0 ; min - height : 0 ; height : 100 % ; cursor : pointer ; }
.rate { display : flex ; flex - direction : column ; align - items : flex - end ; gap : 4px ; text - align : right ; }
.rate input { width : 110px ; min - height : 34px ; padding : 4px 10 px ; }
.cats { display : grid ; grid - template - columns : repeat ( 6 , minmax ( 0 , 1 fr )); gap : 8px ; }
@media (min-width: 600px) { . cats { grid - template - columns : repeat ( 12 , minmax ( 0 , 1 fr )); } }
.cat {
aspect-ratio: 1; max-height: 56px; display: grid; place-items: center; padding: 0;
border: 2px solid transparent; border-radius: 16px; font-size: 1.35rem; cursor: pointer;
background: color-mix(in srgb, var(--cat) 14%, var(--surface));
transition: background-color 0.12s, border-color 0.12s, transform 0.12s;
}
.cat span { filter : saturate ( 0.85 ); }
.cat[aria-pressed='true'] { border - color : var ( -- cat ); background : color - mix ( in srgb , var ( -- cat ) 34 % , var ( -- surface )); transform : scale ( 1.04 ); }
.cat[aria-pressed='true'] span { filter : none ; }
.cat:focus-visible { outline : 2px solid var ( -- accent ); outline - offset : 2px ; }
.pair { display : grid ; grid - template - columns : minmax ( 0 , 1 fr ) minmax ( 0 , 1 fr ); gap : 10px ; }
.lbl-row { display : flex ; align - items : center ; gap : 4px ; }
.lbl-row .lbl { flex : 1 ; }
.link { border : 0 ; background : none ; color : var ( -- accent ); font : inherit ; font - size : 0.85rem ; font - weight : 600 ; padding : 0 8 px ; cursor : pointer ; }
.member { gap : 10px ; padding : 6px 12 px ; min - height : 46px ; }
.member.off .who, .member.off .share { opacity : 0.45 ; }
.who { display : flex ; align - items : center ; gap : 10px ; min - width : 0 ; cursor : pointer ; }
.name { overflow : hidden ; text - overflow : ellipsis ; white - space : nowrap ; }
.share { flex - shrink : 0 ; }
.value-wrap { gap : 4px ; flex - shrink : 0 ; }
.value { width : 104px ; min - height : 36px ; padding : 4px 10 px ; text - align : right ; }
.value.narrow { width : 64px ; }
.extras { display : flex ; flex - wrap : wrap ; gap : 8px ; }
.extras .btn { flex : 1 1 auto ; gap : 6px ; padding : 0 12 px ; }
.photo { display : flex ; flex - direction : column ; gap : 8px ; }
.photo .photo-thumb { max - height : 160px ; }
.save-bar {
position: sticky; bottom: 0; margin-top: auto; display: flex; flex-direction: column; gap: 8px;
padding: 10px 0 calc(2px + env(safe-area-inset-bottom));
background: linear-gradient(to bottom, transparent, var(--bg) 14px);
}
.save-bar .btn { width : 100 % ; }
.save-bar p { margin : 0 ; }
< / style >