forked from ZyntariHQ/Invoisio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOfflineBanner.tsx
More file actions
159 lines (146 loc) · 3.83 KB
/
Copy pathOfflineBanner.tsx
File metadata and controls
159 lines (146 loc) · 3.83 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
import React, { useEffect, useState } from "react";
import {
View,
Text,
TouchableOpacity,
StyleSheet,
Animated,
Platform,
} from "react-native";
import { useConnectivity } from "../hooks/use-connectivity";
import { offlineQueue } from "../lib/offline-queue";
import { useSafeAreaInsets } from "react-native-safe-area-context";
type BannerType = "offline" | "degraded" | "retry";
interface OfflineBannerProps {
onRetry?: () => void;
onDismiss?: () => void;
}
export function OfflineBanner({ onRetry, onDismiss }: OfflineBannerProps) {
const { isOffline, isDegraded } = useConnectivity();
const [queueSize, setQueueSize] = useState(0);
const [slideAnim] = useState(new Animated.Value(-100));
const insets = useSafeAreaInsets();
// Subscribe to queue changes
useEffect(() => {
const unsubscribe = offlineQueue.subscribe(() => {
setQueueSize(offlineQueue.getQueueSize());
});
setQueueSize(offlineQueue.getQueueSize());
return unsubscribe;
}, []);
// Animate banner in/out
useEffect(() => {
const shouldShow = isOffline || isDegraded || queueSize > 0;
Animated.spring(slideAnim, {
toValue: shouldShow ? 0 : -100,
useNativeDriver: true,
tension: 50,
friction: 7,
}).start();
}, [isOffline, isDegraded, queueSize]);
const getBannerType = (): BannerType => {
if (isOffline) return "offline";
if (isDegraded) return "degraded";
if (queueSize > 0) return "retry";
return "retry";
};
const getMessage = () => {
if (isOffline) {
return queueSize > 0
? `You're offline. ${queueSize} request(s) queued.`
: "You're offline. Reconnect to continue.";
}
if (isDegraded) {
return "Network is slow or unstable. Some operations may fail.";
}
if (queueSize > 0) {
return `${queueSize} request(s) waiting to sync.`;
}
return "";
};
const getActionText = () => {
if (isOffline) return "Retry when online";
if (isDegraded) return "Dismiss";
return `Retry (${queueSize})`;
};
const handleAction = () => {
if (isOffline) {
onDismiss?.();
return;
}
if (isDegraded) {
onDismiss?.();
return;
}
// Retry queued requests
onRetry?.();
};
const shouldShow = isOffline || isDegraded || queueSize > 0;
if (!shouldShow) return null;
const bannerType = getBannerType();
const backgroundColor =
bannerType === "offline"
? "#DC2626"
: bannerType === "degraded"
? "#F59E0B"
: "#3B82F6";
return (
<Animated.View
style={[
styles.container,
{
transform: [{ translateY: slideAnim }],
paddingTop: Platform.OS === "ios" ? insets.top : 0,
backgroundColor,
},
]}
>
<View style={styles.content}>
<Text style={styles.message}>{getMessage()}</Text>
<TouchableOpacity style={styles.actionButton} onPress={handleAction}>
<Text style={styles.actionText}>{getActionText()}</Text>
</TouchableOpacity>
</View>
</Animated.View>
);
}
const styles = StyleSheet.create({
container: {
position: "absolute",
top: 0,
left: 0,
right: 0,
zIndex: 1000,
paddingHorizontal: 16,
paddingVertical: 12,
shadowColor: "#000",
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.1,
shadowRadius: 4,
elevation: 3,
},
content: {
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
flexWrap: "wrap",
},
message: {
color: "#FFFFFF",
fontSize: 14,
fontFamily: "Inter_400Regular",
flex: 1,
marginRight: 12,
},
actionButton: {
paddingHorizontal: 16,
paddingVertical: 6,
backgroundColor: "rgba(255,255,255,0.2)",
borderRadius: 6,
},
actionText: {
color: "#FFFFFF",
fontSize: 14,
fontFamily: "Inter_600SemiBold",
},
});