Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

SSC: Refactor team management#62694

Merged
vdavid merged 8 commits into
mainfrom
dv/refactor-team-management
May 16, 2024
Merged

SSC: Refactor team management#62694
vdavid merged 8 commits into
mainfrom
dv/refactor-team-management

Conversation

@vdavid

@vdavid vdavid commented May 15, 2024

Copy link
Copy Markdown
Contributor

We'll need to reuse at least some of the data fetching I've created (for example, we'll need the subscription summary on pretty much all the pages), so I extracted that logic into easy-to-use hooks.

Also did some renaming and cleaning to make it easier to reuse and test later.

Test plan

Manually tested that the Team management page and the new Subscription page still work.

@vdavid vdavid requested review from a team and chrsmith May 15, 2024 13:06
@cla-bot cla-bot Bot added the cla-signed label May 15, 2024
// take care of exchanging the Sourcegraph session credentials for a SAMS access token.
// And then proxy the request onto the SSC backend, which will actually create the
// checkout session.
// TODO: Use fetchThroughSSCProxy instead of fetch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had this TODO in the code to do this and I meant to do it soon, so here we go.

@@ -0,0 +1,40 @@
import { useCallback } from 'react'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted the response types to interfaces (they can be extended later if we need more data from those responses), and also our internal data types, which are a bit more processed. I put everything needed to use subscription data in this file. Same for invites and team members.

authenticatedUser: AuthenticatedUser
}

// TODO: Remove this mock data

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is less helpful now so cleaned it up.

const newSeatsPurchased: number | null = newSeatsPurchasedParam ? parseInt(newSeatsPurchasedParam, 10) : null

// Load data
const [subscriptionData, setSubscriptionData] = useState<{

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of this red is taken care of our new hooks, in a reusable way.

Comment on lines -87 to -88
const subscriptionSeatCount = subscriptionData?.subscriptionSeatCount
const isProUser = subscriptionData?.isProUser

@vdavid vdavid May 15, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two variables in particular were unhelpful, so I inlined them.


{subscriptionDataError || subscriptionSummaryDataError || membersDataError || invitesDataError ? (
<div className={classNames('mb-4', styles.alert, styles.errorAlert)}>
<H3>Failed to load team data.</H3>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unified these error messages based on @chrsmith's suggestion earlier. We could concat them or remove it altogether—I didn't want to invest more time in this at this point.

Comment thread client/web/src/cody/team/TeamMemberList.tsx Outdated
Comment on lines -12 to -18
export interface TeamMember {
accountId: string
displayName: string | null
email: string
avatarUrl: string | null
role: 'admin' | 'member'
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These types were not right here. Now we have the hooks as type-specific logic, so moved these types from here and reserved this file for the React component only.

Comment on lines +63 to +86
// React hook to fetch data through the SSC proxy and convert the response to a more usable format.
// This is a low-level hook that is meant to be used by other hooks that need to fetch data from the SSC API.
export const useSSCData = <T extends object, U extends object | null>(
endpoint: string,
transformResponse: (response: T) => U
): [U | null, Error | null] => {
const [data, setData] = useState<U | null>(null)
const [error, setError] = useState<Error | null>(null)
useEffect(() => {
async function loadData(): Promise<void> {
try {
const response = await callSSCProxy(endpoint, 'GET')
const responseJson = await response.json()
setData(transformResponse(responseJson))
} catch (error) {
setError(error)
}
}

void loadData()
}, [endpoint, transformResponse])

return [data, error]
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This turned out to be quite complex. One thing I'm not satisfied with is that the useCallback() wrapping is in the higher-level hooks that use this one.
It'd be a lot nicer and less error-prone to include that wrapping here in a single location rather than in every higher-level hook, but I didn't manage to do it.
If you have a solution, @taras-yemets, please throw it in.

@vdavid vdavid requested a review from taras-yemets May 15, 2024 13:16
Comment thread client/web/src/cody/subscription/subscriptions.ts Outdated
Comment thread client/web/src/cody/subscription/subscriptions.ts Outdated
Comment thread client/web/src/cody/management/subscription/new/CodyProCheckoutForm.tsx Outdated
Comment thread client/web/src/cody/team/CodyManageTeamPage.tsx
Comment thread client/web/src/cody/subscription/subscriptions.ts Outdated
Comment thread client/web/src/cody/team/TeamMemberList.tsx Outdated
Comment thread client/web/src/cody/team/TeamMemberList.tsx Outdated
Comment thread client/web/src/cody/team/teamInvites.ts Outdated
Comment thread client/web/src/cody/util.ts
Comment thread client/web/src/cody/util.ts
Comment thread client/web/src/cody/util.ts Outdated
Comment thread client/web/src/cody/subscription/subscriptions.ts Outdated
Comment thread client/web/src/cody/team/teamInvites.ts Outdated
Comment on lines +19 to +20
export const useCodyTeamInvites = (): [TeamInvite[] | null, Error | null] =>
useSSCQuery<InviteResponse, TeamInvite[]>('/team/current/invites', transformResponse)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useCodyTeamInvites and other wrappers around useSSCQuery are used once. Their main functionality is hiding the request URL and the map response function. Maybe we should just use useSSCQuery in components, WDYT?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, I just thought it's neat to make this easy to reuse elsewhere. If you don't mind, I'll leave it as-is for now and inline it if we truly don't use it anywhere else.

Comment thread client/web/src/cody/subscription/subscriptions.ts Outdated

@0xnmn 0xnmn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a couple of comments, overall looks good.

@chrsmith chrsmith left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code in this PR is a step in the right direction, so let's get this checked-in!

I don't think this is the right design for how we surface the data types from our REST API, and that we probably need a more "rigid" approach to that. (But I'll take care of that in a separate PR, as this is strictly an improvement and I don't want to bother you with some other design that can easily land later.)

Comment thread client/web/src/cody/subscription/subscriptions.ts Outdated
Comment thread client/web/src/cody/subscription/subscriptions.ts Outdated
Comment thread client/web/src/cody/team/CodyManageTeamPage.tsx
@vdavid vdavid enabled auto-merge (squash) May 16, 2024 12:38
@vdavid vdavid merged commit aae97ad into main May 16, 2024
@vdavid vdavid deleted the dv/refactor-team-management branch May 16, 2024 12:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants