add support pages

This commit is contained in:
Profile Profile
2026-03-15 01:17:42 +03:00
parent 27fc54733e
commit 636bc47155
3 changed files with 232 additions and 96 deletions

85
src/lib/api/pages.ts Normal file
View File

@@ -0,0 +1,85 @@
import { fetchGraphQL } from './graphql-client.js';
/** подключаем страницу по slug */
interface PageData {
id: string;
title: string;
content: string;
image: string | null;
imageAlt: string;
type: 'page';
}
// Интерфейс для ответа GraphQL
interface GraphQLResponse {
data?: {
pages?: {
nodes: Array<{
id: string;
title: string;
content: string;
slug: string;
featuredImage?: {
node: {
sourceUrl: string;
altText: string;
};
} | null;
}>;
};
};
}
export async function getPageBySlug(slug: string): Promise<PageData | null> {
if (!slug) return null;
const query = `
query GetPageBySlug($slug: String!) {
pages(where: {name: $slug}) {
nodes {
id
title
content
slug
featuredImage {
node {
sourceUrl(size: LARGE)
altText
}
}
}
}
}
`;
try {
// Получаем данные
const response = await fetchGraphQL(query, { slug });
// ПРОВЕРЯЕМ СТРУКТУРУ ОТВЕТА
console.log('🔍 FULL RESPONSE:', JSON.stringify(response, null, 2));
// Пробуем разные варианты доступа к данным
const pages = response?.data?.pages?.nodes || response?.pages?.nodes || [];
const page = pages[0];
if (!page) {
console.log('❌ No page found for slug:', slug);
return null;
}
console.log('✅ Page found:', page.title);
return {
id: page.id,
title: page.title,
content: page.content,
image: page.featuredImage?.node?.sourceUrl || null,
imageAlt: page.featuredImage?.node?.altText || page.title || '',
type: 'page'
};
} catch (error) {
console.error('❌ Error in getPageBySlug:', error);
return null;
}
}

View File

@@ -1,3 +1,5 @@
import { wpInfo } from './wpInfo';
export interface PageTypeInfo {
type: 'single' | 'archive' | 'home' | 'unknown';
contentType?: 'news' | 'post';
@@ -5,14 +7,33 @@ export interface PageTypeInfo {
postSlug?: string;
postId?: number;
page: number;
pageSlug?: string;
uriParts: string[];
}
export function detectPageType(uri: string): PageTypeInfo {
//console.log('🔍 detectPageType INPUT:', uri);
// Убираем query параметры если есть
const uriWithoutQuery = uri.split('?')[0];
// Убираем слэши по краям
const cleanUri = uri.replace(/^\/|\/$/g, '');
const parts = cleanUri ? cleanUri.split('/') : [];
const cleanUri = uriWithoutQuery.replace(/^\/|\/$/g, '');
console.log('📌 Clean URI:', cleanUri);
// Если URI пустой - это главная
if (cleanUri === '') {
console.log('🏠 Home page detected');
return {
type: 'home',
page: 1,
uriParts: []
};
}
const parts = cleanUri.split('/');
console.log('📦 Parts:', parts);
// Ищем пагинацию
const processedParts: string[] = [];
@@ -23,20 +44,27 @@ export function detectPageType(uri: string): PageTypeInfo {
const pageNum = parseInt(parts[i + 1]);
if (pageNum > 0) {
page = pageNum;
i++; // Пропускаем номер страницы
i++;
continue;
}
}
processedParts.push(parts[i]);
}
//console.log('🔄 Processed parts:', processedParts);
// Определяем, это новость или нет
const isNews = processedParts[0] === 'news';
//console.log('📰 isNews:', isNews);
// Для анализа убираем 'news' из начала если есть
const partsWithoutNews = isNews ? processedParts.slice(1) : processedParts;
const partsCount = partsWithoutNews.length;
//console.log('✂️ Parts without news:', partsWithoutNews);
const partsCount = partsWithoutNews.length;
//console.log('🔢 Parts count:', partsCount);
// Домашняя страница
if (partsCount === 0) {
return {
type: 'home',
@@ -45,42 +73,66 @@ export function detectPageType(uri: string): PageTypeInfo {
};
}
// Проверяем, является ли последний сегмент постом (содержит ID)
const lastPart = partsWithoutNews[partsWithoutNews.length - 1];
//console.log('🔚 Last part:', lastPart);
const match = lastPart.match(/-(\d+)$/);
//console.log('🎯 Post ID match:', match);
// Одиночный пост
if (partsCount === 1 || partsCount === 2) {
const lastPart = partsWithoutNews[partsWithoutNews.length - 1];
const match = lastPart.match(/-(\d+)$/);
if (match) {
const id = parseInt(match[1]);
if (id > 0) {
const slugWithoutId = lastPart.substring(0, lastPart.lastIndexOf('-'));
return {
type: 'single',
contentType: isNews ? 'news' : 'post', // Теперь правильно
categorySlug: partsCount === 2 ? partsWithoutNews[0] : undefined,
postSlug: slugWithoutId,
postId: id,
page,
uriParts: processedParts // Сохраняем исходные части
};
}
if (match) {
const id = parseInt(match[1]);
if (id > 0) {
const slugWithoutId = lastPart.substring(0, lastPart.lastIndexOf('-'));
//console.log('📄 Single post detected:', { id, slugWithoutId });
return {
type: 'single',
contentType: isNews ? 'news' : 'post',
categorySlug: partsCount === 2 ? partsWithoutNews[0] : undefined,
postSlug: slugWithoutId,
postId: id,
page,
uriParts: processedParts
};
}
}
// Рубрика
// Проверяем, является ли первый сегмент существующей рубрикой
if (partsCount === 1) {
return {
type: 'archive',
contentType: isNews ? 'news' : undefined,
categorySlug: partsWithoutNews[0],
page,
uriParts: processedParts
};
const potentialCategorySlug = partsWithoutNews[0];
//console.log('📁 Checking if category exists:', potentialCategorySlug);
// Ждем загрузки рубрик если нужно
if (!wpInfo.isLoaded()) {
//console.log('⏳ Waiting for categories to load...');
// В синхронной функции не можем ждать, поэтому проверяем позже
}
const category = wpInfo.getCategoryBySlug(potentialCategorySlug);
//console.log('🏷️ Category found:', category ? 'YES' : 'NO');
if (category) {
//console.log('📁 Archive detected (existing category):', potentialCategorySlug);
return {
type: 'archive',
contentType: isNews ? 'news' : undefined,
categorySlug: potentialCategorySlug,
page,
uriParts: processedParts
};
}
}
// Если это не рубрика - это обычная страница
const pageSlug = partsWithoutNews.join('/');
//console.log('📄 PAGE DETECTED with slug:', pageSlug);
return {
type: 'unknown',
pageSlug: pageSlug,
page,
uriParts: processedParts
};