Skip to content

Commit

Permalink
perf: do not start world sync without a connected wallet and stop it …
Browse files Browse the repository at this point in the history
…when parcel view is closed
  • Loading branch information
tnrdd committed Mar 22, 2024
1 parent be6afa0 commit 6cc4fb9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/components/cadastre/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ function Map(props: MapProps) {
MapStyleName.satellite
);

const { mediaObjects } = useWorld();
const { mediaObjects, stopSync: stopWorldSync } = useWorld();

const handleMapstyle = (newStyle: MapStyleName) => {
localStorage.setItem(MAP_STYLE_KEY, newStyle);
Expand Down Expand Up @@ -945,6 +945,7 @@ function Map(props: MapProps) {
setClaimBase2Coord(null);
setSelectedParcelId("");
setParcelHoverId("");
stopWorldSync();
break;
case STATE.PARCEL_SELECTED:
setClaimBase1Coord(null);
Expand Down
6 changes: 6 additions & 0 deletions src/components/cadastre/cards/ParcelInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import AuctionInfo from "./AuctionInfo";
import ConnectWallet from "../../shared/ConnectWallet";
import NotificationModal from "../NotificationModal";
import { useBasicProfile } from "../../../hooks/geo-web-content/basicProfile";
import { useWorld } from "../../../hooks/geo-web-content/world";
import BN from "bn.js";
import MenuIcon from "../../../assets/menu.svg";
import { PCOLicenseDiamondFactory } from "@geo-web/sdk/dist/contract/index";
Expand Down Expand Up @@ -122,6 +123,7 @@ function ParcelInfo(props: ParcelInfoProps) {
setIsFullScreen,
} = props;
const { isMobile, isTablet } = useMediaQuery();
const { stopSync: stopWorldSync } = useWorld();
const { loading, data, refetch } = useQuery<ParcelQuery>(parcelQuery, {
variables: {
id: selectedParcelId,
Expand Down Expand Up @@ -237,6 +239,10 @@ function ParcelInfo(props: ParcelInfoProps) {
setQueryTimerId(null);
}

if (licenseOwner && licenseOwner !== accountAddress) {
stopWorldSync();
}

if (!outstandingBidder) {
setIsParcelAvailable(true);
return;
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/geo-web-content/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function useWorld() {
const [shouldMediaObjectsUpdate, setShouldMediaObjectsUpdate] =
useState<boolean>(true);

const { tables, useStore } = useMUD();
const { tables, useStore, stopSync } = useMUD();

const modelComponents = useStore((state: any) =>
Object.values(state.getRecords(tables.ModelCom))
Expand Down Expand Up @@ -89,6 +89,7 @@ function useWorld() {
mediaObjects,
shouldMediaObjectsUpdate,
setShouldMediaObjectsUpdate,
stopSync,
};
}

Expand Down
30 changes: 16 additions & 14 deletions src/pages/Index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ type IndexPageProps = {
setShouldRefetchParcelsData: React.Dispatch<React.SetStateAction<boolean>>;
};

const chain = import.meta.env.MODE === "mainnet" ? optimism : optimismSepolia;
const mudChain = {
...chain,
rpcUrls: {
...chain.rpcUrls,
default: {
http: [RPC_URLS_HTTP[NETWORK_ID]],
webSocket: [RPC_URLS_WS[NETWORK_ID]],
},
},
};

function IndexPage(props: IndexPageProps) {
const {
authStatus,
Expand Down Expand Up @@ -345,26 +357,16 @@ function IndexPage(props: IndexPageProps) {

React.useEffect(() => {
(async () => {
const chain =
import.meta.env.MODE === "mainnet" ? optimism : optimismSepolia;
const mudChain = {
...chain,
rpcUrls: {
...chain.rpcUrls,
default: {
http: [RPC_URLS_HTTP[NETWORK_ID]],
webSocket: [RPC_URLS_WS[NETWORK_ID]],
},
},
};
const worldConfig = await syncWorld({
mudChain,
chainId: NETWORK_ID,
world: WORLD,
namespaces: [Number(selectedParcelId).toString()],
indexerUrl: "https://mud-testnet.geoweb.network/trpc",
startSync:
selectedParcelId !== "" && import.meta.env.MODE !== "mainnet",
!!address &&
selectedParcelId !== "" &&
import.meta.env.MODE !== "mainnet",
});

setWorldConfig(worldConfig);
Expand All @@ -373,7 +375,7 @@ function IndexPage(props: IndexPageProps) {
setWorldContract(IWorld__factory.connect(WORLD.worldAddress, library));
}
})();
}, [selectedParcelId, library]);
}, [address, selectedParcelId, library]);

return (
<ApolloProvider client={client}>
Expand Down

0 comments on commit 6cc4fb9

Please sign in to comment.