Skip to content
Closed
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
14 changes: 14 additions & 0 deletions src/helpers/socket.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ export function getSpeechEventType(data: ISocketMessage): string {
return typeof data.data?.type === "string" ? data.data.type : "";
}

export const ASSET_SOCKET_TOAST_TYPES = [
NOTIFICATION_SOCKET_TYPES.ASSET_COMPRESSING,
NOTIFICATION_SOCKET_TYPES.ASSET_COMPRESSED,
NOTIFICATION_SOCKET_TYPES.ASSET_COMPRESSION_FAILED,
NOTIFICATION_SOCKET_TYPES.ASSET_THUMBNAIL_GENERATED,
NOTIFICATION_SOCKET_TYPES.ASSET_THUMBNAIL_FAILED,
] as const;

export function isAssetSocketToast(data: ISocketMessage): boolean {
return (ASSET_SOCKET_TOAST_TYPES as readonly string[]).includes(
getSpeechEventType(data),
);
}

export function isSpeechLiveMessage(data: ISocketMessage): boolean {
if (data.channel === SOCKET_CHANNELS.SPEECH_LIVE) {
return true;
Expand Down
19 changes: 17 additions & 2 deletions src/hooks/useSocket.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { useEffect, useState, useCallback } from "react";
import { useLocation } from "react-router-dom";
import AppRoutes from "../AppRoutes";
import { useAuth } from "../contexts/AuthContext";
import { useToast } from "../contexts/ToastContext";
import { getSocketToast, SOCKET_MESSAGE_TYPES } from "../helpers/socket.helpers";
import {
getSocketToast,
isAssetSocketToast,
SOCKET_MESSAGE_TYPES,
} from "../helpers/socket.helpers";
import SocketService, { ISocketMessage } from "../services/socket.service";
import { ToastTypes } from "../constants";

Expand All @@ -15,6 +21,7 @@ export interface INotification {
export const useSocket = () => {
const { token, isAuthenticated } = useAuth();
const { success, error, info } = useToast();
const location = useLocation();
const [notifications, setNotifications] = useState<INotification[]>([]);

useEffect(() => {
Expand All @@ -25,6 +32,14 @@ export const useSocket = () => {
}

const handleNotification = (data: ISocketMessage) => {
const isAdminAssetPage = location.pathname.startsWith(
AppRoutes.client.protected.admin.ASSETS,
);

if (isAssetSocketToast(data) && !isAdminAssetPage) {
return;
}

const toast = getSocketToast(data);
if (!toast) {
return;
Expand Down Expand Up @@ -63,7 +78,7 @@ export const useSocket = () => {
SocketService.removeListener(handleNotification);
// Don't disconnect here - let the effect handle it
};
}, [token, isAuthenticated, success, error, info]);
}, [token, isAuthenticated, success, error, info, location.pathname]);

const sendMessage = useCallback(
(channel: string, message: string, data: Record<string, unknown> = {}) => {
Expand Down
11 changes: 10 additions & 1 deletion src/routes/AdminHomeRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { NotFoundPage } from "../design/pages";
import { usePermissions } from "../hooks";
import type { AdminResource } from "../modules/admin/role";
import { ADMIN_ACTIONS, ADMIN_RESOURCES } from "../modules/admin";
import { AdminLayout } from "../modules/admin/components/AdminLayout";

const adminEntryRoutes: Array<{
action?: typeof ADMIN_ACTIONS.READ | typeof ADMIN_ACTIONS.CREATE;
Expand Down Expand Up @@ -63,5 +64,13 @@ export const AdminHomeRoute: React.FC = () => {
)
: null;

return entry ? <Navigate to={entry.path} replace /> : <NotFoundPage />;
return entry ? (
<Navigate to={entry.path} replace />
) : (
<AdminLayout>
<div className="flex min-h-[calc(100vh-10rem)] items-center justify-center">
<NotFoundPage />
</div>
</AdminLayout>
);
};
12 changes: 8 additions & 4 deletions src/routes/AdminRootRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ export const AdminRootRoute: React.FC<IAdminRootRouteProps> = ({
? isSuperAdmin
: isAdmin && can(action, resource);

return isAllowed ? (
return (
<AdminLayout>
<Outlet />
{isAllowed ? (
<Outlet />
) : (
<div className="flex min-h-[calc(100vh-10rem)] items-center justify-center">
<NotFoundPage />
</div>
)}
</AdminLayout>
) : (
<NotFoundPage />
);
};
Loading