Skip to content

Commit

Permalink
Merge pull request #678
Browse files Browse the repository at this point in the history
adding Community service and community mod list
  • Loading branch information
gkasdorf authored Jul 17, 2023
2 parents d63b888 + 34b8b2a commit 84e3e17
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 29 deletions.
50 changes: 25 additions & 25 deletions Stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ function FeedStackScreen() {
/>
<FeedStack.Screen name="Post" component={PostScreen} />
<FeedStack.Screen name="Community" component={CommunityFeedScreen} />
<FeedStack.Screen
name="CommunityAbout"
component={CommunityAboutScreen}
options={{ title: t("About") }}
/>
<FeedStack.Screen name="Profile" component={UserProfileScreen} />
<FeedStack.Screen
name="UserComments"
Expand Down Expand Up @@ -138,11 +143,6 @@ function FeedStackScreen() {
component={NewPostBodyScreen}
options={{ title: t("New Post") }}
/>
<FeedStack.Screen
name="CommunityAbout"
component={CommunityAboutScreen}
options={{ title: t("About") }}
/>
</FeedStack.Group>
</FeedStack.Navigator>
);
Expand Down Expand Up @@ -173,6 +173,11 @@ function InboxStackScreen() {
<InboxStack.Screen name="Post" component={PostScreen} />
<InboxStack.Screen name="Community" component={CommunityFeedScreen} />
<InboxStack.Screen name="Profile" component={UserProfileScreen} />
<InboxStack.Screen
name="CommunityAbout"
component={CommunityAboutScreen}
options={{ title: t("About") }}
/>
<InboxStack.Screen
name="UserComments"
component={UserCommentsScreen}
Expand Down Expand Up @@ -221,11 +226,6 @@ function InboxStackScreen() {
component={NewPostBodyScreen}
options={{ title: t("New Post") }}
/>
<InboxStack.Screen
name="CommunityAbout"
component={CommunityAboutScreen}
options={{ title: t("About") }}
/>
</InboxStack.Group>
</InboxStack.Navigator>
);
Expand Down Expand Up @@ -362,6 +362,11 @@ function ProfileStackScreen() {
/>
<ProfileStack.Screen name="Post" component={PostScreen} />
<ProfileStack.Screen name="Community" component={CommunityFeedScreen} />
<ProfileStack.Screen
name="CommunityAbout"
component={CommunityAboutScreen}
options={{ title: t("About") }}
/>
</ProfileStack.Group>

<ProfileStack.Group
Expand Down Expand Up @@ -390,11 +395,6 @@ function ProfileStackScreen() {
component={NewPostBodyScreen}
options={{ title: t("New Post") }}
/>
<ProfileStack.Screen
name="CommunityAbout"
component={CommunityAboutScreen}
options={{ title: t("About") }}
/>
</ProfileStack.Group>
</ProfileStack.Navigator>
);
Expand Down Expand Up @@ -432,6 +432,11 @@ function SearchStackScreen() {
<SearchStack.Screen name="Post" component={PostScreen} />
<SearchStack.Screen name="Community" component={CommunityFeedScreen} />
<SearchStack.Screen name="Profile" component={UserProfileScreen} />
<SearchStack.Screen
name="CommunityAbout"
component={CommunityAboutScreen}
options={{ title: t("About") }}
/>
<SearchStack.Screen
name="UserComments"
component={UserCommentsScreen}
Expand Down Expand Up @@ -480,11 +485,6 @@ function SearchStackScreen() {
component={NewPostBodyScreen}
options={{ title: t("New Post") }}
/>
<SearchStack.Screen
name="CommunityAbout"
component={CommunityAboutScreen}
options={{ title: t("About") }}
/>
</SearchStack.Group>
</SearchStack.Navigator>
);
Expand Down Expand Up @@ -515,6 +515,11 @@ function TraverseStackScreen() {
name="Community"
component={CommunityFeedScreen}
/>
<TraverseStack.Screen
name="CommunityAbout"
component={CommunityAboutScreen}
options={{ title: t("About") }}
/>
<TraverseStack.Screen
name="Profile"
component={UserProfileScreen}
Expand Down Expand Up @@ -568,11 +573,6 @@ function TraverseStackScreen() {
component={NewPostBodyScreen}
options={{ title: t("New Post") }}
/>
<TraverseStack.Screen
name="CommunityAbout"
component={CommunityAboutScreen}
options={{ title: t("About") }}
/>
</TraverseStack.Group>
</TraverseStack.Navigator>
);
Expand Down
14 changes: 12 additions & 2 deletions src/components/common/AvatarUsername.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ function isUserDev(userId: number): boolean {
function getUserPillType({
user,
opId,
isMod,
}: {
user: Person;
opId?: number;
isMod?: boolean;
}): NameType | undefined {
if (isMod) {
return "mod";
}

if (isUserDev(user.id)) {
return "dev";
}
Expand All @@ -44,6 +50,7 @@ interface IProps {
creator: Person;
showAvatar?: boolean;
opId?: number;
isMod?: boolean;
children?: React.ReactNode;
link?: boolean;
}
Expand All @@ -52,12 +59,13 @@ function AvatarUsername({
showAvatar = true,
creator,
opId,
isMod = false,
children,
link = true,
}: IProps) {
const { showInstanceForUsernames } = useAppSelector(selectSettings);
const theme = useTheme();
const type = getUserPillType({ user: creator, opId });
const type = getUserPillType({ user: creator, opId, isMod });

const NameColorMap: Record<
NameType,
Expand Down Expand Up @@ -125,7 +133,9 @@ function AvatarUsername({
)}
</HStack>
{showInstanceForUsernames && (
<Text fontSize="xs">{getBaseUrl(creator.actor_id)}</Text>
<Text fontSize="xs" color={theme.colors.app.textPrimary}>
{getBaseUrl(creator.actor_id)}
</Text>
)}
</VStack>
{children}
Expand Down
17 changes: 17 additions & 0 deletions src/components/common/ModeratorList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from "react";
import { Text } from "native-base";
import AvatarUsername from "./AvatarUsername";

interface IProps {
item: any;
}

function ModeratorList({ item }: IProps) {
return (
<Text marginTop={4}>
- <AvatarUsername creator={item} isMod />
</Text>
);
}

export default React.memo(ModeratorList);
37 changes: 35 additions & 2 deletions src/components/screens/Feed/CommunityAboutScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
import React from "react";
import React, { useEffect } from "react";
import { ScrollView, Text, useTheme, VStack } from "native-base";
import FastImage from "@gkasdorf/react-native-fast-image";
import FastImage from "react-native-fast-image";
import { IconPlanet } from "tabler-icons-react-native";
import { useTranslation } from "react-i18next";
import LoadingView from "../../common/Loading/LoadingView";
import RenderMarkdown from "../../common/Markdown/RenderMarkdown";
import useCommunity from "../../../hooks/communities/useCommunity";
import LoadingErrorView from "../../common/Loading/LoadingErrorView";
import NotFoundView from "../../common/Loading/NotFoundView";
import ModeratorList from "../../common/ModeratorList";

function CommunityAboutScreen({ route }: { route: any }) {
const { t } = useTranslation();
const theme = useTheme();
const community = useCommunity(route.params.communityId);

useEffect(() => {
community.doLoad();
}, []);

if (community.communityNotFound) {
return <NotFoundView />;
}

if (community.communityLoading || !community.community) {
return <LoadingView />;
}

if (community.communityError) {
return <LoadingErrorView onRetryPress={community.doLoad} />;
}

return (
<ScrollView flex={1} backgroundColor={theme.colors.app.bg}>
Expand All @@ -27,6 +49,17 @@ function CommunityAboutScreen({ route }: { route: any }) {
{t("Description")}
</Text>
<RenderMarkdown text={route.params.description} />
<Text fontSize="xl" fontWeight="bold">
Mods:
</Text>
<VStack p={2}>
{[...community.moderators].map((moderator) => (
<ModeratorList
key={moderator.moderator.id}
item={moderator.moderator}
/>
))}
</VStack>
</VStack>
</VStack>
</ScrollView>
Expand Down
92 changes: 92 additions & 0 deletions src/hooks/communities/useCommunity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import React, { useEffect } from "react";
import { CommunityModeratorView, CommunityView } from "lemmy-js-client";
import { lemmyAuthToken, lemmyInstance } from "../../LemmyInstance";
import { handleLemmyError } from "../../helpers/LemmyErrorHelper";

interface UseCommunity {
community: CommunityView | null;
moderators: CommunityModeratorView[] | [];
setCommunity: React.Dispatch<React.SetStateAction<CommunityView | null>>;
communityLoading: boolean;
communityError: boolean;
communityNotFound: boolean;

doLoad: (refresh?: boolean) => void;

loaded: boolean;
setLoaded: React.Dispatch<React.SetStateAction<any>>;
}

const useCommunity = (communityID: number): UseCommunity => {
// State
const [community, setCommunity] = React.useState<CommunityView | null>(null);
const [moderators, setModerators] = React.useState<CommunityModeratorView[]>(
[]
);
const [communityLoading, setCommunityLoading] =
React.useState<boolean>(false);
const [communityError, setCommunityError] = React.useState<boolean>(false);
const [communityNotFound, setCommunityNotFound] =
React.useState<boolean>(false);
const [loaded, setLoaded] = React.useState(false);

useEffect(() => {
if (!communityID) return;

if (lemmyInstance) {
doLoad(true).then();
}
}, [communityID]);

const doLoad = async (refresh = false) => {
const loadCommunity = async () => {
setCommunityLoading(true);
setCommunityError(false);

try {
const res = await lemmyInstance?.getCommunity({
auth: lemmyAuthToken,
id:
typeof communityID === "number"
? (communityID as number)
: undefined,
});

setCommunity(res.community_view);
setModerators(res.moderators);
setCommunityLoading(false);
} catch (e) {
setCommunityLoading(false);
setCommunityError(true);

if (e.toString() === "couldnt_find_community") {
setCommunityNotFound(true);
}

handleLemmyError(e.toString());
}
setLoaded(true);
};

if (communityID && (refresh || !community)) {
loadCommunity().then();
}
};

return {
community,
moderators,
setCommunity,

communityLoading,
communityError,
communityNotFound,

doLoad,

loaded,
setLoaded,
};
};

export default useCommunity;
1 change: 1 addition & 0 deletions src/hooks/feeds/useCommunityFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ const useCommunityFeed = (communityFullName: string): UseCommunityFeed => {
banner: feed.community.community.banner,
description: feed.community.community.description,
title: feed.community.community.title,
communityId: feed.community.community.id,
});
};

Expand Down

0 comments on commit 84e3e17

Please sign in to comment.