-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathshared.tsx
68 lines (56 loc) · 2 KB
/
shared.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { JSONValue } from '@tloncorp/shared';
import * as db from '@tloncorp/shared/db';
import * as store from '@tloncorp/shared/store';
import { ComponentPropsWithoutRef, useCallback, useMemo } from 'react';
import { useLivePost } from '../../contexts';
import { RenderItemType } from '../../contexts/componentsKits';
import { usePostCollectionContext } from '../../contexts/postCollection';
import { PostView } from '../Channel/PostView';
export interface PostCollectionHandle {
scrollToPostAtIndex?: (index: number) => void;
}
export type IPostCollectionView = React.ForwardRefExoticComponent<
React.RefAttributes<PostCollectionHandle>
>;
export function ConnectedPostView({
post,
...overrides
}: { post: db.Post } & Partial<ComponentPropsWithoutRef<typeof PostView>>) {
const ctx = usePostCollectionContext();
// this code is duplicated in packages/ui/components/Channel/PostView.tsx
const standardConfig = useMemo(() => {
const cfg = ctx.collectionConfiguration;
if (cfg == null) {
return null;
}
return {
showAuthor: JSONValue.asBoolean(cfg.showAuthors, false),
showReplies: JSONValue.asBoolean(cfg.showReplies, false),
};
}, [ctx.collectionConfiguration]);
const editPost = useCallback<
Exclude<ComponentPropsWithoutRef<RenderItemType>['editPost'], undefined>
>(async (post, content) => {
await store.editPost({
post,
content,
});
}, []);
const livePost = useLivePost(post);
const postProps = useMemo(
() => ({
post: livePost,
onPress: ctx.goToPost,
isHighlighted: ctx.selectedPostId === livePost.id,
onPressRetry: () => ctx.onPressRetry(livePost),
onPressDelete: () => ctx.onPressDelete(livePost),
editPost,
// TODO: more props to get parity with BaseScrollerItem
...overrides,
showAuthor: standardConfig?.showAuthor,
showReplies: standardConfig?.showReplies,
}),
[ctx, livePost, overrides, standardConfig, editPost]
);
return <PostView {...postProps} />;
}