Initial building map is now fetched from server

This commit is contained in:
Dmitriy Shishkov 2021-09-04 23:18:19 +03:00
parent f0df9d9233
commit 6167f15ee6
No known key found for this signature in database
GPG Key ID: 14358F96FCDD8060
8 changed files with 46 additions and 25 deletions

View File

@ -13,6 +13,7 @@
}, },
"dependencies": { "dependencies": {
"@material-ui/core": "^4.12.3", "@material-ui/core": "^4.12.3",
"@roomruler/messages": "workspace:^0.0.0",
"immer": "^9.0.6", "immer": "^9.0.6",
"react": "^17.0.2", "react": "^17.0.2",
"react-dom": "^17.0.2" "react-dom": "^17.0.2"

View File

@ -41,7 +41,7 @@ export const useCanvas = (
clickCb(x, y); clickCb(x, y);
}, },
[canvasRef.current] [canvasRef.current, clickCb]
); );
return { canvasRef, onClick }; return { canvasRef, onClick };

1
apps/front/src/config.ts Normal file
View File

@ -0,0 +1 @@
export const WS_URL = "ws://localhost:8081";

View File

@ -20,21 +20,8 @@ export const WeekDays = [
"Sunday", "Sunday",
] as const; ] as const;
// It will look like Windows 11 logo, lol
export const defaultState: ContextData = { export const defaultState: ContextData = {
map: [ map: [],
{ x: 5, y: 5 }, char: [],
{ x: 110, y: 5 }, ids: {},
{ x: 5, y: 110 },
{ x: 110, y: 110 },
].map((coordinates, index) => ({
coordinates,
size: { w: 100, h: 100 },
title: index + 1,
})),
char: Array(4)
.fill(0)
.map(() => ({
free: true,
})),
}; };

View File

@ -1,20 +1,47 @@
import produce from "immer"; import produce from "immer";
import { createContext, FC, useContext, useState } from "react"; import { createContext, FC, useContext, useEffect, useState } from "react";
import { defaultState } from "./constants"; import { defaultState } from "./constants";
import { ContextData, ContextValue } from "./types/context"; import { ContextData, ContextValue } from "./types/context";
import { isArrayOf, isMessage, isRoom } from "@roomruler/messages";
import { WS_URL } from "./config";
import { RoomDisplay, RoomState } from "./types/room";
const Context = createContext<ContextValue | undefined>(undefined); const Context = createContext<ContextValue | undefined>(undefined);
const ws = new WebSocket(WS_URL);
export const RoomContextProvider: FC = ({ children }) => { export const RoomContextProvider: FC = ({ children }) => {
const [state, setState] = useState<ContextData>(defaultState); const [state, setState] = useState<ContextData>(defaultState);
const toggleFree = (index: number) => const toggleFree = (index: number) => {};
setState(
produce((draft) => { useEffect(() => {
draft.char[index].free = !draft.char[index].free; ws.onmessage = ({ data }) => {
}) const message: unknown = JSON.parse(data);
); if (isMessage(message) && isArrayOf(message.args, isRoom)) {
const map: RoomDisplay[] = [],
char: RoomState[] = [],
ids: Record<number, number> = {};
for (const {
x,
y,
height: h,
width: w,
title,
free,
id,
} of message.args) {
map.push({ coordinates: { x, y }, size: { w, h }, title: title });
char.push({ free });
ids[id] = map.length - 1;
}
setState({ map, char, ids });
}
};
}, []);
return ( return (
<Context.Provider value={{ state, toggleFree }}> <Context.Provider value={{ state, toggleFree }}>

View File

@ -3,6 +3,7 @@ import { RoomDisplay, RoomState } from "./room";
export interface ContextData { export interface ContextData {
map: RoomDisplay[]; map: RoomDisplay[];
char: RoomState[]; char: RoomState[];
ids: Record<number, number>;
} }
export type ContextValue = { export type ContextValue = {

View File

@ -4,5 +4,7 @@
"target": "es6", "target": "es6",
"module": "ESNext", "module": "ESNext",
"jsx": "react-jsx" "jsx": "react-jsx"
} },
"include": ["src"],
"references": [{ "path": "../../packages/messages/tsconfig.json" }]
} }

View File

@ -4,4 +4,6 @@ export default defineConfig({
esbuild: { esbuild: {
jsxInject: `import React from "react"`, jsxInject: `import React from "react"`,
}, },
optimizeDeps: { include: ["@roomruler/messages"] },
}); });