mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-26 07:38:33 -06:00
sockets now prod, handles when not available
This commit is contained in:
@@ -29,5 +29,4 @@ COPY --from=builder /app/public ./public
|
|||||||
|
|
||||||
RUN mkdir -p storage && rm -rf /app/storage/*
|
RUN mkdir -p storage && rm -rf /app/storage/*
|
||||||
|
|
||||||
# CMD [ "pnpm", "run", "start" ]
|
CMD [ "pnpm", "run", "start" ]
|
||||||
CMD [ "pnpm", "run", "socketProd" ]
|
|
||||||
|
|||||||
@@ -3,11 +3,11 @@
|
|||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "next dev",
|
"devNoSocket": "next dev",
|
||||||
"socket": "node src/websocket.js",
|
"dev": "node src/websocket.js",
|
||||||
"build": "next build",
|
"build": "next build",
|
||||||
"start": "next start",
|
"startNoSocket": "next start",
|
||||||
"socketProd": "NODE_ENV=production node src/websocket.js",
|
"start": "NODE_ENV=production node src/websocket.js",
|
||||||
"lint": "tsc && next lint",
|
"lint": "tsc && next lint",
|
||||||
"test": "vitest"
|
"test": "vitest"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { trpc } from "@/services/trpc/utils";
|
import { trpc } from "@/services/trpc/utils";
|
||||||
import React, { useEffect } from "react";
|
import React, { useCallback, useEffect, useState } from "react";
|
||||||
import { io, Socket } from "socket.io-client";
|
import { io, Socket } from "socket.io-client";
|
||||||
|
|
||||||
interface ServerToClientEvents {
|
interface ServerToClientEvents {
|
||||||
@@ -24,18 +24,48 @@ function removeFileExtension(fileName: string): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function ClientCacheInvalidation() {
|
export function ClientCacheInvalidation() {
|
||||||
const utils = trpc.useUtils();
|
const invalidateCache = useFilePathInvalidation();
|
||||||
|
const [connectionAttempted, setConnectionAttempted] = useState(false);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (!connectionAttempted) {
|
||||||
|
socket.connect();
|
||||||
|
setConnectionAttempted(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
socket.on("connect", () => {
|
||||||
|
console.log("Socket connected successfully.");
|
||||||
|
});
|
||||||
|
|
||||||
socket.on("message", (data) => {
|
socket.on("message", (data) => {
|
||||||
console.log("Received message:", data);
|
console.log("Received message:", data);
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on("fileChanged", (filePath) => {
|
socket.on("fileChanged", invalidateCache);
|
||||||
|
|
||||||
|
socket.on("connect_error", (error) => {
|
||||||
|
console.error("Connection error:", error);
|
||||||
|
console.error("File system real time updates disabled");
|
||||||
|
socket.disconnect();
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
socket.off("message");
|
||||||
|
socket.off("fileChanged");
|
||||||
|
socket.off("connect_error");
|
||||||
|
};
|
||||||
|
}, [connectionAttempted, invalidateCache]);
|
||||||
|
|
||||||
|
return <></>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const useFilePathInvalidation = () => {
|
||||||
|
const utils = trpc.useUtils();
|
||||||
|
return useCallback(
|
||||||
|
(filePath: string) => {
|
||||||
const [courseName, moduleOrLectures, itemType, itemFile] =
|
const [courseName, moduleOrLectures, itemType, itemFile] =
|
||||||
filePath.split("/");
|
filePath.split("/");
|
||||||
|
|
||||||
const itemName = itemFile ? removeFileExtension(itemFile) : undefined;
|
const itemName = itemFile ? removeFileExtension(itemFile) : undefined;
|
||||||
|
|
||||||
const allParts = [courseName, moduleOrLectures, itemType, itemName];
|
const allParts = [courseName, moduleOrLectures, itemType, itemName];
|
||||||
|
|
||||||
if (moduleOrLectures === "settings.yml") {
|
if (moduleOrLectures === "settings.yml") {
|
||||||
@@ -91,28 +121,17 @@ export function ClientCacheInvalidation() {
|
|||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
});
|
},
|
||||||
|
[
|
||||||
socket.on("connect_error", (error) => {
|
utils.assignment.getAllAssignments,
|
||||||
console.error("Connection error:", error);
|
utils.assignment.getAssignment,
|
||||||
});
|
utils.lectures.getLectures,
|
||||||
|
utils.page.getAllPages,
|
||||||
return () => {
|
utils.page.getPage,
|
||||||
socket.off("message");
|
utils.quiz.getAllQuizzes,
|
||||||
socket.off("fileChanged");
|
utils.quiz.getQuiz,
|
||||||
socket.off("connect_error");
|
utils.settings.allCoursesSettings,
|
||||||
};
|
utils.settings.courseSettings,
|
||||||
}, [
|
]
|
||||||
utils.assignment.getAllAssignments,
|
);
|
||||||
utils.assignment.getAssignment,
|
};
|
||||||
utils.lectures.getLectures,
|
|
||||||
utils.page.getAllPages,
|
|
||||||
utils.page.getPage,
|
|
||||||
utils.quiz.getAllQuizzes,
|
|
||||||
utils.quiz.getQuiz,
|
|
||||||
utils.settings.allCoursesSettings,
|
|
||||||
utils.settings.courseSettings,
|
|
||||||
]);
|
|
||||||
|
|
||||||
return <></>;
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user