From 4b77e26ef7f63aa0d4f2a1ce8be7675d27eac63f Mon Sep 17 00:00:00 2001 From: Luke McFarlane Date: Wed, 11 Dec 2024 16:09:20 +1100 Subject: [PATCH 1/2] Updated context provider to store projects locally Signed-off-by: Luke McFarlane --- app/src/context/projects-context.tsx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/src/context/projects-context.tsx b/app/src/context/projects-context.tsx index a6826f777..b16adc7ab 100644 --- a/app/src/context/projects-context.tsx +++ b/app/src/context/projects-context.tsx @@ -3,6 +3,7 @@ import { activateProjectDB, getProjectsDB, setSyncProjectDB, + updateProjectsDB, } from '../dbs/projects-db'; import {activate_project} from '../sync/process-initialization'; import {ProjectExtended} from '../types/project'; @@ -70,7 +71,10 @@ export function ProjectsProvider({children}: {children: ReactNode}) { }); } - setProjects([...newProjectsMap.values()]); + const newProjects = [...newProjectsMap.values()]; + + updateProjectsDB(newProjects); + setProjects(newProjects); }; /** @@ -96,7 +100,10 @@ export function ProjectsProvider({children}: {children: ReactNode}) { }); } - setProjects([...newProjectsMap.values()]); + const newProjects = [...newProjectsMap.values()]; + + updateProjectsDB(newProjects); + setProjects(newProjects); }; /** From 1ddbab2652e2b0e4681df738507f7419d4f4f48a Mon Sep 17 00:00:00 2001 From: Steve Cassidy Date: Thu, 12 Dec 2024 09:42:33 +1300 Subject: [PATCH 2/2] don't crash when offline and fetching projects Signed-off-by: Steve Cassidy --- app/src/context/functions.tsx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/src/context/functions.tsx b/app/src/context/functions.tsx index 77fdb08e5..bb65d3645 100644 --- a/app/src/context/functions.tsx +++ b/app/src/context/functions.tsx @@ -86,18 +86,19 @@ export const getAnyToken = async (): Promise => { * @returns {Promise} - A promise that resolves to an array of ProjectObject. */ const getProjects = async (url: string, token: string) => { + // fetch the projects but guard against being offline and this failing const response = await fetch(`${url}/api/directory`, { headers: { Authorization: `Bearer ${token}`, }, - }); + }).catch(() => null); - if (!response.ok) { + if (response && response.ok) { + return (await response.json()) as ProjectObject[]; + } else { console.error(`Error fetching projects from ${url}`); return [] as ProjectObject[]; } - - return (await response.json()) as ProjectObject[]; }; /**