2024-09-13 18:52:33 -05:00
|
|
|
import { useState, useEffect } from 'react';
|
|
|
|
import { useContentIdsQuery } from '@/hooks/apiQueries/useContentIdsQuery';
|
|
|
|
import { useNDKContext } from '@/context/NDKContext';
|
|
|
|
import { parseEvent, parseCourseEvent } from '@/utils/nostr';
|
2025-04-02 17:47:30 -05:00
|
|
|
import appConfig from '@/config/appConfig';
|
2024-09-13 18:52:33 -05:00
|
|
|
|
|
|
|
export const useContentSearch = () => {
|
2025-04-02 17:47:30 -05:00
|
|
|
const [allContent, setAllContent] = useState([]);
|
|
|
|
const [searchResults, setSearchResults] = useState([]);
|
|
|
|
const { contentIds } = useContentIdsQuery();
|
|
|
|
const { ndk } = useNDKContext();
|
2024-09-13 18:52:33 -05:00
|
|
|
|
2025-04-02 17:47:30 -05:00
|
|
|
const fetchAllEvents = async ids => {
|
|
|
|
try {
|
|
|
|
await ndk.connect();
|
|
|
|
const filter = {
|
|
|
|
authors: appConfig.authorPubkeys,
|
|
|
|
kinds: [30004, 30023, 30402],
|
|
|
|
'#d': ids,
|
|
|
|
};
|
|
|
|
const events = await ndk.fetchEvents(filter);
|
2024-09-13 18:52:33 -05:00
|
|
|
|
2025-04-27 12:10:08 -05:00
|
|
|
const parsedEvents = [];
|
2025-04-02 17:47:30 -05:00
|
|
|
events.forEach(event => {
|
|
|
|
let parsed;
|
|
|
|
if (event.kind === 30004) {
|
|
|
|
parsed = parseCourseEvent(event);
|
|
|
|
} else {
|
|
|
|
parsed = parseEvent(event);
|
2024-09-13 18:52:33 -05:00
|
|
|
}
|
2025-04-27 12:10:08 -05:00
|
|
|
parsedEvents.push(parsed);
|
2025-04-02 17:47:30 -05:00
|
|
|
});
|
|
|
|
setAllContent(parsedEvents);
|
|
|
|
} catch (error) {
|
|
|
|
console.error('error', error);
|
2024-09-13 18:52:33 -05:00
|
|
|
}
|
2025-04-02 17:47:30 -05:00
|
|
|
};
|
2024-09-13 18:52:33 -05:00
|
|
|
|
2025-04-02 17:47:30 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (contentIds) {
|
|
|
|
fetchAllEvents(contentIds);
|
|
|
|
}
|
|
|
|
}, [contentIds]);
|
2024-09-13 18:52:33 -05:00
|
|
|
|
2025-04-02 17:47:30 -05:00
|
|
|
const searchContent = term => {
|
|
|
|
if (term.length > 2) {
|
2025-04-27 12:10:08 -05:00
|
|
|
const searchTerm = term.toLowerCase();
|
|
|
|
const filtered = allContent.filter(content => {
|
2025-04-27 12:47:24 -05:00
|
|
|
// Prepare fields to search in
|
2025-04-02 17:47:30 -05:00
|
|
|
const searchableTitle = (content?.title || content?.name || '').toLowerCase();
|
2025-04-27 12:47:24 -05:00
|
|
|
const searchableDescription = (content?.summary || content?.description || '').toLowerCase();
|
2025-04-27 12:10:08 -05:00
|
|
|
|
2025-04-27 12:47:24 -05:00
|
|
|
// Find matches in title
|
|
|
|
const titleMatch = searchableTitle.includes(searchTerm);
|
2025-04-27 12:10:08 -05:00
|
|
|
|
2025-04-27 12:47:24 -05:00
|
|
|
// Find matches in description
|
|
|
|
const descriptionMatch = searchableDescription.includes(searchTerm);
|
|
|
|
|
|
|
|
// Store match information (only for title and description)
|
|
|
|
if (titleMatch || descriptionMatch) {
|
|
|
|
content._matches = {
|
|
|
|
title: titleMatch ? {
|
|
|
|
text: content?.title || content?.name || '',
|
|
|
|
term: searchTerm
|
|
|
|
} : null,
|
|
|
|
description: descriptionMatch ? {
|
|
|
|
text: content?.summary || content?.description || '',
|
|
|
|
term: searchTerm
|
|
|
|
} : null
|
|
|
|
};
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2025-04-02 17:47:30 -05:00
|
|
|
});
|
2025-04-27 12:10:08 -05:00
|
|
|
|
2025-04-02 17:47:30 -05:00
|
|
|
setSearchResults(filtered);
|
|
|
|
} else {
|
|
|
|
setSearchResults([]);
|
|
|
|
}
|
|
|
|
};
|
2024-09-13 18:52:33 -05:00
|
|
|
|
2025-04-02 17:47:30 -05:00
|
|
|
return { searchContent, searchResults };
|
|
|
|
};
|