Skip to content

Commit

Permalink
Frontend Nested Documents, ID Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvicenti committed Jul 31, 2024
1 parent 5596e62 commit a3d9ae6
Show file tree
Hide file tree
Showing 46 changed files with 529 additions and 1,332 deletions.
18 changes: 9 additions & 9 deletions frontend/apps/desktop/src/app-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export const commentsApi = t.router({
getCommentDrafts: t.procedure
.input(
z.object({
docEid: z.string(),
docUid: z.string(),
}),
)
.query(({input}) => {
const result = commentDraftStore.get(`Doc-${input.docEid}`)
const result = commentDraftStore.get(`Doc-${input.docUid}`)
if (!result) return []
const commentIds = Object.keys(result)
return commentIds
Expand All @@ -40,22 +40,22 @@ export const commentsApi = t.router({
createCommentDraft: t.procedure
.input(
z.object({
targetDocEid: z.string(),
targetDocUid: z.string(),
targetDocVersion: z.string(),
targetCommentId: z.string().or(z.null()),
blocks: z.array(z.any()).optional(),
}),
)
.mutation(async ({input}) => {
const draftId = Math.random().toString(36).slice(2)
const prevIndex = commentDraftStore.get(`Doc-${input.targetDocEid}`) || {}
commentDraftStore.set(`Doc-${input.targetDocEid}`, {
const prevIndex = commentDraftStore.get(`Doc-${input.targetDocUid}`) || {}
commentDraftStore.set(`Doc-${input.targetDocUid}`, {
...prevIndex,
[draftId]: true,
})
commentDraftStore.set(`Comment-${draftId}`, {
blocks: input.blocks || [],
targetDocEid: input.targetDocEid,
targetDocUid: input.targetDocUid,
targetDocVersion: input.targetDocVersion,
targetCommentId: input.targetCommentId,
})
Expand All @@ -82,17 +82,17 @@ export const commentsApi = t.router({
.input(
z.object({
commentId: z.string(),
// targetDocEid: z.string(),
// targetDocUid: z.string(),
}),
)
.mutation(async ({input}) => {
const commentId = input.commentId
const comment = commentDraftStore.get(`Comment-${commentId}`)
if (!comment) throw new Error('Comment with this commentId not found')
commentDraftStore.delete(`Comment-${commentId}`)
const index = commentDraftStore.get(`Doc-${comment.targetDocEid}`)
const index = commentDraftStore.get(`Doc-${comment.targetDocUid}`)
if (!index) throw new Error('Comment index not found')
commentDraftStore.set(`Doc-${comment.targetDocEid}`, {
commentDraftStore.set(`Doc-${comment.targetDocUid}`, {
...index,
[commentId]: undefined,
})
Expand Down
8 changes: 4 additions & 4 deletions frontend/apps/desktop/src/app-diagnosis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const diagnosisApi = t.router({
.mutation(async ({input}) => {
const id = unpackHmId(input.draftId)
if (!id) throw new Error('Invalid draftId')
const draftPath = draftFilePath(id.eid)
const draftPath = draftFilePath(id.uid)
// @ts-ignore
const logExist: boolean = await exists(draftPath)
if (!logExist) {
Expand All @@ -54,16 +54,16 @@ export const diagnosisApi = t.router({
.mutation(async ({input}) => {
const id = unpackHmId(input.draftId)
if (!id) throw new Error('Invalid draftId')
const draftPath = draftFilePath(id.eid)
const draftPath = draftFilePath(id.uid)
await appendFile(draftPath, JSON.stringify(input.event) + '\n')
const pubFilePath = createPubFilePath(id.eid)
const pubFilePath = createPubFilePath(id.uid)
await move(draftPath, pubFilePath)
return pubFilePath
}),
openDraftLog: t.procedure.input(z.string()).mutation(async ({input}) => {
const id = unpackHmId(input)
if (!id) throw new Error('Invalid draftId')
await open(draftFilePath(id.eid))
await open(draftFilePath(id.uid))
}),
openDraftLogFolder: t.procedure.mutation(async () => {
await open(`${userDataPath}/DraftLog`)
Expand Down
14 changes: 8 additions & 6 deletions frontend/apps/desktop/src/changes-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {NavRoute} from '@/utils/routes'
import {useNavigate} from '@/utils/useNavigate'
import {
Change,
createHmId,
createPublicWebHmUrl,
formattedDateLong,
packHmId,
unpackHmId,
} from '@shm/shared'
import {hmId, UnpackedHypermediaId} from '@shm/shared/src/utils/entity-id-url'
Expand Down Expand Up @@ -84,7 +84,7 @@ function ChangeItem({
const navigate = useNavigate()
const openAccount = (e) => {
e.stopPropagation()
navigate({key: 'document', id: hmId('a', change.author)})
navigate({key: 'document', id: hmId('d', change.author)})
}
const navRoute = useNavRoute()
const isActive = new Set(activeVersion?.split('.') || []).has(change.id)
Expand Down Expand Up @@ -124,7 +124,7 @@ function ChangeItem({
const gwUrl = useGatewayUrl()
const publicWebUrl =
parsedEntityId &&
createPublicWebHmUrl(parsedEntityId?.type, parsedEntityId?.eid, {
createPublicWebHmUrl(parsedEntityId?.type, parsedEntityId?.uid, {
version: change.id,
hostname: gwUrl.data,
})
Expand All @@ -146,9 +146,11 @@ function ChangeItem({
icon: ArrowUpRight,
onPress: () => {
open(
createHmId(parsedEntityId.type, parsedEntityId.eid, {
version: change.id,
}),
packHmId(
hmId(parsedEntityId.type, parsedEntityId.uid, {
version: change.id,
}),
),
true,
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function BaseAccountLinkAvatar({
e.preventDefault()
e.stopPropagation()
if (!accountId) return appError('No account ready to load')
navigate({key: 'document', id: hmId('a', accountId)})
navigate({key: 'document', id: hmId('d', accountId)})
}}
position="relative"
height={size}
Expand Down
39 changes: 17 additions & 22 deletions frontend/apps/desktop/src/components/app-embeds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {
EntityComponentProps,
InlineEmbedComponentProps,
UnpackedHypermediaId,
createHmId,
formattedDateMedium,
getBlockNodeById,
getDocumentTitle,
hmId,
packHmId,
unpackHmId,
useDocContentContext,
} from '@shm/shared'
Expand Down Expand Up @@ -80,7 +80,7 @@ function EmbedWrapper({

useEffect(() => {
const val =
(routeParams?.documentId == unpackRef?.qid &&
(routeParams?.documentId == unpackRef?.id &&
routeParams?.version == unpackRef?.version &&
comment) ||
false
Expand All @@ -96,7 +96,7 @@ function EmbedWrapper({
routeParams?.documentId,
routeParams?.version,
comment,
unpackRef?.qid,
unpackRef?.id,
unpackRef?.version,
])

Expand Down Expand Up @@ -424,7 +424,7 @@ export function EmbedDocContent(props: EntityComponentProps) {
size="$2"
icon={ArrowUpRightSquare}
onPress={() => {
if (!props.qid) return
if (!props.id) return
navigate({
key: 'document',
id: props,
Expand All @@ -439,7 +439,7 @@ export function EmbedDocContent(props: EntityComponentProps) {
}

export function EmbedDocumentCard(props: EntityComponentProps) {
const docId = props.type == 'd' ? createHmId('d', props.eid) : undefined
const docId = props.type == 'd' ? packHmId(hmId('d', props.uid)) : undefined
const doc = useEntity(props)
let textContent = useMemo(() => {
if (doc.data?.document?.content) {
Expand Down Expand Up @@ -472,13 +472,10 @@ export function EmbedAccount(
props: EntityComponentProps,
parentBlockId: string | null,
) {
const profile = useEntity(props)
const entity = 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) {
if (entity.status == 'success') {
if (props.block?.attributes?.view == 'content' && entity.data) {
return <EmbedDocContent {...props} />
} else if (props.block?.attributes?.view == 'card') {
return (
Expand Down Expand Up @@ -511,9 +508,9 @@ export function EmbedAccount(
}

export function EmbedComment(props: EntityComponentProps) {
if (props?.type !== 'c')
if (props?.type !== 'comment')
throw new Error('Invalid props as ref for EmbedComment')
const comment = useComment(createHmId('c', props.eid), {
const comment = useComment(hmId('comment', props.uid), {
enabled: !!props,
})
let embedBlocks = useMemo(() => {
Expand Down Expand Up @@ -583,26 +580,24 @@ function AvatarComponent({accountId}: {accountId: string}) {
}

export function EmbedInline(props: InlineEmbedComponentProps) {
if (props?.type == 'a') {
return <AccountInlineEmbed {...props} />
} else if (props?.type == 'd') {
return <PublicationInlineEmbed {...props} />
if (props?.type == 'd') {
return <DocInlineEmbed {...props} />
} else {
console.error('Inline Embed Error', JSON.stringify(props))
return <InlineEmbedButton>??</InlineEmbedButton>
}
}

function AccountInlineEmbed(props: InlineEmbedComponentProps) {
const accountId = props?.type == 'a' ? props.eid : undefined
const accountId = props?.type == 'd' ? props.uid : undefined
if (!accountId)
throw new Error('Invalid props at AccountInlineEmbed (accountId)')
const accountQuery = useAccount_deprecated(accountId)
const navigate = useNavigate()
return (
<InlineEmbedButton
dataRef={props?.id}
onPress={() => navigate({key: 'document', id: hmId('a', accountId)})}
onPress={() => navigate({key: 'document', id: hmId('d', accountId)})}
>
{(accountId &&
accountQuery.status == 'success' &&
Expand All @@ -612,9 +607,9 @@ 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)')
function DocInlineEmbed(props: InlineEmbedComponentProps) {
const pubId = props?.type == 'd' ? props.id : undefined
if (!pubId) throw new Error('Invalid props at DocInlineEmbed (pubId)')
const doc = useEntity(props)
const navigate = useNavigate()
return (
Expand Down
4 changes: 2 additions & 2 deletions frontend/apps/desktop/src/components/citations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function PublicationCitationItem({mention}: {mention: Mention}) {
const docTextContent = useDocTextContent(doc.data?.document)
const destRoute: DocumentRoute = {
key: 'document',
documentId: unpackedSource!.qid,
documentId: unpackedSource!.id,
versionId: mention.sourceBlob?.cid,
blockId: mention.sourceContext,
}
Expand Down Expand Up @@ -149,7 +149,7 @@ function CommentCitationItem({mention}: {mention: Mention}) {
if (commentTarget) {
spawn({
key: 'document',
documentId: commentTarget.qid,
documentId: commentTarget.id,
versionId: commentTarget.version || undefined,
})
}
Expand Down
33 changes: 18 additions & 15 deletions frontend/apps/desktop/src/components/comments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import {
HMComment,
StateStream,
UnpackedHypermediaId,
createHmId,
formattedDateMedium,
getDocumentTitle,
hmId,
packHmId,
serializeBlockRange,
unpackHmId,
} from '@shm/shared'
Expand Down Expand Up @@ -49,11 +50,11 @@ import {WindowsLinuxWindowControls} from './window-controls'

export function CommentGroup({
group,
targetDocEid,
targetDocUid,
targetDocVersion,
}: {
group: CommentGroup
targetDocEid: string
targetDocUid: string
targetDocVersion: string
}) {
const createComment = useCreateComment()
Expand Down Expand Up @@ -81,7 +82,7 @@ export function CommentGroup({
label: 'Reply',
icon: Reply,
onPress: () => {
createComment(targetDocEid, targetDocVersion, comment.id)
createComment(targetDocUid, targetDocVersion, comment.id)
},
},
{
Expand Down Expand Up @@ -111,12 +112,14 @@ export function CommentGroup({
const quotingCommentId = unpackHmId(comment.id)
if (!targetId || !quotingCommentId) return
createComment(
targetDocEid,
targetDocUid,
targetDocVersion,
lastComment.id,
createHmId('c', quotingCommentId.eid, {
blockRef: blockId,
}),
packHmId(
hmId('comment', quotingCommentId.uid, {
blockRef: blockId,
}),
),
)
}}
/>
Expand Down Expand Up @@ -148,7 +151,7 @@ export function CommentGroup({
onPress={() => {
const lastComment = group.comments.at(-1)
if (!lastComment) return
createComment(targetDocEid, targetDocVersion, lastComment.id)
createComment(targetDocUid, targetDocVersion, lastComment.id)
}}
icon={Reply}
>
Expand All @@ -162,14 +165,14 @@ export function CommentGroup({

export function CommentThread({
targetCommentId,
targetDocEid,
targetDocUid,
onReplyBlock,
}: {
targetCommentId: string
targetDocEid: string
targetDocUid: string
onReplyBlock: (commentId: string, blockId: string) => void
}) {
const thread = useCommentReplies(targetCommentId, targetDocEid)
const thread = useCommentReplies(targetCommentId, targetDocUid)
return (
<>
<YStack borderBottomWidth={1} borderColor="$borderColor">
Expand Down Expand Up @@ -198,7 +201,7 @@ export function EntityCommentsAccessory({
activeVersion: string
}) {
const navigate = useNavigate()
const commentGroups = useDocumentCommentGroups(id.eid)
const commentGroups = useDocumentCommentGroups(id.uid)
const createComment = trpc.comments.createCommentDraft.useMutation()
return (
<AccessoryContainer
Expand All @@ -211,7 +214,7 @@ export function EntityCommentsAccessory({
onPress={() => {
createComment
.mutateAsync({
targetDocEid: id.eid,
targetDocUid: id.uid,
targetDocVersion: activeVersion,
targetCommentId: null,
})
Expand All @@ -233,7 +236,7 @@ export function EntityCommentsAccessory({
<CommentGroup
group={group}
key={group.id}
targetDocEid={id.eid}
targetDocUid={id.uid}
targetDocVersion={activeVersion}
/>
))}
Expand Down
Loading

0 comments on commit a3d9ae6

Please sign in to comment.