File size: 9,923 Bytes
6711fd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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
  };
};