diff --git a/.changeset/tame-eagles-crash.md b/.changeset/tame-eagles-crash.md new file mode 100644 index 00000000..91ec3833 --- /dev/null +++ b/.changeset/tame-eagles-crash.md @@ -0,0 +1,6 @@ +--- +"create-hypergraph": patch +--- + +Update vite template to use usePublishToPublicSpace + \ No newline at end of file diff --git a/.changeset/tidy-ghosts-clean.md b/.changeset/tidy-ghosts-clean.md new file mode 100644 index 00000000..ff302c70 --- /dev/null +++ b/.changeset/tidy-ghosts-clean.md @@ -0,0 +1,6 @@ +--- +"@graphprotocol/hypergraph-react": patch +--- + +Add usePublishToPublicSpace hook + \ No newline at end of file diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 00000000..1d7ff813 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +pnpm 10.14.0 diff --git a/apps/create-hypergraph/template-vite-react/src/routes/private-space/$space-id.tsx b/apps/create-hypergraph/template-vite-react/src/routes/private-space/$space-id.tsx index 9fa19500..f1c26d4f 100644 --- a/apps/create-hypergraph/template-vite-react/src/routes/private-space/$space-id.tsx +++ b/apps/create-hypergraph/template-vite-react/src/routes/private-space/$space-id.tsx @@ -2,13 +2,11 @@ import { Button } from '@/components/ui/button'; import { Address } from '@/schema'; import { HypergraphSpaceProvider, - preparePublish, - publishOps, useCreateEntity, - useHypergraphApp, useQuery, useSpace, useSpaces, + usePublishToPublicSpace } from '@graphprotocol/hypergraph-react'; import { createFileRoute } from '@tanstack/react-router'; import { useState } from 'react'; @@ -34,7 +32,10 @@ function PrivateSpace() { const [selectedSpace, setSelectedSpace] = useState(''); const createAddress = useCreateEntity(Address); const [addressName, setAddressName] = useState(''); - const { getSmartSessionClient } = useHypergraphApp(); + const { mutate: publishToPublicSpace, isPending } = usePublishToPublicSpace({ + onSuccess: () => alert('Address published to public space'), + onError: () => alert('Error publishing address to public space') + }); if (!ready) { return ( @@ -53,31 +54,6 @@ function PrivateSpace() { setAddressName(''); }; - const publishToPublicSpace = async (address: Address) => { - if (!selectedSpace) { - alert('No space selected'); - return; - } - try { - const { ops } = await preparePublish({ entity: address, publicSpace: selectedSpace }); - const smartSessionClient = await getSmartSessionClient(); - if (!smartSessionClient) { - throw new Error('Missing smartSessionClient'); - } - const publishResult = await publishOps({ - ops, - space: selectedSpace, - name: 'Publish Address', - walletClient: smartSessionClient, - }); - console.log(publishResult, ops); - alert('Address published to public space'); - } catch (error) { - console.error(error); - alert('Error publishing address to public space'); - } - }; - return (
@@ -153,8 +129,8 @@ function PrivateSpace() {
))} diff --git a/packages/hypergraph-react/src/hooks/usePublishToSpace.ts b/packages/hypergraph-react/src/hooks/usePublishToSpace.ts new file mode 100644 index 00000000..3f763a19 --- /dev/null +++ b/packages/hypergraph-react/src/hooks/usePublishToSpace.ts @@ -0,0 +1,41 @@ +import type { Entity } from '@graphprotocol/hypergraph'; +import { type UseMutationOptions, useMutation } from '@tanstack/react-query'; +import { useHypergraphApp } from '../HypergraphAppContext.js'; +import { preparePublish } from '../prepare-publish.js'; +import { publishOps } from '../publish-ops.js'; +import type { OmitStrict } from '../types.js'; + +type Variables = { + entity: S; + spaceId: string; +}; + +type UsePublishToSpaceOptions = OmitStrict< + UseMutationOptions>, Error, Variables, unknown>, + 'mutationFn' | 'mutationKey' +>; + +export function usePublishToPublicSpace(options: UsePublishToSpaceOptions = {}) { + const { getSmartSessionClient } = useHypergraphApp(); + + return useMutation({ + ...options, + mutationFn: async ({ entity, spaceId }) => { + const { ops } = await preparePublish({ + entity, + publicSpace: spaceId, + }); + const smartSessionClient = await getSmartSessionClient(); + if (!smartSessionClient) { + throw new Error('Missing smartSessionClient'); + } + + return await publishOps({ + ops, + space: spaceId, + name: 'Published entity', + walletClient: smartSessionClient, + }); + }, + }); +} diff --git a/packages/hypergraph-react/src/index.ts b/packages/hypergraph-react/src/index.ts index e8485cd4..f2f80723 100644 --- a/packages/hypergraph-react/src/index.ts +++ b/packages/hypergraph-react/src/index.ts @@ -22,6 +22,7 @@ export { useExternalSpaceInbox } from './hooks/useExternalSpaceInbox.js'; export { useOwnAccountInbox } from './hooks/useOwnAccountInbox.js'; export { useOwnSpaceInbox } from './hooks/useOwnSpaceInbox.js'; export { usePublicAccountInboxes } from './hooks/usePublicAccountInboxes.js'; +export { usePublishToPublicSpace } from './hooks/usePublishToSpace.js'; export { generateDeleteOps as _generateDeleteOps } from './internal/generate-delete-ops.js'; export { useCreateEntityPublic as _useCreateEntityPublic } from './internal/use-create-entity-public.js'; export { useDeleteEntityPublic as _useDeleteEntityPublic } from './internal/use-delete-entity-public.js'; diff --git a/packages/hypergraph-react/src/types.ts b/packages/hypergraph-react/src/types.ts index 9be00c01..df31f3be 100644 --- a/packages/hypergraph-react/src/types.ts +++ b/packages/hypergraph-react/src/types.ts @@ -2,6 +2,8 @@ import type { Op } from '@graphprotocol/grc-20'; import type { Entity } from '@graphprotocol/hypergraph'; import type * as Schema from 'effect/Schema'; +export type OmitStrict = Pick>; + export type EntityLike = { id: string; [key: string]: unknown;