Skip to content

Commit

Permalink
mobile: fix path handling for crash recovery url
Browse files Browse the repository at this point in the history
  • Loading branch information
latter-bolden committed Mar 13, 2024
1 parent c4d2233 commit b96002a
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions apps/tlon-mobile/src/components/SingletonWebview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
type MobileNavTab,
type NativeWebViewOptions,
type WebAppAction,
trimFullPath,
} from '@tloncorp/shared';
import * as Clipboard from 'expo-clipboard';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
Expand Down Expand Up @@ -34,7 +35,7 @@ type WebAppCommand = {
interface CrashState {
isCrashed: boolean;
crashEvent: WebViewTerminatedEvent | WebViewRenderProcessGoneEvent | null;
lastUrl: string;
lastPath: string;
}

// used for forcing the webview to reload
Expand Down Expand Up @@ -62,7 +63,7 @@ export const SingletonWebview = () => {
const [crashRecovery, setCrashRecovery] = useState<CrashState>({
isCrashed: false,
crashEvent: null,
lastUrl: '',
lastPath: '',
});

const [source, setSource] = useState<SourceState>({
Expand All @@ -74,10 +75,13 @@ export const SingletonWebview = () => {
useEffect(() => {
if (appStatus === 'active' && crashRecovery.isCrashed) {
setSource({
url: crashRecovery.lastUrl,
url: createUri(shipUrl, crashRecovery.lastPath),
key: source.key + 1,
});
setCrashRecovery((prev) => ({ ...prev, isCrashed: false }));
setCrashRecovery((prev) => ({
...prev,
isCrashed: false,
}));

// TODO: for debugging purposes, log the crash recovery. Remove before
// shipping to prod.
Expand All @@ -97,7 +101,7 @@ export const SingletonWebview = () => {
);
}, 2000);
}
}, [appStatus, crashRecovery, source]);
}, [appStatus, crashRecovery, shipUrl, source]);

const handleLogout = useCallback(() => {
clearShip();
Expand Down Expand Up @@ -268,8 +272,9 @@ export const SingletonWebview = () => {
}));
}}
// store a reference to the last url the webview was at for crash recovery
onNavigationStateChange={({ url }) => {
setCrashRecovery((prev) => ({ ...prev, lastUrl: url }));
onNavigationStateChange={({ url: rawUrl }) => {
const path = new URL(rawUrl).pathname;
setCrashRecovery((prev) => ({ ...prev, lastPath: trimFullPath(path) }));
}}
/>
);
Expand Down

0 comments on commit b96002a

Please sign in to comment.