import { useEffect, useState } from 'react'; import { useToast } from '../../hooks/use-toast'; import { UserItem } from './UserItem'; import { Check, Copy, Group, UserPlus2, Users2 } from 'lucide-react'; import { httpGet } from '../../lib/http'; import { getUser } from '../../lib/jwt.ts'; import { useCopyText } from '../../hooks/use-copy-text.ts'; import { cn } from '../../lib/classname.ts'; export type FriendshipStatus = | 'none' | 'sent' | 'received' | 'accepted' | 'rejected' | 'got_rejected'; type FriendResourceProgress = { updatedAt: string; title: string; resourceId: string; resourceType: string; learning: number; skipped: number; done: number; total: number; }; export type ListFriendsResponse = { userId: string; name: string; email: string; avatar: string; status: FriendshipStatus; roadmaps: FriendResourceProgress[]; bestPractices: FriendResourceProgress[]; }[]; type ShareFriendListProps = { setFriends: (friends: ListFriendsResponse) => void; friends: ListFriendsResponse; sharedFriendIds: string[]; setSharedFriendIds: (friendIds: string[]) => void; }; export function ShareFriendList(props: ShareFriendListProps) { const userId = getUser()?.id!; const { setFriends, friends, sharedFriendIds, setSharedFriendIds } = props; const toast = useToast(); const { isCopied, copyText } = useCopyText(); const [isLoading, setIsLoading] = useState(true); const [isAddingFriend, setIsAddingFriend] = useState(false); async function loadFriends() { if (friends.length > 0) { return; } setIsLoading(true); const { response, error } = await httpGet( `${import.meta.env.PUBLIC_API_URL}/v1-list-friends`, ); if (error || !response) { toast.error(error?.message || 'Something went wrong'); return; } setFriends(response.filter((friend) => friend.status === 'accepted')); } useEffect(() => { loadFriends().finally(() => { setIsLoading(false); }); }, []); const loadingFriends = isLoading && ( ); const isDev = import.meta.env.DEV; const baseWebUrl = isDev ? 'http://localhost:3000' : 'https://roadmap.sh'; const befriendUrl = `${baseWebUrl}/befriend?u=${userId}`; return ( <> {(friends.length > 0 || isLoading) && (

Select Friends to share the roadmap with

)} {loadingFriends} {friends.length > 0 && !isLoading && ( <> {!isAddingFriend && (

Don't see a Friend?{' '}

)} {isAddingFriend && (

Share the link below with your friends to invite them

{ e.preventDefault(); (e.target as HTMLInputElement).select(); copyText(befriendUrl); }} className={cn( 'w-full rounded-md border px-2 py-2 text-sm focus:shadow-none focus:outline-0', { 'border-green-400 bg-green-50': isCopied, }, )} />
)} )} {friends.length === 0 && !isLoading && (

You do not have any friends yet.
{' '} Invite your friends to share roadmaps with.

)} ); }