Skip to content
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

fix: feature-specific segments link #4299

Merged
merged 9 commits into from
Aug 28, 2024
8 changes: 6 additions & 2 deletions frontend/common/providers/Permission.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ export const useHasPermission = ({
level,
permission,
}: Omit<PermissionType, 'children'>) => {
const { data, isLoading } = useGetPermissionQuery(
const { data, isLoading, isSuccess } = useGetPermissionQuery(
{ id: `${id}`, level },
{ skip: !id || !level },
)
const hasPermission = !!data?.[permission] || !!data?.ADMIN
return { isLoading, permission: !!hasPermission || !!AccountStore.isAdmin() }
return {
isLoading,
isSuccess,
permission: !!hasPermission || !!AccountStore.isAdmin(),
}
}

const Permission: FC<PermissionType> = ({
Expand Down
28 changes: 15 additions & 13 deletions frontend/web/components/pages/SegmentsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,12 @@ const SegmentsPage: FC<SegmentsPageType> = (props) => {
)
}

const { permission: manageSegmentsPermission } = useHasPermission({
id: projectId,
level: 'project',
permission: 'MANAGE_SEGMENTS',
})
const { isSuccess: permissionLoaded, permission: manageSegmentsPermission } =
useHasPermission({
id: projectId,
level: 'project',
permission: 'MANAGE_SEGMENTS',
})

const editSegment = (id: number, readOnly?: boolean) => {
API.trackEvent(Constants.events.VIEW_SEGMENT)
Expand Down Expand Up @@ -161,6 +162,15 @@ const SegmentsPage: FC<SegmentsPageType> = (props) => {
}

const segments = data?.results
useEffect(() => {
if (!preselect.current || !permissionLoaded) {
return
}
if (preselect.current) {
editSegment(preselect.current, !manageSegmentsPermission)
}
}, [segments, permissionLoaded, manageSegmentsPermission])

return (
<div
data-test='segments-page'
Expand Down Expand Up @@ -251,14 +261,6 @@ const SegmentsPage: FC<SegmentsPageType> = (props) => {
{ description, feature, id, name }: Segment,
i: number,
) => {
if (preselect.current === `${id}`) {
editSegment(
preselect.current,
!manageSegmentsPermission,
)
preselect.current = null
}

// TODO: remove this check
// I'm leaving this here for now so that we can deploy the FE and
// API independently, but we should remove this once PR #3430 is
Expand Down
Loading