diff --git a/app/blog/[identifier]/index.tsx b/app/blog/[identifier]/index.tsx index 3a11e92..2e87ef4 100644 --- a/app/blog/[identifier]/index.tsx +++ b/app/blog/[identifier]/index.tsx @@ -44,7 +44,6 @@ export default function BlogPage() { const { identifier: blogId } = useLocalSearchParams<{ identifier?: string; }>(); - const authCtx = useContext(AuthContext); // TODO: This fetches way too often because authCtx is set multiple times // Find another way of getting logged-in user profile @@ -53,12 +52,12 @@ export default function BlogPage() { const profile = await resolveProfile(blogId!); setProfile(profile); if (profile === undefined) return; - const fetchedThreads = await fetchBlogPosts(profile, authCtx); + const fetchedThreads = await fetchBlogPosts(profile); // console.log(fetchedThreads); setThreads(fetchedThreads); setLoading(false); })(); - }, [authCtx, blogId]); + }, [blogId]); return ( <> diff --git a/src/components/NavBar.tsx b/src/components/NavBar.tsx index 7acef98..6bb0eaf 100644 --- a/src/components/NavBar.tsx +++ b/src/components/NavBar.tsx @@ -1,5 +1,11 @@ import { useRouter } from "expo-router"; -import { Button, StyleSheet, TouchableWithoutFeedback, View } from "react-native"; +import { useContext } from "react"; +import { + Button, + StyleSheet, + View, +} from "react-native"; +import { AuthContext } from "../context/AuthContext"; const styles = StyleSheet.create({ alignLeft: { @@ -20,15 +26,17 @@ const styles = StyleSheet.create({ alignItems: "center", }, navBar: { - padding: 10 - } + padding: 10, + }, }); export default function NavBar() { - const router = useRouter() + const router = useRouter(); + const authCtx = useContext(AuthContext); const openComposer = () => { - router.push("/new") - } + if (!authCtx.agent) router.navigate("/login"); + else router.push("/new"); + }; return ( diff --git a/src/components/PostInteraction.tsx b/src/components/PostInteraction.tsx index 5c7fda3..e90aaf1 100644 --- a/src/components/PostInteraction.tsx +++ b/src/components/PostInteraction.tsx @@ -12,6 +12,7 @@ import { useContext, useEffect, useState } from "react"; import { AuthContext } from "../context/AuthContext"; import { Text } from "./primitive/Text"; import FontAwesome from "@expo/vector-icons/FontAwesome"; +import { useRouter } from "expo-router"; export function PostNotesIndicator({ post }: { post: Post }) { const [count, setCount] = useState(); @@ -45,6 +46,7 @@ export function PostNotesIndicator({ post }: { post: Post }) { } export function PostInteractionButtons({ post }: { post: Post }) { + const router = useRouter(); const authCtx = useContext(AuthContext); const [postInteractions, setPostInteractions] = useState({ liked: undefined, @@ -53,29 +55,39 @@ export function PostInteractionButtons({ post }: { post: Post }) { useEffect(() => { (async () => { - const fetchedStatus = await fetchUserPostInteractions( - post, - authCtx.agent?.did!, - ); + const fetchedStatus = + authCtx.agent && authCtx.agent.did + ? await fetchUserPostInteractions(post, authCtx.agent.did) + : ({} as PostInteractions); setPostInteractions(fetchedStatus); })(); }, [post, authCtx]); const setLike = async (newLike: boolean) => { + if (!authCtx.client) { + router.navigate("/login"); + return; + } + const newPostInteractions = await setLikePost( newLike, post, postInteractions, - authCtx.client!, + authCtx.client, ); setPostInteractions(newPostInteractions); }; const addReblog = async () => { + if (!authCtx.client) { + router.navigate("/login"); + return; + } + const newPostInteractions = await addPostReblog( post, postInteractions, - authCtx.client!, + authCtx.client, ); setPostInteractions(newPostInteractions); }; @@ -112,7 +124,6 @@ export function PostLike({ ); } - export function PostReblog({ reblogged, addPostReblog, diff --git a/src/lib/query/feed.ts b/src/lib/query/feed.ts index 8a7557a..0f7045b 100644 --- a/src/lib/query/feed.ts +++ b/src/lib/query/feed.ts @@ -141,7 +141,7 @@ export async function fetchPostThread( } // Fetch posts from a given blog -export async function fetchBlogPosts(profile: Profile, authCtx: AuthState) { +export async function fetchBlogPosts(profile: Profile) { // Get records for user // TODO: Get latest n post and cursor support const result = await getAgent(profile.pds).com.atproto.repo.listRecords({