Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useInterpret, useSelector } from '@xstate/react';
import { dequal } from 'dequal';
import { Feature, Geometry } from 'geojson';
import { LatLngBounds, LatLngLiteral } from 'leaflet';
import React, { useCallback, useMemo } from 'react';
import { useHistory } from 'react-router-dom';
Expand All @@ -12,23 +13,15 @@
defaultPropertyFilter,
IPropertyFilter,
} from '@/features/properties/filter/IPropertyFilter';
import { ApiGen_Concepts_FileProperty } from '@/models/api/generated/ApiGen_Concepts_FileProperty';
import { PIMS_Property_Location_View } from '@/models/layers/pimsPropertyLocationView';
import { exists, firstOrNull, isValidString } from '@/utils';
import { pidParser, pinParser } from '@/utils/propertyUtils';

import { mapMachine } from './machineDefinition/mapMachine';
import { MachineContext, SideBarType } from './machineDefinition/types';
import {
LocationBoundaryDataset,
MapFeatureData,
MarkerSelected,
RequestedCenterTo,
RequestedFlyTo,
} from './models';
import useLocationFeatureLoader, {
LocationFeatureDataset,
SelectedFeatureDataset,
WorklistLocationFeatureDataset,
} from './useLocationFeatureLoader';
import { MapFeatureData, MarkerSelected, RequestedCenterTo, RequestedFlyTo } from './models';
import useLocationFeatureLoader, { LocationFeatureDataset } from './useLocationFeatureLoader';
import { useMapSearch } from './useMapSearch';

export interface IMapStateMachineContext {
Expand All @@ -42,22 +35,21 @@
mapMarkedLocation: LatLngLiteral | null;
mapLocationSelected: LatLngLiteral | null;
mapLocationFeatureDataset: LocationFeatureDataset | null;
selectedFeatures: SelectedFeatureDataset[];
repositioningFeatureDataset: SelectedFeatureDataset | null;
repositioningPropertyIndex: number | null;
locationFeaturesForAddition: LocationFeatureDataset[] | null;
repositioningFeature: Feature<Geometry, PIMS_Property_Location_View> | null;
pendingLocationFeaturesAddition: boolean;
// worklist-related state
worklistSelectedMapLocation: LatLngLiteral | null;
worklistLocationFeatureDataset: WorklistLocationFeatureDataset | null;
worklistLocationFeatureDataset: LocationFeatureDataset | null;
showPopup: boolean;
isLoading: boolean;
mapSearchCriteria: IPropertyFilter | null;
mapFeatureData: MapFeatureData;
filePropertyLocations: LocationBoundaryDataset[];
filePropertyLocations: ApiGen_Concepts_FileProperty[];
pendingFitBounds: boolean;
requestedFitBounds: LatLngBounds;
isSelecting: boolean;
isRepositioning: boolean;
selectingComponentId: string | null;
isFiltering: boolean;
isShowingMapFilter: boolean;
isShowingMapLayers: boolean;
Expand Down Expand Up @@ -94,19 +86,15 @@

// worklist
worklistMapClick: (latlng: LatLngLiteral) => void;
worklistAdd: (dataset: WorklistLocationFeatureDataset) => void;
worklistAdd: (dataset: LocationFeatureDataset) => void;

setMapSearchCriteria: (searchCriteria: IPropertyFilter) => void;
refreshMapProperties: () => void;
prepareForCreation: (selectedFeatures: SelectedFeatureDataset[]) => void;
processCreation: () => void;
startSelection: (selectingComponentId?: string) => void;
requestLocationFeatureAddition: (selectedFeatures: LocationFeatureDataset[]) => void;
processLocationFeaturesAddition: () => void;
startSelection: () => void;
finishSelection: () => void;
startReposition: (
repositioningFeatureDataset: SelectedFeatureDataset,
index: number,
selectingComponentId?: string,
) => void;
startReposition: (featureDataSet: Feature<Geometry, PIMS_Property_Location_View>) => void;
finishReposition: () => void;
toggleMapFilterDisplay: () => void;
toggleMapLayerControl: () => void;
Expand All @@ -116,7 +104,7 @@
openQuickInfo: () => void;
closeQuickInfo: () => void;
minimizeQuickInfo: () => void;
setFilePropertyLocations: (locations: LocationBoundaryDataset[]) => void;
setFilePropertyLocations: (locations: ApiGen_Concepts_FileProperty[]) => void;
setMapLayers: (layers: Set<string>) => void;
setMapLayersToRefresh: (layers: Set<string>) => void;
setDefaultMapLayers: (layers: Set<string>) => void;
Expand Down Expand Up @@ -217,7 +205,7 @@
loadWorklistLocationData: async (
context: MachineContext,
event: AnyEventObject & { type: 'WORKLIST_MAP_CLICK'; latlng: LatLngLiteral },
): Promise<WorklistLocationFeatureDataset> => {
): Promise<LocationFeatureDataset> => {
const response = locationLoader.loadWorklistLocationDetails({ latLng: event.latlng });
return response;
},
Expand Down Expand Up @@ -336,7 +324,7 @@
);

const worklistAdd = useCallback(
(dataset: WorklistLocationFeatureDataset) => {
(dataset: LocationFeatureDataset) => {
serviceSend({
type: 'WORKLIST_ADD',
dataset,
Expand Down Expand Up @@ -415,41 +403,32 @@
[serviceSend],
);

const prepareForCreation = useCallback(
(selectedFeatures: SelectedFeatureDataset[]) => {
serviceSend({ type: 'PREPARE_FOR_CREATION', selectedFeatures });
const requestLocationFeatureAddition = useCallback(
(selectedFeatures: LocationFeatureDataset[]) => {
serviceSend({ type: 'REQUEST_LOCATION_ADDITION', selectedFeatures });
},
[serviceSend],
);

const processCreation = useCallback(() => {
const processLocationFeaturesAddition = useCallback(() => {
serviceSend({
type: 'PROCESS_CREATION',
type: 'PROCESS_LOCATION_ADDITION',
});
}, [serviceSend]);

const startSelection = useCallback(
(selectingComponentId?: string) => {
serviceSend({ type: 'START_SELECTION', selectingComponentId });
},
[serviceSend],
);
const startSelection = useCallback(() => {
serviceSend({ type: 'START_SELECTION' });
}, [serviceSend]);

const finishSelection = useCallback(() => {
serviceSend({ type: 'FINISH_SELECTION' });
}, [serviceSend]);

const startReposition = useCallback(
(
repositioningFeatureDataset: SelectedFeatureDataset,
index: number,
selectingComponentId?: string,
) => {
(feature: Feature<Geometry, PIMS_Property_Location_View>) => {
serviceSend({
type: 'START_REPOSITION',
repositioningFeatureDataset,
repositioningPropertyIndex: index,
selectingComponentId,
feature,
});
},
[serviceSend],
Expand All @@ -460,7 +439,7 @@
}, [serviceSend]);

const setFilePropertyLocations = useCallback(
(locations: LocationBoundaryDataset[]) => {
(locations: ApiGen_Concepts_FileProperty[]) => {
serviceSend({ type: 'SET_FILE_PROPERTY_LOCATIONS', locations });
},
[serviceSend],
Expand Down Expand Up @@ -611,100 +590,101 @@

return (
<MapStateMachineContext.Provider
value={{
mapSideBarViewState: state.context.mapSideBarState,
isShowingSearchBar:
!state.context.mapSideBarState.isOpen &&
!(
isShowingMapFilter ||
!dequal(state.context.advancedSearchCriteria, new PropertyFilterFormModel())
),
pendingFlyTo: state.matches({ mapVisible: { mapRequest: 'pendingFlyTo' } }),
pendingCenterTo: state.matches({ mapVisible: { mapRequest: 'pendingCenterTo' } }),
requestedFlyTo: state.context.requestedFlyTo,
requestedCenterTo: state.context.requestedCenterTo,
mapMarkerSelected: state.context.mapFeatureSelected,
mapLocationSelected: state.context.mapLocationSelected,
mapMarkedLocation: state.context.mapMarkedLocation,
selectedFeatures: state.context.selectedFeatures,
locationFeaturesForAddition: state.context.locationFeaturesForAddition,
mapLocationFeatureDataset: state.context.mapLocationFeatureDataset,
repositioningFeatureDataset: state.context.repositioningFeatureDataset,
repositioningPropertyIndex: state.context.repositioningPropertyIndex,
worklistSelectedMapLocation: state.context.worklistSelectedMapLocation,
worklistLocationFeatureDataset: state.context.worklistLocationFeatureDataset,
showPopup: showPopup,
isLoading: state.context.isLoading,
mapSearchCriteria: state.context.searchCriteria,
advancedSearchCriteria: state.context.advancedSearchCriteria,
mapFeatureData: state.context.mapFeatureData,
filePropertyLocations: state.context.filePropertyLocations,
pendingFitBounds: state.matches({ mapVisible: { mapRequest: 'pendingFitBounds' } }),
requestedFitBounds: state.context.requestedFitBounds,
isSelecting: state.matches({ mapVisible: { featureView: 'selecting' } }),
isRepositioning: isRepositioning,
selectingComponentId: state.context.selectingComponentId,
isFiltering: !dequal(state.context.advancedSearchCriteria, new PropertyFilterFormModel()),
isShowingMapFilter: isShowingMapFilter,
isShowingMapLayers: isShowingMapLayers,
isShowingMapSearch: isShowingMapSearch,
isShowingWorkList: isShowingWorkList,
isShowingQuickInfo: isShowingQuickInfo,
isQuickInfoMinimized: isQuickInfoMinimized,
activeLayers: state.context.activeLayers,
activePimsPropertyIds: state.context.activePimsPropertyIds,
showDisposed: state.context.showDisposed,
showRetired: state.context.showRetired,
mapLayersToRefresh: state.context.mapLayersToRefresh,
isMapVisible: state.matches({ mapVisible: {} }),
currentMapBounds: state.context.currentMapBounds,
isEditPropertiesMode: state.context.isEditPropertiesMode,
pendingLocationFeaturesAddition: state.matches({
mapVisible: { locationFeatureAddition: 'pendingLocationFeatureAddition' },
}),
repositioningFeature: state.context.repositioningFeature,

setMapSearchCriteria,
refreshMapProperties,
processFlyTo,
processCenterTo,
processFitBounds,
openSidebar,
closeSidebar,
mapMarkLocation,
setSelectedLocation,
mapClearLocationMark,
requestFlyToLocation,
requestCenterToLocation,
requestFlyToBounds,
mapClick,
mapMarkerClick,
worklistMapClick,
worklistAdd,
closePopup,
prepareForCreation,
processCreation,
requestLocationFeatureAddition,
processLocationFeaturesAddition,
startSelection,
finishSelection,
startReposition,
finishReposition,
toggleMapFilterDisplay,
toggleMapLayerControl,
toggleMapSearchControl,
toggleWorkListControl,
showMapSearchControl,
openQuickInfo,
minimizeQuickInfo,
closeQuickInfo,
toggleSidebarDisplay,
setFilePropertyLocations,
setVisiblePimsProperties,
setShowDisposed,
setShowRetired,
setMapLayers,
setMapLayersToRefresh,
setDefaultMapLayers,
setFullWidthSideBar,
resetMapFilter,
setAdvancedSearchCriteria,
setCurrentMapBounds,
setEditPropertiesMode,
}}

Check warning on line 687 in source/frontend/src/components/common/mapFSM/MapStateMachineContext.tsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

The object passed as the value prop to the Context provider changes every render. To fix this consider wrapping it in a useMemo hook.

See more on https://sonarcloud.io/project/issues?id=bcgov_PSP&issues=AZsCnroscj_acyJj3MXI&open=AZsCnroscj_acyJj3MXI&pullRequest=5113
>
{children}
</MapStateMachineContext.Provider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { PropertyFilterFormModel } from '@/components/maps/leaflet/Control/Advan
import { pimsBoundaryLayers } from '@/components/maps/leaflet/Control/LayersControl/LayerDefinitions';
import { initialEnabledLayers } from '@/components/maps/leaflet/Control/LayersControl/LayersMenuLayout';
import { defaultPropertyFilter } from '@/features/properties/filter/IPropertyFilter';
import { ApiGen_Concepts_FileProperty } from '@/models/api/generated/ApiGen_Concepts_FileProperty';

import { emptyFeatureData, LocationBoundaryDataset } from '../models';
import { SelectedFeatureDataset } from '../useLocationFeatureLoader';
import { emptyFeatureData } from '../models';
import { MachineContext, SideBarType } from './types';

const featureViewStates = {
Expand All @@ -19,17 +19,12 @@ const featureViewStates = {
on: {
START_SELECTION: {
target: 'selecting',
actions: [
assign({ selectingComponentId: (_, event: any) => event.selectingComponentId }),
],
},
START_REPOSITION: {
target: 'repositioning',
actions: [
assign({
selectingComponentId: (_, event: any) => event.selectingComponentId,
repositioningFeatureDataset: (_, event: any) => event.repositioningFeatureDataset,
repositioningPropertyIndex: (_, event: any) => event.repositioningPropertyIndex,
repositioningFeature: (_, event: any) => event.feature,
}),
],
},
Expand All @@ -39,14 +34,13 @@ const featureViewStates = {
on: {
FINISH_SELECTION: {
target: 'browsing',
actions: [assign({ selectingComponentId: () => null })],
},
SET_FILE_PROPERTY_LOCATIONS: {
actions: [
assign({
filePropertyLocations: (
_,
event: AnyEventObject & { locations: LocationBoundaryDataset[] },
event: AnyEventObject & { locations: ApiGen_Concepts_FileProperty[] },
) => event.locations ?? [],
}),
],
Expand All @@ -59,9 +53,7 @@ const featureViewStates = {
target: 'browsing',
actions: [
assign({
repositioningFeatureDataset: () => null,
repositioningPropertyIndex: () => null,
selectingComponentId: () => null,
repositioningFeature: () => null,
}),
],
},
Expand All @@ -70,7 +62,7 @@ const featureViewStates = {
assign({
filePropertyLocations: (
_,
event: AnyEventObject & { locations: LocationBoundaryDataset[] },
event: AnyEventObject & { locations: ApiGen_Concepts_FileProperty[] },
) => event.locations ?? [],
}),
],
Expand Down Expand Up @@ -239,6 +231,32 @@ const mapRequestStates = {
},
};

const locationFeatureAdditionStates = {
initial: 'nothingPending',
states: {
nothingPending: {
on: {
REQUEST_LOCATION_ADDITION: {
actions: assign({
locationFeaturesForAddition: (_, event: any) => event.selectedFeatures,
}),
target: 'pendingLocationFeatureAddition',
},
},
},
pendingLocationFeatureAddition: {
on: {
PROCESS_LOCATION_ADDITION: {
actions: assign({
locationFeaturesForAddition: () => null,
}),
target: 'nothingPending',
},
},
},
},
};

const selectedFeatureLoaderStates = {
initial: 'idle',
states: {
Expand All @@ -249,7 +267,7 @@ const selectedFeatureLoaderStates = {
assign({
isLoading: () => false,
showPopup: () => false,
mapLocationFeatureDataset: (context: any, event: any) => {
mapLocationFeatureDataset: (_, event: any) => {
return {
...event.locationDataset,
};
Expand Down Expand Up @@ -312,10 +330,9 @@ const selectedFeatureLoaderStates = {
assign({
isLoading: () => false,
showPopup: () => false,
mapLocationFeatureDataset: (context: any, event: any) => {
mapLocationFeatureDataset: (_, event: any) => {
return {
...event.data,
selectingComponentId: context.selectingComponentId,
};
},
}),
Expand Down Expand Up @@ -396,7 +413,7 @@ const sideBarStates = {
assign({
filePropertyLocations: (
_,
event: AnyEventObject & { locations: LocationBoundaryDataset[] },
event: AnyEventObject & { locations: ApiGen_Concepts_FileProperty[] },
) => event.locations ?? [],
}),
],
Expand Down Expand Up @@ -661,10 +678,7 @@ export const mapMachine = createMachine<MachineContext>({
mapFeatureSelected: null,
mapLocationFeatureDataset: null,
mapMarkedLocation: null,
selectedFeatures: [],
repositioningFeatureDataset: null,
repositioningPropertyIndex: null,
selectingComponentId: null,
locationFeaturesForAddition: null,
worklistSelectedMapLocation: null,
worklistLocationFeatureDataset: null,
isLoading: false,
Expand All @@ -680,6 +694,7 @@ export const mapMachine = createMachine<MachineContext>({
mapLayersToRefresh: new Set(),
currentMapBounds: null,
isEditPropertiesMode: false,
repositioningFeature: null,
},

// State definitions
Expand Down Expand Up @@ -722,19 +737,6 @@ export const mapMachine = createMachine<MachineContext>({
EXIT_MAP: {
target: 'notMap',
},
PREPARE_FOR_CREATION: {
actions: assign({
selectedFeatures: (
_,
event: AnyEventObject & { selectedFeatures: SelectedFeatureDataset[] },
) => event.selectedFeatures,
}),
},
PROCESS_CREATION: {
actions: assign({
selectedFeatures: () => [],
}),
},
SET_EDIT_PROPERTIES_MODE: {
actions: assign({
isEditPropertiesMode: (_, event: AnyEventObject & { isEditPropertiesMode: boolean }) =>
Expand All @@ -755,6 +757,7 @@ export const mapMachine = createMachine<MachineContext>({
featureView: featureViewStates,
featureDataLoader: featureDataLoaderStates,
mapRequest: mapRequestStates,
locationFeatureAddition: locationFeatureAdditionStates,
selectedFeatureLoader: selectedFeatureLoaderStates,
sideBar: sideBarStates,
rightSideBar: rightSideBarStates,
Expand Down
Loading
Loading