Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
Improve title handling for home drafts
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvicenti committed May 7, 2024
1 parent 73358e4 commit ae49419
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
19 changes: 15 additions & 4 deletions frontend/packages/app/components/sidebar-neo.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
GroupVariant,
HMAccount,
HMBlockNode,
HMEntityContent,
Expand Down Expand Up @@ -189,13 +190,19 @@ function getItemDetails(
let title: string | undefined = undefined
let icon: IconDefinition | undefined = undefined
let isDraft = false
let groupHomeVariant: GroupVariant | null = null
if (!entity) return null
if (entity.type === 'a') {
title = entity.account?.profile?.alias
icon = Contact
}
if (entity.type === 'g') {
if (entity.type === 'g' && entity.group?.id) {
title = entity.group?.title
groupHomeVariant = {
key: 'group',
groupId: entity.group?.id,
pathName: '/',
}
icon = Book
}
if (entity.type === 'd') {
Expand Down Expand Up @@ -223,15 +230,19 @@ function getItemDetails(
icon,
headings,
isDraft,
groupHomeVariant,
}
}
type ItemDetails = ReturnType<typeof getItemDetails>

function ResumeDraftButton({docId}: {docId?: string}) {
function ResumeDraftButton({info}: {info: ItemDetails}) {
if (!info) throw new Error('ItemDetails required for ResumeDraftButton')
const {docId} = info
const navigate = useNavigate()
const myAccount = useMyAccount()
const isMyHomeDoc = docId === myAccount.data?.profile?.rootDocument
const drafts = useDocumentDrafts(docId)

const draft = drafts.data?.[0]
if (draft) {
return (
Expand All @@ -242,7 +253,7 @@ function ResumeDraftButton({docId}: {docId?: string}) {
navigate({
key: 'draft',
draftId: draft.id,
variant: null,
variant: info.groupHomeVariant,
isProfileDocument: isMyHomeDoc,
})
}}
Expand Down Expand Up @@ -292,7 +303,7 @@ function ContextItems({
Draft
</SizableText>
) : (
<ResumeDraftButton docId={info.docId} />
<ResumeDraftButton info={info} />
)
}
/>
Expand Down
5 changes: 4 additions & 1 deletion frontend/packages/app/components/titlebar-title.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {Sparkles, Star} from '@tamagui/lucide-icons'
import {useEffect} from 'react'
import {useAccount} from '../models/accounts'
import {useGroup} from '../models/groups'
import {useFixedDraftTitle} from '../pages/draft'
import {
AccountRoute,
DraftRoute,
Expand Down Expand Up @@ -223,7 +224,9 @@ function DraftTitle({
const title = useDraftTitle({
documentId: route.draftId,
})
const displayTitle = title ?? 'Untitled Document'
const realTitle = title ?? 'Untitled Document'
const fixedTitle = useFixedDraftTitle(route)
const displayTitle = fixedTitle || realTitle
useWindowTitle(displayTitle ? `Draft: ${displayTitle}` : undefined)
return (
<>
Expand Down
28 changes: 17 additions & 11 deletions frontend/packages/app/pages/draft.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
handleDragMedia,
} from '../utils/media-drag'
import {useOpenDraft} from '../utils/open-draft'
import {DraftRoute} from '../utils/routes'
import {AppPublicationContentProvider} from './publication-content-provider'

export default function DraftPage() {
Expand Down Expand Up @@ -106,17 +107,7 @@ export default function DraftPage() {

const gwUrl = useGatewayUrl()

const myAccount = useMyAccount()
const contextGroup = useGroup(route.variant?.groupId)

let fixedTitle: string | undefined = undefined
if (route.variant?.groupId && route.variant?.pathName === '/') {
const groupTitle = contextGroup.data?.title
fixedTitle = groupTitle ? `${groupTitle} Home` : 'Group Home'
} else if (route.isProfileDocument) {
const myAlias = myAccount.data?.profile?.alias
fixedTitle = myAlias ? `${myAlias} Home` : 'My Account Home'
}
const fixedTitle = useFixedDraftTitle(route)

function handleFocusAtMousePos(event) {
let ttEditor = (data.editor as BlockNoteEditor)._tiptapEditor
Expand Down Expand Up @@ -485,6 +476,21 @@ function applyTitleResize(target: HTMLTextAreaElement) {
target.style.height = `${target.scrollHeight}px`
}

export function useFixedDraftTitle(route: DraftRoute) {
const myAccount = useMyAccount()
const contextGroup = useGroup(route.variant?.groupId)

let fixedTitle: string | undefined = undefined
if (route.variant?.groupId && route.variant?.pathName === '/') {
const groupTitle = contextGroup.data?.title
fixedTitle = groupTitle ? `${groupTitle} Home` : 'Group Home'
} else if (route.isProfileDocument) {
const myAlias = myAccount.data?.profile?.alias
fixedTitle = myAlias ? `${myAlias} Home` : 'My Account Home'
}
return fixedTitle
}

function DraftTitleInput({
fixedTitle,
onEnter,
Expand Down

0 comments on commit ae49419

Please sign in to comment.