From b861edfd8e34f1f5dd62d2e4d16f3d7f4c885e8a Mon Sep 17 00:00:00 2001 From: David Lee Date: Fri, 22 Nov 2024 10:20:29 -0800 Subject: [PATCH] Move useChannelContext to store to fix circular dependencies --- packages/shared/src/logic/index.ts | 1 - packages/shared/src/store/index.ts | 1 + .../src/{logic => store}/useChannelContext.ts | 30 ++++++++++--------- 3 files changed, 17 insertions(+), 15 deletions(-) rename packages/shared/src/{logic => store}/useChannelContext.ts (76%) diff --git a/packages/shared/src/logic/index.ts b/packages/shared/src/logic/index.ts index 226e9dc9a1..b7fbc5db2a 100644 --- a/packages/shared/src/logic/index.ts +++ b/packages/shared/src/logic/index.ts @@ -8,4 +8,3 @@ export * from './branch'; export * from './deeplinks'; export * from './analytics'; export * from './tiptap'; -export { useChannelContext } from './useChannelContext'; diff --git a/packages/shared/src/store/index.ts b/packages/shared/src/store/index.ts index 56ae20b78c..a8e69d9439 100644 --- a/packages/shared/src/store/index.ts +++ b/packages/shared/src/store/index.ts @@ -17,3 +17,4 @@ export * from './session'; export * from './contactActions'; export * from './clientActions'; export * from './lure'; +export { useChannelContext } from './useChannelContext'; diff --git a/packages/shared/src/logic/useChannelContext.ts b/packages/shared/src/store/useChannelContext.ts similarity index 76% rename from packages/shared/src/logic/useChannelContext.ts rename to packages/shared/src/store/useChannelContext.ts index 55e332c705..20444f21db 100644 --- a/packages/shared/src/logic/useChannelContext.ts +++ b/packages/shared/src/store/useChannelContext.ts @@ -1,10 +1,12 @@ import { useCallback, useEffect, useMemo, useState } from 'react'; -import * as db from '../db'; +import type { Post, PostMetadata } from '../db'; import * as kv from '../db/keyValue'; -import * as store from '../store'; -import * as urbit from '../urbit'; -import { JSONContent } from '../urbit'; +import type { JSONContent, Story } from '../urbit'; +import * as dbHooks from './dbHooks'; +import * as postActions from './postActions'; +import { SyncPriority, syncGroup } from './sync'; +import { useNegotiate } from './useNegotiation'; export const useChannelContext = ({ channelId, @@ -20,36 +22,36 @@ export const useChannelContext = ({ // const storage = useStorageUnsafelyUnwrapped(); // Model context - const channelQuery = store.useChannelWithRelations({ + const channelQuery = dbHooks.useChannelWithRelations({ id: channelId, }); - const groupQuery = store.useGroup({ + const groupQuery = dbHooks.useGroup({ id: channelQuery.data?.groupId ?? '', }); useEffect(() => { if (channelQuery.data?.groupId) { - store.syncGroup(channelQuery.data?.groupId, { - priority: store.SyncPriority.Low, + syncGroup(channelQuery.data?.groupId, { + priority: SyncPriority.Low, }); } }, [channelQuery.data?.groupId]); // Post editing - const [editingPost, setEditingPost] = useState(); + const [editingPost, setEditingPost] = useState(); const editPost = useCallback( async ( - post: db.Post, - content: urbit.Story, + post: Post, + content: Story, parentId?: string, - metadata?: db.PostMetadata + metadata?: PostMetadata ) => { if (!channelQuery.data) { return; } - store.editPost({ + postActions.editPost({ post, content, parentId, @@ -63,7 +65,7 @@ export const useChannelContext = ({ // Version negotiation const channelHost = useMemo(() => channelId.split('/')[1], [channelId]); - const negotiationStatus = store.useNegotiate( + const negotiationStatus = useNegotiate( channelHost, 'channels', 'channels-server'