Skip to content

Commit

Permalink
Add enable & disable commands in socket handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Tinland committed Jul 21, 2024
1 parent 9020318 commit 96677c9
Showing 1 changed file with 45 additions and 31 deletions.
76 changes: 45 additions & 31 deletions lib/hooks/use-server-socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,46 @@ export default function useServerSocket(
const { enableServer, serverSocketPort } = settings.global;
const socket = React.useRef(null);

const isUserWidget = target === "user-widget";

const handleMessage = React.useCallback(
(e) => {
const { action } = JSON.parse(e.data);

if (visible && action === "refresh") {
getter();
}

if (action === "disable" || action === "enable") {
const state = action === "enable";

if (isUserWidget) {
toggleUserWidget(userWidgetIndex, resetWidget, setSettings, state);
} else {
toggleWidget(target, resetWidget, setSettings, state);
}
}

if (action === "toggle") {
if (isUserWidget) {
toggleUserWidget(userWidgetIndex, resetWidget, setSettings);
} else {
toggleWidget(target, resetWidget, setSettings);
}
}
},
[
getter,
isUserWidget,
resetWidget,
setSettings,
target,
userWidgetIndex,
visible,
]
);

React.useEffect(() => {
const isUserWidget = target === "user-widget";
if (enableServer && socket.current === null) {
let queryParams = `target=${target}`;

Expand All @@ -28,35 +66,11 @@ export default function useServerSocket(
`ws://localhost:${serverSocketPort}/?${queryParams}`
);

newSocket.onmessage = (e) => {
const { action } = JSON.parse(e.data);

if (visible && action === "refresh") {
getter();
}
if (action === "toggle") {
if (isUserWidget) {
toggleUserWidget(userWidgetIndex, resetWidget, setSettings);
} else {
toggleWidget(target, resetWidget, setSettings);
}
}
};
newSocket.onmessage = handleMessage;

socket.current = newSocket;
}
}, [
enableServer,
getter,
resetWidget,
serverSocketPort,
settings,
setSettings,
socket,
target,
visible,
userWidgetIndex,
]);
}, [enableServer, handleMessage, serverSocketPort, target, userWidgetIndex]);
}

const settingsKeys = {
Expand All @@ -80,11 +94,11 @@ const settingsKeys = {
zoom: "zoomWidget",
};

async function toggleWidget(widget, resetWidget, setSettings) {
async function toggleWidget(widget, resetWidget, setSettings, forcedState) {
const key = settingsKeys[widget];
setSettings((settings) => {
const { widgets } = settings;
const active = !widgets[key];
const active = forcedState ?? !widgets[key];
if (!active) {
resetWidget?.();
}
Expand All @@ -97,10 +111,10 @@ async function toggleWidget(widget, resetWidget, setSettings) {
});
}

async function toggleUserWidget(index, resetWidget, setSettings) {
async function toggleUserWidget(index, resetWidget, setSettings, forcedState) {
setSettings((settings) => {
const { userWidgetsList = {} } = settings.userWidgets;
const active = !userWidgetsList[index].active;
const active = forcedState ?? !userWidgetsList[index].active;
if (!active) {
resetWidget?.();
}
Expand Down

0 comments on commit 96677c9

Please sign in to comment.