@vvedantb/native-ui 0.37.2 → 0.38.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -7
- package/dist/app-root/AuthGroupGate.jsx +65 -0
- package/dist/app-root/SsoCallbackScreen.jsx +97 -0
- package/dist/app-root/fonts.js +88 -0
- package/dist/app-root/index.js +3 -0
- package/dist/auth/AuthScreen.jsx +252 -74
- package/dist/auth/SecondFactorScreen.jsx +18 -12
- package/dist/auth/SignInScreen.jsx +40 -17
- package/dist/crash/LastCrashDialog.jsx +56 -0
- package/dist/crash/lastCrash.js +213 -0
- package/dist/entries/app-root.js +6 -0
- package/dist/entries/crash.js +5 -0
- package/dist/entries/theme.js +3 -0
- package/dist/index.js +3 -0
- package/dist/neutralTheme.js +163 -0
- package/dist/primitives/icon.jsx +0 -4
- package/dist/primitives/select.jsx +0 -2
- package/dist/settings/ThemeModePicker.jsx +2 -5
- package/dist/themeModeStore.js +67 -0
- package/dist/themeModes.js +9 -0
- package/package.json +47 -19
- package/src/app-root/AuthGroupGate.tsx +46 -0
- package/src/app-root/SsoCallbackScreen.tsx +62 -0
- package/src/app-root/fonts.ts +55 -0
- package/src/app-root/index.ts +6 -0
- package/src/auth/AuthScreen.tsx +102 -1
- package/src/auth/SecondFactorScreen.tsx +20 -2
- package/src/auth/SignInScreen.tsx +119 -53
- package/src/crash/LastCrashDialog.tsx +42 -0
- package/src/crash/lastCrash.ts +239 -0
- package/src/entries/app-root.ts +6 -0
- package/src/entries/crash.ts +16 -0
- package/src/entries/theme.ts +6 -0
- package/src/highlight.ts +1 -1
- package/src/index.ts +5 -0
- package/src/neutralTheme.ts +130 -0
- package/src/primitives/icon.tsx +0 -2
- package/src/primitives/select.tsx +0 -2
- package/src/settings/ThemeModePicker.tsx +2 -5
- package/src/themeModeStore.ts +94 -0
- package/src/themeModes.ts +10 -0
- package/types/prism-react-renderer.d.ts +22 -0
|
@@ -8,7 +8,8 @@ import { Input } from "../Input";
|
|
|
8
8
|
import { PasswordInput } from "../PasswordInput";
|
|
9
9
|
import { toast } from "../toastStore";
|
|
10
10
|
import { useScheme } from "../useScheme";
|
|
11
|
-
import {
|
|
11
|
+
import { SettingsField } from "../settings/SettingsField";
|
|
12
|
+
import { AuthScreen, FieldError, OrDivider, type AuthScreenProps } from "./AuthScreen";
|
|
12
13
|
import {
|
|
13
14
|
authErrorMessage,
|
|
14
15
|
DEFAULT_STAGE_TIMEOUT_MS,
|
|
@@ -31,7 +32,11 @@ import {
|
|
|
31
32
|
type SecondFactor,
|
|
32
33
|
type SecondFactorStrategy,
|
|
33
34
|
} from "./totp";
|
|
34
|
-
import {
|
|
35
|
+
import {
|
|
36
|
+
GoogleSignInButton,
|
|
37
|
+
type AuthNavigationOptions,
|
|
38
|
+
type GoogleSignInButtonProps,
|
|
39
|
+
} from "./GoogleSignInButton";
|
|
35
40
|
|
|
36
41
|
export type SignInMode = "sign-in" | "sign-up" | "reset-password";
|
|
37
42
|
|
|
@@ -148,6 +153,12 @@ export type SignInScreenProps = AuthNavigationOptions & {
|
|
|
148
153
|
errorDisplay?: "toast" | "inline";
|
|
149
154
|
/** Prefix for every `testID`: `sign-in-email`, `sign-in-password`, `sign-in-submit` and the rest. Default the mode. */
|
|
150
155
|
testIDPrefix?: string;
|
|
156
|
+
/**
|
|
157
|
+
* Optional map from suffix name (such as `email`, `password` or `code`) to a full `testID`.
|
|
158
|
+
* When set, the entry overrides the default `${testIDPrefix ?? mode}-${name}` for that
|
|
159
|
+
* name, for example `{ code: "sign-up-verify-code" }`. A name without an entry keeps the default.
|
|
160
|
+
*/
|
|
161
|
+
testIDs?: Partial<Record<string, string>>;
|
|
151
162
|
/** Above the title. App brand chrome goes here. */
|
|
152
163
|
header?: ReactNode;
|
|
153
164
|
/** Below the footer link row, such as an app credit. */
|
|
@@ -171,6 +182,28 @@ export type SignInScreenProps = AuthNavigationOptions & {
|
|
|
171
182
|
* `auth_timeout`. Default 20000.
|
|
172
183
|
*/
|
|
173
184
|
stageTimeoutMs?: number;
|
|
185
|
+
/** Passed to every `AuthScreen`. Default `"default"`; `"hero"` is a coloured band over a rounded sheet. */
|
|
186
|
+
variant?: AuthScreenProps["variant"];
|
|
187
|
+
/** The hero band colour. Only used with `variant="hero"`. */
|
|
188
|
+
heroColor?: string;
|
|
189
|
+
/** The hero title and subtitle colour. Only used with `variant="hero"`. Default white. */
|
|
190
|
+
heroForeground?: string;
|
|
191
|
+
/** Replaces the primary submit label in `sign-in` and `sign-up`. `reset-password` keeps "Send code". */
|
|
192
|
+
submitLabel?: string;
|
|
193
|
+
/** Replaces the muted prompt in the footer, such as "New to Velth?". */
|
|
194
|
+
footerPrompt?: string;
|
|
195
|
+
/** Replaces the footer link label ("Sign up" or "Sign in"), such as "Create an account". */
|
|
196
|
+
footerLinkLabel?: string;
|
|
197
|
+
/** Default `false`. `true` puts a label ("Email", "Password", "Confirm password") above each field. */
|
|
198
|
+
showFieldLabels?: boolean;
|
|
199
|
+
/** Default "Email". */
|
|
200
|
+
emailPlaceholder?: string;
|
|
201
|
+
/** Default "Password". */
|
|
202
|
+
passwordPlaceholder?: string;
|
|
203
|
+
/** Variant of the Google button. Default `"outline"`. */
|
|
204
|
+
googleVariant?: GoogleSignInButtonProps["variant"];
|
|
205
|
+
/** Size of the Google button. Default: the button default. */
|
|
206
|
+
googleSize?: GoogleSignInButtonProps["size"];
|
|
174
207
|
};
|
|
175
208
|
|
|
176
209
|
/**
|
|
@@ -203,11 +236,26 @@ export function SignInScreen({
|
|
|
203
236
|
minPasswordLength,
|
|
204
237
|
errorDisplay = "toast",
|
|
205
238
|
testIDPrefix,
|
|
239
|
+
testIDs,
|
|
206
240
|
header,
|
|
207
241
|
footer,
|
|
208
242
|
onIncomplete,
|
|
209
243
|
stageTimeoutMs = DEFAULT_STAGE_TIMEOUT_MS,
|
|
244
|
+
variant,
|
|
245
|
+
heroColor,
|
|
246
|
+
heroForeground,
|
|
247
|
+
submitLabel,
|
|
248
|
+
footerPrompt,
|
|
249
|
+
footerLinkLabel,
|
|
250
|
+
showFieldLabels = false,
|
|
251
|
+
emailPlaceholder = "Email",
|
|
252
|
+
passwordPlaceholder = "Password",
|
|
253
|
+
googleVariant,
|
|
254
|
+
googleSize,
|
|
210
255
|
}: SignInScreenProps) {
|
|
256
|
+
const heroProps = { variant, heroColor, heroForeground };
|
|
257
|
+
const labelled = (label: string, control: ReactNode) =>
|
|
258
|
+
showFieldLabels ? <SettingsField label={label}>{control}</SettingsField> : control;
|
|
211
259
|
// Both are always defined: before Clerk loads, a call waits for it.
|
|
212
260
|
const signIn = signInCalls(useSignIn().signIn);
|
|
213
261
|
const signUpAttempt = useSignUp().signUp;
|
|
@@ -241,7 +289,7 @@ export function SignInScreen({
|
|
|
241
289
|
const confirmField = useRef<ComponentRef<typeof TextInput>>(null);
|
|
242
290
|
const signUp = mode === "sign-up";
|
|
243
291
|
const reset = mode === "reset-password";
|
|
244
|
-
const id = (name: string) => `${testIDPrefix ?? mode}-${name}`;
|
|
292
|
+
const id = (name: string) => testIDs?.[name] ?? `${testIDPrefix ?? mode}-${name}`;
|
|
245
293
|
|
|
246
294
|
const fail = (heading: string, message: string) => {
|
|
247
295
|
if (errorDisplay === "inline") setError(message);
|
|
@@ -546,7 +594,8 @@ export function SignInScreen({
|
|
|
546
594
|
{linkHref && !pendingVerification ? (
|
|
547
595
|
<>
|
|
548
596
|
<Text className="font-instrument-sans text-sm" style={textStyle("muted", colorScheme)}>
|
|
549
|
-
{
|
|
597
|
+
{footerPrompt ??
|
|
598
|
+
(reset ? "Remembered your password?" : signUp ? "Already have an account?" : "Don't have an account?")}
|
|
550
599
|
</Text>
|
|
551
600
|
<Button
|
|
552
601
|
testID={id(toSignIn ? "sign-in-link" : "sign-up-link")}
|
|
@@ -555,7 +604,7 @@ export function SignInScreen({
|
|
|
555
604
|
fullWidth={false}
|
|
556
605
|
onPress={() => router.push(linkHref)}
|
|
557
606
|
>
|
|
558
|
-
{toSignIn ? "Sign in" : "Sign up"}
|
|
607
|
+
{footerLinkLabel ?? (toSignIn ? "Sign in" : "Sign up")}
|
|
559
608
|
</Button>
|
|
560
609
|
</>
|
|
561
610
|
) : null}
|
|
@@ -571,6 +620,7 @@ export function SignInScreen({
|
|
|
571
620
|
title={AUTH_MESSAGES.newPasswordTitle}
|
|
572
621
|
subtitle={reset ? AUTH_MESSAGES.resetNewPasswordDescription : AUTH_MESSAGES.newPasswordDescription}
|
|
573
622
|
header={header}
|
|
623
|
+
{...heroProps}
|
|
574
624
|
>
|
|
575
625
|
<PasswordInput
|
|
576
626
|
testID={id("new-password")}
|
|
@@ -634,6 +684,7 @@ export function SignInScreen({
|
|
|
634
684
|
onBack={onSecondFactorBack}
|
|
635
685
|
backLabel="Use a different email"
|
|
636
686
|
header={header}
|
|
687
|
+
{...heroProps}
|
|
637
688
|
testIDPrefix={id("email-code")}
|
|
638
689
|
/>
|
|
639
690
|
);
|
|
@@ -668,6 +719,7 @@ export function SignInScreen({
|
|
|
668
719
|
onBack={onSecondFactorBack}
|
|
669
720
|
backLabel="Use a different account"
|
|
670
721
|
header={header}
|
|
722
|
+
{...heroProps}
|
|
671
723
|
testIDPrefix={id("first-factor")}
|
|
672
724
|
/>
|
|
673
725
|
);
|
|
@@ -714,6 +766,7 @@ export function SignInScreen({
|
|
|
714
766
|
onBack={onSecondFactorBack}
|
|
715
767
|
backLabel="Use a different account"
|
|
716
768
|
header={header}
|
|
769
|
+
{...heroProps}
|
|
717
770
|
testIDPrefix={id("second-factor")}
|
|
718
771
|
/>
|
|
719
772
|
);
|
|
@@ -727,6 +780,7 @@ export function SignInScreen({
|
|
|
727
780
|
subtitle="Enter the code we sent to your email."
|
|
728
781
|
header={header}
|
|
729
782
|
footer={hasFooter ? footerRow : undefined}
|
|
783
|
+
{...heroProps}
|
|
730
784
|
>
|
|
731
785
|
<Input
|
|
732
786
|
testID={id("code")}
|
|
@@ -769,6 +823,7 @@ export function SignInScreen({
|
|
|
769
823
|
}
|
|
770
824
|
header={header}
|
|
771
825
|
footer={hasFooter ? footerRow : undefined}
|
|
826
|
+
{...heroProps}
|
|
772
827
|
>
|
|
773
828
|
{showGoogle && !reset ? (
|
|
774
829
|
<>
|
|
@@ -782,61 +837,72 @@ export function SignInScreen({
|
|
|
782
837
|
onError={(message) => fail(message, message)}
|
|
783
838
|
label={googleLabel}
|
|
784
839
|
disabled={submitting}
|
|
840
|
+
{...(googleVariant ? { variant: googleVariant } : null)}
|
|
841
|
+
{...(googleSize ? { size: googleSize } : null)}
|
|
785
842
|
/>
|
|
786
843
|
<OrDivider />
|
|
787
844
|
</>
|
|
788
845
|
) : null}
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
textContentType="emailAddress"
|
|
795
|
-
keyboardType="email-address"
|
|
796
|
-
returnKeyType={reset ? "go" : "next"}
|
|
797
|
-
submitBehavior={reset ? undefined : "submit"}
|
|
798
|
-
onSubmitEditing={() => (reset ? void onResetRequest() : passwordField.current?.focus())}
|
|
799
|
-
value={email}
|
|
800
|
-
placeholder="Email"
|
|
801
|
-
onChangeText={setEmail}
|
|
802
|
-
/>
|
|
803
|
-
{reset ? null : (
|
|
804
|
-
<PasswordInput
|
|
805
|
-
testID={id("password")}
|
|
806
|
-
ref={passwordField}
|
|
807
|
-
accessibilityLabel="Password"
|
|
846
|
+
{labelled(
|
|
847
|
+
"Email",
|
|
848
|
+
<Input
|
|
849
|
+
testID={id("email")}
|
|
850
|
+
accessibilityLabel="Email"
|
|
808
851
|
autoCapitalize="none"
|
|
809
|
-
autoComplete=
|
|
810
|
-
textContentType=
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
onSubmitEditing={() => (signUp ? confirmField.current?.focus() : void onSignIn())}
|
|
820
|
-
/>
|
|
852
|
+
autoComplete="email"
|
|
853
|
+
textContentType="emailAddress"
|
|
854
|
+
keyboardType="email-address"
|
|
855
|
+
returnKeyType={reset ? "go" : "next"}
|
|
856
|
+
submitBehavior={reset ? undefined : "submit"}
|
|
857
|
+
onSubmitEditing={() => (reset ? void onResetRequest() : passwordField.current?.focus())}
|
|
858
|
+
value={email}
|
|
859
|
+
placeholder={emailPlaceholder}
|
|
860
|
+
onChangeText={setEmail}
|
|
861
|
+
/>,
|
|
821
862
|
)}
|
|
863
|
+
{reset
|
|
864
|
+
? null
|
|
865
|
+
: labelled(
|
|
866
|
+
"Password",
|
|
867
|
+
<PasswordInput
|
|
868
|
+
testID={id("password")}
|
|
869
|
+
ref={passwordField}
|
|
870
|
+
accessibilityLabel="Password"
|
|
871
|
+
autoCapitalize="none"
|
|
872
|
+
autoComplete={signUp ? "new-password" : "password"}
|
|
873
|
+
textContentType={signUp ? "newPassword" : "password"}
|
|
874
|
+
returnKeyType={signUp ? "next" : "go"}
|
|
875
|
+
submitBehavior={signUp ? "submit" : undefined}
|
|
876
|
+
value={password}
|
|
877
|
+
placeholder={passwordPlaceholder}
|
|
878
|
+
onChangeText={(text) => {
|
|
879
|
+
setPassword(text);
|
|
880
|
+
setMismatch(false);
|
|
881
|
+
}}
|
|
882
|
+
onSubmitEditing={() => (signUp ? confirmField.current?.focus() : void onSignIn())}
|
|
883
|
+
/>,
|
|
884
|
+
)}
|
|
822
885
|
{signUp ? (
|
|
823
886
|
<>
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
887
|
+
{labelled(
|
|
888
|
+
"Confirm password",
|
|
889
|
+
<PasswordInput
|
|
890
|
+
testID={id("confirm-password")}
|
|
891
|
+
ref={confirmField}
|
|
892
|
+
accessibilityLabel={AUTH_MESSAGES.confirmSignUpPasswordLabel}
|
|
893
|
+
autoCapitalize="none"
|
|
894
|
+
autoComplete="new-password"
|
|
895
|
+
textContentType="newPassword"
|
|
896
|
+
returnKeyType="go"
|
|
897
|
+
value={signUpConfirm}
|
|
898
|
+
placeholder={AUTH_MESSAGES.confirmSignUpPasswordLabel}
|
|
899
|
+
onChangeText={(text) => {
|
|
900
|
+
setSignUpConfirm(text);
|
|
901
|
+
setMismatch(false);
|
|
902
|
+
}}
|
|
903
|
+
onSubmitEditing={() => void onSignUp()}
|
|
904
|
+
/>,
|
|
905
|
+
)}
|
|
840
906
|
<FieldError testID={id("confirm-error")} message={mismatch ? AUTH_MESSAGES.passwordMismatch : ""} />
|
|
841
907
|
</>
|
|
842
908
|
) : null}
|
|
@@ -859,7 +925,7 @@ export function SignInScreen({
|
|
|
859
925
|
loading={submitting}
|
|
860
926
|
onPress={() => void (reset ? onResetRequest() : signUp ? onSignUp() : onSignIn())}
|
|
861
927
|
>
|
|
862
|
-
{reset ? AUTH_MESSAGES.sendResetCode : signUp ? "Create account" : "Sign in"}
|
|
928
|
+
{reset ? AUTH_MESSAGES.sendResetCode : (submitLabel ?? (signUp ? "Create account" : "Sign in"))}
|
|
863
929
|
</Button>
|
|
864
930
|
{/* Bot protection mounts its widget here on web sign-up. */}
|
|
865
931
|
{signUp ? <View nativeID="clerk-captcha" /> : null}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Share } from "react-native";
|
|
2
|
+
import { ConfirmDialog } from "../ConfirmDialog";
|
|
3
|
+
import { crashDetails, dismissLastCrash, useLastCrash } from "./lastCrash";
|
|
4
|
+
|
|
5
|
+
export type LastCrashDialogProps = {
|
|
6
|
+
/** Names the app in the title and the copied details, for example "Velth". */
|
|
7
|
+
appName: string;
|
|
8
|
+
/**
|
|
9
|
+
* Puts the details on the clipboard, for example `expo-clipboard`'s
|
|
10
|
+
* `setStringAsync`. native-ui adds no clipboard package: without it the
|
|
11
|
+
* button opens the share sheet, where the text can be copied too.
|
|
12
|
+
*/
|
|
13
|
+
onCopy?: (text: string) => Promise<unknown>;
|
|
14
|
+
testID?: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* "<App> closed after an error", once at start when the last session ended in
|
|
19
|
+
* a fatal error saved by `installCrashCapture`: the message, and a button for
|
|
20
|
+
* the full details to paste into a chat. Mount it once in the root layout.
|
|
21
|
+
*/
|
|
22
|
+
export function LastCrashDialog({ appName, onCopy, testID = "last-crash" }: LastCrashDialogProps) {
|
|
23
|
+
const crash = useLastCrash();
|
|
24
|
+
return (
|
|
25
|
+
<ConfirmDialog
|
|
26
|
+
testID={testID}
|
|
27
|
+
open={crash !== null}
|
|
28
|
+
title={`${appName} closed after an error`}
|
|
29
|
+
description={crash ? `${crash.name}: ${crash.message}` : undefined}
|
|
30
|
+
cancelLabel="Dismiss"
|
|
31
|
+
confirmLabel={onCopy ? "Copy details" : "Share details"}
|
|
32
|
+
onCancel={dismissLastCrash}
|
|
33
|
+
onConfirm={() => {
|
|
34
|
+
if (crash) {
|
|
35
|
+
const details = crashDetails(crash, appName);
|
|
36
|
+
void (onCopy ? onCopy(details) : Share.share({ message: details })).catch(() => {});
|
|
37
|
+
}
|
|
38
|
+
dismissLastCrash();
|
|
39
|
+
}}
|
|
40
|
+
/>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { useSyncExternalStore } from "react";
|
|
2
|
+
|
|
3
|
+
// The last error that closed the app. A release build closes with no message
|
|
4
|
+
// and no log a phone user can read, so the global error handler saves the
|
|
5
|
+
// error here and the next launch shows it (`LastCrashDialog`), ready to copy.
|
|
6
|
+
// Moved from eva (`apps/mobile/src/logging`). Not moved: eva's native exit
|
|
7
|
+
// record (its own Expo module) and the report to its backend.
|
|
8
|
+
|
|
9
|
+
/** Where the crash is kept between launches, for example `expo-secure-store`. */
|
|
10
|
+
export type CrashStorage = {
|
|
11
|
+
getItem: (key: string) => Promise<string | null>;
|
|
12
|
+
setItem: (key: string, value: string) => Promise<void>;
|
|
13
|
+
removeItem: (key: string) => Promise<void>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/** What a fatal error leaves behind for the next launch. */
|
|
17
|
+
export type FatalReport = { name: string; message: string; stack: string };
|
|
18
|
+
|
|
19
|
+
export type LastCrash = FatalReport & {
|
|
20
|
+
/** Epoch milliseconds. */
|
|
21
|
+
at: number;
|
|
22
|
+
/** The app version that crashed. */
|
|
23
|
+
version: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type SecureStoreModule = {
|
|
27
|
+
getItemAsync: (key: string) => Promise<string | null>;
|
|
28
|
+
setItemAsync: (key: string, value: string) => Promise<void>;
|
|
29
|
+
deleteItemAsync: (key: string) => Promise<void>;
|
|
30
|
+
};
|
|
31
|
+
type ConstantsModule = { default?: { expoConfig?: { version?: string } | null } };
|
|
32
|
+
|
|
33
|
+
const KEY = "v.lastCrash";
|
|
34
|
+
/** SecureStore values over 2 KB warn on iOS, so a report is cut below that. */
|
|
35
|
+
const MAX_CHARS = 1_800;
|
|
36
|
+
/** How long a fatal error waits for the save before the app closes anyway. */
|
|
37
|
+
const FATAL_SAVE_MS = 1_000;
|
|
38
|
+
|
|
39
|
+
// `expo-secure-store` and `expo-constants` are optional peers. Metro bundles a
|
|
40
|
+
// `require` placed directly in a `try` block as optional, so a missing peer
|
|
41
|
+
// does not break the bundle.
|
|
42
|
+
let secureStore: CrashStorage | null | undefined;
|
|
43
|
+
function defaultStorage(): CrashStorage | null {
|
|
44
|
+
if (secureStore === undefined) {
|
|
45
|
+
try {
|
|
46
|
+
const store = require("expo-secure-store") as SecureStoreModule;
|
|
47
|
+
secureStore = {
|
|
48
|
+
getItem: (key) => store.getItemAsync(key),
|
|
49
|
+
setItem: (key, value) => store.setItemAsync(key, value),
|
|
50
|
+
removeItem: (key) => store.deleteItemAsync(key),
|
|
51
|
+
};
|
|
52
|
+
} catch {
|
|
53
|
+
secureStore = null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return secureStore;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function defaultVersion(): string {
|
|
60
|
+
try {
|
|
61
|
+
const constants = require("expo-constants") as ConstantsModule;
|
|
62
|
+
return constants.default?.expoConfig?.version ?? "?";
|
|
63
|
+
} catch {
|
|
64
|
+
return "?";
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
type Settings = { storage: CrashStorage | null; version: string };
|
|
69
|
+
let settings: Settings | null = null;
|
|
70
|
+
const current = (): Settings => (settings ??= { storage: defaultStorage(), version: defaultVersion() });
|
|
71
|
+
|
|
72
|
+
let last: LastCrash | null = null;
|
|
73
|
+
const listeners = new Set<() => void>();
|
|
74
|
+
function publish(next: LastCrash | null) {
|
|
75
|
+
last = next;
|
|
76
|
+
for (const listener of listeners) listener();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
type Caught = Error | object | string | number | boolean | null | undefined;
|
|
80
|
+
const text = (value: unknown) => (typeof value === "string" && value ? value : undefined);
|
|
81
|
+
const field = (caught: Caught, name: "name" | "message" | "stack") =>
|
|
82
|
+
typeof caught === "object" && caught !== null ? text((caught as Record<string, unknown>)[name]) : undefined;
|
|
83
|
+
|
|
84
|
+
/** The name, message and a short stack of a thrown value. Never the value itself. */
|
|
85
|
+
export function fatalReport(caught: Caught): FatalReport {
|
|
86
|
+
return {
|
|
87
|
+
name: field(caught, "name") ?? "Error",
|
|
88
|
+
message: (field(caught, "message") ?? String(caught)).slice(0, 500),
|
|
89
|
+
stack: (field(caught, "stack") ?? "").split("\n").slice(0, 15).join("\n"),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function isLastCrash(value: unknown): value is LastCrash {
|
|
94
|
+
if (typeof value !== "object" || value === null) return false;
|
|
95
|
+
const crash = value as Record<string, unknown>;
|
|
96
|
+
return typeof crash.at === "number" && ["version", "name", "message", "stack"].every((key) => typeof crash[key] === "string");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Saves the report for the next launch. Never throws. */
|
|
100
|
+
export async function saveLastCrash(report: FatalReport): Promise<void> {
|
|
101
|
+
try {
|
|
102
|
+
const { storage, version } = current();
|
|
103
|
+
if (!storage) return;
|
|
104
|
+
const crash: LastCrash = { at: Date.now(), version, ...report };
|
|
105
|
+
let json = JSON.stringify(crash);
|
|
106
|
+
if (json.length > MAX_CHARS) {
|
|
107
|
+
json = JSON.stringify({ ...crash, stack: crash.stack.slice(0, Math.max(0, MAX_CHARS - (json.length - crash.stack.length))) });
|
|
108
|
+
}
|
|
109
|
+
await storage.setItem(KEY, json);
|
|
110
|
+
} catch {
|
|
111
|
+
// A crash logger must never throw.
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Reads the saved crash, so `LastCrashDialog` shows it. Never throws. */
|
|
116
|
+
export async function loadLastCrash(): Promise<void> {
|
|
117
|
+
try {
|
|
118
|
+
const saved = await current().storage?.getItem(KEY);
|
|
119
|
+
const parsed: unknown = saved ? JSON.parse(saved) : null;
|
|
120
|
+
if (isLastCrash(parsed)) publish(parsed);
|
|
121
|
+
} catch {
|
|
122
|
+
// Nothing to show.
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Shows `crash` in `LastCrashDialog` without saving it. For the gallery. */
|
|
127
|
+
export function showLastCrash(crash: LastCrash | null) {
|
|
128
|
+
publish(crash);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Forgets the shown crash. */
|
|
132
|
+
export function dismissLastCrash() {
|
|
133
|
+
publish(null);
|
|
134
|
+
void current().storage?.removeItem(KEY).catch(() => {});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** The crash saved by the last session, or `null`. */
|
|
138
|
+
export function useLastCrash(): LastCrash | null {
|
|
139
|
+
return useSyncExternalStore(
|
|
140
|
+
(listener) => {
|
|
141
|
+
listeners.add(listener);
|
|
142
|
+
return () => listeners.delete(listener);
|
|
143
|
+
},
|
|
144
|
+
() => last,
|
|
145
|
+
() => last,
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** The text Copy puts on the clipboard. */
|
|
150
|
+
export function crashDetails(crash: LastCrash, appName: string): string {
|
|
151
|
+
return [`${appName} ${crash.version}, ${new Date(crash.at).toISOString()}`, `${crash.name}: ${crash.message}`, crash.stack].join("\n");
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
type GlobalHandler = (error: Error, isFatal?: boolean) => void;
|
|
155
|
+
type ErrorUtilsShape = { getGlobalHandler: () => GlobalHandler; setGlobalHandler: (handler: GlobalHandler) => void };
|
|
156
|
+
type Tracker = (options: { allRejections: boolean; onUnhandled: (id: number, error: Error) => void }) => void;
|
|
157
|
+
|
|
158
|
+
function errorUtilsOf(env: object): ErrorUtilsShape | null {
|
|
159
|
+
const value = (env as { ErrorUtils?: Partial<ErrorUtilsShape> }).ErrorUtils;
|
|
160
|
+
return typeof value?.getGlobalHandler === "function" && typeof value.setGlobalHandler === "function" ? (value as ErrorUtilsShape) : null;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function trackerOf(env: object): Tracker | null {
|
|
164
|
+
const tracker = (env as { HermesInternal?: { enablePromiseRejectionTracker?: unknown } }).HermesInternal?.enablePromiseRejectionTracker;
|
|
165
|
+
return typeof tracker === "function" ? (tracker as Tracker) : null;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Logs name, message and a short stack, so release builds show it in logcat. Never throws. */
|
|
169
|
+
function logUncaught(kind: "unhandled-rejection" | "uncaught-error", caught: Caught, isFatal?: boolean) {
|
|
170
|
+
try {
|
|
171
|
+
const report = fatalReport(caught);
|
|
172
|
+
console.error("[uncaught]", {
|
|
173
|
+
kind,
|
|
174
|
+
isFatal,
|
|
175
|
+
errorName: report.name,
|
|
176
|
+
errorMessage: report.message.slice(0, 200),
|
|
177
|
+
stack: report.stack.split("\n").slice(0, 6).join("\n"),
|
|
178
|
+
});
|
|
179
|
+
} catch {
|
|
180
|
+
// A logger must never throw.
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
let installed = false;
|
|
185
|
+
|
|
186
|
+
export type CrashCaptureOptions = {
|
|
187
|
+
/** Default: `expo-secure-store` when it is installed. Without storage nothing is saved. */
|
|
188
|
+
storage?: CrashStorage | null;
|
|
189
|
+
/** Default: `expo-constants`' `expoConfig.version`. */
|
|
190
|
+
version?: string;
|
|
191
|
+
/** The global object. Tests pass a fake. */
|
|
192
|
+
env?: object;
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Call once at the top of the root layout, outside any component. It:
|
|
197
|
+
* - logs unhandled promise rejections and uncaught errors to `console.error`,
|
|
198
|
+
* which release builds otherwise swallow;
|
|
199
|
+
* - saves a fatal error before the app closes (waiting at most one second);
|
|
200
|
+
* - loads the crash the last session saved, for `LastCrashDialog`.
|
|
201
|
+
* Installs once; later calls return `false`. Never throws.
|
|
202
|
+
*/
|
|
203
|
+
export function installCrashCapture(options: CrashCaptureOptions = {}): boolean {
|
|
204
|
+
if (installed) return false;
|
|
205
|
+
try {
|
|
206
|
+
installed = true;
|
|
207
|
+
settings = {
|
|
208
|
+
storage: options.storage === undefined ? defaultStorage() : options.storage,
|
|
209
|
+
version: options.version ?? defaultVersion(),
|
|
210
|
+
};
|
|
211
|
+
const env = options.env ?? globalThis;
|
|
212
|
+
trackerOf(env)?.({ allRejections: true, onUnhandled: (_id, error) => logUncaught("unhandled-rejection", error) });
|
|
213
|
+
const errorUtils = errorUtilsOf(env);
|
|
214
|
+
if (errorUtils) {
|
|
215
|
+
const previous = errorUtils.getGlobalHandler();
|
|
216
|
+
errorUtils.setGlobalHandler((error, isFatal) => {
|
|
217
|
+
logUncaught("uncaught-error", error, isFatal);
|
|
218
|
+
if (!isFatal) {
|
|
219
|
+
previous(error, isFatal);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
// Save first, then let the app close as before.
|
|
223
|
+
const timeout = new Promise<void>((resolve) => setTimeout(resolve, FATAL_SAVE_MS));
|
|
224
|
+
void Promise.race([saveLastCrash(fatalReport(error)), timeout]).finally(() => previous(error, isFatal));
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
void loadLastCrash();
|
|
228
|
+
return true;
|
|
229
|
+
} catch {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** For tests only. */
|
|
235
|
+
export function resetCrashCaptureForTests() {
|
|
236
|
+
installed = false;
|
|
237
|
+
settings = null;
|
|
238
|
+
publish(null);
|
|
239
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// `@vvedantb/native-ui/app-root`: the Instrument Sans font helper, the splash hide
|
|
2
|
+
// helper, `AuthGroupGate` and `SsoCallbackScreen` for Expo app shells. Apps keep their
|
|
3
|
+
// own provider trees. Not on the root entry: it imports `@clerk/expo`, `expo-router`,
|
|
4
|
+
// `expo-font`, `expo-splash-screen` and `@expo-google-fonts/instrument-sans`, and Metro
|
|
5
|
+
// does not tree-shake.
|
|
6
|
+
export * from "../app-root";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// `@vvedantb/native-ui/crash`: shows the error that closed the app last time.
|
|
2
|
+
// Call `installCrashCapture()` once at the top of the root layout and mount
|
|
3
|
+
// `<LastCrashDialog appName="…" />` inside it.
|
|
4
|
+
export { LastCrashDialog } from "../crash/LastCrashDialog";
|
|
5
|
+
export type { LastCrashDialogProps } from "../crash/LastCrashDialog";
|
|
6
|
+
export {
|
|
7
|
+
crashDetails,
|
|
8
|
+
dismissLastCrash,
|
|
9
|
+
fatalReport,
|
|
10
|
+
installCrashCapture,
|
|
11
|
+
loadLastCrash,
|
|
12
|
+
saveLastCrash,
|
|
13
|
+
showLastCrash,
|
|
14
|
+
useLastCrash,
|
|
15
|
+
} from "../crash/lastCrash";
|
|
16
|
+
export type { CrashCaptureOptions, CrashStorage, FatalReport, LastCrash } from "../crash/lastCrash";
|
package/src/entries/theme.ts
CHANGED
|
@@ -12,6 +12,12 @@ export type { NativeUiTheme } from "../theme";
|
|
|
12
12
|
export { palette, withAlpha } from "../palette";
|
|
13
13
|
export type { Palette } from "../palette";
|
|
14
14
|
export { configureTheme } from "../configureTheme";
|
|
15
|
+
export { NEUTRAL_PALETTE, NEUTRAL_CHANNELS, paintDarkFamily } from "../neutralTheme";
|
|
16
|
+
export type { PaintDarkFamilyOverrides } from "../neutralTheme";
|
|
17
|
+
export { THEME_MODES, parseThemeMode } from "../themeModes";
|
|
18
|
+
export type { ThemeMode } from "../themeModes";
|
|
19
|
+
export { createThemeModeStore } from "../themeModeStore";
|
|
20
|
+
export type { ThemeModeStorage, ThemeModeStore, ThemeModeStoreOptions } from "../themeModeStore";
|
|
15
21
|
export type { ThemeConfig, ThemeColors, ThemePalette } from "../configureTheme";
|
|
16
22
|
export { fontClass, fontFamily } from "../typography";
|
|
17
23
|
export type { ThemeFonts } from "../typography";
|
package/src/highlight.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -11,6 +11,11 @@ export type { NativeUiTheme } from "./theme";
|
|
|
11
11
|
export { palette, withAlpha } from "./palette";
|
|
12
12
|
export type { Palette } from "./palette";
|
|
13
13
|
export { configureTheme } from "./configureTheme";
|
|
14
|
+
export { NEUTRAL_PALETTE, NEUTRAL_CHANNELS, paintDarkFamily } from "./neutralTheme";
|
|
15
|
+
export type { PaintDarkFamilyOverrides } from "./neutralTheme";
|
|
16
|
+
export { parseThemeMode } from "./themeModes";
|
|
17
|
+
export { createThemeModeStore } from "./themeModeStore";
|
|
18
|
+
export type { ThemeModeStorage, ThemeModeStore, ThemeModeStoreOptions } from "./themeModeStore";
|
|
14
19
|
export type { ThemeConfig, ThemeColors, ThemePalette } from "./configureTheme";
|
|
15
20
|
export { fontClass, fontFamily } from "./typography";
|
|
16
21
|
export type { ThemeFonts } from "./typography";
|