import { InputField } from '@components/common/form/InputField.js'; import { NumberField } from '@components/common/form/NumberField.js'; import { asArray, Segmented, useArraySetting } from '@components/common/page-builder/index.js'; import { EntitySearchList } from '@components/common/page-builder/pickers/EntitySearchList.js'; import { useScopedFormContext } from '@components/common/page-builder/WidgetSettingsScope.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import React, { useEffect, useState } from 'react'; import { useQuery } from 'urql'; const SEARCH_QUERY = ` query FeaturedBlogsSearch($filters: [FilterInput]) { blogPosts(filters: $filters) { items { uuid name thumbnail category { name } } total } } `; interface PostMeta { name: string; thumbnail?: string | null; } export default function FeaturedBlogsSetting({ featuredBlogsWidget }: { featuredBlogsWidget?: any; }) { const settings = featuredBlogsWidget ?? {}; const { setValue, getValues, watch } = useScopedFormContext(); const selected = useArraySetting('settings.postUuids', []); const columnsV = ((watch('settings.columns') as number) ?? settings.columns ?? 3) as | 1 | 2 | 3 | 4; const [search, setSearch] = useState(''); const [debounced, setDebounced] = useState(''); useEffect(() => { const t = setTimeout(() => setDebounced(search), 300); return () => clearTimeout(t); }, [search]); const filters = debounced ? [ { key: 'keyword', operation: 'like', value: debounced }, { key: 'limit', operation: 'eq', value: '20' } ] : [{ key: 'limit', operation: 'eq', value: '20' }]; const [result] = useQuery({ query: SEARCH_QUERY, variables: { filters } }); const [meta, setMeta] = useState>({}); // Seed selected-post labels from the widget query (legacy page) once. useEffect(() => { const posts = settings.posts; if (Array.isArray(posts) && posts.length) { setMeta((prev) => { const next = { ...prev }; posts.forEach((p: any) => { next[p.uuid] = { name: p.name, thumbnail: p.thumbnail }; }); return next; }); } }, [settings.posts]); // Accumulate labels from search results. useEffect(() => { const items = result.data?.blogPosts?.items; if (Array.isArray(items)) { setMeta((prev) => { const next = { ...prev }; items.forEach((p: any) => { next[p.uuid] = { name: p.name, thumbnail: p.thumbnail }; }); return next; }); } }, [result.data]); const searchItems = (result.data?.blogPosts?.items ?? []).map((p: any) => ({ id: p.uuid, primary: p.name, secondary: p.category?.name ?? null, thumbnailUrl: p.thumbnail ?? null })); const add = (uuid: string) => { const cur = asArray(getValues('settings.postUuids'), []); if (!cur.includes(uuid)) { setValue('settings.postUuids', [...cur, uuid], { shouldDirty: true }); } }; const removeAt = (index: number) => { const cur = asArray(getValues('settings.postUuids'), []); setValue( 'settings.postUuids', cur.filter((_, i) => i !== index), { shouldDirty: true } ); }; const move = (index: number, dir: number) => { const cur = [...asArray(getValues('settings.postUuids'), [])]; const j = index + dir; if (j < 0 || j >= cur.length) return; [cur[index], cur[j]] = [cur[j], cur[index]]; setValue('settings.postUuids', cur, { shouldDirty: true }); }; return (
{_('Columns')}
value={columnsV} options={[ { value: 1, label: '1' }, { value: 2, label: '2' }, { value: 3, label: '3' }, { value: 4, label: '4' } ]} onChange={(v) => setValue('settings.columns', v, { shouldDirty: true }) } />
{_('Featured posts')}
{selected.length === 0 && (

{_('Nothing picked: the latest posts are shown, up to Max posts.')}

)} {selected.length > 0 && (
    {selected.map((uuid, i) => (
  • {meta[uuid]?.thumbnail && ( )} {meta[uuid]?.name || uuid}
  • ))}
)} add(id)} caption={_('Search posts to feature')} emptyHint={debounced ? _('No posts match') : _('No posts yet')} />
); } export const query = ` query Query($eyebrow: String, $heading: String, $subText: String, $postUuids: [String], $count: Int, $columns: Int) { featuredBlogsWidget(eyebrow: $eyebrow, heading: $heading, subText: $subText, postUuids: $postUuids, count: $count, columns: $columns) { eyebrow heading subText count columns posts { uuid name thumbnail } } } `; export const variables = `{ eyebrow: getWidgetSetting("eyebrow"), heading: getWidgetSetting("heading"), subText: getWidgetSetting("subText"), postUuids: getWidgetSetting("postUuids"), count: getWidgetSetting("count"), columns: getWidgetSetting("columns") }`;