Skip to content

Commit 2e4e8e2

Browse files
committed
feat: persist book filters and decouple maker form state
1 parent bd8ae1f commit 2e4e8e2

22 files changed

Lines changed: 109 additions & 84 deletions

File tree

frontend/src/basic/MakerPage/index.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ const MakerPage = (): React.JSX.Element => {
3434
return filterOrders({
3535
federation,
3636
baseFilter: {
37-
currency: fav.currency === 0 ? 1 : fav.currency,
38-
type: fav.type,
39-
mode: fav.mode,
37+
currency: maker.currency === 0 ? 1 : maker.currency,
38+
type: maker.type,
39+
mode: maker.mode,
4040
coordinator: 'robosats',
4141
},
4242
premium: maker.premium ?? null,
@@ -50,7 +50,9 @@ const MakerPage = (): React.JSX.Element => {
5050
});
5151
}, [
5252
federation.book,
53-
fav,
53+
maker.currency,
54+
maker.type,
55+
maker.mode,
5456
maker.premium,
5557
maker.amount,
5658
maker.minAmount,

frontend/src/components/MakerForm/MakerForm.tsx

Lines changed: 53 additions & 57 deletions
Large diffs are not rendered by default.

frontend/src/contexts/AppContext.tsx

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,41 @@ import { getHostUrl, getOrigin } from '../utils/getHost';
2121
import makeTheme, { getWindowSize } from '../utils/theme';
2222
import getSettings from '../utils/settings';
2323

24+
const FAV_STORAGE_KEY = 'robosats.bookFilters.v1';
25+
26+
const defaultFav: Favorites = { type: null, currency: 0, mode: 'fiat', coordinator: 'robosats' };
27+
28+
const loadFavFromStorage = (): Favorites => {
29+
try {
30+
const stored = window.localStorage.getItem(FAV_STORAGE_KEY);
31+
if (stored) {
32+
const parsed = JSON.parse(stored);
33+
34+
if (
35+
typeof parsed === 'object' &&
36+
parsed !== null &&
37+
(parsed.type === null || typeof parsed.type === 'number') &&
38+
typeof parsed.currency === 'number' &&
39+
(parsed.mode === 'fiat' || parsed.mode === 'swap') &&
40+
typeof parsed.coordinator === 'string'
41+
) {
42+
return parsed as Favorites;
43+
}
44+
}
45+
} catch {
46+
// localStorage unavailable or invalid data
47+
}
48+
return defaultFav;
49+
};
50+
51+
const saveFavToStorage = (fav: Favorites): void => {
52+
try {
53+
window.localStorage.setItem(FAV_STORAGE_KEY, JSON.stringify(fav));
54+
} catch {
55+
// localStorage unavailable
56+
}
57+
};
58+
2459
export type TorStatus = 'ON' | 'STARTING' | 'STOPPING' | 'OFF';
2560

2661
export const closeAll: OpenDialogs = {
@@ -110,7 +145,7 @@ export const initialAppContext: UseAppStoreType = {
110145
clientVersion: getClientVersion(),
111146
setAcknowledgedWarning: () => {},
112147
acknowledgedWarning: false,
113-
fav: { type: null, currency: 0, mode: 'fiat', coordinator: 'robosats' },
148+
fav: loadFavFromStorage(),
114149
setFav: () => {},
115150
client: 'web',
116151
view: 'basic',
@@ -217,6 +252,11 @@ export const AppContextProvider = ({ children }: AppContextProviderProps): React
217252
setOpen(closeAll);
218253
}, [page, setOpen]);
219254

255+
// Persist book filters to localStorage whenever they change
256+
useEffect(() => {
257+
saveFavToStorage(fav);
258+
}, [fav]);
259+
220260
return (
221261
<AppContext.Provider
222262
value={{

frontend/src/hooks/useBondEstimate.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { useMemo } from 'react';
22
import { calculateBondAmount } from '../utils/bondCalculator';
3-
import { Maker, Federation, Favorites } from '../models';
3+
import type { Maker, Federation } from '../models';
44

55
interface UseBondEstimateProps {
66
maker: Maker;
7-
fav: Favorites;
87
federation: Federation;
98
currentPrice?: number;
109
federationUpdatedAt: number;
@@ -13,7 +12,6 @@ interface UseBondEstimateProps {
1312

1413
export const useBondEstimate = ({
1514
maker,
16-
fav,
1715
federation,
1816
currentPrice,
1917
federationUpdatedAt,
@@ -33,7 +31,7 @@ export const useBondEstimate = ({
3331
maxAmount: maker.maxAmount,
3432
isRange: makerHasAmountRange,
3533
bondSize: bondPercentage,
36-
mode: fav.mode as 'fiat' | 'swap',
34+
mode: maker.mode,
3735
price: currentPrice ?? 0,
3836
premium: maker.premium ?? 0,
3937
});
@@ -45,7 +43,7 @@ export const useBondEstimate = ({
4543
maker.bondSize,
4644
maker.premium,
4745
currentPrice,
48-
fav.mode,
46+
maker.mode,
4947
maker.coordinator,
5048
federationUpdatedAt,
5149
]);

frontend/src/models/Maker.model.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import defaultFederation from '../../static/federation.json';
22

33
export interface Maker {
4+
type: number | null;
5+
currency: number;
6+
mode: 'swap' | 'fiat';
47
advancedOptions: boolean;
58
coordinator: string;
69
isExplicit: boolean;
@@ -27,6 +30,9 @@ export interface Maker {
2730
}
2831

2932
export const defaultMaker: Maker = {
33+
type: null,
34+
currency: 0,
35+
mode: 'fiat',
3036
advancedOptions: false,
3137
coordinator:
3238
Object.keys(defaultFederation)[

frontend/static/locales/ca.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,6 @@
417417
"Description": "Description",
418418
"Edit order": "Editar ordre",
419419
"Enable advanced options": "Habilitar opcions avançades",
420-
"Enter the destination of the Lightning swap": "Introduïu l'adreça destí de l'intercanvi Lightning",
421420
"Escrow/Invoice Timer (HH:mm)": "Temporitzador Dipòsit/Factura(HH:mm)",
422421
"Escrow/invoice step length": "Longitud passos Dipòsit/Factura",
423422
"Estimated Bond": "Fiança estimada",

frontend/static/locales/cs.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,6 @@
417417
"Description": "Popis",
418418
"Edit order": "Upravit objednávku",
419419
"Enable advanced options": "Povolit pokročilé možnosti",
420-
"Enter the destination of the Lightning swap": "Zadejte cíl Lightning výměny",
421420
"Escrow/Invoice Timer (HH:mm)": "Časovač úschovy faktury (HH:mm)",
422421
"Escrow/invoice step length": "Délka kroku úschovy faktury",
423422
"Estimated Bond": "Odhadovaná kauce",

frontend/static/locales/de.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,6 @@
417417
"Description": "Beschreibung",
418418
"Edit order": "Bestellung bearbeiten",
419419
"Enable advanced options": "Erweiterte Optionen aktivieren",
420-
"Enter the destination of the Lightning swap": "Gib das Ziel des Lightning-Swaps ein",
421420
"Escrow/Invoice Timer (HH:mm)": "Escrow/Rechnungstimer (HH:mm)",
422421
"Escrow/invoice step length": "Escrow/Rechnungsschrittlänge",
423422
"Estimated Bond": "Geschätzte Kaution",

frontend/static/locales/en.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,6 @@
417417
"Description": "Description",
418418
"Edit order": "Edit order",
419419
"Enable advanced options": "Enable advanced options",
420-
"Enter the destination of the Lightning swap": "Enter the destination of the Lightning swap",
421420
"Escrow/Invoice Timer (HH:mm)": "Escrow/Invoice Timer (HH:mm)",
422421
"Escrow/invoice step length": "Escrow/invoice step length",
423422
"Estimated Bond": "Estimated Bond",

frontend/static/locales/es.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,6 @@
417417
"Description": "Descripción",
418418
"Edit order": "Editar orden",
419419
"Enable advanced options": "Activar opciones avanzadas",
420-
"Enter the destination of the Lightning swap": "Indique el destino del intercambio Lightning",
421420
"Escrow/Invoice Timer (HH:mm)": "Plazo depósito/factura (HH:mm)",
422421
"Escrow/invoice step length": "Longitud del paso de depósito/factura",
423422
"Estimated Bond": "Fianza estimada",

0 commit comments

Comments
 (0)