Skip to content

Commit ed7fa81

Browse files
author
mpabarca
committed
Add refreshed state to ProjectsContext to show updated data of projects and branches
1 parent c8134bf commit ed7fa81

File tree

4 files changed

+36
-29
lines changed

4 files changed

+36
-29
lines changed
Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
11
"use client"
22

3-
import { useContext } from "react"
43
import { Box, Stack } from "@mui/material"
54
import { useTheme } from "@mui/material/styles"
65
import TrailingToolbarItem from "@/features/projects/view/toolbar/TrailingToolbarItem"
76
import MobileToolbar from "@/features/projects/view/toolbar/MobileToolbar"
87
import { useProjectSelection } from "@/features/projects/data"
9-
import { ProjectsContext } from "@/common"
108
import SecondaryHeaderPlaceholder from "@/features/sidebar/view/SecondarySplitHeaderPlaceholder"
119
import SecondarySplitHeader from "@/features/sidebar/view/SecondarySplitHeader"
10+
import { useContext } from "react"
11+
import { ProjectsContext } from "@/common"
12+
import LoadingIndicator from "@/common/ui/LoadingIndicator"
1213

1314
export default function Page({ children }: { children: React.ReactNode }) {
14-
const { cached } = useContext(ProjectsContext)
1515
const { project } = useProjectSelection()
16+
const { refreshed } = useContext(ProjectsContext)
17+
1618
const theme = useTheme()
19+
1720
return (
1821
<Stack sx={{ height: "100%" }}>
19-
<>
20-
{project &&
21-
<SecondarySplitHeader mobileToolbar={<MobileToolbar/>}>
22-
<TrailingToolbarItem/>
23-
</SecondarySplitHeader>
24-
}
25-
{!project && cached && <SecondaryHeaderPlaceholder/>}
26-
<Box sx={{ height: "0.5px", background: theme.palette.divider }} />
27-
<main style={{ flexGrow: "1", overflowY: "auto" }}>
28-
{children}
29-
</main>
30-
</>
22+
{refreshed ?
23+
<>
24+
{project &&
25+
<SecondarySplitHeader mobileToolbar={<MobileToolbar/>}>
26+
<TrailingToolbarItem/>
27+
</SecondarySplitHeader>
28+
}
29+
{!project && <SecondaryHeaderPlaceholder/>}
30+
<Box sx={{ height: "0.5px", background: theme.palette.divider }} />
31+
<main style={{ flexGrow: "1", overflowY: "auto" }}>
32+
{children}
33+
</main>
34+
</>
35+
: <LoadingIndicator />
36+
}
3137
</Stack>
3238
)
3339
}

src/app/(authed)/(project-doc)/[...slug]/page.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
"use client"
22

3-
import { useEffect, useContext } from "react"
3+
import { useEffect } from "react"
44
import ErrorMessage from "@/common/ui/ErrorMessage"
55
import { updateWindowTitle } from "@/features/projects/domain"
66
import { useProjectSelection } from "@/features/projects/data"
77
import Documentation from "@/features/projects/view/Documentation"
88
import NotFound from "@/features/projects/view/NotFound"
9-
import { ProjectsContext } from "@/common"
109

1110
export default function Page() {
12-
const { cached } = useContext(ProjectsContext)
1311
const { project, version, specification, navigateToSelectionIfNeeded } = useProjectSelection()
1412
// Ensure the URL reflects the current selection of project, version, and specification.
1513
useEffect(() => {
@@ -26,18 +24,19 @@ export default function Page() {
2624
specification
2725
})
2826
}, [project, version, specification])
27+
2928
return (
3029
<>
3130
{project && version && specification &&
3231
<Documentation url={specification.url} />
3332
}
34-
{project && !version && !cached &&
33+
{project && !version &&
3534
<ErrorMessage text="The selected branch or tag was not found."/>
3635
}
37-
{project && !specification && !cached &&
36+
{project && !specification &&
3837
<ErrorMessage text="The selected specification was not found."/>
3938
}
40-
{!project && !cached && <NotFound/>}
39+
{!project && <NotFound/>}
4140
</>
4241
)
4342
}

src/common/context/ProjectsContext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import { Project } from "@/features/projects/domain"
66
export const SidebarTogglableContext = createContext<boolean>(true)
77

88
type ProjectsContextValue = {
9-
cached: boolean,
9+
refreshed: boolean,
1010
projects: Project[],
1111
setProjects: (projects: Project[]) => void
1212
}
1313

1414
export const ProjectsContext = createContext<ProjectsContextValue>({
15-
cached: true,
15+
refreshed: false,
1616
projects: [],
1717
setProjects: () => {}
1818
})

src/features/projects/view/ProjectsContextProvider.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@ const ProjectsContextProvider = ({
1111
initialProjects?: Project[],
1212
children?: React.ReactNode
1313
}) => {
14-
const [cached, setCached] = useState<boolean>(true)
14+
const [refreshed, setRefreshed] = useState<boolean>(false)
1515
const [projects, setProjects] = useState<Project[]>(initialProjects || [])
16-
const setProjectsAndCached = (projects: Project[]) => {
17-
setProjects(projects)
18-
setCached(false)
16+
17+
const setProjectsAndRefreshed = (value: Project[]) => {
18+
setProjects(value)
19+
// Only set refreshed to true after projects are updated
20+
if (JSON.stringify(projects) !== JSON.stringify(value)) setRefreshed(true)
1921
}
2022
return (
2123
<ProjectsContext.Provider value={{
22-
cached,
24+
refreshed,
2325
projects,
24-
setProjects: setProjectsAndCached
26+
setProjects: setProjectsAndRefreshed
2527
}}>
2628
{children}
2729
</ProjectsContext.Provider>

0 commit comments

Comments
 (0)