Skip to content

Commit eea56a3

Browse files
authored
Merge pull request #393 from shapehq/bugfix/delays-error-messages-until-data-refreshed-mpabarca
Fix: Delays error messages until data is refreshed
2 parents ef70276 + 14d9711 commit eea56a3

File tree

4 files changed

+43
-35
lines changed

4 files changed

+43
-35
lines changed

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

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,33 @@ import { Box, Stack } from "@mui/material"
44
import { useTheme } from "@mui/material/styles"
55
import TrailingToolbarItem from "@/features/projects/view/toolbar/TrailingToolbarItem"
66
import MobileToolbar from "@/features/projects/view/toolbar/MobileToolbar"
7-
import { useProjectSelection } from "@/features/projects/data"
8-
import NotFound from "@/features/projects/view/NotFound"
9-
import dynamic from "next/dynamic"
107
import SecondaryHeaderPlaceholder from "@/features/sidebar/view/SecondarySplitHeaderPlaceholder"
11-
12-
const SecondarySplitHeader = dynamic(() => import("@/features/sidebar/view/SecondarySplitHeader"),
13-
{
14-
loading: () => <SecondaryHeaderPlaceholder />,
15-
ssr: false,
16-
}
17-
)
8+
import { useContext } from "react"
9+
import { ProjectsContext } from "@/common"
10+
import LoadingIndicator from "@/common/ui/LoadingIndicator"
11+
import SecondarySplitHeader from "@/features/sidebar/view/SecondarySplitHeader"
1812

1913
export default function Page({ children }: { children: React.ReactNode }) {
20-
const { project } = useProjectSelection()
14+
const { refreshed } = useContext(ProjectsContext)
15+
2116
const theme = useTheme()
17+
2218
return (
2319
<Stack sx={{ height: "100%" }}>
24-
{!project &&
25-
<>
26-
<SecondarySplitHeader>
27-
<TrailingToolbarItem/>
28-
</SecondarySplitHeader>
29-
<main style={{ flexGrow: "1", overflowY: "auto" }}>
30-
<NotFound />
31-
</main>
32-
</>
33-
}
34-
{project &&
3520
<>
36-
<SecondarySplitHeader mobileToolbar={<MobileToolbar/>}>
37-
<TrailingToolbarItem/>
38-
</SecondarySplitHeader>
21+
{!refreshed ? <SecondaryHeaderPlaceholder/> :
22+
<SecondarySplitHeader mobileToolbar={<MobileToolbar/>}>
23+
<TrailingToolbarItem/>
24+
</SecondarySplitHeader>
25+
}
3926
<Box sx={{ height: "0.5px", background: theme.palette.divider }} />
40-
<main style={{ flexGrow: "1", overflowY: "auto" }}>
41-
{children}
42-
</main>
27+
{refreshed ?
28+
<main style={{ flexGrow: "1", overflowY: "auto" }}>
29+
{children}
30+
</main> :
31+
<LoadingIndicator />
32+
}
4333
</>
44-
}
4534
</Stack>
4635
)
4736
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ 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"
8+
import NotFound from "@/features/projects/view/NotFound"
89

910
export default function Page() {
1011
const { project, version, specification, navigateToSelectionIfNeeded } = useProjectSelection()
@@ -23,17 +24,16 @@ export default function Page() {
2324
specification
2425
})
2526
}, [project, version, specification])
27+
2628
return (
2729
<>
2830
{project && version && specification &&
2931
<Documentation url={specification.url} />
3032
}
31-
{project && !version &&
32-
<ErrorMessage text="The selected branch or tag was not found."/>
33-
}
34-
{project && !specification &&
35-
<ErrorMessage text="The selected specification was not found."/>
33+
{project && (!version || !specification) &&
34+
<ErrorMessage text={`The selected ${!version ? "branch or tag" : "specification"} was not found.`}/>
3635
}
36+
{!project && <NotFound/>}
3737
</>
3838
)
3939
}

src/common/context/ProjectsContext.ts

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

88
type ProjectsContextValue = {
9+
refreshed: boolean,
910
projects: Project[],
1011
setProjects: (projects: Project[]) => void
1112
}
1213

1314
export const ProjectsContext = createContext<ProjectsContextValue>({
15+
refreshed: false,
1416
projects: [],
1517
setProjects: () => {}
1618
})

src/features/projects/view/ProjectsContextProvider.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,26 @@ const ProjectsContextProvider = ({
1111
initialProjects?: Project[],
1212
children?: React.ReactNode
1313
}) => {
14+
const [refreshed, setRefreshed] = useState<boolean>(false)
1415
const [projects, setProjects] = useState<Project[]>(initialProjects || [])
16+
17+
const hasProjectChanged = (value: Project[]) => value.some((project, index) => {
18+
// Compare by project id and version (or any other key fields)
19+
return project.id !== projects[index]?.id || project.versions !== projects[index]?.versions
20+
})
21+
22+
const setProjectsAndRefreshed = (value: Project[]) => {
23+
setProjects(value)
24+
// If any project has changed, update the state and mark as refreshed
25+
if (hasProjectChanged(value)) setRefreshed(true)
26+
27+
}
1528
return (
16-
<ProjectsContext.Provider value={{ projects, setProjects }}>
29+
<ProjectsContext.Provider value={{
30+
refreshed,
31+
projects,
32+
setProjects: setProjectsAndRefreshed
33+
}}>
1734
{children}
1835
</ProjectsContext.Provider>
1936
)

0 commit comments

Comments
 (0)