-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add collections screens and collection controls to thread list
- Loading branch information
1 parent
fcbe399
commit 0daa60a
Showing
9 changed files
with
249 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { | ||
Menu, | ||
MenuButton, | ||
MenuDivider, | ||
MenuGroup, | ||
MenuItem, | ||
MenuList, | ||
} from "@chakra-ui/react"; | ||
import { MinusIcon, PlusIcon } from "@heroicons/react/24/solid"; | ||
|
||
import { Bookmark, BookmarkSolid } from "../Action/Action"; | ||
|
||
import { Props, useCollectionMenu } from "./useCollectionMenu"; | ||
|
||
export function CollectionMenu(props: Props) { | ||
const { collections, isAlreadySaved, onSelect } = useCollectionMenu(props); | ||
|
||
if (!collections) return null; | ||
|
||
return ( | ||
<Menu | ||
preventOverflow={true} | ||
modifiers={[ | ||
{ | ||
name: "preventOverflow", | ||
options: { | ||
altAxis: true, | ||
padding: { bottom: 82 }, | ||
}, | ||
}, | ||
]} | ||
> | ||
<MenuButton | ||
title="Add to collections" | ||
as={isAlreadySaved ? BookmarkSolid : Bookmark} | ||
/> | ||
<MenuList title="Add to collections"> | ||
<MenuGroup title="Add to collections"> | ||
<MenuDivider /> | ||
{collections.map((c) => ( | ||
<MenuItem | ||
key={c.id} | ||
icon={ | ||
c.hasPost ? ( | ||
<MinusIcon width="1.4em" /> | ||
) : ( | ||
<PlusIcon width="1.4em" /> | ||
) | ||
} | ||
onClick={onSelect(c)} | ||
> | ||
{c.name} | ||
</MenuItem> | ||
))} | ||
</MenuGroup> | ||
</MenuList> | ||
</Menu> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { useToast } from "@chakra-ui/react"; | ||
import { mutate } from "swr"; | ||
|
||
import { | ||
collectionAddPost, | ||
collectionRemovePost, | ||
useCollectionList, | ||
} from "src/api/openapi/collections"; | ||
import { ThreadReference } from "src/api/openapi/schemas"; | ||
import { getThreadListKey } from "src/api/openapi/threads"; | ||
import { useSession } from "src/auth"; | ||
|
||
export type Props = { | ||
thread: ThreadReference; | ||
}; | ||
|
||
type CollectionState = { | ||
id: string; | ||
name: string; | ||
hasPost: boolean; | ||
}; | ||
|
||
export function useCollectionMenu(props: Props) { | ||
const account = useSession(); | ||
const toast = useToast(); | ||
const collectionList = useCollectionList(); | ||
|
||
const postCollections = new Set(props.thread.collections.map((c) => c.id)); | ||
const isAlreadySaved = Boolean( | ||
props.thread.collections.filter((c) => c.owner.id === account?.id).length | ||
); | ||
|
||
const collections: CollectionState[] = | ||
collectionList.data?.collections.map((c) => ({ | ||
id: c.id, | ||
name: c.name, | ||
hasPost: postCollections.has(c.id), | ||
})) ?? []; | ||
|
||
const onSelect = (c: CollectionState) => async () => { | ||
if (postCollections.has(c.id)) { | ||
await collectionRemovePost(c.id, props.thread.id); | ||
toast({ title: `Removed from ${c.name}` }); | ||
} else { | ||
await collectionAddPost(c.id, props.thread.id); | ||
toast({ title: `Added to ${c.name}` }); | ||
} | ||
console.log(getThreadListKey()); | ||
await mutate(getThreadListKey({})); | ||
}; | ||
|
||
return { | ||
error: collectionList.error, | ||
collections: collections, | ||
isAlreadySaved, | ||
onSelect, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Flex, Heading, LinkBox, LinkOverlay, Text } from "@chakra-ui/react"; | ||
import NextLink from "next/link"; | ||
|
||
import { CollectionItem } from "src/api/openapi/schemas"; | ||
import { Byline } from "src/screens/thread/components/Byline"; | ||
|
||
export function CollectionItem(props: { item: CollectionItem }) { | ||
const permalink = `/t/${props.item.slug}`; | ||
|
||
return ( | ||
<Flex as="section" flexDir="column" py={2} width="full" gap={2}> | ||
<LinkBox as="article"> | ||
<Flex justifyContent="space-between"> | ||
<Heading size="sm"> | ||
<LinkOverlay as={NextLink} href={permalink}> | ||
{props.item.title} | ||
</LinkOverlay> | ||
</Heading> | ||
</Flex> | ||
|
||
<Text noOfLines={3}>{props.item.short}</Text> | ||
</LinkBox> | ||
|
||
<Flex justifyContent="space-between"> | ||
<Byline | ||
href={permalink} | ||
author={props.item.author.handle} | ||
time={new Date(props.item.createdAt)} | ||
updated={new Date(props.item.updatedAt)} | ||
// more={<ThreadMenu {...props.item} />} | ||
/> | ||
|
||
{/* Tags list */} | ||
</Flex> | ||
</Flex> | ||
); | ||
} |
21 changes: 21 additions & 0 deletions
21
web/src/screens/collection/components/CollectionItemList.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Divider, List } from "@chakra-ui/react"; | ||
import { Fragment } from "react"; | ||
|
||
import { CollectionItem as CollectionItemSchema } from "src/api/openapi/schemas"; | ||
|
||
import { CollectionItem } from "./CollectionItem"; | ||
|
||
type Props = { items: CollectionItemSchema[] }; | ||
|
||
export function CollectionItemList(props: Props) { | ||
return ( | ||
<List width="full" display="flex" flexDirection="column"> | ||
{props.items.map((t) => ( | ||
<Fragment key={t.id}> | ||
<Divider /> | ||
<CollectionItem key={t.id} item={t} /> | ||
</Fragment> | ||
))} | ||
</List> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0daa60a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
storyden-homepage – ./home
storyden-homepage-git-main-southclaws.vercel.app
storyden-homepage-southclaws.vercel.app
storyden.org
storyden-homepage.vercel.app
www.storyden.org