Repository navigation
Expand file tree
/
Copy pathApp.tsx
More file actions
147 lines (137 loc) · 4.33 KB
/
Copy pathApp.tsx
File metadata and controls
147 lines (137 loc) · 4.33 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
import { HotCodePush, useUpdates } from '@hotcodepush/react-native-code-push';
import type { Release, SyncResult } from '@hotcodepush/react-native-code-push';
import { useCallback, useEffect, useState } from 'react';
import { Pressable, StyleSheet, Text, View } from 'react-native';
// Change it, release: the label is how you see the update land.
const VERSION = 'v1';
export default function App() {
const { isSyncing, lastSync, state } = useUpdates();
const [deviceId, setDeviceId] = useState('…');
const [rollbackText, setRollbackText] = useState('none');
const [syncText, setSyncText] = useState<string | null>(null);
useEffect(() => {
HotCodePush.getDevice().then(
device => setDeviceId(device.id),
error => setDeviceId(resolveErrorText(error)),
);
// Fired once, on the start that follows a rollback: the release that failed and why.
const listening = HotCodePush.addListener('updateRolledBack', event =>
setRollbackText(
`from ${resolveReleaseText(event.fromRelease)} · ${event.reason}`,
),
);
return () => {
void listening.then(({ remove }) => remove());
};
}, []);
const syncNow = useCallback(async () => {
try {
setSyncText(resolveResultText(await HotCodePush.sync()));
} catch (error) {
setSyncText(resolveErrorText(error));
}
}, []);
return (
<View style={styles.screen}>
<Text style={styles.eyebrow}>Bundle version</Text>
<Text style={styles.version}>{VERSION}</Text>
<Row
label="Current release"
value={resolveReleaseText(state.currentRelease)}
/>
<Row label="Device id" value={deviceId} />
<Row
label="Last sync"
value={
isSyncing
? 'syncing…'
: (syncText ??
(lastSync ? resolveResultText(lastSync) : 'none yet'))
}
/>
<Row label="Last rollback" value={rollbackText} />
<Pressable
accessibilityRole="button"
disabled={isSyncing}
onPress={syncNow}
style={styles.button}
>
<Text style={styles.buttonText}>Sync now</Text>
</Pressable>
<Pressable
accessibilityRole="button"
onPress={() => void HotCodePush.showDebugScreen()}
style={styles.secondaryButton}
>
<Text style={styles.secondaryButtonText}>Debug screen</Text>
</Pressable>
</View>
);
}
function Row({ label, value }: { label: string; value: string }) {
return (
<View style={styles.row}>
<Text style={styles.label}>{label}</Text>
<Text style={styles.value}>{value}</Text>
</View>
);
}
function resolveReleaseText(release: Release | null): string {
return release ? `#${release.number} · ${release.bundleVersion}` : 'embedded';
}
function resolveResultText(result: SyncResult): string {
switch (result.status) {
case 'up_to_date':
return 'up_to_date';
case 'available':
return `available · ${resolveReleaseText(result.release)}`;
case 'downloaded':
return `downloaded · ${resolveReleaseText(result.release)}, applies ${result.applyStrategy}`;
case 'applied':
return `applied · ${resolveReleaseText(result.release)}`;
case 'skipped':
return `skipped · ${result.reason}`;
case 'failed':
return `failed · ${result.reason}: ${result.message}`;
}
}
function resolveErrorText(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}
const styles = StyleSheet.create({
button: {
alignItems: 'center',
backgroundColor: '#f2572b',
borderRadius: 12,
marginTop: 32,
paddingVertical: 16,
},
buttonText: { color: '#ffffff', fontSize: 17, fontWeight: '600' },
eyebrow: {
color: '#6b6f76',
fontSize: 13,
letterSpacing: 1,
textTransform: 'uppercase',
},
label: { color: '#6b6f76', fontSize: 13 },
row: {
borderBottomColor: '#e6e7ea',
borderBottomWidth: StyleSheet.hairlineWidth,
paddingVertical: 14,
},
screen: {
backgroundColor: '#ffffff',
flex: 1,
paddingHorizontal: 24,
paddingTop: 96,
},
secondaryButton: { alignItems: 'center', paddingVertical: 16 },
secondaryButtonText: { color: '#b33a14', fontSize: 15 },
value: { color: '#111318', fontSize: 17, marginTop: 4 },
version: {
color: '#111318',
fontSize: 64,
fontWeight: '700',
marginBottom: 24,
},
});