File "useSupportArticles.js"

Full Path: /home/leadltht/fastlinkinternet.com/admin/wp-content/wp-content/wp-content/plugins/extendify/src/HelpCenter/hooks/useSupportArticles.js
File size: 604 bytes
MIME-type: text/x-java
Charset: utf-8

import { KB_HOST } from '@constants';
import useSWRImmutable from 'swr/immutable';

export const fetcher = (slug) => {
	const lang = window.extSharedData.wpLanguage || null;
	const params = new URLSearchParams({ lang });
	return fetch(`${KB_HOST}/api/posts/${slug}?${params.toString()}`).then(
		(res) => {
			if (res.status === 404) throw new Error('Not found');
			if (!res.ok) throw new Error(res.statusText);
			return res.json();
		},
	);
};

export const useSupportArticle = (slug) => {
	const { data, error } = useSWRImmutable(slug, fetcher);
	return { data, error, loading: !data && !error };
};