Skip to content

Commit

Permalink
#105 changed data routers matching only for allowed services.
Browse files Browse the repository at this point in the history
  • Loading branch information
artzub committed Mar 12, 2023
1 parent cbfc5e6 commit 0d86578
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useMemo, useState } from 'react';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useNavigate } from 'react-router-dom';

import styled from 'styled-components';
Expand Down Expand Up @@ -65,6 +65,13 @@ const ServiceSelector = () => {
const navigate = useNavigate();
const [value, setValue] = useState(currentService || '');

useEffect(
() => {
setValue(currentService || '');
},
[currentService],
);

const services = useMemo(
() => {
servicesMock.forEach((service) => {
Expand Down
4 changes: 3 additions & 1 deletion src/components/Header/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import branchesSlice from '@/redux/modules/branches';
import profilesSlice from '@/redux/modules/profiles';
import repositoriesSlice from '@/redux/modules/repositories';

import { useUIProperty } from '@/shared/hooks';
import { useRouteMatches, useUIProperty } from '@/shared/hooks';

import BranchStepBody from './components/BranchStep/Body';
import BranchStepHeader from './components/BranchStep/Header';
Expand Down Expand Up @@ -77,6 +77,7 @@ const StepBodies = {
};

const Header = () => {
const { service } = useRouteMatches();
const [step, setStep] = useUIProperty('step');
const [bodyOpen, setBodyOpen] = useUIProperty('bodyOpen');
const ref = useRef();
Expand Down Expand Up @@ -150,6 +151,7 @@ const Header = () => {
<Container>
<ProfileStepHeader
onClick={onClick(StageTypes.profile)}
disabled={!service}
divider
ref={profileRef}
/>
Expand Down
5 changes: 1 addition & 4 deletions src/components/Layout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ const Layout = () => (
path="about"
element={<div />}
/>
<Route
path="*"
element={<StageController />}
/>
</Routes>
<StageController />
<Visualization />
<Progress />
<Header />
Expand Down
23 changes: 16 additions & 7 deletions src/shared/hooks/useRouteMatches.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useRef } from 'react';
import { useMatch } from 'react-router-dom';

const servicePath = '/:service';
Expand All @@ -6,18 +7,26 @@ const repositoryPath = `${profilePath}/:repository`;
const branchPath = `${repositoryPath}/:branch`;
const commitsPath = `${branchPath}/:commits`;

const allowedServices = ['github', 'gitlab', 'bitbucket'];

export const useRouteMatches = () => {
const { params: { service } = {} } = useMatch({ path: servicePath, end: false }) || {};
const { params: { profile } = {} } = useMatch({ path: profilePath, end: false }) || {};
const { params: { repository } = {} } = useMatch({ path: repositoryPath, end: false }) || {};
const { params: { branch } = {} } = useMatch({ path: branchPath, end: false }) || {};
const { params: { commits } = {} } = useMatch({ path: commitsPath, end: false }) || {};

return {
service,
profile,
repository,
branch,
commits,
};
const refs = useRef({});

if (!service || allowedServices.includes(service)) {
refs.current = {
service,
profile,
repository,
branch,
commits,
};
}

return refs.current;
};

0 comments on commit 0d86578

Please sign in to comment.