Repository navigation
[WIP]: refactor(Account): Read the current organisation and user through hooks #9026
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
talissoncosta
wants to merge
7
commits into
main
Choose a base branch
from
refactor/current-organisation-facade
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+676
−441
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a432bb4
refactor(Organisations): Add a facade for reading the current organis…
talissoncosta e4088aa
refactor(Organisations): Read the current organisation through the fa…
talissoncosta 77c73c4
refactor(Organisations): Read the admin role through the facade
talissoncosta f97adee
refactor(Users): Read the current user through a facade
talissoncosta ed68cb0
fix(Organisations): Address review on the current organisation facade
talissoncosta bd8c347
test(Organisations): Cover the current organisation and user getters
talissoncosta 1528902
chore(Lint): Warn on new AccountStore imports
talissoncosta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import AccountStore from 'common/stores/account-store' | ||
|
Check warning on line 1 in frontend/common/hooks/__tests__/currentAccount.test.ts
|
||
| import { | ||
| getCurrentOrganisation, | ||
| isOrganisationAdmin, | ||
| } from 'common/hooks/useCurrentOrganisation' | ||
| import { getCurrentUser } from 'common/hooks/useCurrentUser' | ||
| import { AccountModel, Organisation } from 'common/types/responses' | ||
|
|
||
| jest.mock('common/stores/account-store', () => ({ | ||
| getOrganisation: jest.fn(), | ||
| getUser: jest.fn(), | ||
| })) | ||
|
|
||
| const store = jest.mocked(AccountStore) | ||
|
|
||
| const organisation = (role: Organisation['role']): Organisation => ({ | ||
| api_limit_grace_period_used: false, | ||
| api_limit_restriction_enabled: false, | ||
| block_access_to_admin: false, | ||
| created_date: '2026-01-01T00:00:00Z', | ||
| id: 1, | ||
| name: 'Acme', | ||
| num_seats: 1, | ||
| overage_billing_eligible: false, | ||
| persist_trait_data: true, | ||
| restrict_project_create_to_admin: false, | ||
| role, | ||
| stop_serving_flags: false, | ||
| subscription: { | ||
| cancellation_date: null, | ||
| current_billing_period: null, | ||
| customer_id: '', | ||
| has_active_billing_periods: false, | ||
| id: 1, | ||
| max_api_calls: 50000, | ||
| max_seats: 1, | ||
| notes: null, | ||
| payment_method: null, | ||
| plan: null, | ||
| subscription_date: '2026-01-01T00:00:00Z', | ||
| subscription_id: null, | ||
| uuid: 'subscription-uuid', | ||
| }, | ||
| webhook_notification_email: null, | ||
| }) | ||
|
|
||
| describe('getCurrentOrganisation', () => { | ||
| it.each([ | ||
| ['the organisation', organisation('ADMIN'), organisation('ADMIN')], | ||
| ['undefined for null', null, undefined], | ||
| ['undefined for undefined', undefined, undefined], | ||
| ])('returns %s', (_, stored, expected) => { | ||
| store.getOrganisation.mockReturnValue(stored) | ||
| expect(getCurrentOrganisation()).toEqual(expected) | ||
| }) | ||
| }) | ||
|
|
||
| describe('isOrganisationAdmin', () => { | ||
| it.each([ | ||
| ['an admin', organisation('ADMIN'), true], | ||
| ['a user', organisation('USER'), false], | ||
| ['no organisation', undefined, false], | ||
| ])('for %s returns %s', (_, value, expected) => { | ||
| expect(isOrganisationAdmin(value)).toBe(expected) | ||
| }) | ||
| }) | ||
|
|
||
| describe('getCurrentUser', () => { | ||
| const user: AccountModel = { | ||
| auth_type: 'EMAIL', | ||
| date_joined: '2026-01-01T00:00:00Z', | ||
| email: 'ada@example.com', | ||
| first_name: 'Ada', | ||
| id: 7, | ||
| is_superuser: false, | ||
| last_name: 'Lovelace', | ||
| organisations: [], | ||
| pylon_email_signature: '', | ||
| sign_up_type: 'NO_INVITE', | ||
| uuid: 'user-uuid', | ||
| } | ||
| it.each([ | ||
| ['the user', user, user], | ||
| ['undefined for null', null, undefined], | ||
| ])('returns %s', (_, stored, expected) => { | ||
| store.getUser.mockReturnValue(stored) | ||
| expect(getCurrentUser()).toEqual(expected) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { useSyncExternalStore } from 'react' | ||
| import AccountStore from 'common/stores/account-store' | ||
|
|
||
| // Bumped on every store change, so React can tell a change happened between | ||
| // render and subscribe. Counted from the first render, not at import, so | ||
| // tests that mock the store can still load this module. | ||
| let version = 0 | ||
| let counting = false | ||
| const countChanges = () => { | ||
| if (counting) return | ||
| counting = true | ||
| AccountStore.on('change', () => { | ||
| version += 1 | ||
| }) | ||
| } | ||
|
|
||
| const subscribe = (onChange: () => void) => { | ||
| AccountStore.on('change', onChange) | ||
| return () => { | ||
| AccountStore.off('change', onChange) | ||
| } | ||
| } | ||
|
|
||
| const getVersion = () => version | ||
|
|
||
| // Re-renders the caller whenever the legacy account store changes. | ||
| const useAccountStoreUpdates = () => { | ||
| countChanges() | ||
| useSyncExternalStore(subscribe, getVersion) | ||
| } | ||
|
|
||
| export default useAccountStoreUpdates |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import AccountStore from 'common/stores/account-store' | ||
| import { Organisation } from 'common/types/responses' | ||
| import useAccountStoreUpdates from './useAccountStoreUpdates' | ||
|
|
||
| export const getCurrentOrganisation = (): Organisation | undefined => | ||
| AccountStore.getOrganisation() ?? undefined | ||
|
|
||
| export const isOrganisationAdmin = (organisation?: Organisation): boolean => | ||
| organisation?.role === 'ADMIN' | ||
|
|
||
| export const useCurrentOrganisation = (): Organisation | undefined => { | ||
| useAccountStoreUpdates() | ||
| return getCurrentOrganisation() | ||
| } | ||
|
|
||
| export const useIsOrganisationAdmin = (): boolean => | ||
| isOrganisationAdmin(useCurrentOrganisation()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import AccountStore from 'common/stores/account-store' | ||
| import { AccountModel } from 'common/types/responses' | ||
| import useAccountStoreUpdates from './useAccountStoreUpdates' | ||
|
|
||
| export const getCurrentUser = (): AccountModel | undefined => | ||
| AccountStore.getUser() ?? undefined | ||
|
|
||
| export const useCurrentUser = (): AccountModel | undefined => { | ||
| useAccountStoreUpdates() | ||
| return getCurrentUser() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import React, { ComponentType } from 'react' | ||
| import { Organisation } from 'common/types/responses' | ||
| import { useCurrentOrganisation } from './useCurrentOrganisation' | ||
|
|
||
| export type CurrentOrganisationProps = { | ||
| organisation: Organisation | undefined | ||
| } | ||
|
|
||
| // For class components, which cannot call the hook. | ||
| const withCurrentOrganisation = <P extends object>( | ||
| Component: ComponentType<P & CurrentOrganisationProps>, | ||
| ) => { | ||
| const WithCurrentOrganisation = (props: P) => { | ||
| const organisation = useCurrentOrganisation() | ||
| return <Component {...props} organisation={organisation} /> | ||
| } | ||
| return WithCurrentOrganisation | ||
| } | ||
|
|
||
| export default withCurrentOrganisation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.