Skip to content

Commit

Permalink
fix: simplify the hook
Browse files Browse the repository at this point in the history
  • Loading branch information
GresilleSiffle committed Oct 24, 2024
1 parent 6544b7e commit 76a7cfb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
2 changes: 1 addition & 1 deletion frontend/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export { useRefreshSettings } from './useRefreshSettings';
export { useScrollToLocation } from './useScrollToLocation';
export { useStats } from './useStats';
export { usePreferredLanguages } from './usePreferredLanguages';
export { useRestorePreviousTitle } from './useRestorePreviousTitle';
export { useDocumentTitle } from './useDocumentTitle';
15 changes: 15 additions & 0 deletions frontend/src/hooks/useDocumentTitle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useEffect, useState } from 'react';

export const useDocumentTitle = (pageTitle: string) => {
const [previousTitle] = useState(document.title);

useEffect(() => {
document.title = pageTitle;
}, [pageTitle]);

useEffect(() => {
return () => {
document.title = previousTitle;
};
}, [previousTitle]);
};
18 changes: 0 additions & 18 deletions frontend/src/hooks/useRestorePreviousTitle.tsx

This file was deleted.

23 changes: 12 additions & 11 deletions frontend/src/pages/entities/EntityAnalysisPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import { Link, useParams } from 'react-router-dom';
import { Box, Button, Container, Grid, Typography } from '@mui/material';

import { LoaderWrapper } from 'src/components';
import {
useCurrentPoll,
useLoginState,
useRestorePreviousTitle,
} from 'src/hooks';
import { useCurrentPoll, useLoginState, useDocumentTitle } from 'src/hooks';
import {
ApiError,
PollsService,
Expand Down Expand Up @@ -106,8 +102,18 @@ const EntityAnalysisPage = () => {
const [entity, setEntity] = useState<Recommendation>();
const [isLoading, setIsLoading] = useState(true);
const [apiError, setApiError] = useState<ApiError>();
const [pageTitle, setPageTitle] = useState('Tournesol');

useRestorePreviousTitle();
useDocumentTitle(pageTitle);

useEffect(() => {
if (entity) {
const title = createPageTitle(t, pollName, entity.entity);
if (title) {
setPageTitle(title);
}
}
}, [currentLang, entity, pollName, t]);

const tryToCreateVideo = async () => {
if (pollName !== YOUTUBE_POLL_NAME) {
Expand Down Expand Up @@ -148,11 +154,6 @@ const EntityAnalysisPage = () => {
try {
const entity = await getEntityWithPollStats();
setEntity(entity);

const title = createPageTitle(t, pollName, entity.entity);
if (title) {
document.title = title;
}
} catch (error) {
const reason: ApiError = error;
if (reason.status === 404 && createVideo) {
Expand Down

0 comments on commit 76a7cfb

Please sign in to comment.