Skip to content

Commit

Permalink
fix invalid time format due to invalid strings
Browse files Browse the repository at this point in the history
  • Loading branch information
walmat committed Jan 11, 2025
1 parent d312658 commit 2a0839d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
19 changes: 11 additions & 8 deletions src/components/backup/ChooseBackupStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { sharedCoolModalTopOffset } from '@/navigation/config';
import { ImgixImage } from '@/components/images';
import MenuContainer from '@/screens/SettingsSheet/components/MenuContainer';
import Menu from '@/screens/SettingsSheet/components/Menu';
import { format } from 'date-fns';
import MenuItem from '@/screens/SettingsSheet/components/MenuItem';
import Routes from '@/navigation/routesNames';
import { BackupFile, parseTimestampFromFilename } from '@/model/backup';
Expand All @@ -23,7 +22,7 @@ import Spinner from '@/components/Spinner';
import ActivityIndicator from '@/components/ActivityIndicator';
import { useTheme } from '@/theme';
import { backupsStore, CloudBackupState, LoadingStates } from '@/state/backups/backups';
import { titleForBackupState } from '@/screens/SettingsSheet/utils';
import { dateFormatter, titleForBackupState } from '@/screens/SettingsSheet/utils';

const Title = styled(RNText).attrs({
align: 'left',
Expand Down Expand Up @@ -148,7 +147,7 @@ export function ChooseBackupStep() {
<Box>
<Menu
description={lang.t(lang.l.back_up.cloud.latest_backup, {
date: format(new Date(mostRecentBackup.lastModified), "M/d/yy 'at' h:mm a"),
date: dateFormatter(mostRecentBackup.lastModified),
})}
>
<MenuItem
Expand All @@ -169,9 +168,13 @@ export function ChooseBackupStep() {
{backups.files
.filter(backup => backup.name !== mostRecentBackup?.name)
.sort((a, b) => {
const timestampA = new Date(parseTimestampFromFilename(a.name)).getTime();
const timestampB = new Date(parseTimestampFromFilename(b.name)).getTime();
return timestampB - timestampA;
try {
const timestampA = new Date(parseTimestampFromFilename(a.name)).getTime();
const timestampB = new Date(parseTimestampFromFilename(b.name)).getTime();
return timestampB - timestampA;
} catch (error) {
return 0;
}
})
.map(backup => (
<MenuItem
Expand All @@ -183,8 +186,8 @@ export function ChooseBackupStep() {
<MenuItem.Title
isLink
text={lang.t(lang.l.back_up.cloud.older_backups_title, {
date: format(parseTimestampFromFilename(backup.name), 'M/d/yy'),
time: format(parseTimestampFromFilename(backup.name), 'p'),
date: dateFormatter(parseTimestampFromFilename(backup.name), 'M/d/yy'),
time: dateFormatter(parseTimestampFromFilename(backup.name), 'p'),
})}
/>
}
Expand Down
27 changes: 17 additions & 10 deletions src/screens/SettingsSheet/components/Backups/ViewCloudBackups.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ import Menu from '../Menu';
import MenuContainer from '../MenuContainer';
import MenuItem from '../MenuItem';
import { BackupFile, parseTimestampFromFilename } from '@/model/backup';
import { format } from 'date-fns';
import { useNavigation } from '@/navigation';
import Routes from '@/navigation/routesNames';
import walletBackupStepTypes from '@/helpers/walletBackupStepTypes';
import { Page } from '@/components/layout';
import Spinner from '@/components/Spinner';
import ActivityIndicator from '@/components/ActivityIndicator';
import { useTheme } from '@/theme';
import { ThemeContextProps, useTheme } from '@/theme';
import { CloudBackupState, LoadingStates, backupsStore } from '@/state/backups/backups';
import { titleForBackupState } from '../../utils';
import { titleForBackupState, dateFormatter } from '@/screens/SettingsSheet/utils';
import { Box } from '@/design-system';

const LoadingText = styled(RNText).attrs(({ theme: { colors } }: any) => ({
type LoadingTextProps = {
theme: ThemeContextProps;
};

const LoadingText = styled(RNText).attrs(({ theme: { colors } }: LoadingTextProps) => ({
color: colors.blueGreyDark,
lineHeight: ios ? 'none' : 24,
size: 'large',
Expand Down Expand Up @@ -64,7 +67,7 @@ const ViewCloudBackups = () => {
<Box>
<Menu
description={i18n.t(i18n.l.back_up.cloud.latest_backup, {
date: format(new Date(mostRecentBackup.lastModified), "M/d/yy 'at' h:mm a"),
date: dateFormatter(mostRecentBackup.lastModified),
})}
>
<MenuItem
Expand All @@ -87,9 +90,13 @@ const ViewCloudBackups = () => {
{backups.files
.filter(backup => backup.name !== mostRecentBackup?.name)
.sort((a, b) => {
const timestampA = new Date(parseTimestampFromFilename(a.name)).getTime();
const timestampB = new Date(parseTimestampFromFilename(b.name)).getTime();
return timestampB - timestampA;
try {
const timestampA = new Date(parseTimestampFromFilename(a.name)).getTime();
const timestampB = new Date(parseTimestampFromFilename(b.name)).getTime();
return timestampB - timestampA;
} catch (error) {
return 0;
}
})
.map(backup => (
<MenuItem
Expand All @@ -101,8 +108,8 @@ const ViewCloudBackups = () => {
<MenuItem.Title
isLink
text={i18n.t(i18n.l.back_up.cloud.older_backups_title, {
date: format(parseTimestampFromFilename(backup.name), 'M/d/yy'),
time: format(parseTimestampFromFilename(backup.name), 'p'),
date: dateFormatter(parseTimestampFromFilename(backup.name), 'M/d/yy'),
time: dateFormatter(parseTimestampFromFilename(backup.name), 'p'),
})}
/>
}
Expand Down
9 changes: 9 additions & 0 deletions src/screens/SettingsSheet/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { backupsStore, CloudBackupState } from '@/state/backups/backups';
import { RainbowWallet } from '@/model/wallet';
import { IS_ANDROID, IS_IOS } from '@/env';
import { normalizeAndroidBackupFilename } from '@/handlers/cloudBackup';
import { format } from 'date-fns';

type WalletBackupStatus = {
allBackedUp: boolean;
Expand Down Expand Up @@ -119,3 +120,11 @@ export const isWalletBackedUpForCurrentAccount = ({ backupType, backedUp, backup
const backupFiles = backupsStore.getState().backups;
return backupFiles.files.some(file => normalizeAndroidBackupFilename(file.name) === normalizeAndroidBackupFilename(backupFile));
};

export const dateFormatter = (date: string | number, formatString = "M/d/yy 'at' h:mm a") => {
try {
return format(new Date(date), formatString);
} catch (error) {
return 'Unknown Date';
}
};

0 comments on commit 2a0839d

Please sign in to comment.