Skip to content

Commit

Permalink
fix: add titles for pages (#632)
Browse files Browse the repository at this point in the history
  • Loading branch information
CK-7vn authored Jan 9, 2025
1 parent 3548edf commit 2948b37
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
48 changes: 48 additions & 0 deletions frontend/src/Components/TitleManager.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { useMatches, useLocation } from 'react-router-dom';
import { useEffect } from 'react';
import { TitleHandler } from '@/common';

export function TitleManager() {
const matches = useMatches();
const location = useLocation();

useEffect(() => {
const updateTitle = () => {
const isLibraryViewer =
location.pathname.includes('/viewer/libraries/');

if (isLibraryViewer) {
setTimeout(() => {
const titleInterval = setInterval(() => {
const libraryViewer = document.getElementById(
'library-viewer-iframe'
) as HTMLIFrameElement;
if (libraryViewer?.contentWindow?.document) {
const iframeTitle =
libraryViewer.contentWindow.document.title;
if (iframeTitle) {
document.title = iframeTitle + ' - UnlockEd';
}
}
}, 1000);

return () => clearInterval(titleInterval);
}, 5000);
return;
}

const activeHandle = matches
.filter((match) =>
Boolean((match.handle as TitleHandler)?.title)
)
.pop();
const title =
(activeHandle?.handle as TitleHandler)?.title ?? 'UnlockEd';
document.title = title + ' - UnlockEd';
};

updateTitle();
}, [matches, location.pathname]);

return null;
}
4 changes: 2 additions & 2 deletions frontend/src/Pages/LibraryViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ export default function LibraryViewer() {
</div>
) : src != '' ? (
<iframe
sandbox="allow-scripts allow-same-origin"
sandbox="allow-scripts allow-same-origin allow-modals allow-popups"
className="w-full h-screen pt-4"
id="library-viewer"
id="library-viewer-iframe"
src={src}
/>
) : (
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ import StudentLayer0 from './Pages/StudentLayer0.tsx';
import StudentLayer2 from './Pages/StudentDashboard.tsx';
import AdminLayer1 from './Pages/AdminLayer1.tsx';
import HelpfulLinks from './Pages/HelpfulLinks.tsx';
import { TitleManager } from './Components/TitleManager.tsx';

const WithAuth: React.FC = () => {
return (
<AuthProvider>
<ToastProvider>
<PathValueProvider>
<TitleManager />
<Outlet />
</PathValueProvider>
</ToastProvider>
Expand Down Expand Up @@ -104,6 +106,7 @@ function WithAdmin() {
<ToastProvider>
<PathValueProvider>
<AdminOnly>
<TitleManager />
<Outlet />
</AdminOnly>
</PathValueProvider>
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -854,3 +854,7 @@ export interface OpenContentItem {
provider_name?: string;
channel_title?: string;
}
export interface TitleHandler {
title: string;
path?: string[];
}

0 comments on commit 2948b37

Please sign in to comment.