Skip to content

Commit

Permalink
survey nearly done, needs wiring
Browse files Browse the repository at this point in the history
  • Loading branch information
factoidforrest committed Feb 6, 2023
1 parent 9377514 commit 980d76f
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 53 deletions.
88 changes: 86 additions & 2 deletions lunatrace/bsl/frontend/src/api/generated.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
query GetCurrentUserInfo($kratos_id: uuid!) {
users(where: {kratos_id: {_eq: $kratos_id}}) {
role
survey
}
}
37 changes: 13 additions & 24 deletions lunatrace/bsl/frontend/src/contexts/UserContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,29 @@
* limitations under the License.
*
*/
import { skipToken } from '@reduxjs/toolkit/query/react';
import React, { useEffect } from 'react';

import { api, GetCurrentUserInfoQuery } from '../api/generated';
import useAppSelector from '../hooks/useAppSelector';
import { selectKratosId } from '../store/slices/authentication';
import { User } from '../types/user';

interface UserProviderState {
isAdmin: boolean;
user: User | null;
}

const initialState = {
isAdmin: false,
user: null,
};

const UserContext = React.createContext<UserProviderState>(initialState);

function userIsAdmin(data: GetCurrentUserInfoQuery | undefined): boolean {
function getUserInfo(data: GetCurrentUserInfoQuery | undefined): UserProviderState {
if (!data || data.users.length === 0) {
return false;
return initialState;
}

const users = data.users;
Expand All @@ -41,35 +45,20 @@ function userIsAdmin(data: GetCurrentUserInfoQuery | undefined): boolean {
const user = users[0];
const userRole = user.role;

return userRole === 'lunatrace_admin';
return {
user,
isAdmin: userRole === 'lunatrace_admin',
};
}

function UserProvider({ children }: { children: React.ReactNode }) {
const userId = useAppSelector(selectKratosId);

const [trigger, result] = api.useLazyGetCurrentUserInfoQuery();
const { data } = api.useGetCurrentUserInfoQuery(userId ? { kratos_id: userId } : skipToken);

useEffect(() => {
if (userId) {
void trigger({
kratos_id: userId,
});
}
}, [userId]);
const userInfo = getUserInfo(data);

const { data } = result;

const isAdmin = userIsAdmin(data);

return (
<UserContext.Provider
value={{
isAdmin,
}}
>
{children}
</UserContext.Provider>
);
return <UserContext.Provider value={userInfo}>{children}</UserContext.Provider>;
}

export { UserProvider, UserContext };
71 changes: 44 additions & 27 deletions lunatrace/bsl/frontend/src/pages/homepage/AuthenticatedHome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,66 @@
*
*/
import React, { useContext } from 'react';
import { Col, Container, Row } from 'react-bootstrap';
import { Col, Container, Row, Spinner } from 'react-bootstrap';
import { Helmet } from 'react-helmet-async';

import { ConditionallyRender } from '../../components/utils/ConditionallyRender';
import { LunaTraceIntroVideo } from '../../components/LunaTraceIntroVideo';
import { ConditionallyRender } from '../../components/utils/ConditionallyRender';
import { UserContext } from '../../contexts/UserContext';
import { WizardOpenContext } from '../../contexts/WizardContext';

import { NewCustomerSurvey } from './NewCustomerSurvey';
import { RecentGuidesCard } from './cards/RecentGuides';
import { RecentProjectsCard } from './cards/RecentProjects';
import { SetupWizard } from './cards/SetupWizard';

export const AuthenticatedHome: React.FunctionComponent = (_props) => {
const wizardOpen = useContext(WizardOpenContext);

const { user } = useContext(UserContext);

if (!user) {
return <Spinner animation="border" />;
}

const surveyNotComplete = !user.survey;

return (
<>
<Helmet title="Home" />
<Container>
<div className="text-center mb-4">
<h1>Welcome to LunaTrace</h1>

{wizardOpen ? (
<p>
{surveyNotComplete ? (
<NewCustomerSurvey user={user} />
) : (
<>
{' '}
<div className="text-center mb-4">
<h1>Welcome to LunaTrace</h1>
{wizardOpen ? (
<p>
{' '}
You are almost on your way to finding vulnerabilities with LunaTrace! Follow the steps below to finish
setting up your account.
</p>
) : (
<p>View your existing projects in the sidebar.</p>
)}
<LunaTraceIntroVideo />
</div>
<Row>
{' '}
You are almost on your way to finding vulnerabilities with LunaTrace! Follow the steps below to finish
setting up your account.
</p>
) : (
<p>View your existing projects in the sidebar.</p>
)}
<LunaTraceIntroVideo />
</div>
<Row>
{' '}
<ConditionallyRender if={!wizardOpen}>
<Col lg={7}>
<RecentProjectsCard />
<RecentGuidesCard />
</Col>
</ConditionallyRender>
<Col lg={wizardOpen ? '12' : '5'}>
<SetupWizard compact={!wizardOpen} />
</Col>
</Row>
<ConditionallyRender if={!wizardOpen}>
<Col lg={7}>
<RecentProjectsCard />
<RecentGuidesCard />
</Col>
</ConditionallyRender>
<Col lg={wizardOpen ? '12' : '5'}>
<SetupWizard compact={!wizardOpen} />
</Col>
</Row>
</>
)}
</Container>
</>
);
Expand Down
Loading

0 comments on commit 980d76f

Please sign in to comment.