Skip to content

Commit

Permalink
wip: small randoms + separated socketio hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
tabarra committed Nov 28, 2023
1 parent 9a9d459 commit c2ab7a4
Show file tree
Hide file tree
Showing 17 changed files with 61 additions and 44 deletions.
8 changes: 5 additions & 3 deletions panel/src/hooks/pages.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { atom, useSetAtom } from 'jotai';
import { atom, useAtomValue, useSetAtom } from 'jotai';
import { atomEffect } from 'jotai-effect'
import faviconDefault from '/favicon_default.svg';
import faviconOnline from '/favicon_online.svg';
import faviconPartial from '/favicon_partial.svg';
import faviconOffline from '/favicon_offline.svg';
import { globalStatusAtom } from './socketio';
import { globalStatusAtom } from './status';
import { playerCountAtom } from './playerlist';

/**
* This atom is used to change the key of the main page error boundry, which also resets the router
Expand Down Expand Up @@ -43,6 +44,7 @@ export const pageTitleWatcher = atomEffect((get, set) => {
if (!window.txConsts.isWebInterface) return;
const pageTitle = get(pageTitleAtom);
const globalStatus = get(globalStatusAtom);
const playerCount = get(playerCountAtom);

if (!globalStatus) {
faviconEl.href = faviconDefault;
Expand All @@ -55,7 +57,7 @@ export const pageTitleWatcher = atomEffect((get, set) => {
} else {
faviconEl.href = faviconOffline;
}
document.title = `(${globalStatus.server.players}) ${globalStatus.server.name} · ${pageTitle}`;
document.title = `(${playerCount}) ${globalStatus.server.name} · ${pageTitle}`;
}

return () => {
Expand Down
10 changes: 10 additions & 0 deletions panel/src/hooks/playerlist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PlayerlistEventType, PlayerlistPlayerType } from "@shared/socketioTypes";
import { atom, useAtomValue } from "jotai";


/**
* Atoms
*/
export const playerlistAtom = atom<PlayerlistPlayerType[]>([]);
export const playerCountAtom = atom((get) => get(playerlistAtom).length);

18 changes: 1 addition & 17 deletions panel/src/hooks/socketio.ts → panel/src/hooks/status.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
import { GlobalStatusType } from "@shared/socketioTypes";
import { atom, useAtomValue, useSetAtom } from "jotai";
import { io } from "socket.io-client";


export const getSocket = (rooms: string[] | string) => {
const socketOpts = {
transports: ['polling'],
upgrade: false,
query: { rooms }
};

const socket = window.txConsts.isWebInterface
? io({ ...socketOpts, path: '/socket.io' })
: io('monitor', { ...socketOpts, path: '/WebPipe/socket.io' });

return socket;
}

/**
* Consts
* Atoms
*/
export const globalStatusAtom = atom<GlobalStatusType | null>(null);
export const serverNameAtom = atom((get) => get(globalStatusAtom)?.server.name ?? 'unconfigured');
export const processInstantiatedAtom = atom((get) => get(globalStatusAtom)?.server.instantiated ?? false);
export const serverMutexAtom = atom((get) => get(globalStatusAtom)?.server.mutex);


/**
Expand Down
8 changes: 5 additions & 3 deletions panel/src/layout/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import { useAuth } from "@/hooks/auth";
import { useGlobalMenuSheet, usePlayerlistSheet, useServerSheet } from "@/hooks/sheets";
import { useTheme } from "@/hooks/useTheme";
import { FaDiscord } from "react-icons/fa";
import { useAtomValue } from "jotai";
import { serverNameAtom } from "@/hooks/status";
import { playerCountAtom } from "@/hooks/playerlist";


function ServerTitle() {
// FIXME: make data dynamic
const playerCount = 1234;
const serverName = '{{serverName}}';
const playerCount = useAtomValue(playerCountAtom);
const serverName = useAtomValue(serverNameAtom);

return (
<div className="flex justify-start">
Expand Down
6 changes: 3 additions & 3 deletions panel/src/layout/MainShell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import MainSheets from './MainSheets';
import WarningBar from './WarningBar';
import BreakpointDebugger from '@/components/BreakpointDebugger';
import { useEffect, useRef } from 'react';
import { getSocket, useSetGlobalStatus } from '@/hooks/socketio';
import { useSetGlobalStatus } from '@/hooks/status';
import { useSetOfflineWarning } from '@/hooks/useWarningBar';
import { pageTitleWatcher } from '@/hooks/pages';
import { useAtomValue } from 'jotai';
import { getSocket } from '@/lib/utils';



Expand Down Expand Up @@ -54,7 +55,6 @@ export default function MainShell() {
expireSession('main socketio');
});
socket.on('status', function (status) {
console.log('status', status);
setGlobalStatus(status);
});
socket.on('playerlist', function (playerlistData) {
Expand All @@ -73,7 +73,7 @@ export default function MainShell() {

return <>
<Header />
<div className="px-3 py-4 w-full max-w-[1920px] flex flex-row gap-2">
<div className="px-3 py-4 w-full max-w-[1920px] mx-auto flex flex-row gap-2">
<ServerSidebar />
<main className="flex flex-1 min-h-[calc(100vh-5.5rem-1px)]">
<MainRouter />
Expand Down
2 changes: 1 addition & 1 deletion panel/src/layout/serverSidebar/ServerControls.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import KickAllIcon from '@/components/KickAllIcon';
import { processInstantiatedAtom } from '@/hooks/socketio';
import { processInstantiatedAtom } from '@/hooks/status';
import { cn } from '@/lib/utils';
import { cva } from 'class-variance-authority';
import { useAtomValue } from 'jotai';
Expand Down
2 changes: 1 addition & 1 deletion panel/src/layout/serverSidebar/ServerMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MenuNavLink } from '@/components/MainPageLink';
import { serverNameAtom } from '@/hooks/socketio';
import { serverNameAtom } from '@/hooks/status';
import { useAtomValue } from 'jotai';
import { Box, ChevronRightSquare, Dna, Eye, FileEdit, LayoutDashboard } from 'lucide-react';

Expand Down
2 changes: 1 addition & 1 deletion panel/src/layout/serverSidebar/ServerSchedule.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Button } from '@/components/ui/button';
import { useGlobalStatus } from '@/hooks/socketio';
import { useGlobalStatus } from '@/hooks/status';
import { cn, msToDuration } from '@/lib/utils';
import { PenLineIcon, PlayCircleIcon, PlusCircleIcon, XCircleIcon } from 'lucide-react';

Expand Down
2 changes: 1 addition & 1 deletion panel/src/layout/serverSidebar/ServerStatus.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useGlobalStatus } from '@/hooks/socketio';
import { useGlobalStatus } from '@/hooks/status';
import { VariantProps, cva } from 'class-variance-authority';
import {
Tooltip,
Expand Down
19 changes: 19 additions & 0 deletions panel/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"
import humanizeDuration from '@/lib/humanizeDuration';
import { io } from "socket.io-client";


/**
Expand Down Expand Up @@ -56,3 +57,21 @@ export const msToShortDuration = humanizeDuration.humanizer({
spacer: '',
language: 'shortEn',
});


/**
* Returns a socket.io client instance
*/
export const getSocket = (rooms: string[] | string) => {
const socketOpts = {
transports: ['polling'],
upgrade: false,
query: { rooms }
};

const socket = window.txConsts.isWebInterface
? io({ ...socketOpts, path: '/socket.io' })
: io('monitor', { ...socketOpts, path: '/WebPipe/socket.io' });

return socket;
}
2 changes: 1 addition & 1 deletion panel/src/pages/testing/TmpColors.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Button } from "@/components/ui/button";
import { PersonStandingIcon } from "lucide-react";
import TmpHexHslConverter from "./tmpHexHslConverter";
import TmpHexHslConverter from "./TmpHexHslConverter";

export default function TmpColors() {
return <>
Expand Down
2 changes: 1 addition & 1 deletion panel/src/pages/testing/TmpSocket.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Button } from "@/components/ui/button";
import { getSocket } from "@/hooks/socketio";
import { getSocket } from "@/lib/utils";
import { useEffect, useRef, useState } from "react";
import { Socket } from "socket.io-client";

Expand Down
Binary file removed web/public/img/favicon_default.png
Binary file not shown.
Binary file removed web/public/img/favicon_offline.png
Binary file not shown.
Binary file removed web/public/img/favicon_online.png
Binary file not shown.
Binary file removed web/public/img/favicon_partial.png
Binary file not shown.
24 changes: 12 additions & 12 deletions web/public/js/txadmin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const setNextRestartTimeClass = (cssClass) => {
}
};

const updateStatusCard = (discordData, serverData) => {
const updateStatusCard = (discordData, serverData, schedulerData) => {
if(!statusCard.self) return;

setBadgeColor(statusCard.discord, discordData.statusClass);
Expand All @@ -49,13 +49,13 @@ const updateStatusCard = (discordData, serverData) => {
statusCard.server.textContent = serverData.status;
statusCard.serverProcess.textContent = serverData.process;

if (typeof serverData.scheduler.nextRelativeMs !== 'number') {
if (typeof schedulerData.nextRelativeMs !== 'number') {
setNextRestartTimeClass('font-weight-light');
statusCard.nextRestartTime.textContent = 'not scheduled';
} else {
const tempFlag = (serverData.scheduler.nextIsTemp)? '(tmp)' : '';
const relativeTime = msToDuration(serverData.scheduler.nextRelativeMs, {units: ['h', 'm']});
const isLessThanMinute = serverData.scheduler.nextRelativeMs < 60_000;
const tempFlag = (schedulerData.nextIsTemp)? '(tmp)' : '';
const relativeTime = msToDuration(schedulerData.nextRelativeMs, {units: ['h', 'm']});
const isLessThanMinute = schedulerData.nextRelativeMs < 60_000;
if(isLessThanMinute){
statusCard.nextRestartTime.textContent = `right now ${tempFlag}`;
statusCard.nextRestartBtnCancel.classList.add('d-none');
Expand All @@ -64,7 +64,7 @@ const updateStatusCard = (discordData, serverData) => {
statusCard.nextRestartTime.textContent = `in ${relativeTime} ${tempFlag}`;
}

if (serverData.scheduler.nextSkip) {
if (schedulerData.nextSkip) {
setNextRestartTimeClass('text-muted');
if(!isLessThanMinute) {
statusCard.nextRestartBtnCancel.classList.add('d-none');
Expand Down Expand Up @@ -107,11 +107,11 @@ const updateHostStats = (hostData) => {
};

function updateStatus(data) {
updateStatusCard(data.discord, data.server);
if (isWebInterface) {
updatePageTitle(data.server.statusClass, data.server.name, data.server.players);
updateHostStats(data.host);
}
updateStatusCard(data.discord, data.server, data.scheduler);
// if (isWebInterface) {
// updatePageTitle(data.server.statusClass, data.server.name, data.server.players);
// updateHostStats(data.host);
// }
}
function updateStatusOffline() {
if (statusCard.self) {
Expand Down Expand Up @@ -226,7 +226,7 @@ const getSocket = (rooms) => {
}

const startMainSocket = () => {
return;
// return;
const rooms = isWebInterface ? ['status', 'playerlist'] : ['status'];
const socket = getSocket(rooms);
socket.on('error', (error) => {
Expand Down

0 comments on commit c2ab7a4

Please sign in to comment.