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

Commit

Permalink
migrate to useEntity API, improving use of account ID
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvicenti committed Jul 11, 2024
1 parent 723a8bf commit 1af4309
Show file tree
Hide file tree
Showing 33 changed files with 694 additions and 1,509 deletions.
4 changes: 2 additions & 2 deletions frontend/apps/desktop/src/app-account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export function AccountWizardDialog() {
if (isSaveWords) {
saveWords.mutate({key: 'main', value: words})
}
invalidate([queryKeys.KEYS_LIST])
invalidate([queryKeys.LOCAL_ACCOUNT_ID_LIST])
setCreatedAccount(res.accountId)
setStep('complete')
})
Expand Down Expand Up @@ -291,7 +291,7 @@ export function AccountWizardDialog() {
disabled={!isExistingWordsSave}
onPress={() => {
addExistingAccount.mutateAsync().then((res) => {
invalidate([queryKeys.KEYS_LIST])
invalidate([queryKeys.LOCAL_ACCOUNT_ID_LIST])
setCreatedAccount(res.accountId)
setStep('complete')
})
Expand Down
116 changes: 44 additions & 72 deletions frontend/apps/desktop/src/components/app-embeds.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {useAccount_deprecated, useProfile} from '@/models/accounts'
import {useAccount_deprecated} from '@/models/accounts'
import {useEntity} from '@/models/entities'
import {
API_FILE_URL,
BlockContentUnknown,
Expand Down Expand Up @@ -40,7 +41,6 @@ import {
import {YStackProps} from 'tamagui'
import {useAccounts} from '../models/accounts'
import {useComment} from '../models/comments'
import {useDocument} from '../models/documents'
import {getAvatarUrl} from '../utils/account-url'
import {useNavRoute} from '../utils/navigation'
import {getRouteContext, useOpenInContext} from '../utils/route-context'
Expand Down Expand Up @@ -251,8 +251,8 @@ const EmbedSideAnnotation = forwardRef<
/>
)
if (unpacked && unpacked.type != 'd') return null
const doc = useDocument(unpacked?.qid, unpacked?.version || undefined)
const editors = useAccounts(doc.data?.authors || [])
const entity = useEntity(unpacked)
const editors = useAccounts(entity.data?.document?.authors || [])

return (
<YStack
Expand All @@ -267,14 +267,14 @@ const EmbedSideAnnotation = forwardRef<
>
{/* <XStack ai="center" gap="$2" bg="green"> */}
<SizableText size="$1" fontWeight="600">
{doc?.data?.metadata?.name}
{getDocumentTitle(entity?.data?.document)}
</SizableText>
{/* <SizableText fontSize={12} color="$color9">
{formattedDateMedium(pub.data?.document?.publishTime)}
</SizableText> */}
{/* </XStack> */}
<SizableText size="$1" color="$color9">
{formattedDateMedium(doc.data?.updateTime)}
{formattedDateMedium(entity.data?.document?.updateTime)}
</SizableText>
<XStack
marginHorizontal="$2"
Expand Down Expand Up @@ -334,14 +334,9 @@ const CommentSideAnnotation = forwardRef(function CommentSideAnnotation(
}
}, [comment])

const pubTarget = useDocument(
unpackedTarget?.qid,
unpackedTarget?.version || undefined,
)
const pubTarget = useEntity(unpackedTarget)

const editors = useAccounts(
pubTarget.data?.publication?.document?.editors || [],
)
const editors = useAccounts(pubTarget.data?.document?.authors || [])

if (pubTarget.status == 'success') {
return (
Expand All @@ -359,17 +354,15 @@ const CommentSideAnnotation = forwardRef(function CommentSideAnnotation(
<SizableText size="$1">
comment on{' '}
<SizableText size="$1" fontWeight="600">
{pubTarget?.data?.publication?.document?.title}
{getDocumentTitle(pubTarget?.data?.document)}
</SizableText>
</SizableText>
{/* <SizableText fontSize={12} color="$color9">
{formattedDateMedium(pub.data?.document?.publishTime)}
</SizableText> */}
{/* </XStack> */}
<SizableText size="$1" color="$color9">
{formattedDateMedium(
pubTarget.data?.publication?.document?.updateTime,
)}
{formattedDateMedium(pubTarget.data?.document?.updateTime)}
</SizableText>
<XStack
marginHorizontal="$2"
Expand Down Expand Up @@ -427,19 +420,8 @@ export function EmbedDocument(props: EntityComponentProps) {
}

export function EmbedDocContent(props: EntityComponentProps) {
const documentId = props.type == 'd' ? createHmId('d', props.eid) : undefined
const [showReferenced, setShowReferenced] = useState(false)
const doc = useDocument(
documentId,
showReferenced && props.version
? props.version
: props.latest
? undefined
: props.version || undefined,
{
enabled: !!documentId,
},
)
const doc = useEntity(props)
const route = useNavRoute()
const navigate = useNavigate()
return (
Expand All @@ -448,48 +430,37 @@ export function EmbedDocContent(props: EntityComponentProps) {
isLoading={doc.isInitialLoading}
showReferenced={showReferenced}
onShowReferenced={setShowReferenced}
pub={doc.data}
document={doc.data?.document}
EmbedWrapper={EmbedWrapper}
parentBlockId={props.parentBlockId}
renderOpenButton={() =>
documentId && (
<Button
size="$2"
icon={ArrowUpRightSquare}
onPress={() => {
if (!documentId) return
navigate({
key: 'document',
documentId,
versionId: props.version || undefined,
context: getRouteContext(
route,
props.parentBlockId || undefined,
),
})
}}
>
Open Document
</Button>
)
}
renderOpenButton={() => (
<Button
size="$2"
icon={ArrowUpRightSquare}
onPress={() => {
if (!props.qid) return
navigate({
key: 'document',
documentId: props.qid,
versionId: props.version || undefined,
context: getRouteContext(route, props.parentBlockId || undefined),
})
}}
>
Open Document
</Button>
)}
/>
)
}

export function EmbedDocumentCard(props: EntityComponentProps) {
const docId = props.type == 'd' ? createHmId('d', props.eid) : undefined
const doc = useDocument(
docId,
props.latest ? undefined : props.version || undefined,
{
enabled: !!docId,
},
)
const doc = useEntity(props)
let textContent = useMemo(() => {
if (doc.data?.content) {
if (doc.data?.document?.content) {
let content = ''
doc.data?.content.forEach((bn) => {
doc.data?.document?.content.forEach((bn) => {
content += bn.block?.text + ' '
})
return content
Expand All @@ -503,11 +474,11 @@ export function EmbedDocumentCard(props: EntityComponentProps) {
viewType={props.block.attributes?.view == 'card' ? 'card' : 'content'}
>
<DocumentCardView
title={doc.data?.metadata?.name}
title={getDocumentTitle(doc.data?.document)}
textContent={textContent}
editors={doc.data?.authors || []}
editors={doc.data?.document?.authors || []}
AvatarComponent={AvatarComponent}
date={doc.data?.updateTime}
date={doc.data?.document?.updateTime}
/>
</EmbedWrapper>
)
Expand All @@ -517,21 +488,22 @@ export function EmbedAccount(
props: EntityComponentProps,
parentBlockId: string | null,
) {
console.log(`== ~ props EmbedAccount:`, props)
const accountId = props.type == 'a' ? props.eid : undefined
const profile = useProfile(accountId)
const profile = useEntity(props)

if (profile.status == 'success') {
const account =
profile.data?.type === 'a' ? profile.data?.account : undefined
if (!account) return null
if (props.block?.attributes?.view == 'content' && profile.data) {
return <EmbedDocContent {...props} {...unpackedRef} />
return <EmbedDocContent {...props} />
} else if (props.block?.attributes?.view == 'card') {
return (
<EmbedWrapper
hmRef={props.id}
parentBlockId={parentBlockId}
viewType="card"
>
<EmbedAccountContent account={profile.data!} />
<EmbedAccountContent account={account} />
</EmbedWrapper>
)
}
Expand All @@ -542,7 +514,7 @@ export function EmbedAccount(
parentBlockId={parentBlockId}
viewType="card"
>
<EmbedAccountContent account={accountQuery.data!} />
<EmbedAccountContent account={account} />
<XStack p="$2" theme="red" gap="$2">
<FileWarning size={14} />
<SizableText size="$1">
Expand Down Expand Up @@ -659,7 +631,7 @@ function AccountInlineEmbed(props: InlineEmbedComponentProps) {
function PublicationInlineEmbed(props: InlineEmbedComponentProps) {
const pubId = props?.type == 'd' ? props.qid : undefined
if (!pubId) throw new Error('Invalid props at PublicationInlineEmbed (pubId)')
const doc = useDocument(pubId, props?.version || undefined)
const doc = useEntity(props)
const navigate = useNavigate()
return (
<InlineEmbedButton
Expand All @@ -672,7 +644,7 @@ function PublicationInlineEmbed(props: InlineEmbedComponentProps) {
})
}
>
{getDocumentTitle(doc.data)}
{getDocumentTitle(doc.data?.document)}
</InlineEmbedButton>
)
}
Expand Down
37 changes: 21 additions & 16 deletions frontend/apps/desktop/src/components/citations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {AccountLinkAvatar} from '@/components/account-link-avatar'
import {useAccount_deprecated} from '@/models/accounts'
import {useComment} from '@/models/comments'
import {useEntityMentions} from '@/models/content-graph'
import {useDocTextContent, useDocument} from '@/models/documents'
import {useDocTextContent} from '@/models/documents'
import {useEntity} from '@/models/entities'
import {DocumentRoute} from '@/utils/routes'
import {useNavigate} from '@/utils/useNavigate'
import {
Expand Down Expand Up @@ -46,12 +47,20 @@ function CitationItem({mention}: {mention: Mention}) {
function PublicationCitationItem({mention}: {mention: Mention}) {
const spawn = useNavigate('spawn')
const unpackedSource = unpackHmId(mention.source)
const doc = useDocument(mention.source, mention.sourceBlob?.cid, {
enabled: !!mention.source,
})
let {data: account} = useAccount_deprecated(doc.data?.author)
const doc = useEntity(
unpackedSource
? {
...unpackedSource,
version: mention.sourceBlob?.cid || null,
}
: undefined,
{
enabled: !!unpackedSource,
},
)
let {data: account} = useAccount_deprecated(doc.data?.document?.owner)

const docTextContent = useDocTextContent(doc.data)
const docTextContent = useDocTextContent(doc.data?.document)
const destRoute: DocumentRoute = {
key: 'document',
documentId: unpackedSource!.qid,
Expand All @@ -60,16 +69,18 @@ function PublicationCitationItem({mention}: {mention: Mention}) {
}
return (
<PanelCard
title={getDocumentTitle(doc.data)}
title={getDocumentTitle(doc.data?.document)}
content={docTextContent}
author={account}
date={formattedDateMedium(doc.data?.createTime)}
date={formattedDateMedium(doc.data?.document?.createTime)}
onPress={() => {
if (unpackedSource) {
spawn(destRoute)
}
}}
avatar={<AccountLinkAvatar accountId={doc.data?.author} size={24} />}
avatar={
<AccountLinkAvatar accountId={doc.data?.document?.owner} size={24} />
}
/>
)
}
Expand All @@ -88,13 +99,7 @@ function CommentCitationItem({mention}: {mention: Mention}) {
return null
}, [comment])

const doc = useDocument(
commentTarget?.qid,
commentTarget?.version || undefined,
{
enabled: !!commentTarget,
},
)
const doc = useEntity(commentTarget)

let {data: account} = useAccount_deprecated(comment?.author)

Expand Down
10 changes: 5 additions & 5 deletions frontend/apps/desktop/src/components/comments.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {useAccount_deprecated} from '@/models/accounts'
import {useDocument} from '@/models/documents'
import {useEntity} from '@/models/entities'
import {AppDocContentProvider} from '@/pages/document-content-provider'
import {trpc} from '@/trpc'
import {useNavigate} from '@/utils/useNavigate'
Expand Down Expand Up @@ -350,10 +350,10 @@ export function CommentPageTitlebarWithDocId({
}) {
const docId = useStream<string | null>(targetDocIdStream)
const usableDocId = targetDocId || docId || undefined
const doc = useDocument(usableDocId)
const doc = useEntity(unpackHmId(usableDocId))
const spawn = useNavigate('spawn')
const author = doc.data?.author
const title = getDocumentTitle(doc.data)
const author = doc.data?.document?.owner
const title = getDocumentTitle(doc.data?.document)
if (!doc || !author || !title || !usableDocId)
return (
<CommentPageTitlebar>
Expand All @@ -377,7 +377,7 @@ export function CommentPageTitlebarWithDocId({
spawn({
key: 'document',
documentId: usableDocId,
versionId: doc.data?.version,
versionId: doc.data?.document?.version,
})
}}
>
Expand Down
Loading

0 comments on commit 1af4309

Please sign in to comment.