-
Notifications
You must be signed in to change notification settings - Fork 21
Pm 3255 #1388
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
Pm 3255 #1388
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,11 @@ import { FC, PropsWithChildren, useContext, useMemo } from 'react' | |
| import { useParams } from 'react-router-dom' | ||
|
|
||
| import { convertBackendSubmissionToSubmissionInfo } from '../models' | ||
| import type { ChallengeDetailContextModel, SubmissionInfo } from '../models' | ||
| import type { | ||
| ChallengeDetailContextModel, | ||
| ReviewAppContextModel, | ||
| SubmissionInfo, | ||
| } from '../models' | ||
| import { | ||
| useFetchChallengeInfo, | ||
| useFetchChallengeInfoProps, | ||
|
|
@@ -16,11 +20,13 @@ import { | |
| } from '../hooks' | ||
|
|
||
| import { ChallengeDetailContext } from './ChallengeDetailContext' | ||
| import { ReviewAppContext } from './ReviewAppContext' | ||
|
|
||
| export const ChallengeDetailContextProvider: FC<PropsWithChildren> = props => { | ||
| const { challengeId = '' }: { challengeId?: string } = useParams<{ | ||
| challengeId: string | ||
| }>() | ||
| const { loginUserInfo }: ReviewAppContextModel = useContext(ReviewAppContext) | ||
| // fetch challenge info | ||
| const { | ||
| challengeInfo, | ||
|
|
@@ -37,10 +43,21 @@ export const ChallengeDetailContextProvider: FC<PropsWithChildren> = props => { | |
| isLoading: isLoadingChallengeResources, | ||
| resourceMemberIdMapping, | ||
| }: useFetchChallengeResourcesProps = useFetchChallengeResources(challengeId) | ||
| const submissionViewer = useMemo( | ||
| () => ({ | ||
| roles: myRoles, | ||
| tokenRoles: loginUserInfo?.roles, | ||
| userId: loginUserInfo?.userId, | ||
| }), | ||
| [loginUserInfo?.roles, loginUserInfo?.userId, myRoles], | ||
| ) | ||
| const { | ||
| challengeSubmissions, | ||
| isLoading: isLoadingChallengeSubmissions, | ||
| }: useFetchChallengeSubmissionsProps = useFetchChallengeSubmissions(challengeId) | ||
| }: useFetchChallengeSubmissionsProps = useFetchChallengeSubmissions( | ||
| challengeId, | ||
| submissionViewer, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| ) | ||
|
|
||
| const submissionInfos = useMemo<SubmissionInfo[]>( | ||
| () => challengeSubmissions.map(convertBackendSubmissionToSubmissionInfo), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import { | |
| import useSWR, { SWRResponse } from 'swr' | ||
|
|
||
| import { handleError } from '~/libs/shared' | ||
| import { UserRole } from '~/libs/core' | ||
|
|
||
| import { BackendSubmission, BackendSubmissionStatus } from '../models' | ||
| import { fetchSubmissions } from '../services' | ||
|
|
@@ -22,13 +23,20 @@ interface ChallengeSubmissionsMemoResult { | |
| filteredSubmissions: BackendSubmission[] | ||
| } | ||
|
|
||
| export interface ChallengeSubmissionsViewer { | ||
| roles?: Array<string | undefined | null> | ||
| tokenRoles?: Array<string | undefined | null> | ||
| userId?: string | number | null | ||
| } | ||
|
|
||
| /** | ||
| * Fetch challenge submissions | ||
| * @param challengeId challenge id | ||
| * @returns challenge submissions | ||
| */ | ||
| export function useFetchChallengeSubmissions( | ||
| challengeId?: string, | ||
| viewer?: ChallengeSubmissionsViewer, | ||
| ): useFetchChallengeSubmissionsProps { | ||
| // Use swr hooks for submissions fetching | ||
| const { | ||
|
|
@@ -46,6 +54,92 @@ export function useFetchChallengeSubmissions( | |
| isPaused: () => !challengeId, | ||
| }) | ||
|
|
||
| const normalizedRoles = useMemo<string[]>( | ||
| () => (viewer?.roles ?? []) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| .map(role => (role ? `${role}`.toLowerCase() | ||
| .trim() : '')) | ||
| .filter(Boolean), | ||
| [viewer?.roles], | ||
| ) | ||
| const normalizedTokenRoles = useMemo<string[]>( | ||
| () => (viewer?.tokenRoles ?? []) | ||
| .map(role => (typeof role === 'string' ? role.toLowerCase() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| .trim() : '')) | ||
| .filter(Boolean), | ||
| [viewer?.tokenRoles], | ||
| ) | ||
| const hasSubmitterRole = useMemo<boolean>( | ||
| () => normalizedRoles.some(role => role.includes('submitter')), | ||
| [normalizedRoles], | ||
| ) | ||
| const hasCopilotRole = useMemo<boolean>( | ||
| () => normalizedRoles.some(role => role.includes('copilot')), | ||
| [normalizedRoles], | ||
| ) | ||
| const hasReviewerRole = useMemo<boolean>( | ||
| () => normalizedRoles.some(role => role.includes('reviewer')), | ||
| [normalizedRoles], | ||
| ) | ||
| const hasManagerRole = useMemo<boolean>( | ||
| () => normalizedRoles.some(role => role.includes('manager')), | ||
| [normalizedRoles], | ||
| ) | ||
| const hasScreenerRole = useMemo<boolean>( | ||
| () => normalizedRoles.some(role => role.includes('screener')), | ||
| [normalizedRoles], | ||
| ) | ||
| const hasApproverRole = useMemo<boolean>( | ||
| () => normalizedRoles.some(role => role.includes('approver')), | ||
| [normalizedRoles], | ||
| ) | ||
| const isProjectManager = useMemo<boolean>( | ||
| () => normalizedTokenRoles.some( | ||
| role => role === UserRole.projectManager.toLowerCase(), | ||
| ) | ||
| || normalizedRoles.some(role => role.includes('project manager')), | ||
| [normalizedRoles, normalizedTokenRoles], | ||
| ) | ||
| const isAdmin = useMemo<boolean>( | ||
| () => normalizedTokenRoles.some( | ||
| role => role === UserRole.administrator.toLowerCase(), | ||
| ) | ||
| || normalizedRoles.some(role => role.includes('admin')), | ||
| [normalizedRoles, normalizedTokenRoles], | ||
| ) | ||
| const canViewAllSubmissions = useMemo<boolean>( | ||
| () => (viewer ? ( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| isAdmin | ||
| || hasCopilotRole | ||
| || hasReviewerRole | ||
| || hasManagerRole | ||
| || hasScreenerRole | ||
| || hasApproverRole | ||
| || isProjectManager | ||
| ) : true), | ||
| [ | ||
| viewer, | ||
| isAdmin, | ||
| hasCopilotRole, | ||
| hasReviewerRole, | ||
| hasManagerRole, | ||
| hasScreenerRole, | ||
| hasApproverRole, | ||
| isProjectManager, | ||
| ], | ||
| ) | ||
| const viewerMemberId = useMemo<string | undefined>( | ||
| () => { | ||
| const raw = viewer?.userId | ||
| if (raw === undefined || raw === null) { | ||
| return undefined | ||
| } | ||
|
|
||
| const normalized = `${raw}`.trim() | ||
| return normalized.length ? normalized : undefined | ||
| }, | ||
| [viewer?.userId], | ||
| ) | ||
|
|
||
| // Show backend error when fetching data fail | ||
| useEffect(() => { | ||
| if (error) { | ||
|
|
@@ -69,6 +163,10 @@ export function useFetchChallengeSubmissions( | |
| const normalizedDeletedIds = new Set<string>() | ||
| const normalizedDeletedLegacyIds = new Set<string>() | ||
| const activeSubmissions: BackendSubmission[] = [] | ||
| const shouldRestrictToCurrentMember = Boolean( | ||
| hasSubmitterRole | ||
| && !canViewAllSubmissions, | ||
| ) | ||
|
|
||
| const normalizeStatus = (status: unknown): string => { | ||
| if (typeof status === 'string') { | ||
|
|
@@ -105,12 +203,23 @@ export function useFetchChallengeSubmissions( | |
| activeSubmissions.push(submission) | ||
| }) | ||
|
|
||
| const visibleSubmissions = shouldRestrictToCurrentMember | ||
| ? activeSubmissions.filter(submission => (viewerMemberId | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| ? `${submission?.memberId ?? ''}` === viewerMemberId | ||
| : false)) | ||
| : activeSubmissions | ||
|
|
||
| return { | ||
| deletedLegacySubmissionIds: normalizedDeletedLegacyIds, | ||
| deletedSubmissionIds: normalizedDeletedIds, | ||
| filteredSubmissions: activeSubmissions, | ||
| filteredSubmissions: visibleSubmissions, | ||
| } | ||
| }, [challengeSubmissions]) | ||
| }, [ | ||
| challengeSubmissions, | ||
| canViewAllSubmissions, | ||
| hasSubmitterRole, | ||
| viewerMemberId, | ||
| ]) | ||
|
|
||
| return { | ||
| challengeSubmissions: filteredSubmissions, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -436,7 +436,16 @@ export function useFetchScreeningReview(): useFetchScreeningReviewProps { | |
| reviewers: challengeReviewers, | ||
| resources, | ||
| myResources, | ||
| myRoles, | ||
| }: ChallengeDetailContextModel = useContext(ChallengeDetailContext) | ||
| const submissionViewer = useMemo( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [💡 |
||
| () => ({ | ||
| roles: myRoles, | ||
| tokenRoles: loginUserInfo?.roles, | ||
| userId: loginUserInfo?.userId, | ||
| }), | ||
| [loginUserInfo?.roles, loginUserInfo?.userId, myRoles], | ||
| ) | ||
|
|
||
| const challengeLegacy = (challengeInfo as unknown as { | ||
| legacy?: { | ||
|
|
@@ -451,7 +460,10 @@ export function useFetchScreeningReview(): useFetchScreeningReviewProps { | |
| deletedLegacySubmissionIds, | ||
| deletedSubmissionIds, | ||
| isLoading, | ||
| }: useFetchChallengeSubmissionsProps = useFetchChallengeSubmissions(challengeId) | ||
| }: useFetchChallengeSubmissionsProps = useFetchChallengeSubmissions( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| challengeId, | ||
| submissionViewer, | ||
| ) | ||
|
|
||
| const visibleChallengeSubmissions = useMemo<BackendSubmission[]>( | ||
| () => challengeSubmissions, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[⚠️
correctness]The destructuring of
loginUserInfofromReviewAppContextassumes thatuseContext(ReviewAppContext)will always return a valid object. Consider adding a default value or null check to prevent potential runtime errors if the context is not properly initialized.