Search fallback: show relevant videos when user not found
This commit is contained in:
parent
467056fa9c
commit
37394f27e7
1 changed files with 35 additions and 13 deletions
|
|
@ -457,6 +457,7 @@ export const Feed: React.FC = () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Direct username search - bypasses state update delay
|
// Direct username search - bypasses state update delay
|
||||||
|
// Falls back to keyword search if user not found
|
||||||
const searchByUsername = async (username: string) => {
|
const searchByUsername = async (username: string) => {
|
||||||
setSearchInput(`@${username}`);
|
setSearchInput(`@${username}`);
|
||||||
setActiveTab('search');
|
setActiveTab('search');
|
||||||
|
|
@ -470,26 +471,47 @@ export const Feed: React.FC = () => {
|
||||||
if (userVideos.length > 0) {
|
if (userVideos.length > 0) {
|
||||||
setSearchResults(userVideos);
|
setSearchResults(userVideos);
|
||||||
} else {
|
} else {
|
||||||
setSearchResults([{
|
// No videos from user profile, try keyword search
|
||||||
id: `no-videos-${username}`,
|
console.log(`No videos from @${username}, trying keyword search...`);
|
||||||
url: '',
|
await fallbackToKeywordSearch(username);
|
||||||
author: username,
|
|
||||||
description: `No videos found for @${username}`
|
|
||||||
}]);
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error fetching user videos:', err);
|
console.error('Error fetching user videos, trying keyword search:', err);
|
||||||
setSearchResults([{
|
// User not found or error - fallback to keyword search
|
||||||
id: `error-${username}`,
|
await fallbackToKeywordSearch(username);
|
||||||
url: '',
|
|
||||||
author: username,
|
|
||||||
description: `Could not fetch videos`
|
|
||||||
}]);
|
|
||||||
} finally {
|
} finally {
|
||||||
setIsSearching(false);
|
setIsSearching(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Fallback search when user profile fails
|
||||||
|
const fallbackToKeywordSearch = async (keyword: string) => {
|
||||||
|
try {
|
||||||
|
const res = await axios.get(`${API_BASE_URL}/user/search?query=${encodeURIComponent(keyword)}&limit=12`);
|
||||||
|
const searchVideos = res.data.videos as Video[];
|
||||||
|
|
||||||
|
if (searchVideos.length > 0) {
|
||||||
|
setSearchResults(searchVideos);
|
||||||
|
} else {
|
||||||
|
// Still no results - show friendly message
|
||||||
|
setSearchResults([{
|
||||||
|
id: `no-results-${keyword}`,
|
||||||
|
url: '',
|
||||||
|
author: 'search',
|
||||||
|
description: `No videos found for "${keyword}". Try a different search term.`
|
||||||
|
}]);
|
||||||
|
}
|
||||||
|
} catch (searchErr) {
|
||||||
|
console.error('Keyword search also failed:', searchErr);
|
||||||
|
setSearchResults([{
|
||||||
|
id: `search-error`,
|
||||||
|
url: '',
|
||||||
|
author: 'search',
|
||||||
|
description: `Search is temporarily unavailable. Please try again later.`
|
||||||
|
}]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Direct keyword search - bypasses state update delay
|
// Direct keyword search - bypasses state update delay
|
||||||
const searchByKeyword = async (keyword: string) => {
|
const searchByKeyword = async (keyword: string) => {
|
||||||
setSearchInput(keyword);
|
setSearchInput(keyword);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue