Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions app/(app)/(tabs)/tablon/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export default function PostDetailScreen() {
) : null}

<View className="flex-row items-center gap-2">
{post.author.avatar_url ? (
{post.author?.avatar_url ? (
<Image
source={{ uri: post.author.avatar_url }}
contentFit="cover"
Expand All @@ -141,7 +141,7 @@ export default function PostDetailScreen() {
) : (
<View className="w-8 h-8 rounded-full bg-nun-sand items-center justify-center">
<Text className="text-xs font-semibold text-nun-muted">
{[post.author.name, post.author.surname]
{[post.author?.name, post.author?.surname]
.filter(Boolean)
.map((w) => w![0])
.join('')
Expand All @@ -151,7 +151,7 @@ export default function PostDetailScreen() {
</View>
)}
<Text className="text-xs text-nun-muted flex-1" numberOfLines={1}>
{[post.author.name, post.author.surname].filter(Boolean).join(' ') || '—'}
{[post.author?.name, post.author?.surname].filter(Boolean).join(' ') || '—'}
{post.published_at ? ` · ${formatRelativeTime(post.published_at)}` : ''}
</Text>
</View>
Expand Down
2 changes: 1 addition & 1 deletion components/PostCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function PostCard({ post, onPress }: Props) {
const { myReaction, counts, loading, toggle } = usePostReactions(post.id);
const thumbSource = post.cover_image_url ? { uri: getThumbUrl(post.cover_image_url) } : null;
const authorName =
[post.author.name, post.author.surname].filter(Boolean).join(' ') || '—';
[post.author?.name, post.author?.surname].filter(Boolean).join(' ') || '—';
const publishedAt = post.published_at ? formatRelativeTime(post.published_at) : '';

return (
Expand Down
15 changes: 10 additions & 5 deletions lib/supabase/queries/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ type ProfileSnippet = { name: string | null; surname: string | null };
type PostAuthor = { id: string; name: string | null; surname: string | null; avatar_url: string | null };

export type PostWithAuthor = Tables<'posts'> & {
author: ProfileSnippet;
author: ProfileSnippet | null;
comments_count: number;
rating_average: number; // 0 when the post has no ratings yet
rating_count: number;
};
export type PostDetail = Tables<'posts'> & { author: PostAuthor };
export type PostDetail = Tables<'posts'> & { author: PostAuthor | null };
export type PostCursor = { published_at: string; id: string };

// PostgREST reverse-embed: post_comments(count) yields [{ count }] per post and
// post_ratings(rating) yields the rating rows, so the feed's comment and rating
// aggregates ride on the same query (no N+1).
type PostRow = Tables<'posts'> & {
author: ProfileSnippet;
author: ProfileSnippet | null;
post_comments: { count: number }[];
post_ratings: { rating: number }[];
};
Expand All @@ -33,9 +33,14 @@ export async function listPublishedPosts({
pageSize?: number;
client?: SupabaseClient<Database>;
}): Promise<{ rows: PostWithAuthor[]; nextCursor: PostCursor | null }> {
// Author is embedded through profiles_public: RLS on profiles only lets staff
// read their own row, so embedding profiles directly returns author: null for
// every post they did not write.
let query = client
.from('posts')
.select('*, author:profiles!author_id(name, surname), post_comments(count), post_ratings(rating)')
.select(
'*, author:profiles_public!author_id(name, surname), post_comments(count), post_ratings(rating)',
)
.eq('status', 'published')
.is('deleted_at', null)
.order('published_at', { ascending: false })
Expand Down Expand Up @@ -79,7 +84,7 @@ export async function listPublishedPosts({
export async function getPostById(id: string): Promise<PostDetail> {
const { data, error } = await supabase
.from('posts')
.select('*, author:profiles!author_id(id, name, surname, avatar_url)')
.select('*, author:profiles_public!author_id(id, name, surname, avatar_url)')
.eq('id', id)
.is('deleted_at', null)
.single();
Expand Down
Loading