Download src/hooks/useLiveView.ts from felix-fx/fx-1: direct link, hf CLI and curl.
- Browser
- Download file 9.92 kB
-
https://huggingface.co/felix-fx/fx-1/resolve/main/src/hooks/useLiveView.ts
- Command line
-
hf download hf://felix-fx/fx-1/src/hooks/useLiveView.ts
-
curl -L -o useLiveView.ts https://huggingface.co/felix-fx/fx-1/resolve/main/src/hooks/useLiveView.ts
9.92 kB
| import { useState, useEffect, useCallback, useRef } from 'react'; | |
| import { useLocation } from 'react-router-dom'; | |
| import { useAuth } from '@/contexts/AuthContext'; | |
| import { useVerification } from '@/hooks/useVerification'; | |
| export interface PageContext { | |
| currentPage: string; | |
| pageTitle: string; | |
| pageDescription: string; | |
| isAuthenticated: boolean; | |
| userInfo?: { | |
| username: string; | |
| name: string; | |
| email: string; | |
| avatar?: string; | |
| }; | |
| verificationInfo?: { | |
| type: 'owner' | 'developer' | 'addon_developer' | 'user'; | |
| color: 'green' | 'blue' | 'purple' | 'gray'; | |
| verifiedAt?: string; | |
| verifiedBy?: string; | |
| }; | |
| pageElements?: { | |
| title?: string; | |
| description?: string; | |
| hasSearchBar?: boolean; | |
| hasFilters?: boolean; | |
| currentCategory?: string; | |
| itemsCount?: number; | |
| }; | |
| timestamp: number; | |
| } | |
| const PAGE_CONTEXT_MAP: Record<string, { title: string; description: string }> = { | |
| '/': { | |
| title: 'الصفحة الرئيسية', | |
| description: 'صفحة البداية لمنصة Minecraft FX - استكشف أفضل الإضافات والمودات' | |
| }, | |
| '/browse': { | |
| title: 'تصفح الإضافات', | |
| description: 'استكشف آلاف الإضافات والمودات المتاحة على المنصة' | |
| }, | |
| '/bedrock': { | |
| title: 'إضافات Bedrock', | |
| description: 'إضافات خاصة بـ Minecraft Bedrock للجوال والأجهزة اللوحية و Xbox' | |
| }, | |
| '/new': { | |
| title: 'الإضافات الجديدة', | |
| description: 'أحدث الإضافات التي تم رفعها على المنصة' | |
| }, | |
| '/popular': { | |
| title: 'الإضافات الشائعة', | |
| description: 'الإضافات الأكثر تحميلاً والأعلى تقييماً' | |
| }, | |
| '/categories': { | |
| title: 'التصنيفات', | |
| description: 'تصفح الإضافات حسب النوع والفئة' | |
| }, | |
| '/upload': { | |
| title: 'رفع إضافة', | |
| description: 'شارك إضافتك مع مجتمع ماينكرافت العربي' | |
| }, | |
| '/profile': { | |
| title: 'الملف الشخصي', | |
| description: 'إدارة بيانات حسابك والإضافات الخاصة بك' | |
| }, | |
| '/developer': { | |
| title: 'لوحة تحكم المطورين', | |
| description: 'إدارة المطورين والصلاحيات والإحصائيات' | |
| }, | |
| '/dashboard': { | |
| title: 'لوحة التحكم', | |
| description: 'إدارة حسابك والإضافات والإحصائيات' | |
| } | |
| }; | |
| const VERIFICATION_TYPE_MAP: Record<string, { type: 'owner' | 'developer' | 'addon_developer' | 'user'; color: 'green' | 'blue' | 'purple' | 'gray' }> = { | |
| 'owner': { type: 'owner', color: 'green' }, | |
| 'developer': { type: 'developer', color: 'blue' }, | |
| 'addon_developer': { type: 'addon_developer', color: 'purple' }, | |
| 'user': { type: 'user', color: 'gray' } | |
| }; | |
| export const useLiveView = () => { | |
| const location = useLocation(); | |
| const { user } = useAuth(); | |
| const { verification } = useVerification(); | |
| const [pageContext, setPageContext] = useState<PageContext | null>(null); | |
| const [isMonitoring, setIsMonitoring] = useState(false); | |
| const observerRef = useRef<MutationObserver | null>(null); | |
| // دالة لاستخراج معلومات الصفحة من DOM | |
| const extractPageElements = useCallback(() => { | |
| const elements = { | |
| title: document.querySelector('h1')?.textContent || undefined, | |
| description: document.querySelector('meta[name="description"]')?.getAttribute('content') || undefined, | |
| hasSearchBar: !!document.querySelector('input[type="search"], input[placeholder*="بحث"], input[placeholder*="search"]'), | |
| hasFilters: !!document.querySelector('[class*="filter"], [class*="Filter"]'), | |
| currentCategory: (document.querySelector('[class*="active"]') as HTMLElement)?.textContent || undefined, | |
| itemsCount: document.querySelectorAll('[class*="card"], [class*="item"], [class*="mod"]').length || undefined | |
| }; | |
| return elements; | |
| }, []); | |
| // دالة لبناء سياق الصفحة الكامل | |
| const buildPageContext = useCallback((): PageContext => { | |
| const pathname = location.pathname; | |
| const contextInfo = PAGE_CONTEXT_MAP[pathname] || { | |
| title: 'صفحة مخصصة', | |
| description: 'صفحة من صفحات منصة Minecraft FX' | |
| }; | |
| let verificationType: 'owner' | 'developer' | 'addon_developer' | 'user' = 'user'; | |
| let verificationColor: 'green' | 'blue' | 'purple' | 'gray' = 'gray'; | |
| // تحديد نوع التوثيق بناءً على بيانات المستخدم | |
| if (user) { | |
| const userVerification = (user as any)?.verification_type || 'user'; | |
| const verificationData = VERIFICATION_TYPE_MAP[userVerification] || { type: 'user', color: 'gray' }; | |
| verificationType = verificationData.type; | |
| verificationColor = verificationData.color; | |
| } | |
| const context: PageContext = { | |
| currentPage: pathname, | |
| pageTitle: contextInfo.title, | |
| pageDescription: contextInfo.description, | |
| isAuthenticated: !!user, | |
| userInfo: user ? { | |
| username: (user as any)?.username || 'مستخدم', | |
| name: (user as any)?.profile?.display_name || (user as any)?.username || 'مستخدم', | |
| email: user.email || '', | |
| avatar: (user as any)?.profile?.avatar_url | |
| } : undefined, | |
| verificationInfo: user ? { | |
| type: verificationType, | |
| color: verificationColor, | |
| verifiedAt: (user as any)?.verified_at, | |
| verifiedBy: (user as any)?.verified_by | |
| } : undefined, | |
| pageElements: extractPageElements(), | |
| timestamp: Date.now() | |
| }; | |
| return context; | |
| }, [location.pathname, user, extractPageElements]); | |
| // مراقبة التغييرات في الصفحة | |
| const startMonitoring = useCallback(() => { | |
| if (observerRef.current) { | |
| observerRef.current.disconnect(); | |
| } | |
| // تحديث السياق عند تغيير الصفحة | |
| setPageContext(buildPageContext()); | |
| // مراقبة التغييرات في DOM | |
| const observer = new MutationObserver(() => { | |
| setPageContext(prevContext => ({ | |
| ...buildPageContext(), | |
| // الحفاظ على بيانات المستخدم والتوثيق | |
| userInfo: prevContext?.userInfo, | |
| verificationInfo: prevContext?.verificationInfo, | |
| isAuthenticated: prevContext?.isAuthenticated || false | |
| })); | |
| }); | |
| observer.observe(document.body, { | |
| childList: true, | |
| subtree: true, | |
| attributes: true, | |
| attributeFilter: ['class', 'data-active', 'aria-current'], | |
| characterData: false | |
| }); | |
| observerRef.current = observer; | |
| setIsMonitoring(true); | |
| }, [buildPageContext]); | |
| // إيقاف المراقبة | |
| const stopMonitoring = useCallback(() => { | |
| if (observerRef.current) { | |
| observerRef.current.disconnect(); | |
| observerRef.current = null; | |
| } | |
| setIsMonitoring(false); | |
| }, []); | |
| // تحديث السياق عند تغيير المسار أو المستخدم | |
| useEffect(() => { | |
| setPageContext(buildPageContext()); | |
| }, [location.pathname, user, buildPageContext]); | |
| // تنظيف عند إلغاء المكون | |
| useEffect(() => { | |
| return () => { | |
| stopMonitoring(); | |
| }; | |
| }, [stopMonitoring]); | |
| // دالة للحصول على وصف السياق بصيغة نصية للـ AI | |
| const getContextSummary = useCallback((): string => { | |
| if (!pageContext) return ''; | |
| let summary = `📍 السياق الحالي للموقع:\n`; | |
| summary += `الصفحة: ${pageContext.pageTitle}\n`; | |
| summary += `الرابط: ${pageContext.currentPage}\n`; | |
| summary += `الوصف: ${pageContext.pageDescription}\n`; | |
| if (pageContext.isAuthenticated && pageContext.userInfo) { | |
| summary += `\n👤 بيانات المستخدم:\n`; | |
| summary += `اسم المستخدم: ${pageContext.userInfo.username}\n`; | |
| summary += `الاسم الكامل: ${pageContext.userInfo.name}\n`; | |
| summary += `البريد الإلكتروني: ${pageContext.userInfo.email}\n`; | |
| if (pageContext.verificationInfo) { | |
| const verificationTypeArabic: Record<string, string> = { | |
| 'owner': '👑 مالك المنصة (أخضر)', | |
| 'developer': '💻 مطور الموقع (أزرق)', | |
| 'addon_developer': '🎨 مطور إضافات (بنفسجي)', | |
| 'user': '👥 مستخدم عادي' | |
| }; | |
| summary += `\n🏆 معلومات التوثيق:\n`; | |
| summary += `النوع: ${verificationTypeArabic[pageContext.verificationInfo.type] || 'غير معروف'}\n`; | |
| if (pageContext.verificationInfo.verifiedAt) { | |
| summary += `تم التوثيق في: ${new Date(pageContext.verificationInfo.verifiedAt).toLocaleDateString('ar')}\n`; | |
| } | |
| if (pageContext.verificationInfo.verifiedBy) { | |
| summary += `وثقه: ${pageContext.verificationInfo.verifiedBy}\n`; | |
| } | |
| } | |
| } | |
| if (pageContext.pageElements) { | |
| summary += `\n📊 عناصر الصفحة:\n`; | |
| if (pageContext.pageElements.title) { | |
| summary += `العنوان: ${pageContext.pageElements.title}\n`; | |
| } | |
| if (pageContext.pageElements.hasSearchBar) { | |
| summary += `- يحتوي على شريط بحث\n`; | |
| } | |
| if (pageContext.pageElements.hasFilters) { | |
| summary += `- يحتوي على فلاتر\n`; | |
| } | |
| if (pageContext.pageElements.currentCategory) { | |
| summary += `الفئة الحالية: ${pageContext.pageElements.currentCategory}\n`; | |
| } | |
| if (pageContext.pageElements.itemsCount) { | |
| summary += `عدد العناصر: ${pageContext.pageElements.itemsCount}\n`; | |
| } | |
| } | |
| return summary; | |
| }, [pageContext]); | |
| return { | |
| pageContext, | |
| isMonitoring, | |
| startMonitoring, | |
| stopMonitoring, | |
| getContextSummary, | |
| buildPageContext | |
| }; | |
| }; | |