Skip to content

Commit

Permalink
merge cleanup into main
Browse files Browse the repository at this point in the history
  • Loading branch information
gkasdorf committed Jun 23, 2023
1 parent 5c3fdff commit 183ee3e
Show file tree
Hide file tree
Showing 30 changed files with 462 additions and 343 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ module.exports = {
"no-nested-ternary": "off",
"import/no-extraneous-dependencies": "off",
"prefer-destructuring": ["error", { object: true, array: false }],
"import/no-named-as-default": "off",
"react/jsx-no-useless-fragment": "off",
},
};
18 changes: 9 additions & 9 deletions Stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -360,15 +360,6 @@ function Tabs() {
: null,
}}
/>
<Tab.Screen
name="SearchStack"
component={SearchStackScreen}
options={{
headerShown: false,
tabBarIcon: ({ color }) => <IconSearch color={color} />,
tabBarLabel: "Search",
}}
/>
<Tab.Screen
name="UserProfileStack"
component={ProfileStackScreen}
Expand All @@ -380,6 +371,15 @@ function Tabs() {
tabBarLabel: "Profile",
}}
/>
<Tab.Screen
name="SearchStack"
component={SearchStackScreen}
options={{
headerShown: false,
tabBarIcon: ({ color }) => <IconSearch color={color} />,
tabBarLabel: "Search",
}}
/>
<Tab.Screen
name="SettingsStack"
component={SettingsStackScreen}
Expand Down
File renamed without changes.
92 changes: 92 additions & 0 deletions components/hooks/feeds/useCommunityFeed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { useEffect, useRef } from "react";
import { NativeStackNavigationProp } from "@react-navigation/native-stack";
import { useNavigation } from "@react-navigation/native";
import { trigger } from "react-native-haptic-feedback";
import { useToast } from "native-base";
import { UseFeed, useFeed } from "./useFeed";
import { useAppDispatch, useAppSelector } from "../../../store";
import { selectPost } from "../../../slices/post/postSlice";
import { subscribeToCommunity } from "../../../slices/communities/communitiesActions";

interface UseCommunityFeed {
feed: UseFeed;

onSubscribePress: () => void;
onAboutPress: () => void;
onPostPress: () => void;
}

const useCommunityFeed = (communityFullName: string): UseCommunityFeed => {
// Global state
const { post } = useAppSelector(selectPost);

// Refs
const creatingPost = useRef<boolean>(false);
const lastPost = useRef<number>(0);

// Hooks
const feed = useFeed(communityFullName);

// Other hooks
const dispatch = useAppDispatch();
const navigation = useNavigation<NativeStackNavigationProp<any>>();
const toast = useToast();

useEffect(() => {
if (creatingPost.current && post && lastPost.current !== post.post.id) {
creatingPost.current = false;

setTimeout(() => {
navigation.push("Post");
}, 500);
}
}, [post]);

// Events
const onSubscribePress = () => {
trigger("impactMedium");

toast.show({
title: `${!feed.subscribed ? "Subscribed to" : "Unsubscribed from"} ${
feed.community.community.name
}`,
duration: 3000,
});

dispatch(
subscribeToCommunity({
communityId: feed.community.community.id,
subscribe: !feed.subscribed,
})
);
};

const onAboutPress = () => {
navigation.push("CommunityAbout", {
name: feed.community.community.name,
banner: feed.community.community.banner,
description: feed.community.community.description,
title: feed.community.community.title,
});
};

const onPostPress = () => {
creatingPost.current = true;
lastPost.current = post ? post.post.id : 0;

navigation.push("NewPost", {
communityId: feed.community.community.id,
communityName: feed.community.community.name,
});
};

return {
feed,

onSubscribePress,
onAboutPress,
onPostPress,
};
};

export default useCommunityFeed;
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,11 @@ export const useFeed = (communityIdOrName?: number | string): UseFeed => {
: undefined,
});

// @ts-ignore
if (res.error && res.error === "couldnt_find_community") {
setCommunityNotFound(true);
return;
}

setCommunity(res.community_view);
setCommunityLoading(false);
setSubscribed(
isSubscribed(res.community_view.community.id, subscribedCommunities)
);
setCommunityLoading(false);
} catch (e) {
writeToLog("Error getting community feed.");
writeToLog(e.toString());
Expand Down
44 changes: 5 additions & 39 deletions components/hooks/post/postHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ import {
removeBookmark,
} from "../../../slices/bookmarks/bookmarksActions";
import { onVoteHapticFeedback } from "../../../helpers/HapticFeedbackHelpers";
import findAndAddComment from "../../../lemmy/LemmyCommentsHelper";
import { writeToLog } from "../../../helpers/LogHelper";
import { ILemmyVote } from "../../../lemmy/types/ILemmyVote";
import {
buildComments,
findAndAddComment,
} from "../../../lemmy/comments/LemmyCommentsHelper";
import NestedComment from "../../../lemmy/comments/NestedComment";

export interface UsePost {
comments: NestedComment[];
Expand Down Expand Up @@ -210,42 +214,4 @@ const usePost = (commentId: string | null): UsePost => {
};
};

export interface NestedComment {
comment: CommentView | CommentReplyView;
replies: NestedComment[];
collapsed: boolean;
myVote: ILemmyVote;
}

function buildComments(comments: CommentView[]): NestedComment[] {
const nestedComments = [];

const commentDict = {};

for (const comment of comments) {
const { path } = comment.comment;
const pathIds = path.split(".").map(Number);
const parentId = pathIds[pathIds.length - 2];

const currentComment = {
comment,
replies: [],
};

commentDict[comment.comment.id] = currentComment;

if (parentId !== 0) {
const parentComment = commentDict[parentId];

try {
parentComment.replies.push(currentComment);
} catch (e) {}
} else {
nestedComments.push(currentComment);
}
}

return nestedComments;
}

export default usePost;
142 changes: 142 additions & 0 deletions components/hooks/post/useNewPost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { useRef, useState } from "react";
import { Alert, TextInput } from "react-native";
import { NativeStackNavigationProp } from "@react-navigation/native-stack";
import { useNavigation } from "@react-navigation/native";
import { CreatePost } from "lemmy-js-client";
import { useAppDispatch } from "../../../store";
import { selectImage } from "../../../helpers/ImageHelper";
import uploadToImgur from "../../../helpers/ImgurHelper";
import { writeToLog } from "../../../helpers/LogHelper";
import { lemmyAuthToken, lemmyInstance } from "../../../lemmy/LemmyInstance";
import { setPost } from "../../../slices/post/postSlice";

interface UseNewPost {
loading;
error;

form: {
body: string;
name: string;
url: string;
nsfw: boolean;
};

inputRef: React.MutableRefObject<TextInput>;

onFormChange: (name: string, value: string | boolean) => void;

doUpload: () => Promise<void>;
doSubmit: () => Promise<void>;
}

const useNewPost = (communityId: number): UseNewPost => {
// State
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<string | undefined>(undefined);

const [form, setForm] = useState({
body: "",
name: "",
url: "",
nsfw: false,
});

// Refs
const inputRef = useRef<TextInput>();

// Other hooks
const navigation = useNavigation<NativeStackNavigationProp<any>>();
const dispatch = useAppDispatch();

const onFormChange = (name: string, value: string | boolean) => {
setForm({
...form,
[name]: value,
});
};

const doUpload = async () => {
setError(undefined);
let path;

try {
path = await selectImage();
} catch (e) {
if (e === "permissions") {
Alert.alert(
"Permissions Error",
"Please allow Memmy to access your camera roll."
);
}

return;
}

setLoading(true);

let imgurLink;

try {
imgurLink = await uploadToImgur(path);
} catch (e) {
writeToLog("Error uploading image.");
writeToLog(e.toString());

setLoading(false);
setError(e.toString());
Alert.alert("Error", "Error uploading image to Imgur.");
}

onFormChange("url", imgurLink);
setLoading(false);
};

const doSubmit = async () => {
try {
setLoading(true);
setError(undefined);

const prepared: CreatePost = {
...form,
name: form.name !== "" ? form.name : undefined,
body: form.body !== "" ? form.body : undefined,
url: form.url !== "" ? form.url : undefined,
auth: lemmyAuthToken,
language_id: 37,
community_id: communityId,
nsfw: form.nsfw,
};

const res = await lemmyInstance.createPost(prepared);

setLoading(false);

dispatch(setPost(res.post_view));

navigation.pop();
} catch (e) {
writeToLog("Error submitting post.");
writeToLog(e.toString());

setLoading(false);
setError(e.toString());
Alert.alert(e.toString());
}
};

return {
loading,
error,

form,

inputRef,

onFormChange,

doUpload,
doSubmit,
};
};

export default useNewPost;
Loading

0 comments on commit 183ee3e

Please sign in to comment.