Skip to content

Commit

Permalink
Address Sentry issue (#1888)
Browse files Browse the repository at this point in the history
  • Loading branch information
npentrel authored Sep 25, 2023
1 parent 7baed13 commit 59de98f
Showing 1 changed file with 133 additions and 86 deletions.
219 changes: 133 additions & 86 deletions assets/js/tutorials.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,23 @@ const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
// query_by is required.
additionalSearchParameters: {
query_by: "title,description",
sort_by: "featured:asc,date:desc"
sort_by: "featured:asc,date:desc",
},
});
const searchClient = typesenseInstantsearchAdapter.searchClient;

const search = instantsearch({
indexName: 'tutorials',
indexName: "tutorials",
searchClient,
});


const customRefinementList = instantsearch.connectors.connectRefinementList(
({ items, refine, widgetParams }, isFirstRender) => {
const container = document.getElementById(widgetParams.container);

if (isFirstRender) {
container.addEventListener('click', ({target}) => {
const input = target.closest('input');
container.addEventListener("click", ({ target }) => {
const input = target.closest("input");

if (input) {
refine(input.value.split(" ")[0]);
Expand All @@ -45,11 +44,9 @@ const customRefinementList = instantsearch.connectors.connectRefinementList(
return;
}

const list = widgetParams.items.map(({label: staticLabel}) => {
const elem = items.find(
({label}) => label === staticLabel
);
let count = 0
const list = widgetParams.items.map(({ label: staticLabel }) => {
const elem = items.find(({ label }) => label === staticLabel);
let count = 0;
let isRefined = false;
if (elem) {
count = elem.count;
Expand All @@ -62,8 +59,8 @@ const customRefinementList = instantsearch.connectors.connectRefinementList(
<input
type="button"
value="${staticLabel} (${count})"
class="${isRefined ? 'refined' : ''}"
${count ? '' : 'disabled'}
class="${isRefined ? "refined" : ""}"
${count ? "" : "disabled"}
/>
</label>
</li>
Expand All @@ -72,16 +69,15 @@ const customRefinementList = instantsearch.connectors.connectRefinementList(

container.innerHTML = `
<ul>
${list.join('')}
${list.join("")}
</ul>
`;
}
},
);


search.addWidgets([
instantsearch.widgets.hits({
container: '#hits',
container: "#hits",
templates: {
item: `
<div class="col tutorial hover-card">
Expand Down Expand Up @@ -123,108 +119,159 @@ search.addWidgets([
hitsPerPage: 12,
}),
customRefinementList({
container: 'languages-list',
attribute: 'languages',
operator: 'or',
sortBy: ['name:asc'],
items: [
{label: "go" },
{label: "python" },
{label: "typescript"},
],
container: "languages-list",
attribute: "languages",
operator: "or",
sortBy: ["name:asc"],
items: [{ label: "go" }, { label: "python" }, { label: "typescript" }],
}),
customRefinementList({
container: 'components-list',
attribute: 'viamcomponents',
operator: 'and',
sortBy: ['name:asc'],
container: "components-list",
attribute: "viamcomponents",
operator: "and",
sortBy: ["name:asc"],
items: [
{label: "arm"},
{label: "base"},
{label: "board"},
{label: "camera"},
{label: "gripper"},
{label: "input_controller"},
{label: "motor"},
{label: "sensor"},
{label: "servo"},
{ label: "arm" },
{ label: "base" },
{ label: "board" },
{ label: "camera" },
{ label: "gripper" },
{ label: "input_controller" },
{ label: "motor" },
{ label: "sensor" },
{ label: "servo" },
],
}),
customRefinementList({
container: 'services-list',
attribute: 'viamservices',
operator: 'and',
sortBy: ['name:asc'],
container: "services-list",
attribute: "viamservices",
operator: "and",
sortBy: ["name:asc"],
items: [
{label: "data"},
{label: "motion"},
{label: "frame_system"},
{label: "mlmodel"},
{label: "navigation"},
{label: "base_rc"},
{label: "sensors"},
{label: "slam"},
{label: "vision"},
{ label: "data" },
{ label: "motion" },
{ label: "frame_system" },
{ label: "mlmodel" },
{ label: "navigation" },
{ label: "base_rc" },
{ label: "sensors" },
{ label: "slam" },
{ label: "vision" },
],
}),
customRefinementList({
container: 'level-list',
attribute: 'level',
operator: 'or',
sortBy: ['name:asc'],
container: "level-list",
attribute: "level",
operator: "or",
sortBy: ["name:asc"],
items: [
{label: "Beginner"},
{label: "Intermediate"},
{label: "Advanced"},
{ label: "Beginner" },
{ label: "Intermediate" },
{ label: "Advanced" },
],
}),
instantsearch.widgets.pagination({
container: '#pagination',
container: "#pagination",
}),
]);


search.start();
search.on('render', function() {
if (search.helper.state.disjunctiveFacetsRefinements.languages && search.helper.state.disjunctiveFacetsRefinements.languages.length) {
document.getElementById("filter-languages").textContent = "Languages (" + search.helper.state.disjunctiveFacetsRefinements.languages.length + ")";
search.on("render", function () {
if (
search.helper.state.disjunctiveFacetsRefinements.languages &&
search.helper.state.disjunctiveFacetsRefinements.languages.length
) {
document.getElementById("filter-languages").textContent =
"Languages (" +
search.helper.state.disjunctiveFacetsRefinements.languages.length +
")";
} else {
document.getElementById("filter-languages").textContent = "Languages";
}
if (search.helper.state.facetsRefinements.viamcomponents && search.helper.state.facetsRefinements.viamcomponents.length) {
document.getElementById("filter-components").textContent = "Components (" + search.helper.state.facetsRefinements.viamcomponents.length + ")";
if (
search.helper.state.facetsRefinements.viamcomponents &&
search.helper.state.facetsRefinements.viamcomponents.length
) {
document.getElementById("filter-components").textContent =
"Components (" +
search.helper.state.facetsRefinements.viamcomponents.length +
")";
} else {
document.getElementById("filter-components").textContent = "Components";
}
if (search.helper.state.facetsRefinements.viamservices && search.helper.state.facetsRefinements.viamservices.length) {
document.getElementById("filter-services").textContent = "Services (" + search.helper.state.facetsRefinements.viamservices.length + ")";
if (
search.helper.state.facetsRefinements.viamservices &&
search.helper.state.facetsRefinements.viamservices.length
) {
document.getElementById("filter-services").textContent =
"Services (" +
search.helper.state.facetsRefinements.viamservices.length +
")";
} else {
document.getElementById("filter-services").textContent = "Services";
}
if (search.helper.state.disjunctiveFacetsRefinements.level && search.helper.state.disjunctiveFacetsRefinements.level.length) {
document.getElementById("filter-level").textContent = "Level (" + search.helper.state.disjunctiveFacetsRefinements.level.length + ")";
if (
search.helper.state.disjunctiveFacetsRefinements.level &&
search.helper.state.disjunctiveFacetsRefinements.level.length
) {
document.getElementById("filter-level").textContent =
"Level (" +
search.helper.state.disjunctiveFacetsRefinements.level.length +
")";
} else {
document.getElementById("filter-level").textContent = "Level";
}
if (search.helper.state.facetsRefinements && search.helper.state.disjunctiveFacetsRefinements.languages && search.helper.state.facetsRefinements.viamcomponents) {
document.querySelectorAll('.pill-lang').forEach(e => search.helper.state.disjunctiveFacetsRefinements.languages.includes(e.textContent) ? e.classList.add('pill-highlight') : e.classList.remove('pill-highlight'));
document.querySelectorAll('.pill-component').forEach(e => (search.helper.state.facetsRefinements.viamcomponents.includes(e.textContent) || search.helper.state.facetsRefinements.viamservices.includes(e.textContent)) ? e.classList.add('pill-highlight') : e.classList.remove('pill-highlight'));
if (
search.helper.state.facetsRefinements &&
search.helper.state.disjunctiveFacetsRefinements.languages &&
search.helper.state.facetsRefinements.viamcomponents
) {
document
.querySelectorAll(".pill-lang")
.forEach((e) =>
search.helper.state.disjunctiveFacetsRefinements.languages.includes(
e.textContent,
)
? e.classList.add("pill-highlight")
: e.classList.remove("pill-highlight"),
);
document
.querySelectorAll(".pill-component")
.forEach((e) =>
search.helper.state.facetsRefinements.viamcomponents.includes(
e.textContent,
) ||
search.helper.state.facetsRefinements.viamservices.includes(
e.textContent,
)
? e.classList.add("pill-highlight")
: e.classList.remove("pill-highlight"),
);
}

observer.observe()
observer.observe();
});

document.body.addEventListener('click', function(event) {
let filter_box = document.getElementById('tutorial-filter-items');
let tutorial_menu = document.getElementById('tutorial-menu');
if (!filter_box.contains(event.target) && !tutorial_menu.contains(event.target)) {
document.querySelectorAll('.filter').forEach( el => {
el.setAttribute("aria-expanded", false);
el.classList.add("collapsed");
});
let open_elem = filter_box.getElementsByClassName("show");
if (open_elem) {
open_elem[0].classList.remove("show");
document.body.addEventListener(
"click",
function (event) {
let filter_box = document.getElementById("tutorial-filter-items");
let tutorial_menu = document.getElementById("tutorial-menu");
if (
!filter_box.contains(event.target) &&
!tutorial_menu.contains(event.target)
) {
document.querySelectorAll(".filter").forEach((el) => {
el.setAttribute("aria-expanded", false);
if (el && el.classList) {
el.classList.add("collapsed");
}
});
let open_elem = filter_box.getElementsByClassName("show");
if (open_elem) {
open_elem[0].classList.remove("show");
}
}
}
}, true);
},
true,
);

0 comments on commit 59de98f

Please sign in to comment.