diff --git a/src/components/realtime/ClientCacheInvalidation.tsx b/src/components/realtime/ClientCacheInvalidation.tsx index dfbde2b..eef5b28 100644 --- a/src/components/realtime/ClientCacheInvalidation.tsx +++ b/src/components/realtime/ClientCacheInvalidation.tsx @@ -4,6 +4,8 @@ import { useTRPC } from "@/services/serverFunctions/trpcClient"; import React, { useCallback, useEffect, useState } from "react"; import { io, Socket } from "socket.io-client"; import { useQueryClient } from "@tanstack/react-query"; +import { useGlobalSettingsQuery } from "@/features/local/globalSettings/globalSettingsHooks"; +import { GlobalSettings } from "@/features/local/globalSettings/globalSettingsModels"; interface ServerToClientEvents { message: (data: string) => void; @@ -24,6 +26,22 @@ function removeFileExtension(fileName: string): string { 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() { const invalidateCache = useFilePathInvalidation(); const [connectionAttempted, setConnectionAttempted] = useState(false); @@ -62,13 +80,32 @@ export function ClientCacheInvalidation() { const useFilePathInvalidation = () => { const trpc = useTRPC(); const queryClient = useQueryClient(); + const { data: settings } = useGlobalSettingsQuery(); + return useCallback( (filePath: string) => { - const [courseName, moduleOrLectures, itemType, itemFile] = - filePath.split("/"); + const courseName = getCourseNameByPath(filePath, settings); + // 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 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") { queryClient.invalidateQueries({ @@ -141,6 +178,8 @@ const useFilePathInvalidation = () => { }); return; } + + console.log("no cache invalidation match for file ", allParts); }, [ queryClient, @@ -153,6 +192,7 @@ const useFilePathInvalidation = () => { trpc.quiz.getQuiz, trpc.settings.allCoursesSettings, trpc.settings.courseSettings, + settings, ] ); };