Skip to content

Commit

Permalink
try to deal with offline project listings - not working
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Cassidy <steve.cassidy@mq.edu.au>
  • Loading branch information
stevecassidy committed Dec 10, 2024
1 parent d8c9fe0 commit 5a40203
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/src/context/functions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ const getProjects = async (url: string, token: string) => {
headers: {
Authorization: `Bearer ${token}`,
},
}).catch(() => {
return null;
});

if (!response.ok) {
if (response === null || (response && !response.ok)) {
console.error(`Error fetching projects from ${url}`);
return [] as ProjectObject[];
}
Expand Down
36 changes: 36 additions & 0 deletions app/src/context/projects-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
import {activate_project} from '../sync/process-initialization';
import {ProjectExtended} from '../types/project';
import {getLocalActiveMap, getProjectMap, getRemoteProjects} from './functions';
import {getAvailableProjectsFromListing} from '../sync/projects';
import {getAllListingIDs, getListing} from '../sync/state';

export const ProjectsContext = createContext<{
projects: ProjectExtended[];
Expand All @@ -30,6 +32,7 @@ export const ProjectsContext = createContext<{
export function ProjectsProvider({children}: {children: ReactNode}) {
const [projects, setProjects] = useState<ProjectExtended[]>([]);

console.debug('projectsProvider', projects);
useEffect(() => {
initProjects();
}, []);
Expand All @@ -52,6 +55,36 @@ export function ProjectsProvider({children}: {children: ReactNode}) {

const localActiveMap = await getLocalActiveMap();

// if we're offline, remote projects is empty so just
// return the local ones we know already

if (remoteProjects.length === 0) {
const allStoredProjects: ProjectExtended[] = [];
getAllListingIDs().forEach(async (listing_id: string) => {
const storedProjects =
await getAvailableProjectsFromListing(listing_id);
console.debug('storedProjects', storedProjects);
// storedProjects is ProjectInformation[] and we need ProjectExtended[]
const listing = await getListing(listing_id);
allStoredProjects.concat(
storedProjects.map(project => {
return {
...project,
_id: project.project_id,
conductor_url: listing.listing.conductor_url,
listing: listing_id,
activated: localActiveMap.has(project.project_id),
sync: localProjectsMap.get(project.project_id)?.sync ?? false,
// do we also need the two database connections here???
};
})
);
});
console.debug('allStoredProjects', allStoredProjects);
setProjects(allStoredProjects);
return;
}

for (const remoteProject of remoteProjects) {
const activated =
localProjectsMap.get(remoteProject._id)?.activated ??
Expand All @@ -70,6 +103,7 @@ export function ProjectsProvider({children}: {children: ReactNode}) {
});
}

console.debug('setting projects', [...newProjectsMap.values()]);
setProjects([...newProjectsMap.values()]);
};

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

if (remoteProjects.length === 0) return;

// update project list, preserve activated and sync states from existing list
for (const remoteProject of remoteProjects) {
newProjectsMap.set(remoteProject._id, {
Expand Down
2 changes: 2 additions & 0 deletions app/src/dbs/projects-db.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const updateProjectsDB = async (projects: ProjectExtended[]) =>
* @returns A promise that resolves when the project is activated.
*/
export const activateProjectDB = async (_id: string) => {
console.debug('activateProjectDB', await db.info());
const doc = await db.get(_id);

await db.put({
Expand Down Expand Up @@ -73,6 +74,7 @@ export const setSyncProjectDB = async (_id: string, sync: boolean) => {
* @returns An array of local projects.
*/
export const getProjectsDB = async () => {
console.debug('getProjectsDB', await db.info());
const {rows} = await db.allDocs({include_docs: true});

return rows.map(row => row?.doc?.project).filter(x => x !== undefined);
Expand Down
2 changes: 2 additions & 0 deletions app/src/gui/components/workspace/notebooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export default function NoteBooks() {

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

console.debug('PPPPP', projects, activatedProjects);

const [tabID, setTabID] = useState('1');

const history = useNavigate();
Expand Down

0 comments on commit 5a40203

Please sign in to comment.