Skip to content

Commit

Permalink
Fixed issues recommended by coderabbitai
Browse files Browse the repository at this point in the history
  • Loading branch information
tlesicka committed Oct 25, 2024
1 parent b99d314 commit 0b226b8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,13 @@ export function LoadBackupModal({
isLoading={loading === 'backup'}
onPress={async () => {
setLoading('backup');
await dispatch(makeBackup());
setLoading(null);
try {
await dispatch(makeBackup());
} catch (error) {
console.error('Failed to create backup:', error);
} finally {
setLoading(null);
}
}}
>
<Trans>Backup now</Trans>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ export function DuplicateFileModal({
if (trimmedName.length > 100) {
return t('Budget name is too long (max length 100)');
}
if (!/^[a-zA-Z0-9 .\-_()]+$/.test(trimmedName)) {
return t('Budget name contains invalid characters');
}
// Additional validation checks can go here

return null;
Expand All @@ -75,22 +78,29 @@ export function DuplicateFileModal({
if (!error) {
setLoadingState(sync === 'cloudSync' ? 'cloud' : 'local');

await dispatch(
duplicateBudget({
id: 'id' in file ? file.id : undefined,
cloudId:
sync === 'cloudSync' && 'cloudFileId' in file
? file.cloudFileId
: undefined,
oldName: file.name,
newName,
cloudSync: sync === 'cloudSync',
managePage,
loadBudget,
}),
);

setLoadingState(null);
try {
await dispatch(
duplicateBudget({
id: 'id' in file ? file.id : undefined,
cloudId:
sync === 'cloudSync' && 'cloudFileId' in file
? file.cloudFileId
: undefined,
oldName: file.name,
newName,
cloudSync: sync === 'cloudSync',
managePage,
loadBudget,
}),
);
} catch (e) {
const newError = new Error('Failed to duplicate budget');

Check warning on line 97 in packages/desktop-client/src/components/modals/manager/DuplicateFileModal.tsx

View workflow job for this annotation

GitHub Actions / lint

Delete `·`
if (onComplete) onComplete({ status: 'failed', error: newError });
else console.error('Failed to duplicate budget:', e);
} finally {
setLoadingState(null);
}

if (onComplete) onComplete({ status: 'success' });
} else {
const failError = new Error(error);
Expand Down
17 changes: 13 additions & 4 deletions packages/loot-core/src/server/backups.web.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @ts-strict-ignore
import { type Database } from '@jlongster/sql.js';
import * as dateFns from 'date-fns';

import * as connection from '../platform/server/connection';
Expand Down Expand Up @@ -116,10 +117,18 @@ export async function makeBackup(id: string) {
);

// Remove all the messages from the backup
const db = await sqlite.openDatabase(fs.join(budgetDir, backupId));
await sqlite.runQuery(db, 'DELETE FROM messages_crdt');
await sqlite.runQuery(db, 'DELETE FROM messages_clock');
sqlite.closeDatabase(db);
let db: Database;
try {
db = await sqlite.openDatabase(fs.join(budgetDir, backupId));
await sqlite.runQuery(db, 'DELETE FROM messages_crdt');
await sqlite.runQuery(db, 'DELETE FROM messages_clock');
} catch (error) {
console.error('Error cleaning up backup messages:', error);
} finally {
if (db) {
sqlite.closeDatabase(db);
}
}

const toRemove = await updateBackups(await getBackups(id));
for (const id of toRemove) {
Expand Down
6 changes: 6 additions & 0 deletions packages/loot-core/src/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1820,6 +1820,12 @@ handlers['duplicate-budget'] = async function ({
open,
}): Promise<string> {
if (!id) throw new Error('Unable to duplicate a budget that is not local.');
if (!newName?.trim()) {
throw new Error('Budget name is required and cannot be empty');
}
if (!/^[a-zA-Z0-9 .\-_()]+$/.test(newName)) {
throw new Error('Budget name contains invalid characters');
}

const budgetDir = fs.getBudgetDir(id);

Expand Down

0 comments on commit 0b226b8

Please sign in to comment.