Skip to content

Commit

Permalink
feat: added folder operations
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanVicens committed Feb 29, 2024
1 parent ab3816e commit 7785431
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/apps/main/preload.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ declare interface Window {
): () => void;

onSyncInfoUpdate(
func: (value: import('../shared/types').DriveInfo) => void
func: (value: import('../shared/types').DriveOperationInfo) => void
): () => void;

userIsUnauthorized(): void;
Expand Down
10 changes: 7 additions & 3 deletions src/apps/main/windows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getSettingsWindow } from './settings';
import { getFeedbackWindow } from './feedback';
import { getWidget } from './widget';
import { openVirtualDriveRootFolder } from '../virtual-root-folder/service';
import { VirtualDriveIssue } from '../../../shared/issues/VirtualDriveIssue';
import { DriveOperationInfo } from '../../shared/types';

function closeAuxWindows() {
getProcessIssuesWindow()?.close();
Expand All @@ -33,11 +33,15 @@ export function broadcastToWindows(eventName: string, data: any) {
renderers.forEach((r) => r?.webContents.send(eventName, data));
}

export function notifyIssueToUser({ error, name }: VirtualDriveIssue) {
export function virtualDriveUpdate(info: DriveOperationInfo) {
const windows = [getWidget(), getProcessIssuesWindow()];

windows.forEach((window) =>
window?.webContents.send('sync-info-update', { action: error, name })
window?.webContents.send('sync-info-update', {
action: info.action,
name: info.name,
oldName: info.oldName,
})
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/apps/renderer/hooks/useDriveInfoHistory.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useEffect, useState } from 'react';
import { DriveInfo } from '../../shared/types';
import { DriveOperationInfo } from '../../shared/types';
import throttle from 'lodash/throttle';

export function useDriveInfoHistory() {
const [driveHistory, setDriveHistory] = useState<DriveInfo[]>([]);
const [driveHistory, setDriveHistory] = useState<DriveOperationInfo[]>([]);

const addItemToHistoryDebounced = throttle((item: DriveInfo) => {
const addItemToHistoryDebounced = throttle((item: DriveOperationInfo) => {
const MAX_ITEMS = 50;

setDriveHistory((prevList) => {
Expand All @@ -21,7 +21,7 @@ export function useDriveInfoHistory() {
});
}, 1000);

function addItemToHistory(item: DriveInfo) {
function addItemToHistory(item: DriveOperationInfo) {
addItemToHistoryDebounced(item);
}

Expand Down
4 changes: 2 additions & 2 deletions src/apps/renderer/pages/Widget/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Check } from '@phosphor-icons/react';
import { CircularProgressbar, buildStyles } from 'react-circular-progressbar';
import { useTranslationContext } from '../../context/LocalContext';
import { getBaseName, getExtension } from '../../utils/path';
import { DriveInfo } from '../../../shared/types';
import { DriveOperationInfo } from '../../../shared/types';
import { fileIcon } from '../../assets/icons/getIcon';

export function Item({ name, action, progress }: DriveInfo) {
export function Item({ name, action, progress }: DriveOperationInfo) {
const { translate } = useTranslationContext();
const progressDisplay = progress ? `${Math.ceil(progress * 100)}%` : '';

Expand Down
34 changes: 29 additions & 5 deletions src/apps/shared/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
type DriveOperationInProgress = {
action: 'UPLOADING' | 'DOWNLOADING' | 'RENAMING' | 'DELETING';
const actionsInProgress = ['UPLOADING', 'DOWNLOADING'] as const;

type ActionWithProgress = (typeof actionsInProgress)[number];

type DriveOperationWithProgress = {
action: ActionWithProgress;
progress: number;
oldName: string;
name: string;
};
type DriveOperationCompleted = {
action: 'UPLOADED' | 'DOWNLOADED' | 'RENAMED' | 'DELETED';

const actions = [
'RENAMING',
'DELETING',
'UPLOADED',
'DOWNLOADED',
'RENAMED',
'DELETED',
'RENAMING_FOLDER',
'CREATING_FOLDER',
'FOLDER_RENAMED',
'FOLDER_CREATED',
] as const;

type Action = (typeof actions)[number];

type DriveOperation = {
action: Action;
name: string;
oldName: string | undefined;
progress: undefined; // Needed so ts does not complain with the union type
};

export type DriveInfo = DriveOperationInProgress | DriveOperationCompleted;
export type DriveOperationInfo = DriveOperationWithProgress | DriveOperation;

export type DriveAction = Action | ActionWithProgress;
Original file line number Diff line number Diff line change
@@ -1,31 +1,60 @@
import { trackVirtualDriveError } from '../../../../../apps/main/analytics/service';
import { addVirtualDriveIssue } from '../../../../../apps/main/issues/virtual-drive';
import { setTrayStatus } from '../../../../../apps/main/tray/tray';
import { notifyIssueToUser } from '../../../../../apps/main/windows';
import { virtualDriveUpdate } from '../../../../../apps/main/windows';
import { VirtualDriveFolderIssue } from '../../../../../shared/issues/VirtualDriveIssue';
import { SyncFolderMessenger } from '../../domain/SyncFolderMessenger';

export class MainProcessSyncFolderMessenger implements SyncFolderMessenger {
async rename(_name: string, _newName: string): Promise<void> {
async rename(name: string, newName: string): Promise<void> {
setTrayStatus('SYNCING');

virtualDriveUpdate({
action: 'RENAMING_FOLDER',
oldName: name,
name: newName,
progress: undefined,
});
}

async renamed(_name: string, _newName: string): Promise<void> {
async renamed(name: string, newName: string): Promise<void> {
setTrayStatus('IDLE');

virtualDriveUpdate({
action: 'FOLDER_RENAMED',
oldName: name,
name: newName,
progress: undefined,
});
}

async creating(_name: string): Promise<void> {
async creating(name: string): Promise<void> {
setTrayStatus('SYNCING');

virtualDriveUpdate({
action: 'CREATING_FOLDER',
oldName: undefined,
name: name,
progress: undefined,
});
}

async created(_name: string): Promise<void> {
async created(name: string): Promise<void> {
setTrayStatus('IDLE');

virtualDriveUpdate({
action: 'FOLDER_CREATED',
oldName: undefined,
name: name,
progress: undefined,
});
}

async error(issue: VirtualDriveFolderIssue): Promise<void> {
setTrayStatus('ALERT');

trackVirtualDriveError(issue);

notifyIssueToUser(issue);
addVirtualDriveIssue(issue);
}
}

0 comments on commit 7785431

Please sign in to comment.