Skip to content

Commit

Permalink
feat: add default values for users #682
Browse files Browse the repository at this point in the history
- moved long constants to const for display names
- created functions to update new users with name, image, displayname, and username
- updated jwt callback
- updated create personal team and new team to include a team image too
- minor tweaks to team switcher component

Closes #682
  • Loading branch information
srizvi committed Jan 28, 2024
1 parent 17f127c commit 130817b
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 279 deletions.
19 changes: 11 additions & 8 deletions apps/ai/src/components/teams/team-switcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,12 @@ export const TeamSwitcher: React.FC<TeamSwitcherProps> = (props) => {
}
alt={activeTeam?.name ?? 'Active Team'}
/>
<AvatarFallback>
{(activeTeam?.name ?? 'Active Team')[0]}
</AvatarFallback>
<AvatarFallback>&!</AvatarFallback>
</Avatar>
{activeTeam?.name ?? 'Active Team'}
<span className="truncate text-xs">
{activeTeam?.name ?? 'Active Team'}
</span>

<Icons.caretDown
className={cn(
'ml-auto h-4 w-4 shrink-0 transition-transform',
Expand Down Expand Up @@ -154,9 +155,11 @@ export const TeamSwitcher: React.FC<TeamSwitcherProps> = (props) => {
personalTeam?.name ?? 'Personal Team'
}`}
/>
<AvatarFallback>{personalTeam?.name?.[0]}</AvatarFallback>
<AvatarFallback>&!</AvatarFallback>
</Avatar>
{personalTeam?.name ?? 'Personal Team'}
<span className="truncate text-xs">
{personalTeam?.name ?? 'Personal Team'}
</span>
<Icons.check
className={cn(
'ml-auto h-4 w-4 text-foreground',
Expand Down Expand Up @@ -191,9 +194,9 @@ export const TeamSwitcher: React.FC<TeamSwitcherProps> = (props) => {
}
alt={`Profile avatar of ${team?.name ?? 'My Team'}`}
/>
<AvatarFallback>{team.name?.[0]}</AvatarFallback>
<AvatarFallback>&!</AvatarFallback>
</Avatar>
<p className="truncate">{team.name}</p>
<span className="truncate">{team.name}</span>
<Icons.check
className={cn(
'ml-auto h-4 w-4 text-foreground',
Expand Down
4 changes: 4 additions & 0 deletions apps/ai/src/lib/actions/teams/create-team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { APP_BP } from '@av/utils';

import { db } from '#/lib/db';
import { getSession } from '#/lib/operations/user/session';
import { createImage } from '#/lib/operations/user/update-new-user';

export const createTeam = async (data: NewTeamFormValues) => {
'use server';
Expand All @@ -29,10 +30,13 @@ export const createTeam = async (data: NewTeamFormValues) => {
});
//console.log('Generated slug:', slug);

const teamImageUrl = createImage(session.user.id, 'team');

const team = await db.team.create({
data: {
name: data.name,
slug,
image: teamImageUrl,
members: {
create: {
userId: session.user.id,
Expand Down
19 changes: 8 additions & 11 deletions apps/ai/src/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import GoogleProvider from 'next-auth/providers/google';

import { db } from '#/lib/db';
import { createPersonalTeam } from '#/lib/operations/user/create-personal-team';
import { generateUniqueDisplayName } from '#/lib/operations/user/generate-unique-display-name';
import { getSession } from '#/lib/operations/user/session';
import { sendVerificationRequest } from '#/lib/resend/send-verification-request';
import { sendWelcomeEmail } from '#/lib/resend/send-welcome-email';

import { updateNewUser } from './operations/user/update-new-user';

const VERCEL_DEPLOYMENT = !!process.env.VERCEL_URL;

export const authOptions: NextAuthOptions = {
Expand Down Expand Up @@ -94,16 +95,12 @@ export const authOptions: NextAuthOptions = {
return token;
}

if (!dbUser.displayName) {
const uniqueUsername = await generateUniqueDisplayName();
await db.user.update({
where: { id: dbUser.id },
data: { displayName: uniqueUsername },
});
token.displayName = uniqueUsername;
} else {
token.displayName = dbUser.displayName;
}
const updates = await updateNewUser(dbUser.id);

token.displayName = updates.displayName ?? dbUser.displayName;
token.image = updates.image ?? dbUser.image;
token.name = updates.name ?? dbUser.name;
token.username = updates.username ?? dbUser.username;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { team, isNew } = await createPersonalTeam(dbUser.id);
Expand Down
225 changes: 225 additions & 0 deletions apps/ai/src/lib/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,228 @@ export const SITE_URL =
process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview'
? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`
: process.env.NEXT_PUBLIC_APP_URL ?? VERCEL_URL ?? 'http://localhost:3001';

export const pokemonNames: string[] = [
'Bulbasaur',
'Ivysaur',
'Venusaur',
'Charmander',
'Charmeleon',
'Charizard',
'Squirtle',
'Wartortle',
'Blastoise',
'Caterpie',
'Metapod',
'Butterfree',
'Weedle',
'Kakuna',
'Beedrill',
'Pidgey',
'Pidgeotto',
'Pidgeot',
'Rattata',
'Raticate',
'Spearow',
'Fearow',
'Ekans',
'Arbok',
'Pikachu',
'Raichu',
'Sandshrew',
'Sandslash',
'Nidoran',
'Nidorina',
'Nidoqueen',
'Nidoran',
'Nidorino',
'Nidoking',
'Clefairy',
'Clefable',
'Vulpix',
'Ninetales',
'Jigglypuff',
'Wigglytuff',
'Zubat',
'Golbat',
'Oddish',
'Gloom',
'Vileplume',
'Paras',
'Parasect',
'Venonat',
'Venomoth',
'Diglett',
'Dugtrio',
'Meowth',
'Persian',
'Psyduck',
'Golduck',
'Mankey',
'Primeape',
'Growlithe',
'Arcanine',
'Poliwag',
'Poliwhirl',
'Poliwrath',
'Abra',
'Kadabra',
'Alakazam',
'Machop',
'Machoke',
'Machamp',
'Bellsprout',
'Weepinbell',
'Victreebel',
'Tentacool',
'Tentacruel',
'Geodude',
'Graveler',
'Golem',
'Ponyta',
'Rapidash',
'Slowpoke',
'Slowbro',
'Magnemite',
'Magneton',
'Farfetched',
'Doduo',
'Dodrio',
'Seel',
'Dewgong',
'Grimer',
'Muk',
'Shellder',
'Cloyster',
'Gastly',
'Haunter',
'Gengar',
'Onix',
'Drowzee',
'Hypno',
'Krabby',
'Kingler',
'Voltorb',
'Electrode',
'Exeggcute',
'Exeggutor',
'Cubone',
'Marowak',
'Hitmonlee',
'Hitmonchan',
'Lickitung',
'Koffing',
'Weezing',
'Rhyhorn',
'Rhydon',
'Chansey',
'Tangela',
'Kangaskhan',
'Horsea',
'Seadra',
'Goldeen',
'Seaking',
'Staryu',
'Starmie',
'Mr. Mime',
'Scyther',
'Jynx',
'Electabuzz',
'Magmar',
'Pinsir',
'Tauros',
'Magikarp',
'Gyarados',
'Lapras',
'Ditto',
'Eevee',
'Vaporeon',
'Jolteon',
'Flareon',
'Porygon',
'Omanyte',
'Omastar',
'Kabuto',
'Kabutops',
'Aerodactyl',
'Snorlax',
'Articuno',
'Zapdos',
'Moltres',
'Dratini',
'Dragonair',
'Dragonite',
'Mewtwo',
'Mew',
];

export const colorNames: string[] = [
'Red',
'Blue',
'Green',
'Yellow',
'Orange',
'Purple',
'Magenta',
'Turquoise',
'Teal',
'Lime',
'Coral',
'Aqua',
'Mauve',
'Lavender',
'Pink',
'Maroon',
'Olive',
'Navy',
'Grey',
'Tan',
'Crimson',
'Fuchsia',
'Cyan',
'Indigo',
'Violet',
'Cerise',
'Rust',
'Gold',
'Silver',
'Bronze',
'Amber',
'Peach',
'Mint',
'Emerald',
'Sapphire',
'Ruby',
'Periwinkle',
'Beige',
'Khaki',
'Jade',
'Auburn',
'Burgundy',
'Chartreuse',
'Azure',
'Ivory',
'Champagne',
'Taupe',
'Orchid',
'Plum',
'Rose',
'Sienna',
'Mustard',
'Charcoal',
'Apricot',
'Turmeric',
'Saffron',
'Lemon',
'Raspberry',
'Scarlet',
'Wine',
'Umber',
'Cobalt',
'Copper',
'Sangria',
'Caramel',
'Sand',
'Pistachio',
'Lilac',
];
7 changes: 6 additions & 1 deletion apps/ai/src/lib/operations/user/create-personal-team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import slugify from 'slugify';

import { db } from '#/lib/db';

import { createImage } from './update-new-user';

export const createPersonalTeam = async (userId: string) => {
const existingPersonalTeam = await db.team.findFirst({
where: {
Expand Down Expand Up @@ -30,14 +32,17 @@ export const createPersonalTeam = async (userId: string) => {
throw new Error('User not found or displayName is missing');
}

const teamImageUrl = createImage(userId, 'team');

const teamSlug = user.name
? slugify(user.name, { lower: true, strict: true })
: user.displayName;

const team = await db.team.create({
data: {
name: user.name ?? user.displayName,
name: user.displayName ?? user.name,
slug: teamSlug,
image: teamImageUrl,
isPersonal: true,
members: {
create: {
Expand Down
Loading

0 comments on commit 130817b

Please sign in to comment.