Tech Table & History components completely rewritten & cleaned up. Just need to implement the realtime subscription now.

This commit is contained in:
2025-06-13 11:38:33 -05:00
parent c96bdab91b
commit b80bf9cd3f
20 changed files with 639 additions and 545 deletions

View File

@ -1,9 +1,10 @@
'use server';
import { createServerClient } from '@/utils/supabase';
import type { Profile, Result, Status } from '@/utils/supabase';
import { getUser, getProfile } from '@/lib/hooks'
import type { Profile, Result } from '@/utils/supabase';
import { getUser, getProfile } from '@/lib/hooks';
type UserWithStatus = {
export type UserWithStatus = {
id?: string;
user: Profile;
status: string;
created_at: string;
@ -11,8 +12,7 @@ type UserWithStatus = {
};
type PaginatedHistory = {
profile?: Profile;
statuses: Status[];
statuses: UserWithStatus[];
meta: {
current_page: number;
per_page: number;
@ -28,16 +28,21 @@ export const getRecentUsersWithStatuses = async (): Promise<
const supabase = await createServerClient();
const oneDayAgo = new Date(Date.now() - 1000 * 60 * 60 * 24);
const { data, error } = await supabase
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[], error: unknown };
.order('created_at', { ascending: false })) as {
data: UserWithStatus[];
error: unknown;
};
if (error) throw error as Error;
if (!data?.length) return { success: true, data: [] };
@ -69,10 +74,12 @@ export const broadcastStatusUpdates = async (
payload: {
user_status: userStatus,
timestamp: new Date().toISOString(),
}
},
});
if (broadcast === 'error' || broadcast === 'timed out')
throw new Error('Failed to broadcast status update. Timed out or errored.');
throw new Error(
'Failed to broadcast status update. Timed out or errored.',
);
}
return { success: true, data: undefined };
} catch (error) {
@ -96,16 +103,14 @@ export const updateStatuses = async (
const user = userResponse.data;
const userProfile = profileResponse.data;
const inserts = userIds.map(usersId => ({
const inserts = userIds.map((usersId) => ({
user_id: usersId,
status,
updated_by_id: user.id,
}));
const { data: insertedStatuses, error: insertedStatusesError } = await supabase
.from('statuses')
.insert(inserts)
.select();
const { data: insertedStatuses, error: insertedStatusesError } =
await supabase.from('statuses').insert(inserts).select();
if (insertedStatusesError) throw insertedStatusesError as Error;
if (insertedStatuses) {
@ -130,7 +135,6 @@ export const updateStatuses = async (
await broadcastStatusUpdates(broadcastArray);
}
return { success: true, data: undefined };
} catch (error) {
return {
success: false,
@ -139,13 +143,17 @@ export const updateStatuses = async (
}
};
export const updateUserStatus = async (status: string): Promise<Result<void>> => {
export const updateUserStatus = async (
status: string,
): Promise<Result<void>> => {
try {
const supabase = await createServerClient();
const userResponse = await getUser();
if (!userResponse.success) throw new Error(`Not authenticated! ${userResponse.error}`);
if (!userResponse.success)
throw new Error(`Not authenticated! ${userResponse.error}`);
const profileResponse = await getProfile();
if (!profileResponse.success) throw new Error(`Could not get profile! ${profileResponse.error}`);
if (!profileResponse.success)
throw new Error(`Could not get profile! ${profileResponse.error}`);
const user = userResponse.data;
const userProfile = profileResponse.data;
@ -168,12 +176,11 @@ export const updateUserStatus = async (status: string): Promise<Result<void>> =>
await broadcastStatusUpdates([userStatus]);
return { success: true, data: undefined };
} catch (error) {
return {
success: false,
error: `Error updating user's status: ${error as Error}`,
}
};
}
};
@ -185,7 +192,8 @@ export const getUserHistory = async (
try {
const supabase = await createServerClient();
const userResponse = await getUser();
if (!userResponse.success) throw new Error(`Not authenticated! ${userResponse.error}`);
if (!userResponse.success)
throw new Error(`Not authenticated! ${userResponse.error}`);
const offset = (page - 1) * perPage;
const { count } = await supabase
@ -193,19 +201,30 @@ export const getUserHistory = async (
.select('*', { count: 'exact', head: true })
.eq('user_id', userId);
const { data: statuses, error: statusesError } = await supabase
const { data: statuses, error: statusesError } = (await supabase
.from('statuses')
.select('*')
.select(
`
id,
user:profiles!user_id(*),
status,
created_at,
updated_by:profiles!updated_by_id(*)
`,
)
.eq('user_id', userId)
.order('created_at', { ascending: false })
.range(offset, offset + perPage - 1) as {data: Status[], error: unknown};
.range(offset, offset + perPage - 1)) as {
data: UserWithStatus[];
error: unknown;
};
if (statusesError) throw statusesError as Error;
const { data: profile, error: profileError } = await supabase
const { data: profile, error: profileError } = (await supabase
.from('profiles')
.select('*')
.eq('id', userId)
.single() as { data: Profile, error: unknown };
.single()) as { data: Profile; error: unknown };
if (profileError) throw profileError as Error;
if (!profile) throw new Error('User profile not found!');
@ -215,7 +234,6 @@ export const getUserHistory = async (
return {
success: true,
data: {
profile,
statuses,
meta: {
current_page: page,
@ -225,7 +243,6 @@ export const getUserHistory = async (
},
},
};
} catch (error) {
return {
success: false,
@ -241,18 +258,30 @@ export const getAllHistory = async (
try {
const supabase = await createServerClient();
const userResponse = await getUser();
if (!userResponse.success) throw new Error(`Not authenticated! ${userResponse.error}`);
if (!userResponse.success)
throw new Error(`Not authenticated! ${userResponse.error}`);
const offset = (page - 1) * perPage;
const { count } = await supabase
.from('statuses')
.select('*', { count: 'exact', head: true });
const { data: statuses, error: statusesError } = await supabase
const { data: statuses, error: statusesError } = (await supabase
.from('statuses')
.select('*')
.select(
`
id,
user:profiles!user_id(*),
status,
created_at,
updated_by:profiles!updated_by_id(*)
`,
)
.order('created_at', { ascending: false })
.range(offset, offset + perPage - 1) as {data: Status[], error: unknown};
.range(offset, offset + perPage - 1)) as {
data: UserWithStatus[];
error: unknown;
};
if (statusesError) throw statusesError as Error;
const totalCount = count ?? 0;
@ -270,7 +299,6 @@ export const getAllHistory = async (
},
},
};
} catch (error) {
return {
success: false,