Skip to content

Commit

Permalink
Add cards collection layout
Browse files Browse the repository at this point in the history
  • Loading branch information
davidisaaclee committed Oct 23, 2024
1 parent cadf53e commit f27d449
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/shared/src/api/channelContentConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export enum CollectionRendererId {
notebook = 'tlon.r0.collection.notebook',
chat = 'tlon.r0.collection.chat',
gallery = 'tlon.r0.collection.gallery',
cards = 'tlon.r0.collection.cards',
}

export enum DraftInputId {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ function labelForCollectionLayout(l: CollectionRendererId): string {
return 'Gallery';
case CollectionRendererId.notebook:
return 'Notebook';
case CollectionRendererId.cards:
return 'Cards';
}
}

Expand Down Expand Up @@ -216,6 +218,7 @@ const CustomChannelConfigurationForm = forwardRef<{
CollectionRendererId.chat,
CollectionRendererId.gallery,
CollectionRendererId.notebook,
CollectionRendererId.cards,
].map((id) => ({
title: labelForCollectionLayout(id),
value: id,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { forwardRef, useEffect, useImperativeHandle, useState } from 'react';
import { Text, View, useWindowDimensions } from 'tamagui';

import { usePostCollectionContextUnsafelyUnwrapped } from '../../contexts/postCollection';
import { ConnectedPostView, IPostCollectionView } from './shared';

export const CardsPostCollection: IPostCollectionView = forwardRef(
function CardsPostCollection(_props, forwardedRef) {
const ctx = usePostCollectionContextUnsafelyUnwrapped();
useImperativeHandle(forwardedRef, () => ({
scrollToPostAtIndex(_index: number) {
console.warn('not implemented');
},
}));

const [displayedIndex, setDisplayedIndex] = useState<number | null>(null);

useEffect(() => {
if (ctx.posts && displayedIndex == null) {
setDisplayedIndex(0);
}

if (
ctx.posts &&
displayedIndex != null &&
displayedIndex > ctx.posts.length
) {
setDisplayedIndex(ctx.posts.length - 1);
}
}, [ctx.posts, displayedIndex]);

const displayedPost = ctx.posts?.[displayedIndex ?? 0];

const windowDimensions = useWindowDimensions();

return (
<View
onPress={(event) => {
if (displayedIndex != null && ctx.posts != null) {
const delta =
event.nativeEvent.locationY < windowDimensions.width * 0.5
? -1
: 1;
setDisplayedIndex((displayedIndex + delta) % ctx.posts.length);
} else {
setDisplayedIndex(null);
}
}}
alignItems="stretch"
justifyContent="center"
flex={1}
>
{displayedPost == null ? (
<Text>Loading...</Text>
) : (
<ConnectedPostView post={displayedPost} showAuthor />
)}
</View>
);
}
);
2 changes: 2 additions & 0 deletions packages/ui/src/contexts/componentsKits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
GalleryInput,
NotebookInput,
} from '../components/draftInputs';
import { CardsPostCollection } from '../components/postCollectionViews/CardsPostCollectionView';
import { ListPostCollection } from '../components/postCollectionViews/ListPostCollectionView';
import { IPostCollectionView } from '../components/postCollectionViews/shared';

Expand Down Expand Up @@ -105,6 +106,7 @@ const BUILTIN_COLLECTION_RENDERERS: {
[CollectionRendererId.chat]: ListPostCollection,
[CollectionRendererId.gallery]: ListPostCollection,
[CollectionRendererId.notebook]: ListPostCollection,
[CollectionRendererId.cards]: CardsPostCollection,
};

export function ComponentsKitContextProvider({
Expand Down

0 comments on commit f27d449

Please sign in to comment.