252 lines
6.2 KiB
TypeScript
252 lines
6.2 KiB
TypeScript
import { useState, useEffect } from 'react';
|
|
import {
|
|
getTicketCategories,
|
|
getTicketStatuses,
|
|
getTicketPriorities,
|
|
getKnowledgeBaseCategories,
|
|
getKnowledgeBaseArticles,
|
|
getFeaturedArticles,
|
|
getKnowledgeBaseArticle,
|
|
getArticlesByCategory,
|
|
TicketCategory,
|
|
TicketStatus,
|
|
TicketPriority,
|
|
KnowledgeBaseCategory,
|
|
KnowledgeBaseArticle
|
|
} from '../api/supportService';
|
|
|
|
/**
|
|
* Hook to fetch ticket categories
|
|
*/
|
|
export const useTicketCategories = () => {
|
|
const [categories, setCategories] = useState<TicketCategory[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
const fetchCategories = async () => {
|
|
try {
|
|
setLoading(true);
|
|
const data = await getTicketCategories();
|
|
setCategories(data);
|
|
setError(null);
|
|
} catch (err: any) {
|
|
setError(err.message || 'Failed to fetch ticket categories');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
fetchCategories();
|
|
}, []);
|
|
|
|
return { categories, loading, error };
|
|
};
|
|
|
|
/**
|
|
* Hook to fetch ticket statuses
|
|
*/
|
|
export const useTicketStatuses = () => {
|
|
const [statuses, setStatuses] = useState<TicketStatus[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
const fetchStatuses = async () => {
|
|
try {
|
|
setLoading(true);
|
|
const data = await getTicketStatuses();
|
|
setStatuses(data);
|
|
setError(null);
|
|
} catch (err: any) {
|
|
setError(err.message || 'Failed to fetch ticket statuses');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
fetchStatuses();
|
|
}, []);
|
|
|
|
return { statuses, loading, error };
|
|
};
|
|
|
|
/**
|
|
* Hook to fetch ticket priorities
|
|
*/
|
|
export const useTicketPriorities = () => {
|
|
const [priorities, setPriorities] = useState<TicketPriority[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
const fetchPriorities = async () => {
|
|
try {
|
|
setLoading(true);
|
|
const data = await getTicketPriorities();
|
|
setPriorities(data);
|
|
setError(null);
|
|
} catch (err: any) {
|
|
setError(err.message || 'Failed to fetch ticket priorities');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
fetchPriorities();
|
|
}, []);
|
|
|
|
return { priorities, loading, error };
|
|
};
|
|
|
|
/**
|
|
* Hook to fetch knowledge base categories
|
|
*/
|
|
export const useKnowledgeBaseCategories = () => {
|
|
const [categories, setCategories] = useState<KnowledgeBaseCategory[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
const fetchCategories = async () => {
|
|
try {
|
|
setLoading(true);
|
|
const data = await getKnowledgeBaseCategories();
|
|
setCategories(data);
|
|
setError(null);
|
|
} catch (err: any) {
|
|
setError(err.message || 'Failed to fetch knowledge base categories');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
fetchCategories();
|
|
}, []);
|
|
|
|
return { categories, loading, error };
|
|
};
|
|
|
|
/**
|
|
* Hook to fetch knowledge base articles with optional search
|
|
*/
|
|
export const useKnowledgeBaseArticles = (search?: string) => {
|
|
const [articles, setArticles] = useState<KnowledgeBaseArticle[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
const fetchArticles = async () => {
|
|
try {
|
|
setLoading(true);
|
|
const data = await getKnowledgeBaseArticles(search);
|
|
setArticles(data);
|
|
setError(null);
|
|
} catch (err: any) {
|
|
setError(err.message || 'Failed to fetch knowledge base articles');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
fetchArticles();
|
|
}, [search]);
|
|
|
|
return { articles, loading, error };
|
|
};
|
|
|
|
/**
|
|
* Hook to fetch featured knowledge base articles
|
|
*/
|
|
export const useFeaturedArticles = () => {
|
|
const [articles, setArticles] = useState<KnowledgeBaseArticle[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
const fetchArticles = async () => {
|
|
try {
|
|
setLoading(true);
|
|
const data = await getFeaturedArticles();
|
|
setArticles(data);
|
|
setError(null);
|
|
} catch (err: any) {
|
|
setError(err.message || 'Failed to fetch featured articles');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
fetchArticles();
|
|
}, []);
|
|
|
|
return { articles, loading, error };
|
|
};
|
|
|
|
/**
|
|
* Hook to fetch a single knowledge base article
|
|
*/
|
|
export const useKnowledgeBaseArticle = (slug: string | null) => {
|
|
const [article, setArticle] = useState<KnowledgeBaseArticle | null>(null);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (!slug) {
|
|
setLoading(false);
|
|
return;
|
|
}
|
|
|
|
const fetchArticle = async () => {
|
|
try {
|
|
setLoading(true);
|
|
const data = await getKnowledgeBaseArticle(slug);
|
|
setArticle(data);
|
|
setError(null);
|
|
} catch (err: any) {
|
|
setError(err.message || 'Failed to fetch knowledge base article');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
fetchArticle();
|
|
}, [slug]);
|
|
|
|
return { article, loading, error };
|
|
};
|
|
|
|
/**
|
|
* Hook to fetch articles by category
|
|
*/
|
|
export const useArticlesByCategory = (categorySlug: string | null) => {
|
|
const [articles, setArticles] = useState<KnowledgeBaseArticle[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (!categorySlug) {
|
|
setLoading(false);
|
|
return;
|
|
}
|
|
|
|
const fetchArticles = async () => {
|
|
try {
|
|
setLoading(true);
|
|
const data = await getArticlesByCategory(categorySlug);
|
|
setArticles(data);
|
|
setError(null);
|
|
} catch (err: any) {
|
|
setError(err.message || 'Failed to fetch articles by category');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
fetchArticles();
|
|
}, [categorySlug]);
|
|
|
|
return { articles, loading, error };
|
|
};
|
|
|