forked from TrustUp-app/TrustUp-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainLayout.tsx
More file actions
242 lines (217 loc) · 8.36 KB
/
Copy pathMainLayout.tsx
File metadata and controls
242 lines (217 loc) · 8.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import React, { ReactNode, useState, isValidElement, cloneElement } from 'react';
import { View } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { BottomBar } from './BottomBar';
import { Header } from './Header';
import { NotificationsPanel } from './NotificationsPanel';
import { Toast } from './Toast';
import SettingsScreen from '../pages/SettingsScreen';
import LoanHistoryScreen from '../pages/LoanHistoryScreen';
import LoanDetailScreen from '../pages/LoanDetailScreen';
import ReputationScreen from '../pages/ReputationScreen';
import MerchantsScreen from '../pages/MerchantsScreen';
import MerchantDetailScreen from '../pages/MerchantDetailScreen';
import ProfileScreen from '../pages/ProfileScreen';
import EditProfileScreen from '../pages/EditProfileScreen';
import { useProfile, getInitials } from '../../hooks/profile/use-profile';
import { useNotifications } from '../../hooks/notifications/use-notifications';
import type { Loan } from '../../types/Loan';
import type { MerchantSummary } from '../../types/api';
interface MainLayoutProps {
children: ReactNode;
/** Called when the user disconnects the wallet or signs out. */
onSignOut?: () => void;
}
/** Callbacks injected into the direct child of MainLayout for navigation + toasts. */
interface PayScreenChildProps {
onLoanHistoryPress?: () => void;
onViewReputationPress?: () => void;
onExploreMerchantsPress?: () => void;
onToast?: (message: string) => void;
}
export const MainLayout = ({ children, onSignOut }: MainLayoutProps) => {
const [activeTab, setActiveTab] = useState('home');
const [isNotificationsOpen, setIsNotificationsOpen] = useState(false);
const [isSettingsOpen, setIsSettingsOpen] = useState(false);
const [isLoanHistoryOpen, setIsLoanHistoryOpen] = useState(false);
const [isLoanDetailOpen, setIsLoanDetailOpen] = useState(false);
const [isReputationOpen, setIsReputationOpen] = useState(false);
const [isMerchantsOpen, setIsMerchantsOpen] = useState(false);
const [isMerchantDetailOpen, setIsMerchantDetailOpen] = useState(false);
const [isProfileOpen, setIsProfileOpen] = useState(false);
const [isEditProfileOpen, setIsEditProfileOpen] = useState(false);
const [selectedLoan, setSelectedLoan] = useState<Loan | null>(null);
const [selectedMerchant, setSelectedMerchant] = useState<MerchantSummary | null>(null);
const [toastMessage, setToastMessage] = useState<string | null>(null);
const { profile, isLoading, error, disconnectWallet, saveProfile } = useProfile();
const { unreadCount } = useNotifications();
const handleLoanPress = (loan: Loan) => {
setSelectedLoan(loan);
setIsLoanDetailOpen(true);
};
const handleLoanDetailBack = () => {
setIsLoanDetailOpen(false);
setSelectedLoan(null);
};
const handleMerchantPress = (merchant: MerchantSummary) => {
setSelectedMerchant(merchant);
setIsMerchantDetailOpen(true);
};
const handleMerchantDetailBack = () => {
setIsMerchantDetailOpen(false);
setSelectedMerchant(null);
};
// No dedicated loan-application screen exists yet — return to the base
// screen (where the BNPL purchase flow lives) and confirm the selection.
const handleStartPurchase = (merchant: MerchantSummary) => {
setIsMerchantDetailOpen(false);
setIsMerchantsOpen(false);
setSelectedMerchant(null);
setToastMessage(`Selected ${merchant.name} — continue your BNPL purchase below`);
};
const handleDisconnect = async () => {
await disconnectWallet();
setIsProfileOpen(false);
setIsSettingsOpen(false);
onSignOut?.();
};
const handleSignOut = async () => {
await disconnectWallet();
setIsSettingsOpen(false);
onSignOut?.();
};
// Any full-screen overlay hides the header/bottom bar
const hasOverlay =
isSettingsOpen ||
isLoanHistoryOpen ||
isLoanDetailOpen ||
isReputationOpen ||
isMerchantsOpen ||
isMerchantDetailOpen ||
isProfileOpen ||
isEditProfileOpen;
// Inject callbacks into children so PayScreen can trigger navigation
const enhancedChildren = isValidElement(children)
? cloneElement(children as React.ReactElement<PayScreenChildProps>, {
onLoanHistoryPress: () => setIsLoanHistoryOpen(true),
onViewReputationPress: () => setIsReputationOpen(true),
onExploreMerchantsPress: () => setIsMerchantsOpen(true),
onToast: (message: string) => setToastMessage(message),
})
: children;
return (
<SafeAreaView className="flex-1 bg-white" edges={['top']}>
<View className="flex-1 bg-background">
<View className="flex-1 pb-[60px]">
{!hasOverlay && (
<Header
displayName={profile?.displayName}
avatarUrl={profile?.avatarUrl}
initials={profile ? getInitials(profile.displayName) : undefined}
unreadNotificationsCount={unreadCount}
onNotificationsPress={() => setIsNotificationsOpen(true)}
onSettingsPress={() => setIsSettingsOpen(true)}
onProfilePress={() => setIsProfileOpen(true)}
/>
)}
<View className="flex-1">{enhancedChildren}</View>
</View>
{!hasOverlay && (
<View className="absolute bottom-0 left-0 right-0 z-10 h-[60px] bg-transparent">
<BottomBar activeTab={activeTab} setActiveTab={setActiveTab} />
</View>
)}
{/* Settings Overlay */}
{isSettingsOpen && (
<View className="absolute inset-0 z-20">
<SettingsScreen
onBack={() => setIsSettingsOpen(false)}
onProfilePress={() => {
setIsSettingsOpen(false);
setIsProfileOpen(true);
}}
onSignOut={handleSignOut}
/>
</View>
)}
{/* Profile Overlay */}
{isProfileOpen && (
<View className="absolute inset-0 z-20">
<ProfileScreen
profile={profile}
isLoading={isLoading}
error={error}
onBack={() => setIsProfileOpen(false)}
onEditPress={() => setIsEditProfileOpen(true)}
onDisconnect={handleDisconnect}
/>
</View>
)}
{/* Edit Profile Overlay */}
{isEditProfileOpen && (
<View className="absolute inset-0 z-30">
<EditProfileScreen
profile={profile}
onBack={() => setIsEditProfileOpen(false)}
onSave={async (changes) => {
await saveProfile(changes);
}}
/>
</View>
)}
{/* Loan History Overlay */}
{isLoanHistoryOpen && (
<View className="absolute inset-0 z-20">
<LoanHistoryScreen
onBack={() => setIsLoanHistoryOpen(false)}
onLoanPress={handleLoanPress}
/>
</View>
)}
{/* Loan Detail Overlay */}
{isLoanDetailOpen && selectedLoan && (
<View className="absolute inset-0 z-30">
<LoanDetailScreen loan={selectedLoan} onBack={handleLoanDetailBack} />
</View>
)}
{/* Reputation Overlay */}
{isReputationOpen && (
<View className="absolute inset-0 z-20">
<ReputationScreen onBack={() => setIsReputationOpen(false)} />
</View>
)}
{/* Merchants Overlay */}
{isMerchantsOpen && (
<View className="absolute inset-0 z-20">
<MerchantsScreen
onBack={() => setIsMerchantsOpen(false)}
onMerchantPress={handleMerchantPress}
/>
</View>
)}
{/* Merchant Detail Overlay */}
{isMerchantDetailOpen && selectedMerchant && (
<View className="absolute inset-0 z-30">
<MerchantDetailScreen
merchant={selectedMerchant}
onBack={handleMerchantDetailBack}
onStartPurchasePress={handleStartPurchase}
/>
</View>
)}
{/* Notifications Overlay */}
<NotificationsPanel
isOpen={isNotificationsOpen}
onClose={() => setIsNotificationsOpen(false)}
/>
{/* App-level toast host */}
<Toast
visible={toastMessage !== null}
message={toastMessage ?? ''}
type="success"
onHide={() => setToastMessage(null)}
/>
</View>
</SafeAreaView>
);
};