1- import { useCallback , useEffect , useMemo , useState } from 'react'
1+ import { useCallback , useEffect , useMemo , useRef , useState } from 'react'
22import Button from '@material-ui/core/Button'
33import { torrentsHost , torrentUploadHost } from 'utils/Hosts'
44import axios from 'axios'
@@ -11,11 +11,18 @@ import usePreviousState from 'utils/usePreviousState'
1111import { useQuery } from 'react-query'
1212import { getTorrents } from 'utils/Utils'
1313import parseTorrent from 'parse-torrent'
14+ import ptt from 'parse-torrent-title'
1415import { ButtonWrapper } from 'style/DialogStyles'
1516import { StyledDialog , StyledHeader } from 'style/CustomMaterialUiStyles'
1617import useOnStandaloneAppOutsideClick from 'utils/useOnStandaloneAppOutsideClick'
1718
18- import { checkImageURL , getMoviePosters , checkTorrentSource , parseTorrentTitle } from './helpers'
19+ import {
20+ checkImageURL ,
21+ getMoviePosters ,
22+ checkTorrentSource ,
23+ parseTorrentTitle ,
24+ shortenTitleForPosterSearch ,
25+ } from './helpers'
1926import { Content } from './style'
2027import RightSideComponent from './RightSideComponent'
2128import LeftSideComponent from './LeftSideComponent'
@@ -48,6 +55,7 @@ export default function AddDialog({
4855 const [ skipDebounce , setSkipDebounce ] = useState ( false )
4956 const [ isCustomTitleEnabled , setIsCustomTitleEnabled ] = useState ( false )
5057 const [ currentSourceHash , setCurrentSourceHash ] = useState ( )
58+ const editModePosterSearchedRef = useRef ( false )
5159
5260 const ref = useOnStandaloneAppOutsideClick ( handleClose )
5361
@@ -108,6 +116,20 @@ export default function AddDialog({
108116 setPosterUrl ( '' )
109117 }
110118
119+ // Edit mode: init original/parsed title from name so poster can be searched
120+ useEffect ( ( ) => {
121+ if ( ! originalHash || ( ! originalName && ! originalTitle ) ) return
122+ const source = originalName || originalTitle
123+ setOriginalTorrentTitle ( source )
124+ try {
125+ const parsed = ptt . parse ( source )
126+ setParsedTitle ( parsed ?. title || '' )
127+ } catch ( _ ) {
128+ setParsedTitle ( '' )
129+ }
130+ editModePosterSearchedRef . current = false
131+ } , [ originalHash , originalName , originalTitle ] )
132+
111133 useEffect ( ( ) => {
112134 if ( originalHash ) {
113135 checkImageURL ( posterUrl ) . then ( correctImage => {
@@ -126,8 +148,9 @@ export default function AddDialog({
126148 removePoster ( )
127149 return
128150 }
151+ const query = shortenTitleForPosterSearch ( String ( movieName ) . trim ( ) )
129152
130- getMoviePosters ( movieName , language ) . then ( urlList => {
153+ getMoviePosters ( query || movieName , language ) . then ( urlList => {
131154 if ( urlList ) {
132155 setPosterList ( urlList )
133156 if ( ! shouldRefreshMainPoster && isUserInteractedWithPoster ) return
@@ -167,6 +190,22 @@ export default function AddDialog({
167190 updateTitleFromSource ( )
168191 } , [ prevTorrentSourceState , selectedFile , torrentSource , updateTitleFromSource ] )
169192
193+ // Edit mode: auto-search poster once when we have title and no poster
194+ useEffect ( ( ) => {
195+ if (
196+ ! originalHash ||
197+ editModePosterSearchedRef . current ||
198+ originalPoster ||
199+ ! ( parsedTitle || originalTitle || title )
200+ ) {
201+ return
202+ }
203+ const searchTitle = parsedTitle || title || originalTitle
204+ if ( ! shortenTitleForPosterSearch ( searchTitle ) ) return
205+ editModePosterSearchedRef . current = true
206+ posterSearch ( searchTitle , posterSearchLanguage , { shouldRefreshMainPoster : true } )
207+ } , [ originalHash , originalPoster , parsedTitle , originalTitle , title , posterSearchLanguage , posterSearch ] )
208+
170209 const prevTitleState = usePreviousState ( title )
171210
172211 useEffect ( ( ) => {
0 commit comments