Skip to content

Commit

Permalink
Fix: Open in new tab not working
Browse files Browse the repository at this point in the history
I fixed it by just making sure we insert a # between the host and the
location part in the url in the Home Component (that is where 404
requests ultimately land, if they don't have a # in the url).

fixes: #477
  • Loading branch information
reglim committed Mar 29, 2023
1 parent 5cf6034 commit 5fa4bb3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
1 change: 1 addition & 0 deletions docat/docat/nginx/default
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ server {
}

location / {
try_files $uri /index.html;
}
}
4 changes: 2 additions & 2 deletions web/src/components/Project.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ interface Props {
onFavoriteChanged: () => void
}

export default function Project(props: Props): JSX.Element {
export default function Project (props: Props): JSX.Element {
return (
<div className={styles['project-card']}>
<div className={styles['project-card-header']}>
<Tooltip title={props.project.name} placement="top-start" arrow>
<Link to={`/${props.project.name}/latest`}>
<Link to={`${props.project.name}/latest`}>
{props.project.logo
? (
<>
Expand Down
16 changes: 15 additions & 1 deletion web/src/pages/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react'
import React, { useEffect, useState } from 'react'

import ProjectRepository from '../repositories/ProjectRepository'
import { useProjects } from '../data-providers/ProjectDataProvider'
Expand All @@ -15,14 +15,28 @@ import LoadingPage from './LoadingPage'
import styles from './../style/pages/Home.module.css'
import { ErrorOutline } from '@mui/icons-material'
import { Project } from '../models/ProjectsResponse'
import { useLocation } from 'react-router'

export default function Home (): JSX.Element {
const { projects, loadingFailed } = useProjects()
const [nonFavoriteProjects, setNonFavoriteProjects] = useState<Project[]>([])
const [favoriteProjects, setFavoriteProjects] = useState<Project[]>([])

const location = useLocation()

document.title = 'Home | docat'

// insert # into the url if it's missing
useEffect(() => {
const nonHostPart = window.location.href.replace(window.location.origin, '')

if (nonHostPart.startsWith('#') || nonHostPart.startsWith('/#')) {
return
}

window.location.replace(`/#${nonHostPart}`)
}, [location])

const updateFavorites = (): void => {
if (projects == null) return

Expand Down
10 changes: 6 additions & 4 deletions web/src/pages/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const NO_RESULTS: SearchResult = {
}
const debounceMs = 600

export default function Search(): JSX.Element {
export default function Search (): JSX.Element {
const queryParam = useSearchParams()[0].get('query') ?? ''

const { projects, loadingFailed } = useProjects()
Expand Down Expand Up @@ -47,7 +47,9 @@ export default function Search(): JSX.Element {

useEffect(() => {
searchDebounced.cancel()
window.history.pushState({}, '', `/#/search?query=${searchQuery}`)
if (window.location.hash !== `#/search?query=${searchQuery}`) {
window.history.pushState({}, '', `/#/search?query=${searchQuery}`)
}
searchDebounced()
}, [searchQuery, projects, loadingFailed])

Expand All @@ -69,10 +71,10 @@ export default function Search(): JSX.Element {
/>
{results === null
? (
<div className="loading-spinner" />
<div className="loading-spinner" />
)
: (
<SearchResults searchQuery={searchQuery} results={results} />
<SearchResults searchQuery={searchQuery} results={results} />
)}
</PageLayout>
)
Expand Down

0 comments on commit 5fa4bb3

Please sign in to comment.