| 1 |
import { Article } from '@help-center/components/knowledge-base/Article'; |
| 2 |
import { ArticlesList } from '@help-center/components/knowledge-base/ArticlesList'; |
| 3 |
import { SearchForm } from '@help-center/components/knowledge-base/SearchForm'; |
| 4 |
import { useSearchArticles } from '@help-center/hooks/useSearchArticles'; |
| 5 |
import { useKnowledgeBaseStore } from '@help-center/state/knowledge-base'; |
| 6 |
import { safeParseJson } from '@shared/lib/parsing'; |
| 7 |
import { Spinner } from '@wordpress/components'; |
| 8 |
import { __ } from '@wordpress/i18n'; |
| 9 |
|
| 10 |
const mainArticles = [ |
| 11 |
'wordpress-block-editor', |
| 12 |
'blocks-list', |
| 13 |
'adding-a-new-block', |
| 14 |
'block-pattern', |
| 15 |
'block-pattern-directory', |
| 16 |
]; |
| 17 |
|
| 18 |
const allArticles = safeParseJson( |
| 19 |
window.extSharedData.resourceData, |
| 20 |
)?.supportArticles?.filter((article) => mainArticles.includes(article.slug)); |
| 21 |
|
| 22 |
export const KnowledgeBaseDashboard = ({ onOpen }) => { |
| 23 |
const { setSearchTerm } = useKnowledgeBaseStore(); |
| 24 |
return ( |
| 25 |
<section className="rounded-md border" data-test="help-center-kb-section"> |
| 26 |
<div className="border-b border-gray-150 bg-gray-100 p-2.5 pb-4"> |
| 27 |
<h1 className="m-0 mb-3 p-0 text-lg font-medium"> |
| 28 |
{__('Knowledge Base', 'extendify-local')} |
| 29 |
</h1> |
| 30 |
<SearchForm |
| 31 |
onChange={(term) => { |
| 32 |
setSearchTerm(term); |
| 33 |
onOpen(); |
| 34 |
}} |
| 35 |
/> |
| 36 |
</div> |
| 37 |
<ArticlesList articles={allArticles.slice(0, 5)} /> |
| 38 |
</section> |
| 39 |
); |
| 40 |
}; |
| 41 |
|
| 42 |
export const KnowledgeBase = () => { |
| 43 |
const { setSearchTerm, searchTerm } = useKnowledgeBaseStore(); |
| 44 |
const { data, loading } = useSearchArticles(searchTerm); |
| 45 |
|
| 46 |
return ( |
| 47 |
<section className="p-4"> |
| 48 |
<div className=""> |
| 49 |
<div className="mb-4"> |
| 50 |
<h2 className="m-0 mb-2 text-sm"> |
| 51 |
{searchTerm && loading |
| 52 |
? __('Searching...', 'extendify-local') |
| 53 |
: data?.length > 0 |
| 54 |
? __('Search results', 'extendify-local') |
| 55 |
: __('Search the knowledge base', 'extendify-local')} |
| 56 |
</h2> |
| 57 |
<SearchForm onChange={setSearchTerm} /> |
| 58 |
</div> |
| 59 |
{loading && searchTerm ? ( |
| 60 |
<div className="p-8 text-center text-base"> |
| 61 |
<Spinner /> |
| 62 |
</div> |
| 63 |
) : ( |
| 64 |
<ArticlesList articles={data?.slice(0, 10) ?? []} /> |
| 65 |
)} |
| 66 |
</div> |
| 67 |
</section> |
| 68 |
); |
| 69 |
}; |
| 70 |
|
| 71 |
export const KnowledgeBaseArticle = () => ( |
| 72 |
<section className="p-4"> |
| 73 |
<div className=""> |
| 74 |
<div className=""> |
| 75 |
<Article /> |
| 76 |
</div> |
| 77 |
</div> |
| 78 |
</section> |
| 79 |
); |
| 80 |
|
| 81 |
export const routes = [ |
| 82 |
{ |
| 83 |
slug: 'knowledge-base', |
| 84 |
title: __('Knowledge Base', 'extendify-local'), |
| 85 |
component: KnowledgeBase, |
| 86 |
}, |
| 87 |
{ |
| 88 |
slug: 'knowledge-base-article', |
| 89 |
title: __('Knowledge Base', 'extendify-local'), |
| 90 |
component: KnowledgeBaseArticle, |
| 91 |
}, |
| 92 |
]; |
| 93 |
|