Skip to content

Commit fab1b8b

Browse files
committed
refactor: Filter and display only relevant teams, remove debug log, and un-hardcode checkbox state.
1 parent 096e2e5 commit fab1b8b

6 files changed

Lines changed: 24 additions & 92 deletions

File tree

api/auth.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@ export async function handleGoogleCallback(
100100
// Verify and decode the ID token
101101
await verifyGoogleToken(tokens.id_token)
102102
const userInfo = decodeGoogleJWT(tokens.id_token) as GoogleUserInfo
103-
if (userInfo.picture) {
104-
userInfo.picture = await savePicture(userInfo.picture)
105-
}
103+
userInfo.picture &&= await savePicture(userInfo.picture)
106104
const sessionId = await authenticateOauthUser(userInfo)
107105

108106
// Return response with session cookie

api/lmdb-store.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { STORE_SECRET, STORE_URL } from '/api/lib/env.ts'
22

33
const headers = { authorization: `Bearer ${STORE_SECRET}` }
4-
export const pending = new Set<Promise<unknown>>()
5-
export const sync = () => Promise.all([...pending])
64
export const getOne = async <T>(
75
path: string,
86
id: string,
@@ -13,17 +11,6 @@ export const getOne = async <T>(
1311
return res.json()
1412
}
1513

16-
export const set = (path: string, id: unknown, dt: unknown) => {
17-
const body = typeof dt === 'string' ? dt : JSON.stringify(dt)
18-
const p = fetch(
19-
`${STORE_URL}/${path}/${encodeURIComponent(String(id))}`,
20-
{ method: 'POST', body, headers },
21-
)
22-
pending.add(p)
23-
p.finally(() => pending.delete(p))
24-
return 1
25-
}
26-
2714
export const get = async <T>(
2815
path: string,
2916
params?: { q?: string; limit?: number; from?: number },

api/routes.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
TeamDetailDef,
1212
User,
1313
UserDef,
14-
// UsersCollection,
1514
} from './schema.ts'
1615
import {
1716
ARR,
@@ -67,11 +66,11 @@ const withDeploymentSession = async (ctx: RequestContext) => {
6766

6867
const userInTeam = async (teamId: string, userId?: string) => {
6968
if (!userId) return false
70-
const matches = await get<{ id: string }[]>(
69+
const matches = await getOne<{ id: string }>(
7170
`google/group/${teamId}`,
72-
{ q: `select(.id == "${userId}") | { id: .id }` },
71+
userId,
7372
)
74-
return matches.length > 0
73+
return !!matches
7574
}
7675

7776
const withDeploymentTableAccess = async (
@@ -121,6 +120,15 @@ const projectOutput = OBJ({
121120
updatedAt: optional(NUM('The last update date of the project')),
122121
})
123122

123+
const userNameCache = new Map<string, string>()
124+
const getUserName = async (userId: string) => {
125+
if (userNameCache.has(userId)) return userNameCache.get(userId)
126+
const user = await getOne<{ name: { fullName: string } }>('google/user', userId)
127+
const name = user?.name?.fullName ?? userId
128+
userNameCache.set(userId, name)
129+
return name
130+
}
131+
124132
const defs = {
125133
'GET/api/health': route({
126134
fn: () => new Response('OK'),
@@ -166,7 +174,7 @@ const defs = {
166174
const groups = await get<{ id: string; name: string }[]>(
167175
'google/group',
168176
{
169-
q: 'select((.kind == "admin#directory#group") and (.email | contains("@01edu")) and (.directMembersCount > 1)) | { id: .id, name: .name }',
177+
q: 'select((.kind == "admin#directory#group") and (.email | endswith("@01edu.ai")) and (.directMembersCount > 1)) | { id: .id, name: .name }',
170178
},
171179
)
172180

@@ -203,15 +211,12 @@ const defs = {
203211

204212
const enrichedMembers = await Promise.all(
205213
members.map(async (m) => {
206-
const user = await getOne<{ name: { fullName: string } }>(
207-
'google/user',
208-
m.id,
209-
)
214+
const name = await getUserName(m.id)
210215
const admin = AdminsCollection.get(m.id)
211216
return {
212217
email: m.email,
213218
id: m.id,
214-
name: user?.name?.fullName ?? m.email,
219+
name,
215220
isAdmin: !!admin,
216221
}
217222
}),

tasks/clickhouse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ if (import.meta.main) {
4141
console.log('logs table is ready')
4242
} catch (error) {
4343
console.error('Error creating ClickHouse table:', { error })
44-
// Deno.exit(1)
44+
Deno.exit(1)
4545
}
4646
}

tasks/seed.ts

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,8 @@ import {
33
DeploymentsCollection,
44
type Project,
55
ProjectsCollection,
6-
type User,
7-
UsersCollection,
86
} from '/api/schema.ts'
97

10-
const users: User[] = [
11-
{
12-
userEmail: 'admin@example.com',
13-
userFullName: 'Admin User',
14-
userPicture: undefined,
15-
isAdmin: true,
16-
},
17-
{
18-
userEmail: 'member1@example.com',
19-
userFullName: 'Member One',
20-
userPicture: undefined,
21-
isAdmin: false,
22-
},
23-
{
24-
userEmail: 'member2@example.com',
25-
userFullName: 'Member Two',
26-
userPicture: undefined,
27-
isAdmin: false,
28-
},
29-
{
30-
userEmail: 'clement@01talent.com',
31-
userFullName: 'Clement',
32-
userPicture: undefined,
33-
isAdmin: true,
34-
},
35-
{
36-
userEmail: 'abdou.top@01talent.com',
37-
userFullName: 'Abdou Top',
38-
userPicture: undefined,
39-
isAdmin: true,
40-
},
41-
]
42-
438
const projects: Omit<Project, 'createdAt'>[] = [
449
{
4510
slug: 'website-redesign',
@@ -66,7 +31,6 @@ const projects: Omit<Project, 'createdAt'>[] = [
6631

6732
async function clearCollection(
6833
collection:
69-
| typeof UsersCollection
7034
| typeof ProjectsCollection
7135
| typeof DeploymentsCollection,
7236
) {
@@ -81,17 +45,9 @@ async function seed() {
8145
console.log('Starting seeding process...')
8246

8347
// Clear existing data
84-
await clearCollection(UsersCollection)
8548
await clearCollection(ProjectsCollection)
8649
await clearCollection(DeploymentsCollection)
8750

88-
// Seed users
89-
console.log('Seeding users...')
90-
for (const user of users) {
91-
await UsersCollection.insert(user)
92-
}
93-
console.log('Users seeded.')
94-
9551
// Seed projects
9652
console.log('Seeding projects...')
9753
for (const [_, project] of projects.entries()) {

web/pages/ProjectsPage.tsx

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ const ProjectCard = (
123123
{ project, members }: { project: Project; members: string[] },
124124
) => {
125125
const isMember = members.includes(user.data?.id || '')
126-
console.log(members, user.data?.id, isMember)
127126
return (
128127
<A
129128
key={project.slug}
@@ -187,12 +186,6 @@ const TeamMembersRow = ({ user }: { user: Team['members'][number] }) => (
187186
<input
188187
type='checkbox'
189188
class='toggle toggle-sm toggle-primary'
190-
checked={true}
191-
// onChange={(e) => {
192-
// if ((e.target as HTMLInputElement).checked) {
193-
// addUserToTeam(user, team)
194-
// } else removeUserFromTeam(user, team)
195-
// }}
196189
/>
197190
</td>
198191
</tr>
@@ -569,19 +562,12 @@ export function ProjectsPage() {
569562
? ''
570563
: 'pointer-events-none cursor-not-allowed opacity-20'
571564

572-
const hasTeams = teams.data && teams.data.length > 0
573-
const sortedTeams = teams.data?.sort((a, b) => {
574-
const aHasUser = a.members.includes(user.data?.id || '')
575-
const bHasUser = b.members.includes(user.data?.id || '')
576-
const aProjects = projectsByTeam[a.id] ?? []
577-
const bProjects = projectsByTeam[b.id] ?? []
578-
if (aHasUser && !bHasUser) return -1
579-
if (!aHasUser && bHasUser) return 1
580-
if (aProjects.length > bProjects.length) return -1
581-
if (aProjects.length < bProjects.length) return 1
582-
return 0
583-
}) ?? []
584-
565+
const sortedTeams =
566+
teams.data?.filter((t) =>
567+
(t.members.includes(user.data?.id || '') || isAdmin) &&
568+
projectsByTeam[t.id]?.length > 0
569+
) ?? []
570+
585571
return (
586572
<PageLayout>
587573
<PageHeader>
@@ -617,7 +603,7 @@ export function ProjectsPage() {
617603
</PageHeader>
618604

619605
<PageContent>
620-
{!hasTeams
606+
{sortedTeams.length < 1
621607
? (
622608
<EmptyState
623609
icon={Search}

0 commit comments

Comments
 (0)