Rewriting status card to make it look good and to go over the code
This commit is contained in:
@ -40,12 +40,14 @@ export const getRecentUsersWithStatuses = async (): Promise<
|
||||
|
||||
const { data, error } = (await supabase
|
||||
.from('statuses')
|
||||
.select(`
|
||||
.select(
|
||||
`
|
||||
user:profiles!user_id(*),
|
||||
status,
|
||||
created_at,
|
||||
updated_by:profiles!updated_by_id(*)
|
||||
`)
|
||||
`,
|
||||
)
|
||||
.gte('created_at', oneDayAgo.toISOString())
|
||||
.order('created_at', { ascending: false })) as {
|
||||
data: UserWithStatus[];
|
||||
@ -66,11 +68,13 @@ export const getRecentUsersWithStatuses = async (): Promise<
|
||||
const filteredWithAvatars = new Array<UserWithStatus>();
|
||||
for (const userWithStatus of filtered) {
|
||||
if (userWithStatus.user.avatar_url)
|
||||
userWithStatus.user.avatar_url =
|
||||
await getAvatarUrl(userWithStatus.user.avatar_url);
|
||||
userWithStatus.user.avatar_url = await getAvatarUrl(
|
||||
userWithStatus.user.avatar_url,
|
||||
);
|
||||
if (userWithStatus.updated_by?.avatar_url)
|
||||
userWithStatus.updated_by.avatar_url =
|
||||
await getAvatarUrl(userWithStatus.updated_by?.avatar_url);
|
||||
userWithStatus.updated_by.avatar_url = await getAvatarUrl(
|
||||
userWithStatus.updated_by?.avatar_url,
|
||||
);
|
||||
filteredWithAvatars.push(userWithStatus);
|
||||
}
|
||||
|
||||
@ -119,27 +123,31 @@ export const updateStatuses = async (
|
||||
if (!profileResponse.success) throw new Error('Not authenticated!');
|
||||
const user = profileResponse.data;
|
||||
|
||||
const {
|
||||
data: insertedStatuses,
|
||||
error: insertedStatusesError
|
||||
} = await supabase
|
||||
.from('statuses')
|
||||
.insert(usersWithStatuses.map((userWithStatus) => ({
|
||||
user_id: userWithStatus.user.id,
|
||||
status,
|
||||
updated_by_id: user.id,
|
||||
})))
|
||||
.select();
|
||||
const { data: insertedStatuses, error: insertedStatusesError } =
|
||||
await supabase
|
||||
.from('statuses')
|
||||
.insert(
|
||||
usersWithStatuses.map((userWithStatus) => ({
|
||||
user_id: userWithStatus.user.id,
|
||||
status,
|
||||
updated_by_id: user.id,
|
||||
})),
|
||||
)
|
||||
.select();
|
||||
|
||||
if (insertedStatusesError) throw new Error('Couldn\'t insert statuses!');
|
||||
if (insertedStatusesError) throw new Error("Couldn't insert statuses!");
|
||||
else if (insertedStatuses) {
|
||||
const createdAtFallback = new Date(Date.now()).toISOString();
|
||||
await broadcastStatusUpdates(usersWithStatuses.map((s, i) => { return {
|
||||
user: s.user,
|
||||
status: status,
|
||||
created_at: insertedStatuses[i]?.created_at ?? createdAtFallback,
|
||||
updated_by: user,
|
||||
}}));
|
||||
await broadcastStatusUpdates(
|
||||
usersWithStatuses.map((s, i) => {
|
||||
return {
|
||||
user: s.user,
|
||||
status: status,
|
||||
created_at: insertedStatuses[i]?.created_at ?? createdAtFallback,
|
||||
updated_by: user,
|
||||
};
|
||||
}),
|
||||
);
|
||||
}
|
||||
return { success: true, data: undefined };
|
||||
} catch (error) {
|
||||
@ -171,12 +179,14 @@ export const updateUserStatus = async (
|
||||
.single();
|
||||
if (insertedStatusError) throw insertedStatusError as Error;
|
||||
|
||||
await broadcastStatusUpdates([{
|
||||
user: userProfile,
|
||||
status: insertedStatus.status,
|
||||
created_at: insertedStatus.created_at,
|
||||
updated_by: userProfile,
|
||||
}]);
|
||||
await broadcastStatusUpdates([
|
||||
{
|
||||
user: userProfile,
|
||||
status: insertedStatus.status,
|
||||
created_at: insertedStatus.created_at,
|
||||
updated_by: userProfile,
|
||||
},
|
||||
]);
|
||||
|
||||
return { success: true, data: undefined };
|
||||
} catch (error) {
|
||||
|
Reference in New Issue
Block a user