-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RHOAIENG-9232 Create useTrackUser (#3024)
- Loading branch information
Showing
4 changed files
with
82 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,50 @@ | ||
import React from 'react'; | ||
import { useUser } from '~/redux/selectors'; | ||
import { useAccessReview } from '~/api'; | ||
import { AccessReviewResourceAttributes } from '~/k8sTypes'; | ||
import { IdentifyEventProperties } from '~/concepts/analyticsTracking/trackingProperties'; | ||
|
||
export const useTrackUser = (username?: string): [IdentifyEventProperties, boolean] => { | ||
const { isAdmin } = useUser(); | ||
const [anonymousId, setAnonymousId] = React.useState<string | undefined>(undefined); | ||
|
||
const [loaded, setLoaded] = React.useState(false); | ||
const createReviewResource: AccessReviewResourceAttributes = { | ||
group: 'project.openshift.io', | ||
resource: 'projectrequests', | ||
verb: 'create', | ||
}; | ||
const [allowCreate, acLoaded] = useAccessReview(createReviewResource); | ||
|
||
React.useEffect(() => { | ||
const computeAnonymousUserId = async () => { | ||
const anonymousIDBuffer = await crypto.subtle.digest( | ||
'SHA-1', | ||
new TextEncoder().encode(username), | ||
); | ||
const anonymousIDArray = Array.from(new Uint8Array(anonymousIDBuffer)); | ||
const aId = anonymousIDArray.map((b) => b.toString(16).padStart(2, '0')).join(''); | ||
return aId; | ||
}; | ||
|
||
if (!anonymousId) { | ||
computeAnonymousUserId().then((val) => { | ||
setAnonymousId(val); | ||
}); | ||
} | ||
if (acLoaded && anonymousId) { | ||
setLoaded(true); | ||
} | ||
}, [username, anonymousId, acLoaded]); | ||
|
||
const props: IdentifyEventProperties = React.useMemo( | ||
() => ({ | ||
isAdmin, | ||
canCreateProjects: allowCreate, | ||
anonymousID: anonymousId, | ||
}), | ||
[isAdmin, allowCreate, anonymousId], | ||
); | ||
|
||
return [props, loaded]; | ||
}; |