Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix project activation bug #1229

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/src/context/functions.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
active_db,
directory_db_pouch,
ExistingActiveDoc,
JWTTokenInfo,
local_auth_db,
} from '../sync/databases';
Expand Down Expand Up @@ -146,3 +148,24 @@ export const getRemoteProjects = async () => {
*/
export const getProjectMap = (projects: ProjectExtended[]) =>
new Map(projects.map(project => [project._id, project]));

/**
* Retrieves the locally active projects from the active database.
*/
export const getLocalActiveMap = async () => {
const locallyActive = await active_db.allDocs({include_docs: true});
const localActiveMap = new Map<
string,
{sync: boolean; sync_attachments: boolean}
>();
if (locallyActive.rows.length > 0) {
locallyActive.rows.forEach(record => {
const project = record.doc as ExistingActiveDoc;
localActiveMap.set(project.project_id, {
sync: project.is_sync,
sync_attachments: project.is_sync_attachments,
});
});
}
return localActiveMap;
};
20 changes: 15 additions & 5 deletions app/src/context/projects-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '../dbs/projects-db';
import {activate_project} from '../sync/process-initialization';
import {ProjectExtended} from '../types/project';
import {getProjectMap, getRemoteProjects} from './functions';
import {getLocalActiveMap, getProjectMap, getRemoteProjects} from './functions';

export const ProjectsContext = createContext<{
projects: ProjectExtended[];
Expand Down Expand Up @@ -50,13 +50,23 @@ export function ProjectsProvider({children}: {children: ReactNode}) {
const localProjectsMap = getProjectMap(localProjects);
const newProjectsMap = getProjectMap(localProjects);

const localActiveMap = await getLocalActiveMap();

for (const remoteProject of remoteProjects) {
const activated =
localProjectsMap.get(remoteProject._id)?.activated ?? false;
localProjectsMap.get(remoteProject._id)?.activated ??
localActiveMap.has(remoteProject._id) ??
false;

const sync =
localProjectsMap.get(remoteProject._id)?.sync ??
localActiveMap.get(remoteProject._id)?.sync ??
false;

newProjectsMap.set(remoteProject._id, {
...remoteProject,
activated,
sync,
});
}

Expand All @@ -77,12 +87,12 @@ export function ProjectsProvider({children}: {children: ReactNode}) {
const projectsMap = getProjectMap(projects);
const newProjectsMap = getProjectMap(projects);

// update project list, preserve activated and sync states from existing list
for (const remoteProject of remoteProjects) {
const activated = projectsMap.get(remoteProject._id)?.activated ?? false;

newProjectsMap.set(remoteProject._id, {
...remoteProject,
activated,
activated: projectsMap.get(remoteProject._id)?.activated ?? false,
sync: projectsMap.get(remoteProject._id)?.sync ?? false,
});
}

Expand Down
13 changes: 11 additions & 2 deletions app/src/gui/components/workspace/notebooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {grey} from '@mui/material/colors';
import {useTheme} from '@mui/material/styles';
import useMediaQuery from '@mui/material/useMediaQuery';
import {GridColDef} from '@mui/x-data-grid';
import {useContext, useState} from 'react';
import {useContext, useEffect, useState} from 'react';
import {useNavigate} from 'react-router-dom';
import {
NOTEBOOK_LIST_TYPE,
Expand All @@ -49,7 +49,16 @@ export default function NoteBooks() {

const activatedProjects = projects.filter(({activated}) => activated);

const [tabID, setTabID] = useState(activatedProjects.length > 0 ? '1' : '2');
const [tabID, setTabID] = useState('1');

useEffect(() => {
// If there are activated projects, set the tab to the first 'Activated' tab
if (activatedProjects.length > 0) {
setTabID('1');
} else {
setTabID('2');
}
}, [activatedProjects]);

const history = useNavigate();

Expand Down
7 changes: 0 additions & 7 deletions app/src/sync/process-initialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,8 @@ export async function activate_project(
}
const active_id = resolve_project_id(listing_id, project_id);
if (await project_is_active(active_id)) {
console.debug('Have already activated', active_id);
return active_id;
} else {
console.debug('%cActivating', 'background-color: pink;', active_id);
const active_doc = {
_id: active_id,
listing_id: listing_id,
Expand All @@ -316,11 +314,6 @@ export async function activate_project(
active_doc.listing_id,
project_id
);
console.log(
'%cProject Object',
'background-color: pink;',
project_object
);
if (project_object)
await ensure_project_databases(
{...active_doc, _rev: response.rev},
Expand Down
Loading