Skip to content

feat: add useError hook and use it in UiContainer#117

Open
tvanlaerhoven wants to merge 2 commits into
mainfrom
devin/1782478639-add-useerror-hook
Open

feat: add useError hook and use it in UiContainer#117
tvanlaerhoven wants to merge 2 commits into
mainfrom
devin/1782478639-add-useerror-hook

Conversation

@tvanlaerhoven

@tvanlaerhoven tvanlaerhoven commented Jun 26, 2026

Copy link
Copy Markdown
Member

Summary

Adds a useError hook that exposes the current PlayerError | undefined, following the same pattern as usePaused, useEnded, etc. Refactors UiContainer to use it instead of inline error state management.

Since UiContainer provides PlayerContext and useError consumes it, the error-conditional rendering is extracted into an ErrorGate child component rendered inside the provider:

// Before: inline state in UiContainer
const [error, setError] = useState<PlayerError | undefined>(undefined);
// + handleLoadStart, handleError, addEventListener/removeEventListener for ERROR & LOAD_START

// After: useError hook via ErrorGate child component
function ErrorGate({ children }: PropsWithChildren) {
  const error = useError();
  if (error !== undefined) {
    return <ErrorDisplay error={error} />;
  }
  return <>{children}</>;
}

Usage for custom components:

const error = useError();
if (error) {
  return <Text>{error.errorMessage}</Text>;
}

Companion PR in react-native-theoplayer (#857) ensures contentprotectionerror events from the web SDK are forwarded as PlayerEventType.ERROR.

Link to Devin session: https://dolby.devinenterprise.com/sessions/4f3f8d74c30b45dfb651a76c65693a83
Requested by: @tvanlaerhoven


Open in Devin Review

Add a reusable useError hook that exposes the current player error,
updating automatically on PlayerEventType.ERROR events and clearing
when a new source is loaded (SOURCE_CHANGE or LOAD_START).

This makes error state accessible to custom UI components, similar to
existing hooks like usePaused, useEnded, etc.

Co-Authored-By: tom.vanlaerhoven <tom.vanlaerhoven@dolby.com>
@tvanlaerhoven tvanlaerhoven self-assigned this Jun 26, 2026
@devin-ai-integration

Copy link
Copy Markdown

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@changeset-bot

changeset-bot Bot commented Jun 26, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: e1b02e3

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

devin-ai-integration[bot]

This comment was marked as resolved.

Replace inline error state management in UiContainer with the new
useError hook. Since UiContainer provides PlayerContext and useError
consumes it, the error-conditional rendering is extracted into an
ErrorGate child component rendered inside the provider.

Co-Authored-By: tom.vanlaerhoven <tom.vanlaerhoven@dolby.com>
@devin-ai-integration devin-ai-integration Bot changed the title feat: add useError hook for accessing player error state feat: add useError hook and use it in UiContainer Jun 26, 2026

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 new potential issue.

Open in Devin Review

Comment thread src/ui/hooks/useError.ts
Comment on lines +18 to +36
export const useError = (): PlayerError | undefined => {
const [error, setError] = useState<PlayerError | undefined>(undefined);
const { player } = useContext(PlayerContext);

useEffect(() => {
if (!player) return;
const onEvent = (event: { type: ErrorChangeEventType }) => {
if (event.type === PlayerEventType.ERROR) {
setError((event as ErrorEvent).error);
} else {
setError(undefined);
}
};

player.addEventListener(ERROR_CHANGE_EVENTS, onEvent);
return () => {
player.removeEventListener(ERROR_CHANGE_EVENTS, onEvent);
};
}, [player]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚩 Error state persists across player prop changes in useError hook

When the player prop changes, the useEffect in useError at src/ui/hooks/useError.ts:22-36 re-runs to register new listeners, but the error state (line 19) is never explicitly reset. If the previous player had an error, it will persist until a LOAD_START or SOURCE_CHANGE event fires on the new player. This was the same behavior in the old code (the error useState in UiContainer also persisted across player changes), so this is not a regression. However, for a public hook, consumers may find it surprising.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a regression — matches the previous UiContainer behavior exactly (as the review notes). In practice, the player instance doesn't change during a session's lifetime, so this edge case doesn't arise. If needed in the future, a setError(undefined) at the top of the effect would handle it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant