Skip to content

Commit

Permalink
Move useChannelContext to store to fix circular dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
davidisaaclee committed Nov 22, 2024
1 parent 0c8d6d7 commit b861edf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
1 change: 0 additions & 1 deletion packages/shared/src/logic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ export * from './branch';
export * from './deeplinks';
export * from './analytics';
export * from './tiptap';
export { useChannelContext } from './useChannelContext';
1 change: 1 addition & 0 deletions packages/shared/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export * from './session';
export * from './contactActions';
export * from './clientActions';
export * from './lure';
export { useChannelContext } from './useChannelContext';
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<db.Post>();
const [editingPost, setEditingPost] = useState<Post>();

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,
Expand All @@ -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'
Expand Down

0 comments on commit b861edf

Please sign in to comment.