So its like broken but we are rewriting status.ts & TechTable & HistoryTable

This commit is contained in:
2025-06-12 16:55:24 -05:00
parent 185a7ea500
commit 653fe64bbf
23 changed files with 2536 additions and 647 deletions

142
src/lib/actions/_status.ts Normal file
View File

@ -0,0 +1,142 @@
'use server';
import 'server-only';
import { createServerClient } from '@/utils/supabase';
import type { Result } from '.';
import type { Profile } from '@/utils/supabase';
export type UserStatus = Profile & {
status: string;
created_at: string;
updated_by?: Profile;
}
export const getRecentUsers = async (): Promise<Result<string[]>> => {
try {
const supabase = await createServerClient();
// Get users who have had status updates in the last week
const oneWeekAgo = new Date();
oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
const { data, error } = await supabase
.from('statuses')
.select('user_id')
.gte('created_at', oneWeekAgo.toISOString())
.order('created_at', { ascending: false });
if (error) throw error;
// Get unique user IDs
const uniqueUserIds = [...new Set(data.map(status => status.user_id))];
return { success: true, data: uniqueUserIds };
} catch (error) {
return {
success: false,
error:
error instanceof Error
? error.message
: 'Unknown error getting recent users',
};
}
};
export const getUserStatuses = async (): Promise<Result<UserStatus[]>> => {
try {
const supabase = await createServerClient();
// First get the recent users
const recentUsersResult = await getRecentUsers();
if (!recentUsersResult.success) {
throw new Error(recentUsersResult.error);
}
const userIds = recentUsersResult.data;
if (userIds.length === 0) {
return { success: true, data: [] };
}
// Get the most recent status for each user
const { data: statusData, error: statusError } = await supabase
.from('statuses')
.select('user_id, status, created_at, updated_by_id')
.in('user_id', userIds)
.order('created_at', { ascending: false });
if (statusError) throw statusError;
if (!statusData) {
return { success: true, data: [] };
}
// Group by user_id and get the most recent status for each user
const userStatusMap = new Map<string, typeof statusData[0]>();
statusData.forEach(status => {
if (!userStatusMap.has(status.user_id)) {
userStatusMap.set(status.user_id, status);
}
});
// Get all unique user IDs from the status data
const statusUserIds = Array.from(userStatusMap.keys());
// Get profile information for these users
const { data: profileData, error: profileError } = await supabase
.from('profiles')
.select('id, full_name, email, avatar_url, provider, updated_at')
.in('id', statusUserIds);
if (profileError) throw profileError;
// Get updated_by profile information
const updatedByIds = Array.from(userStatusMap.values())
.map(status => status.updated_by_id)
.filter((id): id is string => id !== null);
const { data: updatedByData, error: updatedByError } = await supabase
.from('profiles')
.select('id, full_name, email, avatar_url, provider, updated_at')
.in('id', updatedByIds);
if (updatedByError) throw updatedByError;
// Create maps for easy lookup
const profileMap = new Map(profileData?.map(profile => [profile.id, profile]) ?? []);
const updatedByMap = new Map(updatedByData?.map(profile => [profile.id, profile]) ?? []);
// Transform the data to match UserStatus type
const userStatuses: UserStatus[] = [];
for (const status of userStatusMap.values()) {
const profile = profileMap.get(status.user_id);
const updatedBy = status.updated_by_id ? updatedByMap.get(status.updated_by_id) : undefined;
if (!profile) continue; // Skip if no profile found
userStatuses.push({
// Profile fields
id: profile.id,
full_name: profile.full_name,
email: profile.email,
avatar_url: profile.avatar_url,
provider: profile.provider,
updated_at: profile.updated_at,
// Status fields
status: status.status,
created_at: status.created_at,
updated_by: updatedBy,
});
}
return { success: true, data: userStatuses };
} catch (error) {
return {
success: false,
error:
error instanceof Error
? error.message
: 'Unknown error getting user statuses',
};
}
};

View File

@ -1,6 +1,7 @@
export * from './auth';
export * from './storage';
export * from './public';
export * from './status';
export * from './storage';
export type Result<T> =
| { success: true; data: T }

53
src/lib/actions/status.ts Normal file
View File

@ -0,0 +1,53 @@
'use server'
import 'server-only';
import {
createServerClient,
type Profile,
type Result,
type Status,
} from '@/utils/supabase';
type UserWithStatus = {
user: Profile;
status: string;
created_at: string;
updated_by: Profile;
};
type PaginatedHistory = UserWithStatus[] & {
meta: {
current_page: number;
per_page: number;
total_pages: number;
total_count: number;
};
};
export const getUsersWithStatuses = async () => {
try {
const supabase = await createServerClient();
// Get only users with recent statuses (Past 7 days)
const oneWeekAgo = new Date();
oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
const { data: recentStatuses, error } = await supabase
.from('statuses')
.select(`
user:profiles!user_id(*),
status,
created_at,
updated_by:profiles!updated_by_id(*)
`)
.gte('created_at', oneWeekAgo.toISOString())
.order('created_at', { ascending: false });
if (error) throw error;
if (!recentStatuses.length) return { success: true, data: []};
return { success: true, data: recentStatuses };
} catch (error) {
return { success: false, error: `Error: ${error as string}` };
}
};

401
src/lib/hooks/_status.ts Normal file
View File

@ -0,0 +1,401 @@
'use client';
import { createClient } from '@/utils/supabase';
import type { Result } from '.';
import type { Profile, Status } from '@/utils/supabase';
export type UserStatus = Profile & {
status: string;
created_at: string;
updated_by?: Profile;
};
export type HistoryEntry = {
id: string;
status: string;
created_at: string;
updated_by?: Profile;
user_profile: Profile;
};
export type PaginatedHistory = {
data: HistoryEntry[];
meta: {
current_page: number;
per_page: number;
total_pages: number;
total_count: number;
};
};
export const getUserStatuses = async (): Promise<Result<UserStatus[]>> => {
try {
const supabase = createClient();
// Get users with recent activity (last 7 days)
const oneWeekAgo = new Date();
oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
const { data: recentStatuses, error: statusError } = await supabase
.from('statuses')
.select('*')
.gte('created_at', oneWeekAgo.toISOString())
.order('created_at', { ascending: false });
if (statusError) throw statusError;
if (!recentStatuses?.length) return { success: true, data: [] };
// Properly type the status data
const typedStatuses: Status[] = recentStatuses;
// Get most recent status per user
const userStatusMap = new Map<string, Status>();
typedStatuses.forEach(status => {
if (!userStatusMap.has(status.user_id)) {
userStatusMap.set(status.user_id, status);
}
});
const userIds = Array.from(userStatusMap.keys());
// Get profiles
const { data: profiles, error: profileError } = await supabase
.from('profiles')
.select('*')
.in('id', userIds);
if (profileError) throw profileError;
// Get updated_by profiles - filter out nulls properly
const updatedByIds = Array.from(userStatusMap.values())
.map(s => s.updated_by_id)
.filter((id): id is string => id !== null);
let updatedByProfiles: Profile[] = [];
if (updatedByIds.length > 0) {
const { data, error } = await supabase
.from('profiles')
.select('*')
.in('id', updatedByIds);
if (error) throw error;
updatedByProfiles = data ?? [];
}
const profileMap = new Map((profiles ?? []).map(p => [p.id, p]));
const updatedByMap = new Map(updatedByProfiles.map(p => [p.id, p]));
const userStatuses: UserStatus[] = [];
for (const [userId, status] of userStatusMap) {
const profile = profileMap.get(userId);
if (!profile) continue;
userStatuses.push({
...profile,
status: status.status,
created_at: status.created_at,
updated_by: status.updated_by_id ? updatedByMap.get(status.updated_by_id) : undefined,
});
}
return { success: true, data: userStatuses };
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
};
}
};
export const broadcastStatusUpdate = async (
userStatus: UserStatus
): Promise<Result<void>> => {
try {
const supabase = createClient();
const broadcast = await supabase.channel('status_updates').send({
type: 'broadcast',
event: 'status_updated',
payload: {
user_status: userStatus,
timestamp: new Date().toISOString(),
}
});
if (broadcast === 'error') throw new Error('Failed to broadcast status update');
if (broadcast === 'ok') return { success: true, data: undefined };
else throw new Error('Broadcast timed out!')
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
};
}
};
// Update your existing functions to broadcast after database updates
export const updateUserStatus = async (
userIds: string[],
status: string,
): Promise<Result<void>> => {
try {
const supabase = createClient();
const { data: { user } } = await supabase.auth.getUser();
if (!user) throw new Error('Not authenticated');
const inserts = userIds.map(userId => ({
user_id: userId,
status,
updated_by_id: user.id,
}));
const { data: insertedStatuses, error } = await supabase
.from('statuses')
.insert(inserts)
.select();
if (error) throw error;
// Broadcast the updates
if (insertedStatuses) {
for (const insertedStatus of insertedStatuses) {
// Get the user profile for broadcasting
const { data: profile } = await supabase
.from('profiles')
.select('*')
.eq('id', insertedStatus.user_id)
.single();
if (profile) {
const userStatus: UserStatus = {
...profile,
status: insertedStatus.status,
created_at: insertedStatus.created_at,
};
await broadcastStatusUpdate(userStatus);
}
}
}
return { success: true, data: undefined };
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
};
}
};
export const updateCurrentUserStatus = async (
status: string,
): Promise<Result<void>> => {
try {
const supabase = createClient();
const { data: { user } } = await supabase.auth.getUser();
if (!user) throw new Error('Not authenticated');
const { data: insertedStatus, error } = await supabase
.from('statuses')
.insert({
user_id: user.id,
status,
updated_by_id: user.id,
})
.select()
.single();
if (error) throw error;
// Get the user profile for broadcasting
const { data: profile } = await supabase
.from('profiles')
.select('*')
.eq('id', user.id)
.single();
if (profile && insertedStatus) {
const userStatus: UserStatus = {
...profile,
status: insertedStatus.status,
created_at: insertedStatus.created_at,
};
await broadcastStatusUpdate(userStatus);
}
return { success: true, data: undefined };
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
};
}
};
export const getUserHistory = async (
userId: string,
page = 1,
perPage = 50,
): Promise<Result<PaginatedHistory>> => {
try {
const supabase = createClient();
const offset = (page - 1) * perPage;
// Get count
const { count } = await supabase
.from('statuses')
.select('*', { count: 'exact', head: true })
.eq('user_id', userId);
// Get data
const { data: statuses, error } = await supabase
.from('statuses')
.select('*')
.eq('user_id', userId)
.order('created_at', { ascending: false })
.range(offset, offset + perPage - 1);
if (error) throw error;
const typedStatuses: Status[] = statuses ?? [];
// Get user profile
const { data: userProfile, error: userProfileError } = await supabase
.from('profiles')
.select('*')
.eq('id', userId)
.single();
if (userProfileError) throw userProfileError;
if (!userProfile) throw new Error('User profile not found');
// Get updated_by profiles - filter out nulls properly
const updatedByIds = typedStatuses
.map(s => s.updated_by_id)
.filter((id): id is string => id !== null);
let updatedByProfiles: Profile[] = [];
if (updatedByIds.length > 0) {
const { data, error: updatedByError } = await supabase
.from('profiles')
.select('*')
.in('id', updatedByIds);
if (updatedByError) throw updatedByError;
updatedByProfiles = data ?? [];
}
const updatedByMap = new Map(updatedByProfiles.map(p => [p.id, p]));
const historyEntries: HistoryEntry[] = typedStatuses.map(entry => ({
id: entry.id,
status: entry.status,
created_at: entry.created_at,
updated_by: entry.updated_by_id ? updatedByMap.get(entry.updated_by_id) : undefined,
user_profile: userProfile,
}));
const totalCount = count ?? 0;
const totalPages = Math.ceil(totalCount / perPage);
return {
success: true,
data: {
data: historyEntries,
meta: {
current_page: page,
per_page: perPage,
total_pages: totalPages,
total_count: totalCount
},
},
};
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
};
}
};
export const getAllHistory = async (
page = 1,
perPage = 50,
): Promise<Result<PaginatedHistory>> => {
try {
const supabase = createClient();
const offset = (page - 1) * perPage;
// Get count
const { count } = await supabase
.from('statuses')
.select('*', { count: 'exact', head: true });
// Get data
const { data: statuses, error } = await supabase
.from('statuses')
.select('*')
.order('created_at', { ascending: false })
.range(offset, offset + perPage - 1);
if (error) throw error;
const typedStatuses: Status[] = statuses ?? [];
// Get all profiles - filter out nulls properly
const userIds = [...new Set(typedStatuses.map(s => s.user_id))];
const updatedByIds = typedStatuses
.map(s => s.updated_by_id)
.filter((id): id is string => id !== null);
const allIds = [...new Set([...userIds, ...updatedByIds])];
const { data: profiles, error: profileError } = await supabase
.from('profiles')
.select('*')
.in('id', allIds);
if (profileError) throw profileError;
const profileMap = new Map((profiles ?? []).map(p => [p.id, p]));
const historyEntries: HistoryEntry[] = typedStatuses.map(entry => {
const userProfile = profileMap.get(entry.user_id);
if (!userProfile) {
throw new Error(`User profile not found for ID: ${entry.user_id}`);
}
return {
id: entry.id,
status: entry.status,
created_at: entry.created_at,
updated_by: entry.updated_by_id ? profileMap.get(entry.updated_by_id) : undefined,
user_profile: userProfile,
};
});
const totalCount = count ?? 0;
const totalPages = Math.ceil(totalCount / perPage);
return {
success: true,
data: {
data: historyEntries,
meta: {
current_page: page,
per_page: perPage,
total_pages: totalPages,
total_count: totalCount
},
},
};
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
};
}
};

View File

@ -1,5 +1,6 @@
export * from './auth';
export * from './public';
export * from './status';
export * from './storage';
export * from './useFileUpload';

52
src/lib/hooks/status.ts Normal file
View File

@ -0,0 +1,52 @@
'use client';
import {
createClient,
type Profile,
type Result,
type Status,
} from '@/utils/supabase';
type UserWithStatus = {
user: Profile;
status: string;
created_at: string;
updated_by: Profile;
};
type PaginatedHistory = UserWithStatus[] & {
meta: {
current_page: number;
per_page: number;
total_pages: number;
total_count: number;
};
};
export const getUsersWithStatuses = async (): Promise<Result<UserWithStatus[]>> => {
try {
const supabase = createClient();
// Get only users with recent statuses (Past 7 days)
const oneWeekAgo = new Date();
oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
const { data: recentStatuses, error } = await supabase
.from('statuses')
.select(`
user:profiles!user_id(*),
status,
created_at,
updated_by:profiles!updated_by_id(*)
`)
.gte('created_at', oneWeekAgo.toISOString())
.order('created_at', { ascending: false }) as {data: UserWithStatus[], error: unknown};
if (error) throw error;
if (!recentStatuses.length) return { success: true, data: []};
return { success: true, data: recentStatuses };
} catch (error) {
return { success: false, error: `Error: ${error as string}` };
}
};