-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[209] Simplify the project views by sharing common code in the router
Bug: #209 Signed-off-by: Stéphane Bégaudeau <[email protected]>
- Loading branch information
1 parent
d648c12
commit 480cea7
Showing
25 changed files
with
381 additions
and
389 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
frontend/svalyn-studio-app/src/projects/ProjectShell.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* Copyright (c) 2023 Stéphane Bégaudeau. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
* associated documentation files (the "Software"), to deal in the Software without restriction, | ||
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial | ||
* portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT | ||
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
import { gql, useQuery } from '@apollo/client'; | ||
import CorporateFareIcon from '@mui/icons-material/CorporateFare'; | ||
import Box from '@mui/material/Box'; | ||
import Link from '@mui/material/Link'; | ||
import { useEffect, useState } from 'react'; | ||
import { Link as RouterLink, useParams } from 'react-router-dom'; | ||
import { Navbar } from '../navbars/Navbar'; | ||
import { NotFoundView } from '../notfound/NotFoundView'; | ||
import { goToDomains, goToHelp, goToHome, goToNotifications, goToSettings } from '../palette/DefaultPaletteActions'; | ||
import { PaletteNavigationAction } from '../palette/Palette.types'; | ||
import { usePalette } from '../palette/usePalette'; | ||
import { ErrorSnackbar } from '../snackbar/ErrorSnackbar'; | ||
import { ProjectDrawer } from './ProjectDrawer'; | ||
import { GetProjectData, GetProjectVariables, ProjectShellProps, ProjectShellState } from './ProjectShell.types'; | ||
import { ProjectContext } from './useProject'; | ||
|
||
const getProjectQuery = gql` | ||
query getProject($identifier: ID!) { | ||
viewer { | ||
project(identifier: $identifier) { | ||
identifier | ||
name | ||
description | ||
organization { | ||
identifier | ||
name | ||
role | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
export const ProjectShell = ({ children }: ProjectShellProps) => { | ||
const [state, setState] = useState<ProjectShellState>({ | ||
errorSnackbarOpen: false, | ||
}); | ||
|
||
const { projectIdentifier } = useParams(); | ||
const variables: GetProjectVariables = { identifier: projectIdentifier ?? '' }; | ||
const { data, error } = useQuery<GetProjectData, GetProjectVariables>(getProjectQuery, { variables }); | ||
|
||
const { setActions } = usePalette(); | ||
|
||
useEffect(() => { | ||
if (data) { | ||
const { | ||
viewer: { project }, | ||
} = data; | ||
if (project) { | ||
setState((prevState) => ({ ...prevState, project })); | ||
|
||
const backToOrganization: PaletteNavigationAction = { | ||
type: 'navigation-action', | ||
id: 'go-to-organization', | ||
icon: <CorporateFareIcon fontSize="small" />, | ||
label: project.organization.name, | ||
to: `/orgs/${project.organization.identifier}`, | ||
}; | ||
setActions([goToHome, backToOrganization, goToDomains, goToNotifications, goToSettings, goToHelp]); | ||
} | ||
} | ||
setState((prevState) => ({ ...prevState, errorSnackbarOpen: !!error })); | ||
}, [data, error]); | ||
|
||
const handleCloseSnackbar = () => setState((prevState) => ({ ...prevState, message: null })); | ||
|
||
if (!data) { | ||
return null; | ||
} | ||
|
||
if (!data.viewer.project) { | ||
return <NotFoundView />; | ||
} | ||
|
||
return ( | ||
<> | ||
<Box sx={{ minHeight: '100vh', display: 'flex', flexDirection: 'column' }}> | ||
<Navbar> | ||
<Link component={RouterLink} to="/domains" color="inherit" underline="hover" fontWeight={800}> | ||
Domains | ||
</Link> | ||
</Navbar> | ||
<Box | ||
sx={{ | ||
display: 'grid', | ||
gridTemplateRows: '1fr', | ||
gridTemplateColumns: 'min-content 1fr', | ||
flexGrow: '1', | ||
}} | ||
> | ||
<ProjectContext.Provider value={{ project: data.viewer.project }}> | ||
<ProjectDrawer /> | ||
{children} | ||
</ProjectContext.Provider> | ||
</Box> | ||
</Box> | ||
<ErrorSnackbar open={state.errorSnackbarOpen} message={error?.message ?? null} onClose={handleCloseSnackbar} /> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.