Skip to content

Commit

Permalink
Renaming and fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvicenti committed Jun 24, 2024
1 parent 480f340 commit e798c71
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 60 deletions.
3 changes: 3 additions & 0 deletions frontend/apps/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"dependencies": {
"@ariakit/react": "0.3.5",
"@bufbuild/protobuf": "^1.10.0",
"@connectrpc/connect-web": "1.1.3",
"@electron-forge/maker-dmg": "7.1.0",
"@electron-forge/maker-rpm": "7.1.0",
Expand All @@ -39,6 +40,8 @@
"@shm/prettier": "*",
"@shm/shared": "*",
"@shm/ui": "*",
"@tanstack/react-query": "4.33.0",
"@tanstack/react-query-devtools": "4.33.0",
"@trpc/client": "10.40.0",
"@trpc/react-query": "10.40.0",
"@trpc/server": "10.40.0",
Expand Down
52 changes: 52 additions & 0 deletions frontend/apps/desktop/src/models/documents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {toPlainMessage} from '@bufbuild/protobuf'
import {useGRPCClient} from '@shm/app/app-context'
import {queryKeys} from '@shm/app/models/query-keys'
import {GRPCClient, HMDocument} from '@shm/shared'
import {useQueries, useQuery, UseQueryOptions} from '@tanstack/react-query'

export function useDocument(
docId: string | undefined,
version: string | undefined,
options: UseQueryOptions<HMDocument | null> & {
draftId?: string
},
) {
const grpcClient = useGRPCClient()
return useQuery(queryDocument({docId, version, grpcClient, ...options}))
}

export function useDocuments(
ids: string[],
options?: UseQueryOptions<HMDocument | null>,
) {
const grpcClient = useGRPCClient()
return useQueries({
queries: ids.map((docId) => queryDocument({docId, grpcClient})),
...(options || {}),
})
}

export function queryDocument({
docId,
version,
grpcClient,
...options
}: {
docId?: string
version?: string
grpcClient: GRPCClient
} & UseQueryOptions<HMDocument | null>): UseQueryOptions<HMDocument | null> {
return {
enabled: !!docId,
queryKey: [queryKeys.EDITOR_DRAFT, docId],
useErrorBoundary: false,
queryFn: async () => {
const doc = await grpcClient.documents.getDocument({
documentId: docId,
version: version || '',
})
return toPlainMessage(doc)
},
...options,
}
}
2 changes: 1 addition & 1 deletion frontend/packages/app/components/edit-doc-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function useEditDraft(
isProfileDocument,
})
invalidate([queryKeys.GET_DRAFT_LIST])
invalidate([queryKeys.GET_PUBLICATION_DRAFTS, draft.id])
invalidate([queryKeys.DOCUMENT_DRAFTS, draft.id])
} catch (error: any) {
if (
error?.message.match('[failed_precondition]') &&
Expand Down
2 changes: 1 addition & 1 deletion frontend/packages/app/models/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function useSetTrusted(
invalidate([queryKeys.RESOURCE_FEED_LATEST_EVENT])
invalidate([queryKeys.GET_ACCOUNT, input.accountId])
invalidate([queryKeys.GET_ALL_ACCOUNTS])
invalidate([queryKeys.GET_PUBLICATION_LIST, 'trusted'])
invalidate([queryKeys.DOCUMENT_LIST, 'trusted'])
opts?.onSuccess?.(result, input, ctx)
},
...opts,
Expand Down
51 changes: 23 additions & 28 deletions frontend/packages/app/models/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ export function usePublicationList(
const grpcClient = useGRPCClient()
const pubListQuery = useInfiniteQuery({
...queryOpts,
queryKey: [
queryKeys.GET_PUBLICATION_LIST,
trustedOnly ? 'trusted' : 'global',
],
queryKey: [queryKeys.DOCUMENT_LIST, trustedOnly ? 'trusted' : 'global'],
refetchOnMount: true,
queryFn: async (context) => {
const result = await grpcClient.publications.listPublications({
Expand Down Expand Up @@ -178,7 +175,7 @@ export function useDeleteDraft(
onSuccess: (response, documentId, context) => {
setTimeout(() => {
invalidate([queryKeys.GET_DRAFT_LIST])
invalidate([queryKeys.GET_PUBLICATION_DRAFTS, documentId])
invalidate([queryKeys.DOCUMENT_DRAFTS, documentId])
invalidate([queryKeys.ENTITY_TIMELINE, documentId])
invalidate([queryKeys.EDITOR_DRAFT, documentId])
queryClient.client.removeQueries([queryKeys.EDITOR_DRAFT, documentId])
Expand Down Expand Up @@ -227,7 +224,7 @@ export function queryPublication(
versionId?: string,
): UseQueryOptions<HMPublication> | FetchQueryOptions<HMPublication> {
return {
queryKey: [queryKeys.GET_PUBLICATION, documentId, versionId],
queryKey: [queryKeys.DOCUMENT, documentId, versionId],
enabled: !!documentId,
// retry: false, // to test error handling faster
// default is 5. the backend waits ~1s for discovery, so we retry for a little while in case document is on its way.
Expand Down Expand Up @@ -453,10 +450,10 @@ export function usePublishDraft(
opts?.onSuccess?.(result, variables, context)
invalidate([queryKeys.FEED_LATEST_EVENT])
invalidate([queryKeys.RESOURCE_FEED_LATEST_EVENT])
invalidate([queryKeys.GET_PUBLICATION_LIST])
invalidate([queryKeys.DOCUMENT_LIST])
invalidate([queryKeys.GET_DRAFT_LIST])
invalidate([queryKeys.GET_PUBLICATION_DRAFTS, documentId])
invalidate([queryKeys.GET_PUBLICATION, documentId])
invalidate([queryKeys.DOCUMENT_DRAFTS, documentId])
invalidate([queryKeys.DOCUMENT, documentId])
invalidate([queryKeys.ENTITY_TIMELINE, documentId])
invalidate([queryKeys.GET_ALL_ACCOUNTS]) // accounts invalidate because profile doc may be updated
invalidate([queryKeys.GET_ACCOUNT, myAccount.data?.id])
Expand Down Expand Up @@ -499,14 +496,14 @@ type MoveBlockAction = {
}

export function useDraft({
documentId,
draftId,
...options
}: UseQueryOptions<HMDocument | null> & {
documentId?: string
draftId?: string
}) {
const grpcClient = useGRPCClient()
const diagnosis = useDraftDiagnosis()
return useQuery(queryDraft({documentId, grpcClient, diagnosis, ...options}))
return useQuery(queryDraft({draftId, grpcClient, diagnosis, ...options}))
}

export function useDrafts(
Expand All @@ -515,44 +512,42 @@ export function useDrafts(
) {
const grpcClient = useGRPCClient()
return useQueries({
queries: ids.map((draftId) =>
queryDraft({documentId: draftId, grpcClient}),
),
queries: ids.map((draftId) => queryDraft({draftId: draftId, grpcClient})),
...(options || {}),
})
}

export function queryDraft({
documentId,
draftId,
grpcClient,
diagnosis,
...options
}: {
documentId?: string
draftId?: string
grpcClient: GRPCClient
diagnosis?: ReturnType<typeof useDraftDiagnosis>
} & UseQueryOptions<HMDocument | null>): UseQueryOptions<HMDocument | null> {
return {
enabled: !!documentId,
queryKey: [queryKeys.EDITOR_DRAFT, documentId],
enabled: !!draftId,
queryKey: [queryKeys.EDITOR_DRAFT, draftId],
useErrorBoundary: false,
queryFn: async () => {
try {
let serverDraft = await grpcClient.drafts.getDraft({
documentId,
draftId,
})

// const doc = serverDraft
const doc = serverDraft ? hmDocument(serverDraft) : null

diagnosis?.append(documentId!, {
diagnosis?.append(draftId!, {
key: 'getDraft',
value: doc,
})

return doc
} catch (error) {
diagnosis?.append(documentId!, {
diagnosis?.append(draftId!, {
key: 'getDraftError',
value: JSON.stringify(error),
})
Expand Down Expand Up @@ -634,7 +629,7 @@ export function useDraftEditor({
// @ts-expect-error
if (event.output) {
invalidate([queryKeys.GET_DRAFT_LIST])
invalidate([queryKeys.GET_PUBLICATION_DRAFTS, documentId])
invalidate([queryKeys.DOCUMENT_DRAFTS, documentId])
invalidate([queryKeys.EDITOR_DRAFT, documentId])
}
},
Expand All @@ -661,7 +656,7 @@ export function useDraftEditor({
value: `Delete draft ${documentId} success`,
})
invalidate([queryKeys.GET_DRAFT_LIST])
invalidate([queryKeys.GET_PUBLICATION_DRAFTS, documentId])
invalidate([queryKeys.DOCUMENT_DRAFTS, documentId])
})
} catch (error) {
diagnosis?.append(documentId!, {
Expand Down Expand Up @@ -754,7 +749,7 @@ export function useDraftEditor({
})

invalidate([queryKeys.GET_DRAFT_LIST])
invalidate([queryKeys.GET_PUBLICATION_DRAFTS, documentId])
invalidate([queryKeys.DOCUMENT_DRAFTS, documentId])

return res
})
Expand All @@ -776,7 +771,7 @@ export function useDraftEditor({
})

invalidate([queryKeys.GET_DRAFT_LIST])
invalidate([queryKeys.GET_PUBLICATION_DRAFTS, documentId])
invalidate([queryKeys.DOCUMENT_DRAFTS, documentId])

return newDraft
} catch (error) {
Expand Down Expand Up @@ -883,7 +878,7 @@ export function useDraftEditor({
title: state.context.title,
}).then(() => {
invalidate([queryKeys.GET_DRAFT_LIST])
invalidate([queryKeys.GET_PUBLICATION_DRAFTS, documentId])
invalidate([queryKeys.DOCUMENT_DRAFTS, documentId])
invalidate([queryKeys.EDITOR_DRAFT, documentId])
})
}
Expand Down Expand Up @@ -1431,7 +1426,7 @@ export function useAccountPublicationFullList(
export function useAccountPublications(accountId?: string | undefined) {
const grpcClient = useGRPCClient()
return useQuery({
queryKey: [queryKeys.GET_ACCOUNT_PUBLICATIONS, accountId],
queryKey: [queryKeys.ACCOUNT_DOCUMENTS, accountId],
enabled: !!accountId,
queryFn: async () => {
const result = await grpcClient.publications.listAccountPublications({
Expand Down
12 changes: 6 additions & 6 deletions frontend/packages/app/models/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ export function useDeleteEntity(
) => {
const hmId = unpackHmId(variables.id)
if (hmId?.type === 'd') {
invalidate([queryKeys.GET_PUBLICATION, variables.id])
invalidate([queryKeys.GET_ACCOUNT_PUBLICATIONS])
invalidate([queryKeys.GET_PUBLICATION_LIST])
invalidate([queryKeys.DOCUMENT, variables.id])
invalidate([queryKeys.ACCOUNT_DOCUMENTS])
invalidate([queryKeys.DOCUMENT_LIST])
} else if (hmId?.type === 'a') {
invalidate([queryKeys.GET_ALL_ACCOUNTS])
invalidate([queryKeys.GET_ACCOUNT, hmId.eid])
Expand Down Expand Up @@ -95,9 +95,9 @@ export function useUndeleteEntity(
onSuccess: (result: void, variables: {id: string}, context) => {
const hmId = unpackHmId(variables.id)
if (hmId?.type === 'd') {
invalidate([queryKeys.GET_PUBLICATION, variables.id])
invalidate([queryKeys.GET_ACCOUNT_PUBLICATIONS])
invalidate([queryKeys.GET_PUBLICATION_LIST])
invalidate([queryKeys.DOCUMENT, variables.id])
invalidate([queryKeys.ACCOUNT_DOCUMENTS])
invalidate([queryKeys.DOCUMENT_LIST])
} else if (hmId?.type === 'a') {
invalidate([queryKeys.GET_ALL_ACCOUNTS])
invalidate([queryKeys.GET_ACCOUNT, hmId.eid])
Expand Down
6 changes: 5 additions & 1 deletion frontend/packages/app/models/publication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {queryKeys} from './query-keys'
export function useDocumentDrafts(docId: string | undefined) {
const grpcClient = useGRPCClient()
const drafts = useQuery({
queryKey: [queryKeys.GET_PUBLICATION_DRAFTS, docId],
queryKey: [queryKeys.DOCUMENT_DRAFTS, docId],
enabled: !!docId,
queryFn: async () => {
const result = await grpcClient.drafts.listDocumentDrafts({
Expand All @@ -24,3 +24,7 @@ export function useDocumentDrafts(docId: string | undefined) {
})
return drafts
}

export function usePublicationVariant() {
throw new Error('not implemented right now')
}
14 changes: 7 additions & 7 deletions frontend/packages/app/models/query-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ export const queryKeys = {

// documents
GET_DRAFT_LIST: 'GET_DRAFT_LIST', //
GET_PUBLICATION_DRAFTS: 'GET_PUBLICATION_DRAFTS', //, docId: string
GET_ACCOUNT_PUBLICATIONS: 'GET_ACCOUNT_PUBLICATIONS', //, accountId: string
GET_PUBLICATION_LIST: 'GET_PUBLICATION_LIST', // 'trusted' | 'global'
DOCUMENT_DRAFTS: 'DOCUMENT_DRAFTS', //, docId: string
ACCOUNT_DOCUMENTS: 'ACCOUNT_DOCUMENTS', //, accountId: string
DOCUMENT_LIST: 'DOCUMENT_LIST', // 'trusted' | 'global'
EDITOR_DRAFT: 'EDITOR_DRAFT', // , docId: string
GET_PUBLICATION: 'GET_PUBLICATION', //, docId: string, versionId?: string
DOCUMENT: 'DOCUMENT', //, docId: string, versionId?: string

// comments
COMMENT: 'COMMENT', //, commentId: string
Expand Down Expand Up @@ -95,13 +95,13 @@ export function labelOfQueryKey(key: QueryKey) {
// documents
case queryKeys.GET_DRAFT_LIST:
return 'Drafts'
case queryKeys.GET_ACCOUNT_PUBLICATIONS:
case queryKeys.ACCOUNT_DOCUMENTS:
return 'Account Publications'
case queryKeys.GET_PUBLICATION_LIST:
case queryKeys.DOCUMENT_LIST:
return 'Publications'
case queryKeys.EDITOR_DRAFT:
return `Editor Draft ${abbreviateCid(arg1)}`
case queryKeys.GET_PUBLICATION:
case queryKeys.DOCUMENT:
return `Publication ${abbreviateCid(arg1)}`

// comments
Expand Down
2 changes: 1 addition & 1 deletion frontend/packages/app/pages/main.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useListen } from '@shm/app/app-context'

import { AppErrorPage } from '@shm/app//components/app-error'
import { AppErrorPage } from '@shm/app/components/app-error'
import { TitleBar } from '@shm/app/components/titlebar'
import { getRouteKey, useNavRoute } from '@shm/app/utils/navigation'
import { useNavigate } from '@shm/app/utils/useNavigate'
Expand Down
2 changes: 1 addition & 1 deletion frontend/packages/app/utils/open-draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function useOpenDraft(navigateMode: NavMode = 'spawn') {
contextRoute,
}
invalidate([queryKeys.GET_DRAFT_LIST])
invalidate([queryKeys.GET_PUBLICATION_DRAFTS, docId])
invalidate([queryKeys.DOCUMENT_DRAFTS, docId])
navigate(draftRoute)
})
.catch((err) => {
Expand Down
2 changes: 2 additions & 0 deletions frontend/packages/shared/src/client/grpc-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export * from './.generated/accounts/v2alpha/accounts_connect'
export * from './.generated/accounts/v2alpha/accounts_pb'
export * from './.generated/activity/v1alpha/activity_connect'
export * from './.generated/activity/v1alpha/activity_pb'
export * from './.generated/daemon/v1alpha/daemon_connect'
export * from './.generated/daemon/v1alpha/daemon_pb'
export * from './.generated/documents/v2alpha/changes_connect'
Expand Down
3 changes: 3 additions & 0 deletions frontend/packages/shared/src/grpc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Comments,
ContentGraph,
Daemon,
Documents,
Drafts,
Entities,
Merge,
Expand All @@ -17,6 +18,7 @@ export type GRPCClient = {
comments: PromiseClient<typeof Comments>
contentGraph: PromiseClient<typeof ContentGraph>
daemon: PromiseClient<typeof Daemon>
documents: PromiseClient<typeof Documents>
drafts: PromiseClient<typeof Drafts>
entities: PromiseClient<typeof Entities>
networking: PromiseClient<typeof Networking>
Expand All @@ -30,6 +32,7 @@ export function createGRPCClient(transport: any): GRPCClient {
comments: createPromiseClient(Comments, transport),
contentGraph: createPromiseClient(ContentGraph, transport),
daemon: createPromiseClient(Daemon, transport),
documents: createPromiseClient(Documents, transport),
drafts: createPromiseClient(Drafts, transport),
entities: createPromiseClient(Entities, transport),
networking: createPromiseClient(Networking, transport),
Expand Down
Loading

0 comments on commit e798c71

Please sign in to comment.