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);
|
||||
}
|
||||
|
||||
@ -112,47 +116,52 @@ export const broadcastStatusUpdates = async (
|
||||
export const updateStatuses = async (
|
||||
usersWithStatuses: UserWithStatus[],
|
||||
status: string,
|
||||
): Promise<Result<void>> => {
|
||||
): Promise<Result<UserWithStatus[]>> => {
|
||||
try {
|
||||
const supabase = createClient();
|
||||
const profileResponse = await getProfileWithAvatar();
|
||||
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("Error inserting 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,
|
||||
}}));
|
||||
const statusUpdates = usersWithStatuses.map((s, i) => {
|
||||
return {
|
||||
user: s.user,
|
||||
status: status,
|
||||
created_at: insertedStatuses[i]?.created_at ?? createdAtFallback,
|
||||
updated_by: user,
|
||||
}
|
||||
});
|
||||
await broadcastStatusUpdates(statusUpdates);
|
||||
return { success: true, data: statusUpdates };
|
||||
} else {
|
||||
return { success: false, error: 'No inserted statuses returned!' };
|
||||
}
|
||||
return { success: true, data: undefined };
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Unknown error',
|
||||
error: `Error updating statuses: ${error as Error}`,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export const updateUserStatus = async (
|
||||
status: string,
|
||||
): Promise<Result<void>> => {
|
||||
): Promise<Result<UserWithStatus[]>> => {
|
||||
try {
|
||||
const supabase = createClient();
|
||||
const profileResponse = await getProfileWithAvatar();
|
||||
@ -171,14 +180,15 @@ export const updateUserStatus = async (
|
||||
.single();
|
||||
if (insertedStatusError) throw insertedStatusError as Error;
|
||||
|
||||
await broadcastStatusUpdates([{
|
||||
const statusUpdate = {
|
||||
user: userProfile,
|
||||
status: insertedStatus.status,
|
||||
created_at: insertedStatus.created_at,
|
||||
updated_by: userProfile,
|
||||
}]);
|
||||
};
|
||||
await broadcastStatusUpdates([statusUpdate]);
|
||||
|
||||
return { success: true, data: undefined };
|
||||
return { success: true, data: [statusUpdate] };
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
|
Reference in New Issue
Block a user