Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add titles for pages #632

Merged
merged 1 commit into from
Jan 9, 2025
Merged
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
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[];
}
Loading