Skip to content

Commit

Permalink
fix: resolve migration status sync (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulyadav-57 authored Sep 23, 2024
1 parent c05e978 commit 144af9d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/components/project/MigrateToUnifiedFS/MigrateToUnifiedFS.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Project,
Tree,
} from '@/interfaces/workspace.interface';
import EventEmitter from '@/utility/eventEmitter';
import { Button, ConfigProvider, message, Modal, Popconfirm } from 'antd';
import { FC, useEffect, useState } from 'react';
import { IndexedDBHelper } from './IndexedDBHelper';
Expand All @@ -29,15 +30,17 @@ const MigrateToUnifiedFS: FC<Props> = ({ hasDescription = false }) => {
const [isMigrationView, setIsMigrationView] = useState(false);
const fileSystem = new IndexedDBHelper('NujanFiles', 'files', 10);
const [projects, setProjects] = useState<IProject[] | null>(null);
const [migrationStatus, setMigrationStatus] = useState('pending');
const [migrationStatus, setMigrationStatus] = useState<
'pending' | 'migrating' | 'failed' | 'completed' | 'done'
>('pending');
const { createProject } = useProject();
const { createLog } = useLogActivity();
const note = `We've recently upgraded the IDE, and some of your projects may not be visible.`;

const checkMigration = async () => {
const filesInDB = (await fileSystem.getAllFiles()) as DBFile[];
const localStorageItems = localStorage.getItem('recoil-persist');
if (localStorageItems) {
const filesInDB = (await fileSystem.getAllFiles()) as DBFile[];
const parsedItems = JSON.parse(localStorageItems);
const existingProjects = parsedItems?.['workspaceState']?.[
'projects'
Expand Down Expand Up @@ -102,6 +105,7 @@ const MigrateToUnifiedFS: FC<Props> = ({ hasDescription = false }) => {
'success',
);
setMigrationStatus('completed');
EventEmitter.emit('PROJECT_MIGRATED');
} catch (error) {
createLog('Failed to migrate project', 'error');
setMigrationStatus('failed');
Expand All @@ -125,6 +129,12 @@ const MigrateToUnifiedFS: FC<Props> = ({ hasDescription = false }) => {
}
};

const onProjectMigrated = () => {
if (migrationStatus !== 'pending') return;
setMigrationStatus('completed');
setIsMigrationView(true);
};

useEffect(() => {
const migrationStatus = localStorage.getItem('migrationStatus');
if (migrationStatus === 'completed') {
Expand All @@ -135,6 +145,10 @@ const MigrateToUnifiedFS: FC<Props> = ({ hasDescription = false }) => {
} catch (error) {
/* empty */
}
EventEmitter.on('PROJECT_MIGRATED', onProjectMigrated);
return () => {
EventEmitter.off('PROJECT_MIGRATED', onProjectMigrated);
};
}, []);

if (projects === null || migrationStatus === 'done') return null;
Expand Down
1 change: 1 addition & 0 deletions src/utility/eventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface EventEmitterPayloads {
TEST_CASE_LOG: string;
RELOAD_PROJECT_FILES: string;
OPEN_PROJECT: string;
PROJECT_MIGRATED: undefined;
}

const EventEmitter = {
Expand Down

0 comments on commit 144af9d

Please sign in to comment.