Skip to content

Commit

Permalink
added link to universe tab from fleet movements
Browse files Browse the repository at this point in the history
  • Loading branch information
ametel01 committed Jan 29, 2024
1 parent 285b7af commit 027cd61
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 20 deletions.
1 change: 0 additions & 1 deletion packages/frontend/src/colony/ColonySelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ const ColonySelect = ({ planetId, selectedColonyId, handleChange }: Props) => {
const menuItems = Array.isArray(coloniesArray)
? coloniesArray.map((colony, index) => {
const colonyId = colony[0];
console.log('colony', colony);
return (
<MenuItem key={index} value={colonyId.toString()}>
colony {colonyId.toString()} - {Number(colony[1].system)}/
Expand Down
7 changes: 5 additions & 2 deletions packages/frontend/src/components/auth/AuthController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import AuthScreen from '../../views/LoginOrGenerate';
import Dashboard from '../../views/DashBoard';
import { useAccount } from '@starknet-react/core';
import Header from '../ui/Header';
import { DestinationProvider } from '../../context/DestinationContext';

const AuthController = () => {
const { address } = useAccount();
Expand Down Expand Up @@ -41,8 +42,10 @@ const AuthController = () => {
/>
) : (
<>
<Header planetId={Number(planetId)} />
<Dashboard planetId={Number(planetId)} />
<DestinationProvider>
<Header planetId={Number(planetId)} />
<Dashboard planetId={Number(planetId)} />
</DestinationProvider>
</>
);
};
Expand Down
12 changes: 11 additions & 1 deletion packages/frontend/src/components/ui/MissionRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { usePlanetPosition } from '../../hooks/usePlanetPosition';
import fleetIcon from '../../assets/uiIcons/Fleet.svg';
import { StyledButton } from '../../shared/styled/Button';
import { useDestination } from '../../context/DestinationContext';

const FleetTooltipContent = styled('div')({
display: 'flex',
Expand Down Expand Up @@ -74,6 +75,13 @@ export const MissionRow = memo(
});
}, [recallFleet]);

const { handleDestinationClick } = useDestination();

// Updated to handle the click event
const onClickDestination = () => {
handleDestinationClick(mission.destination);
};

const renderFleetDetails = () => (
<FleetTooltipContent>
<div>Carrier: {Number(mission.fleet.carrier)}</div>
Expand All @@ -99,7 +107,9 @@ export const MissionRow = memo(
</Tooltip>
</MissionText>
<MissionText>{origin}</MissionText>
<MissionText>{destination}</MissionText>
<MissionText onClick={onClickDestination} style={{ cursor: 'pointer' }}>
{destination}
</MissionText>
<MissionText>
{mission.category == MissionCategory.Debris
? 'Debris'
Expand Down
35 changes: 35 additions & 0 deletions packages/frontend/src/context/DestinationContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React, { createContext, useContext, useState, ReactNode } from 'react';

// Create a context
type DestinationContextType = {
selectedDestination: number | null;
handleDestinationClick: (destinationId: number) => void;
};

// Create a context with a default value
const DestinationContext = createContext<DestinationContextType>({
selectedDestination: null,
handleDestinationClick: () => {},
});

// Create a provider component
export const DestinationProvider = ({ children }: { children: ReactNode }) => {
const [selectedDestination, setSelectedDestination] = useState<number | null>(
null
);

const handleDestinationClick = (destinationId: number) => {
setSelectedDestination(destinationId);
};

return (
<DestinationContext.Provider
value={{ selectedDestination, handleDestinationClick }}
>
{children}
</DestinationContext.Provider>
);
};

// Hook for easy access to the context
export const useDestination = () => useContext(DestinationContext);
11 changes: 11 additions & 0 deletions packages/frontend/src/panels/MainTabPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ import {
ResourcesSectionArgs,
} from '.';

import { useEffect } from 'react';
import { useDestination } from '../context/DestinationContext';

export const ResourcesSection = ({
planetId,
colonyId,
Expand All @@ -51,6 +54,14 @@ export const ResourcesSection = ({
const celestiaAvailable = useGetCelestiaAvailable(planetId);
const defencesCost = getBaseDefenceCost();

const { selectedDestination } = useDestination();

useEffect(() => {
if (selectedDestination !== null) {
setActiveTab(5); // Set to Universe tab
}
}, [selectedDestination]);

if (
!compoundsLevels ||
!techLevels ||
Expand Down
44 changes: 28 additions & 16 deletions packages/frontend/src/panels/UniverseViewTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
useGetColonyMotherPlanet,
useGetColonyShips,
} from '../hooks/ColoniesHooks';
// import { Switch, Typography } from '@mui/material';
import { useDestination } from '../context/DestinationContext';

const UniverseBoxItem = ({
ownPlanetId,
Expand Down Expand Up @@ -111,7 +111,9 @@ export const UniverseViewTabPanel = ({
const [planetsData, setPlanetsData] = useState<PlanetDetails[]>([]);
const [currentPage, setCurrentPage] = useState(1);
const itemsPerPage = 6;
const pageCount = Math.ceil(planetsData.length / itemsPerPage); // Adjust as needed
const pageCount = Math.ceil(planetsData.length / itemsPerPage);

const { selectedDestination } = useDestination();

useEffect(() => {
fetchPlanetsData()
Expand All @@ -123,23 +125,34 @@ export const UniverseViewTabPanel = ({
return Number(a.position.system) - Number(b.position.system);
});

// Find the index of the planet with the ownPlanetId
const ownPlanetIndex = sortedData.findIndex((planet) =>
colonyId === 0
? planet.planetId === ownPlanetId
: planet.planetId === ownPlanetId * 1000 + colonyId
);
// Calculate the initial page based on the index
const initialPage = Math.ceil((ownPlanetIndex + 1) / itemsPerPage);

setPlanetsData(sortedData);
// Set the initial page
setCurrentPage(initialPage);

if (selectedDestination !== null) {
const destinationIndex = sortedData.findIndex(
(planet) => planet.planetId === Number(selectedDestination)
);
if (destinationIndex !== -1) {
// Check if index is valid
const destinationPage = Math.ceil(
(destinationIndex + 1) / itemsPerPage
);
setCurrentPage(destinationPage);
}
} else {
// Default page setting logic
const ownPlanetIndex = sortedData.findIndex((planet) =>
colonyId === 0
? planet.planetId === ownPlanetId
: planet.planetId === ownPlanetId * 1000 + colonyId
);
const initialPage = Math.ceil((ownPlanetIndex + 1) / itemsPerPage);
setCurrentPage(initialPage);
}
})
.catch((error) => {
console.error('Error fetching planets data:', error);
});
}, [colonyId, ownPlanetId]);
}, [colonyId, ownPlanetId, selectedDestination]);

const startIndex = (currentPage - 1) * itemsPerPage;
const selectedPlanets = planetsData.slice(
Expand Down Expand Up @@ -178,12 +191,11 @@ export const UniverseViewTabPanel = ({
variant="outlined"
shape="rounded"
sx={{
// ...
'.MuiPaginationItem-root.MuiPaginationItem-root': {
color: 'white',
},
'.MuiPaginationItem-root.Mui-selected': {
backgroundColor: 'rgba(211, 211, 211, 0.5)', // Lighter gray background for the selected page
backgroundColor: 'rgba(211, 211, 211, 0.5)',
},
}}
/>
Expand Down

0 comments on commit 027cd61

Please sign in to comment.