Skip to content

Commit

Permalink
fix: Tab doesn't represent the page you are on for non-data asset pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Salman-Apptware committed Dec 15, 2023
1 parent 0ea6145 commit 0c42436
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions datahub-web-react/src/app/AppProviders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import UserContextProvider from './context/UserContextProvider';
import QuickFiltersProvider from '../providers/QuickFiltersProvider';
import SearchContextProvider from './search/context/SearchContextProvider';
import EntityRegistryProvider from './EntityRegistryProvider';
import { BrowserTitleProvider } from './shared/BrowserTabTitleContext';

interface Props {
children: React.ReactNode;
Expand All @@ -15,11 +16,13 @@ export default function AppProviders({ children }: Props) {
<AppConfigProvider>
<UserContextProvider>
<EntityRegistryProvider>
<BrowserTitleProvider>
<EducationStepsProvider>
<QuickFiltersProvider>
<SearchContextProvider>{children}</SearchContextProvider>
</QuickFiltersProvider>
</EducationStepsProvider>
</BrowserTitleProvider>
</EntityRegistryProvider>
</UserContextProvider>
</AppConfigProvider>
Expand Down
27 changes: 27 additions & 0 deletions datahub-web-react/src/app/search/SearchablePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useHistory, useLocation } from 'react-router';
import { debounce } from 'lodash';
import * as QueryString from 'query-string';
import { useTheme } from 'styled-components';
import { Helmet } from 'react-helmet-async';
import { SearchHeader } from './SearchHeader';
import { useEntityRegistry } from '../useEntityRegistry';
import { EntityType, FacetFilterInput } from '../../types.generated';
Expand All @@ -19,6 +20,7 @@ import { useQuickFiltersContext } from '../../providers/QuickFiltersContext';
import { useUserContext } from '../context/useUserContext';
import { useSelectedSortOption } from './context/SearchContext';
import { HALF_SECOND_IN_MS } from '../entity/shared/tabs/Dataset/Queries/utils/constants';
import { useBrowserTitle } from '../shared/BrowserTabTitleContext';

const styles = {
children: {
Expand Down Expand Up @@ -68,6 +70,28 @@ export const SearchablePage = ({ onSearch, onAutoComplete, children }: Props) =>
const { user } = userContext;
const viewUrn = userContext.localState?.selectedViewUrn;

const { title, updateTitle } = useBrowserTitle();

useEffect(() => {
// Update the title only if it's not already set and there is a valid pathname
if (!title && location.pathname) {
const formattedPath = location.pathname
.split('/')
.filter(word => word !== '')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' | ');

if (formattedPath) {
return updateTitle(formattedPath);
}
}

// Clean up the title when the component unmounts
return () => {
updateTitle('');
};
}, [location.pathname, title, updateTitle]);

useEffect(() => {
if (suggestionsData !== undefined) {
setNewSuggestionData(suggestionsData);
Expand Down Expand Up @@ -140,6 +164,9 @@ export const SearchablePage = ({ onSearch, onAutoComplete, children }: Props) =>
authenticatedUserPictureLink={user?.editableProperties?.pictureLink}
entityRegistry={entityRegistry}
/>
<Helmet>
<title>{title}</title>
</Helmet>
<div style={styles.children}>{children}</div>
</>
);
Expand Down
30 changes: 30 additions & 0 deletions datahub-web-react/src/app/shared/BrowserTabTitleContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { createContext, ReactNode, useContext } from 'react';

interface BrowserTitleContextProps {
title: string;
updateTitle: (newTitle: string) => void;
}

const BrowserTitleContext = createContext<BrowserTitleContextProps | undefined>(undefined);

export const BrowserTitleProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
const [title, setTitle] = React.useState<string>('');

const updateTitle = (newTitle: string) => {
setTitle(newTitle);
};

return (
<BrowserTitleContext.Provider value={{ title, updateTitle }}>
{children}
</BrowserTitleContext.Provider>
);
};

export const useBrowserTitle = () => {
const context = useContext(BrowserTitleContext);
if (!context) {
throw new Error('useBrowserTitle must be used within a BrowserTitleProvider');
}
return context;
};

0 comments on commit 0c42436

Please sign in to comment.