-
Notifications
You must be signed in to change notification settings - Fork 63
Feat/aotechtask 7695 #2005
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
base: master
Are you sure you want to change the base?
Feat/aotechtask 7695 #2005
Changes from all commits
a8d668a
f117225
7469566
4b42bad
18e3dc2
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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| '@alfalab/core-components': patch | ||
| '@alfalab/core-components-gallery': patch | ||
| --- | ||
|
|
||
| ##### Gallery | ||
|
|
||
| - Заменен статический импорт hls.js на динамический. | ||
| - Добавлена retry логика для надежной загрузки. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,9 +5,10 @@ import React, { | |
| useContext, | ||
| useEffect, | ||
| useRef, | ||
| useState, | ||
| } from 'react'; | ||
| import cn from 'classnames'; | ||
| import Hls from 'hls.js'; | ||
| import type Hls from 'hls.js'; | ||
|
|
||
| import { Circle } from '@alfalab/core-components-icon-view/circle'; | ||
| import PlayCompactMIcon from '@alfalab/icons-glyph/PlayCompactMIcon'; | ||
|
|
@@ -28,6 +29,7 @@ type Props = { | |
| export const Video = ({ url, index, className, isActive }: Props) => { | ||
| const playerRef = useRef<HTMLVideoElement>(null); | ||
| const timer = useRef<ReturnType<typeof setTimeout>>(); | ||
| const [hlsSupported, setHlsSupported] = useState<boolean>(true); | ||
|
|
||
| const { | ||
| setImageMeta, | ||
|
|
@@ -49,32 +51,86 @@ export const Video = ({ url, index, className, isActive }: Props) => { | |
| /* eslint-disable-next-line react-hooks/exhaustive-deps */ | ||
| }, [index]); | ||
|
|
||
| const loadHlsLibrary = useCallback( | ||
| async (attempt = 1, maxAttempts = 3): Promise<typeof Hls | null> => { | ||
| try { | ||
| const { default: HlsLib } = await import( | ||
| /* webpackChunkName: "hls-js-gallery" */ 'hls.js' | ||
|
Contributor
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. может лучше взять
Author
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.
Contributor
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. 2 галереи ето конечно плохо да. но цена субтитров конечно запредельная :( |
||
| ); | ||
|
|
||
| return HlsLib; | ||
| } catch { | ||
| if (attempt < maxAttempts) { | ||
| /* Экспоненциальная задержка ретрая: 300ms, 600ms, 1200ms */ | ||
| await new Promise<void>((resolve) => { | ||
| setTimeout( | ||
| () => { | ||
| resolve(); | ||
| }, | ||
| 300 * 2 ** (attempt - 1), | ||
| ); | ||
| }); | ||
|
|
||
| return loadHlsLibrary(attempt + 1, maxAttempts); | ||
| } | ||
|
|
||
| setHlsSupported(false); | ||
| setImageMeta({ player: { current: null }, broken: true }, index); | ||
|
|
||
| return null; | ||
| } | ||
| }, | ||
| [setImageMeta, index], | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| const hls = new Hls(); | ||
|
|
||
| if (Hls.isSupported()) { | ||
| hls.on(Hls.Events.ERROR, (_, data) => { | ||
| if (data.fatal) { | ||
| switch (data.type) { | ||
| case Hls.ErrorTypes.MEDIA_ERROR: | ||
| hls.recoverMediaError(); | ||
| break; | ||
| case Hls.ErrorTypes.NETWORK_ERROR: | ||
| setImageMeta({ player: { current: null }, broken: true }, index); | ||
| break; | ||
| default: | ||
| hls.destroy(); | ||
| break; | ||
| } | ||
| let hls: Hls | null = null; | ||
|
|
||
| const initHls = async () => { | ||
| try { | ||
| const HlsLib = await loadHlsLibrary(); | ||
|
|
||
| if (!HlsLib || !playerRef.current) { | ||
| return; | ||
| } | ||
| }); | ||
|
|
||
| hls.loadSource(url); | ||
| if (playerRef.current) { | ||
| hls.attachMedia(playerRef.current); | ||
| hls.subtitleDisplay = false; | ||
| if (!HlsLib.isSupported()) { | ||
| setHlsSupported(false); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| hls = new HlsLib(); | ||
|
|
||
| hls.on(HlsLib.Events.ERROR, (_, data) => { | ||
| if (data.fatal && hls) { | ||
| switch (data.type) { | ||
| case HlsLib.ErrorTypes.MEDIA_ERROR: | ||
| hls.recoverMediaError(); | ||
| break; | ||
| case HlsLib.ErrorTypes.NETWORK_ERROR: | ||
| setImageMeta({ player: { current: null }, broken: true }, index); | ||
| break; | ||
| default: | ||
| hls.destroy(); | ||
| break; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| hls.loadSource(url); | ||
|
|
||
| if (playerRef.current) { | ||
| hls.attachMedia(playerRef.current); | ||
| hls.subtitleDisplay = false; | ||
| } | ||
| } catch { | ||
| setHlsSupported(false); | ||
| setImageMeta({ player: { current: null }, broken: true }, index); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| initHls(); | ||
|
|
||
| return () => { | ||
| if (hls) { | ||
|
|
@@ -183,7 +239,7 @@ export const Video = ({ url, index, className, isActive }: Props) => { | |
| playsInline={true} | ||
| muted={mutedVideo} | ||
| loop={true} | ||
| src={Hls.isSupported() ? undefined : url} | ||
| src={hlsSupported ? undefined : url} | ||
| className={cn(styles.video, { [styles.mobile]: view === 'mobile' }, className)} | ||
| > | ||
| <track kind='captions' /> | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.