Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: segregate community list alphabetically #80

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 43 additions & 17 deletions src/pages/posts/CommunitiesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import { home, library, people } from "ionicons/icons";
import styled from "@emotion/styled";
import { pullAllBy, sortBy, uniqBy } from "lodash";
import { notEmpty } from "../../helpers/array";
import { useMemo, useRef } from "react";
import { Fragment, useMemo, useRef } from "react";
import { useSetActivePage } from "../../features/auth/AppContext";
import { useBuildGeneralBrowseLink } from "../../helpers/routes";
import ItemIcon from "../../features/labels/img/ItemIcon";
import { jwtSelector } from "../../features/auth/authSlice";
import { Community } from "lemmy-js-client";

const SubIcon = styled(IonIcon)<{ color: string }>`
border-radius: 50%;
Expand Down Expand Up @@ -86,6 +87,26 @@ export default function CommunitiesPage() {
return communities;
}, [follows, communityByHandle]);

const communitiesGroupedByLetter = useMemo(() => {
const alphabeticallySortedCommunities = sortBy(communities, (c) =>
c.name.toLowerCase()
);

return Object.entries(
alphabeticallySortedCommunities.reduce<Record<string, Community[]>>(
(acc, community) => {
const firstLetter = community.name[0].toUpperCase();
if (!acc[firstLetter]) {
acc[firstLetter] = [];
}
acc[firstLetter].push(community);
return acc;
},
{}
)
);
}, [communities]);

return (
<IonPage ref={pageRef}>
<IonHeader>
Expand Down Expand Up @@ -125,22 +146,27 @@ export default function CommunitiesPage() {
</IonItem>
</IonItemGroup>

<IonItemGroup>
<IonItemDivider>
<IonLabel>Communities</IonLabel>
</IonItemDivider>
</IonItemGroup>

{sortBy(communities, "name")?.map((community) => (
<IonItem
key={community.id}
routerLink={buildGeneralBrowseLink(`/c/${getHandle(community)}`)}
>
<Content>
<ItemIcon item={community} size={28} />
{getHandle(community)}
</Content>
</IonItem>
{communitiesGroupedByLetter.map(([letter, communities]) => (
<Fragment key={letter}>
<IonItemGroup>
<IonItemDivider>
<IonLabel>{letter}</IonLabel>
</IonItemDivider>
</IonItemGroup>
{communities.map((community) => (
<IonItem
key={community.id}
routerLink={buildGeneralBrowseLink(
`/c/${getHandle(community)}`
)}
>
<Content>
<ItemIcon item={community} size={28} />
{getHandle(community)}
</Content>
</IonItem>
))}
</Fragment>
))}
</IonList>
</AppContent>
Expand Down