Skip to content

Commit

Permalink
♻️ Refactor titles to useTitle hook
Browse files Browse the repository at this point in the history
  • Loading branch information
foysalit committed Feb 6, 2024
1 parent 2f7540d commit 73af95f
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 123 deletions.
7 changes: 2 additions & 5 deletions app/communication-template/[id]/edit/page.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use client'

import { useTitle } from 'react-use'
import { CommunicationTemplateForm } from 'components/communication-template/form'
import { useEffect } from 'react'

export default function CommunicationTemplateEditPage({ params: { id } }) {
// Change page title dynamically
useEffect(() => {
document.title = `Edit Communication Templates #${id}`
}, [id])
useTitle(`Edit Communication Templates #${id}`)

return (
<div className="w-5/6 md:w-2/3 lg:w-1/2 mx-auto">
Expand Down
7 changes: 2 additions & 5 deletions app/communication-template/create/page.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use client'

import { useTitle } from 'react-use'
import { CommunicationTemplateForm } from 'components/communication-template/form'
import { useEffect } from 'react'

export default function CommunicationTemplateCreatePage() {
// Change page title dynamically
useEffect(() => {
document.title = `Create Communication Templates`
}, [])
useTitle(`Create Communication Templates`)

return (
<div className="w-5/6 md:w-2/3 lg:w-1/2 mx-auto">
Expand Down
8 changes: 3 additions & 5 deletions app/communication-template/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use client'

import format from 'date-fns/format'
import { useEffect, useState } from 'react'
import { useState } from 'react'
import Link from 'next/link'
import { PencilIcon, PlusIcon, TrashIcon } from '@heroicons/react/20/solid'
import { useTitle } from 'react-use'

import { LabelChip } from '@/common/labels'
import { Loading, LoadingFailed } from '@/common/Loader'
Expand All @@ -17,10 +18,7 @@ export default function CommunicationTemplatePage() {
string | undefined
>()

// Change page title dynamically
useEffect(() => {
document.title = `Communication Templates`
}, [])
useTitle(`Communication Templates`)

if (isLoading) {
return <Loading message="Loading templates" />
Expand Down
20 changes: 8 additions & 12 deletions app/events/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use client'
import { useQuery } from '@tanstack/react-query'
import { useTitle } from 'react-use'
import client from '@/lib/client'
import { Loading, LoadingFailed } from '@/common/Loader'
import { EventView } from '@/mod-event/View'
import { useEffect } from 'react'
import { MOD_EVENT_TITLES } from '@/mod-event/constants'

export default function EventViewPage({ params }: { params: { id: string } }) {
Expand All @@ -19,17 +19,13 @@ export default function EventViewPage({ params }: { params: { id: string } }) {
},
})

// Change page title dynamically
// Use a human-readable event name once event details are fetched
useEffect(() => {
if (event) {
const eventTitle =
MOD_EVENT_TITLES[event.event.$type as string] || 'Moderation'
document.title = `${eventTitle} Event #${id}`
} else {
document.title = `Moderation Event #${id}`
}
}, [id, event])
let pageTitle = `Moderation Event #${id}`
if (event) {
const eventTitle =
MOD_EVENT_TITLES[event.event.$type as string] || 'Moderation'
pageTitle = `${eventTitle} Event #${id}`
}
useTitle(pageTitle)

if (error) {
return <LoadingFailed error={error} />
Expand Down
6 changes: 2 additions & 4 deletions app/events/page-content.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useTitle } from 'react-use'
import { ModEventList } from '@/mod-event/EventList'
import { emitEvent } from '@/mod-event/helpers/emitEvent'
import { ComAtprotoAdminEmitModerationEvent } from '@atproto/api'
import { ModActionPanelQuick } from 'app/actions/ModActionPanel/QuickAction'
import { usePathname, useRouter, useSearchParams } from 'next/navigation'
import { useEffect } from 'react'

export default function EventListPageContent() {
const searchParams = useSearchParams()
Expand All @@ -20,9 +20,7 @@ export default function EventListPageContent() {
router.push((pathname ?? '') + '?' + newParams.toString())
}

useEffect(() => {
document.title = `Moderation Events`
}, [])
useTitle(`Moderation Events`)

return (
<div>
Expand Down
69 changes: 44 additions & 25 deletions app/reports/page-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { AuthContext } from '@/shell/AuthContext'
import { ButtonGroup } from '@/common/buttons'
import { useFluentReportSearch } from '@/reports/useFluentReportSearch'
import { SubjectTable } from 'components/subject/table'
import { useTitle } from 'react-use'

const TABS = [
{
Expand All @@ -48,6 +49,42 @@ const TABS = [
{ key: 'all', name: 'All', href: '/reports' },
]

const buildPageTitle = ({
currentTab,
takendown,
includeMuted,
appealed,
}: {
currentTab: string
takendown: boolean
includeMuted: boolean
appealed: boolean
}) => {
const titleFromTab =
currentTab === 'all'
? `All subjects`
: `${currentTab[0].toUpperCase()}${currentTab.slice(1)}`
const additionalFragments: string[] = []

if (takendown) {
additionalFragments.push('Taken Down')
}

if (includeMuted) {
additionalFragments.push('Include Muted')
}

if (appealed) {
additionalFragments.push('Appealed')
}

const additionalTitle = additionalFragments.length
? ` (${additionalFragments.join(', ')})`
: ''
const title = `Queue - ${titleFromTab}${additionalTitle}`
return title
}

const ResolvedFilters = () => {
const router = useRouter()
const pathname = usePathname()
Expand Down Expand Up @@ -197,31 +234,13 @@ export const ReportsPageContent = () => {
),
)

useEffect(() => {
const titleFromTab =
currentTab === 'all'
? `All subjects`
: `${currentTab[0].toUpperCase()}${currentTab.slice(1)}`
const additionalFragments: string[] = []

if (takendown) {
additionalFragments.push('Taken Down')
}

if (includeMuted) {
additionalFragments.push('Include Muted')
}

if (appealed) {
additionalFragments.push('Appealed')
}

const additionalTitle = additionalFragments.length
? ` (${additionalFragments.join(', ')})`
: ''
const title = `Queue - ${titleFromTab}${additionalTitle}`
document.title = title
}, [currentTab, takendown, includeMuted, appealed])
const pageTitle = buildPageTitle({
currentTab,
takendown,
includeMuted,
appealed,
})
useTitle(pageTitle)

return (
<>
Expand Down
60 changes: 36 additions & 24 deletions app/repositories/[id]/[...record]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,36 @@ import { usePathname, useRouter, useSearchParams } from 'next/navigation'
import { ModActionPanelQuick } from 'app/actions/ModActionPanel/QuickAction'
import { emitEvent } from '@/mod-event/helpers/emitEvent'
import { useEffect } from 'react'
import { useTitle } from 'react-use'

const buildPageTitle = ({
handle,
collection,
rkey,
}: {
handle?: string
collection?: string
rkey?: string
}) => {
let title = `Record Details`

if (collection) {
const titleFromCollection = collection.split('.').pop()
if (titleFromCollection) {
title =
titleFromCollection[0].toUpperCase() + titleFromCollection.slice(1)
}
}

if (handle) {
title += ` - ${handle}`
}

if (rkey) {
title += ` - ${rkey}`
}
return title
}

export default function Record({
params,
Expand Down Expand Up @@ -116,30 +146,12 @@ export default function Record({
}
}, [data, reportUri])

// Change title dynamically
// Show the collection name
// Once we retrieve the profile/repo details, show the handle
useEffect(() => {
let title = `Record Details`

if (collection) {
const titleFromCollection = collection.split('.').pop()
if (titleFromCollection) {
title =
titleFromCollection[0].toUpperCase() + titleFromCollection.slice(1)
}
}

if (data?.record?.repo) {
title += ` - ${data.record.repo.handle}`
}

if (rkey) {
title += ` - ${rkey}`
}

document.title = title
}, [data, collection])
const pageTitle = buildPageTitle({
handle: data?.record?.repo.handle,
rkey,
collection,
})
useTitle(pageTitle)

if (error) {
return <LoadingFailed error={error} />
Expand Down
50 changes: 27 additions & 23 deletions app/repositories/[id]/page-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,29 @@ import { ComAtprotoAdminEmitModerationEvent } from '@atproto/api'
import { ModActionPanelQuick } from 'app/actions/ModActionPanel/QuickAction'
import { usePathname, useRouter, useSearchParams } from 'next/navigation'
import { emitEvent } from '@/mod-event/helpers/emitEvent'
import { useEffect } from 'react'
import { useTitle } from 'react-use'

const buildPageTitle = ({
handle,
tab,
}: {
handle: string
tab: string | null
}) => {
let title = `Repository Details`
const titleFragments: string[] = [title]
const titleFromTab = tab ? tab[0].toUpperCase() + tab.slice(1) : ''

if (titleFromTab) {
titleFragments.unshift(titleFromTab)
}

if (handle) {
titleFragments.unshift(handle)
}

return titleFragments.join(' - ')
}
export function RepositoryViewPageContent({ id }: { id: string }) {
const {
error,
Expand All @@ -30,28 +51,11 @@ export function RepositoryViewPageContent({ id }: { id: string }) {
}
const tab = searchParams.get('tab')

// Change title dynamically
// Once we retrieve the profile/repo details, show the handle
// Show the current tab name from account view
useEffect(() => {
let title = `Repository Details`
const titleFragments: string[] = [title]
const titleFromTab = tab ? tab[0].toUpperCase() + tab.slice(1) : ''

if (titleFromTab) {
titleFragments.unshift(titleFromTab)
}

if (profile) {
titleFragments.unshift(profile.handle)
} else if (repo) {
titleFragments.unshift(repo.handle)
} else {
titleFragments.unshift(id)
}

document.title = titleFragments.join(' - ')
}, [id, repo, profile, tab])
const pageTitle = buildPageTitle({
handle: profile?.handle || repo?.handle || id,
tab,
})
useTitle(pageTitle)

return (
<>
Expand Down
15 changes: 6 additions & 9 deletions app/repositories/page-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useSearchParams } from 'next/navigation'
import { useInfiniteQuery } from '@tanstack/react-query'
import client from '@/lib/client'
import { useEffect } from 'react'
import { useTitle } from 'react-use'

export default function RepositoriesListPage() {
const params = useSearchParams()
Expand All @@ -24,16 +25,12 @@ export default function RepositoriesListPage() {
getNextPageParam: (lastPage) => lastPage.cursor,
})

// Change title dynamically, if there's a search term, include that
useEffect(() => {
let title = `Repositories`
let pageTitle = `Repositories`
if (term) {
pageTitle += ` - ${term}`
}

if (term) {
title += ` - ${term}`
}

document.title = title
}, [term])
useTitle(pageTitle)

const repos = data?.pages.flatMap((page) => page.repos) ?? []
return (
Expand Down
Loading

0 comments on commit 73af95f

Please sign in to comment.