mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-25 23:28:33 -06:00
fixed server side file editing
This commit is contained in:
@@ -4,6 +4,8 @@ import { useTRPC } from "@/services/serverFunctions/trpcClient";
|
|||||||
import React, { useCallback, useEffect, useState } from "react";
|
import React, { useCallback, useEffect, useState } from "react";
|
||||||
import { io, Socket } from "socket.io-client";
|
import { io, Socket } from "socket.io-client";
|
||||||
import { useQueryClient } from "@tanstack/react-query";
|
import { useQueryClient } from "@tanstack/react-query";
|
||||||
|
import { useGlobalSettingsQuery } from "@/features/local/globalSettings/globalSettingsHooks";
|
||||||
|
import { GlobalSettings } from "@/features/local/globalSettings/globalSettingsModels";
|
||||||
|
|
||||||
interface ServerToClientEvents {
|
interface ServerToClientEvents {
|
||||||
message: (data: string) => void;
|
message: (data: string) => void;
|
||||||
@@ -24,6 +26,22 @@ function removeFileExtension(fileName: string): string {
|
|||||||
return fileName;
|
return fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getCourseNameByPath(
|
||||||
|
filePath: string,
|
||||||
|
settings: GlobalSettings
|
||||||
|
) {
|
||||||
|
const courseSettings = settings.courses.find((c) => {
|
||||||
|
const normalizedFilePath = filePath.startsWith("./")
|
||||||
|
? filePath.substring(2)
|
||||||
|
: filePath;
|
||||||
|
const normalizedCoursePath = c.path.startsWith("./")
|
||||||
|
? c.path.substring(2)
|
||||||
|
: c.path;
|
||||||
|
return normalizedFilePath.startsWith(normalizedCoursePath);
|
||||||
|
});
|
||||||
|
return courseSettings?.name;
|
||||||
|
}
|
||||||
|
|
||||||
export function ClientCacheInvalidation() {
|
export function ClientCacheInvalidation() {
|
||||||
const invalidateCache = useFilePathInvalidation();
|
const invalidateCache = useFilePathInvalidation();
|
||||||
const [connectionAttempted, setConnectionAttempted] = useState(false);
|
const [connectionAttempted, setConnectionAttempted] = useState(false);
|
||||||
@@ -62,13 +80,32 @@ export function ClientCacheInvalidation() {
|
|||||||
const useFilePathInvalidation = () => {
|
const useFilePathInvalidation = () => {
|
||||||
const trpc = useTRPC();
|
const trpc = useTRPC();
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
const { data: settings } = useGlobalSettingsQuery();
|
||||||
|
|
||||||
return useCallback(
|
return useCallback(
|
||||||
(filePath: string) => {
|
(filePath: string) => {
|
||||||
const [courseName, moduleOrLectures, itemType, itemFile] =
|
const courseName = getCourseNameByPath(filePath, settings);
|
||||||
filePath.split("/");
|
// console.log(filePath, settings, courseName);
|
||||||
|
if (!courseName) {
|
||||||
|
console.log(
|
||||||
|
"no course settings found for file path, not invalidating cache",
|
||||||
|
filePath
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const splitPath = filePath.split("/");
|
||||||
|
const [moduleOrLectures, itemType, itemFile] = splitPath.slice(-3);
|
||||||
|
|
||||||
const itemName = itemFile ? removeFileExtension(itemFile) : undefined;
|
const itemName = itemFile ? removeFileExtension(itemFile) : undefined;
|
||||||
const allParts = [courseName, moduleOrLectures, itemType, itemName];
|
const allParts = { courseName, moduleOrLectures, itemType, itemName };
|
||||||
|
// console.log(
|
||||||
|
// "received file to invalidate",
|
||||||
|
// filePath,
|
||||||
|
// allParts,
|
||||||
|
// itemName,
|
||||||
|
// itemType
|
||||||
|
// );
|
||||||
|
|
||||||
if (moduleOrLectures === "settings.yml") {
|
if (moduleOrLectures === "settings.yml") {
|
||||||
queryClient.invalidateQueries({
|
queryClient.invalidateQueries({
|
||||||
@@ -141,6 +178,8 @@ const useFilePathInvalidation = () => {
|
|||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log("no cache invalidation match for file ", allParts);
|
||||||
},
|
},
|
||||||
[
|
[
|
||||||
queryClient,
|
queryClient,
|
||||||
@@ -153,6 +192,7 @@ const useFilePathInvalidation = () => {
|
|||||||
trpc.quiz.getQuiz,
|
trpc.quiz.getQuiz,
|
||||||
trpc.settings.allCoursesSettings,
|
trpc.settings.allCoursesSettings,
|
||||||
trpc.settings.courseSettings,
|
trpc.settings.courseSettings,
|
||||||
|
settings,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user