@progress/sitefinity-nextjs-sdk
Version:
Provides OOB widgets developed using the Next.js framework, which includes an abstraction layer for Sitefinity communication. Additionally, it offers an expanded API, typings, and tools for further development and integration.
88 lines (87 loc) • 4.19 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { ResultsDefaultView } from './results.view';
import { getMinimumWidgetContext } from '../../editor/widget-framework/widget-context';
import { htmlAttributes, getCustomAttributes } from '../../editor/widget-framework/attributes';
import { classNames } from '../../editor/utils/classNames';
import { StyleGenerator } from '../styling/style-generator.service';
import { RenderView } from '../common/render-view';
import { RootUrlService } from '../../rest-sdk/root-url.service';
import { RestClient } from '../../rest-sdk/rest-client';
import { Tracer } from '@progress/sitefinity-nextjs-sdk/diagnostics/empty';
async function performFind(searchQuery, knowledgeBoxName, configurationName, traceContext) {
try {
const url = `${RootUrlService.getServerCmsServiceUrl()}/AgenticRag/find`;
const response = await RestClient.sendRequest({
url,
method: 'POST',
data: {
KnowledgeBoxName: knowledgeBoxName,
Query: searchQuery,
ConfigurationName: configurationName,
Show: ['basic', 'origin', 'values'],
Take: 200
},
traceContext
});
const resultItems = [];
if (response && response.Resources) {
for (const resourceKey of Object.keys(response.Resources)) {
const resource = response.Resources[resourceKey];
if (!resource) {
continue;
}
const allParagraphs = [];
if (resource.Fields) {
for (const fieldKey of Object.keys(resource.Fields)) {
const field = resource.Fields[fieldKey];
if (!field || !field.Paragraphs) {
continue;
}
for (const paraKey of Object.keys(field.Paragraphs)) {
const paragraph = field.Paragraphs[paraKey];
if (paragraph) {
allParagraphs.push(paragraph);
}
}
}
}
allParagraphs.sort((a, b) => a.Order - b.Order);
resultItems.push({ Title: resource.Title, Link: resource.Origin.Url, Order: allParagraphs[0]?.Order ?? 0 });
}
}
resultItems.sort((a, b) => a.Order - b.Order);
return resultItems;
}
catch {
return [];
}
}
export async function Results(props) {
const { span, ctx } = Tracer.traceWidget(props, true);
const entity = props.model.Properties;
const requestContext = props.requestContext;
let dataAttributes = htmlAttributes(props);
const marginClass = entity.Margins && StyleGenerator.getMarginClasses(entity.Margins);
const customAttributes = getCustomAttributes(entity.Attributes, 'Results');
dataAttributes['className'] = classNames(entity.CssClass, marginClass);
const searchQuery = requestContext.searchParams?.['searchQuery'];
const knowledgeBoxName = requestContext.searchParams?.['knowledgeBoxName'];
const searchConfigurationName = requestContext.searchParams?.['searchConfigurationName'];
let searchResults = null;
let resultsHeader = entity.NoResultsHeader.replace('{0}', searchQuery || '');
if (searchQuery && knowledgeBoxName) {
searchResults = await performFind(searchQuery, knowledgeBoxName, searchConfigurationName, ctx);
if (searchResults.length > 0) {
resultsHeader = entity.SearchResultsHeader.replace('{0}', searchQuery);
}
}
const viewProps = {
searchResults,
resultsHeader,
resultsNumberLabel: entity.ResultsNumberLabel,
pageSize: entity.PageSize ?? 20,
attributes: { ...dataAttributes, ...customAttributes },
widgetContext: getMinimumWidgetContext(props)
};
return (_jsx(RenderView, { viewName: entity.SfViewName, widgetKey: props.model.Name, traceSpan: span, viewProps: viewProps, children: _jsx(ResultsDefaultView, { ...viewProps }) }));
}