-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Attach task interaction id to table instance (#2778)
- Loading branch information
1 parent
1a3ef83
commit 296fff3
Showing
4 changed files
with
62 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,39 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { useEffect } from 'react'; | ||
import { useEffect, useRef } from 'react'; | ||
|
||
import { ComponentMetrics } from '../../analytics'; | ||
import { useRandomId } from '../use-unique-id'; | ||
|
||
function useTaskInteractionAttribute(elementRef: React.RefObject<HTMLElement>, value: string) { | ||
const attributeName = 'data-analytics-task-interaction-id'; | ||
|
||
const attributeValueRef = useRef<string | undefined>(); | ||
|
||
useEffect(() => { | ||
// With this effect, we apply the attribute only on the client, to avoid hydration errors. | ||
attributeValueRef.current = value; | ||
elementRef.current?.setAttribute(attributeName, value); | ||
}, [value, elementRef]); | ||
|
||
return { | ||
[attributeName]: attributeValueRef.current, | ||
}; | ||
} | ||
|
||
export function useComponentAnalytics( | ||
componentName: string, | ||
elementRef: React.RefObject<HTMLElement>, | ||
getDetails: () => Record<string, string | boolean | number | undefined> | ||
) { | ||
useEffect(() => { | ||
ComponentMetrics.componentMounted({ componentName, details: getDetails() }); | ||
const taskInteractionId = useRandomId(); | ||
const attributes = useTaskInteractionAttribute(elementRef, taskInteractionId); | ||
|
||
useEffect(() => { | ||
ComponentMetrics.componentMounted({ taskInteractionId, componentName, details: getDetails() }); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
}, [taskInteractionId]); | ||
|
||
return { taskInteractionId, attributes }; | ||
} |
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