Tuesday, September 26, 2023

From the Kitchen of the Restaurant of Mistaken Orders: Whipping Up Faster Loading Speeds with 'From Path' in Sitecore Content Hub

restaurant of mistaken orders - sitecore content hub

Bonjour, tech gourmets! Chef Roel here, whisking up another delectable dish from the bustling kitchen of the Restaurant of Mistaken Orders. Today's special on the menu: A deep dive into the delightful world of Sitecore Content Hub, React components, and the ever-so-crucial choice between "From path" and "From asset". Grab your forks and let's dig in! 🍽️

The Appetizer: Setting the Table

Before we dive into the main course, let's set the table with some context. When working with Sitecore Content Hub and React, we often face the conundrum of how to link to an external React component. The two most popular choices are:

  • From asset: This method links directly to the asset.
  • From path: This method links to the component's path.

Now, I know what you're thinking: "Chef, isn't linking directly to the asset the quickest way to get to the dessert?" Ah, my discerning diners, this is where the plot thickens!

The Main Course: Why "From path" is the Crème de la Crème

1. Straight to the Source:

Using "From path" lets you link directly to the component's path. Think of it like having a shortcut to the freshest ingredients in your pantry. No more rummaging through aisles and shelves!

2. Speedy Service with Portal Assets:

When your React component resides in the Portal Assets, the serving time is even faster. Imagine this: You're hosting a grand feast and instead of fetching wine from the cellar in a distant vineyard (like an Azure environment), you grab a bottle from the wine rack right in your dining room. Swift, efficient, and oh-so-convenient!

A Side Dish: How to Measure Serving Speed

Now, for those of you who love to time your soufflés to perfection, here's a nifty trick to measure the speed difference between the two methods:

  1. Fire up your browser and head to your Sitecore Content Hub.
  2. Open Developer Tools (typically F12 or right-click > Inspect).
  3. Navigate to the 'Network' tab. This is where the magic happens!
  4. Reload your page.
  5. Sort the results by 'Time' to see how long each component takes to load.

Now, perform this for both "From asset" and "From path" (especially when linked from Portal Assets) and see the difference for yourself. Just like how some soups simmer slowly and others boil up quickly, you'll notice a delightful difference in loading times.

The Dessert: Wrapping Up with Some Sugar

So, dear diners, the next time you find yourself pondering the pathways in Sitecore Content Hub, remember our little culinary adventure today. "From path", especially from Portal Assets, is the way to go if you fancy a swift, efficient, and flavorsome experience.

As always, from the bustling stoves of the Restaurant of Mistaken Orders, we aim to serve you not just meals, but experiences. And remember, in the world of tech (and cooking), it's always the little details that spice things up!

Bon Appétit! 🍷🍝

Saturday, September 23, 2023

Ordering Up the Perfect Search Filter": A Deep Dive into Sitecore Content Hub's External Component

Welcome to the "Restaurant of Mistaken Orders"! 🍽️ Here, where unexpected combinations lead to delightful discoveries, today's special is a scrumptious walkthrough of an "external component" in Sitecore Content Hub. Our aim? To preset a search filter based on a user group.

Before we dive into the intricacies of our dish, allow me to present to you the recipe card. Our entire cooking procedure, ingredients, and secret techniques are documented in our digital cookbook, right here: Sitecore Content Hub External Component on GitHub. As we journey through this culinary adventure, we'll be referring to this repository. It's the source of our inspiration and the backbone of today's dish! 

With that said, a quick reminder: our restaurant thrives on the unexpected. Just as you might find a sprinkle of chocolate on your spaghetti (surprisingly delicious), expect a blend of technical details, humor, and chef anecdotes as we delve deeper.

1. The Ingredients: Our TypeScript Files

To cook up our search filter dish, we have the following TypeScript files as our main ingredients:

FilterDictionary.ts: This is like our recipe book. It defines how each user group correlates to a specific filter.

index.tsx: Our main entry point, just like the kitchen's bustling center!

HideSearchFilters.tsx & AddSearchFilters.tsx: These are the spices and seasonings, handling how filters appear or disappear based on user actions.

FilterConfig.ts: Think of this as the kitchen's guideline – setting the configurations for our filters.

2. Cooking Procedure: How it Works

a) Determining the User Group

First, our code checks which user group the logged-in user belongs to. Using our FilterDictionary.ts, it then fetches the corresponding preset filter for that group. Just like how I, as a chef, would pick a unique seasoning for each dish!

b) Setting the Filters

With the preset filter determined, the code utilizes the components HideSearchFilters.tsx and AddSearchFilters.tsx to adjust the search filters accordingly.

For example:

This process ensures that the user sees only the search filters relevant to their group.

3. Sourcing the Secret Sauce: Fetching toFilterRequest Values

Here's where our secret ingredient comes in: the toFilterRequest values. But how do we source these?

Accessing Browser Developer Tools: Just like opening the secret drawer of spices, open your browser and head to the Developer Tools.

Navigating to the Network Tab: Within Developer Tools, select the "Network" tab.

Filtering for Fetch/XHR: Make sure you have the "Fetch/XHR" option selected.

Performing a Search: On your Sitecore Content Hub, perform a search. In the Developer Tools, you'll notice an entry titled "search" on the left.

Extracting the Values: By setting the filter, you can now extract the required toFilterRequest values to use in your code.



This is similar to how I sometimes peek into other chefs' recipes to find that perfect spice mix. 😉

4. Final Plating

With everything set, our user will now experience a search filter uniquely tailored to their user group. A delightful experience that ensures content relevance and reduces unnecessary noise.

5. A Chef's Note

Remember, just as in our restaurant, it's all about experimentation. You might not get the desired taste in the first go, but with a pinch of persistence and a sprinkle of creativity, you'll cook up a masterpiece!

Wrapping Up

Thank you for dining with us today at the "Restaurant of Mistaken Orders"! We hope this detailed walkthrough has not only satisfied your tech cravings but also added a hint of fun to your coding journey. Until next time, keep experimenting and happy coding! 🍲👩‍🍳👨‍🍳

P.S. If you ever want to try spaghetti with chocolate sprinkles, let us know. We're always up for culinary adventures! 😉

Full source code available here: https://github.com/RoelRoozendaal/ch-facets-for-search

Wednesday, September 20, 2023

Sitecore Content Hub - User Group Display Component!


Hello there, fellow foodies of the coding world! Welcome to our delightful restaurant of mistaken orders, where our primary dish today is the spicy, flavorful, and ever so complex: "Sitecore Content Hub User Group Display Component!" Don your aprons and chef hats, and let's dive deep into the recipe of this code dish. 🍳

🍛 The Idea:

Before we stir the pot, let's discuss the idea. The code dish we're preparing serves to display the user groups that a user belongs to, right on their profile image. Imagine a pizza, and each slice is a user group. As a user, you can see which slices (or groups) you're a part of, just by looking at the pizza (your profile image). Savory, right?

User Profile Usergroups








🍜 Ingredients:

  • jQuery: The aromatic herb we'll be using to traverse and manipulate our HTML.
  • MutationObserver: The secret sauce ensuring that our ingredients (DOM elements) are ready before we start our culinary magic.
  • User Groups: The meaty chunks that we'll be refining to suit our taste.

🍳 Cooking Instructions:

Preparing the Meat (User Groups):

First, we filter out the basic flavors. We want our dish to stand out, so we're removing groups containing ".Base" or ".Role".

But wait! If our list contains the spicy "M.Consumer.US" flavor, we further refine by removing the "DML.Consumer.Base" group. It's all about balance!

Marinating the Meat:

To add some zest, we tweak the naming of our user groups. We remove any unwanted prefixes like "m" or "M." and replace any dot (.) with a space, making it more palatable.

Refining the Dish:

We don't want any overpowering flavors. Thus, we remove groups like "Everyone" or "TermsAndConditions".

To ensure uniqueness, we don't want duplicate ingredients. We get the unique groups and further refine them by removing words like "Base" or "Role".

Serving the Dish:

Now, we place our refined and unique user groups on the plate (or rather, into the HTML). Each group is garnished with a fancy icon and made clickable, though it doesn't lead anywhere right now (maybe to more recipes in the future? 😉).

Ensuring Perfect Temperature:

We wait for the right moment (using our secret sauce, the MutationObserver) to ensure our profile settings link is ready.

Once ready, we serve our dish hot! When you hover over the profile settings, our delicious user groups are displayed, and they hide when you move away.

🥘 Pro Tips from the Chef:

The kitchen can be unpredictable. Thus, we wrap parts of our code in try-catch to handle any unforeseen spills or burns (errors).

The waitForElm function is our oven timer. It ensures we don't start garnishing until our main dish (DOM element) is fully baked and ready.

🍰 Dessert:

After savoring this flavorful dish, remember: coding, like cooking, is an art. Sometimes it’s the mistaken orders that bring out the most delightful flavors. And while our restaurant might serve orders with a twist, it's always a gastronomic journey to remember!

The Code (code tab):

$(document).ready(function () {
var optionsUserGroups = options.userGroups.filter(x => !(x.includes(".Base") || x.includes(".Role")));

if (optionsUserGroups.includes("M.Consumer.US")) {
optionsUserGroups = jQuery.grep(optionsUserGroups, function (value) {
return value !== "M.Consumer.Base";
});
}

try {
var userGroups = optionsUserGroups.map(function (value) {
var newValue = value.replace(/m|M\./g, "");
return newValue ? newValue.replace(".", " ") : value;
});
} catch (err) {
console.log(`There was an error in Getting usergroups for user profile. ${err}`);
}

try {
if (userGroups.length > 0) {
var updatedUserGroupsArray = userGroups.filter(function (value) {
return !/Everyone|TermsAndConditions/.test(value);
});
}
} catch (err) {
console.log(`There was an error in Getting usergroups for user profile. ${err}`);
}

try {
if (updatedUserGroupsArray.length > 0) {
var uniqueUserGroups = [...new Set(updatedUserGroupsArray)];
var finalUserGroups = uniqueUserGroups.map(function (value) {
var newValue = value.replace(/Base|Role/g, "");
return newValue ? newValue.replace(".", " ") : value;
});
$.each(finalUserGroups, function (index, value) {
$(`<a class="dropdown-item" href="#"><li class="m-icon m-icon-people"></li>${value}</a>"`).insertAfter(".dropdown-divider");
});
}
} catch (err) {
console.log(`There was an error in Getting usergroups for user profile. ${err}`);
}

var selector = 'a[title*="Profile and settings"]';
waitForElm(selector).then((elem) => {
$(selector).click(function() {
$("#profile-groups-menu").hide();
$("#profile-groups-menu").addClass("hide");
});

$(selector).hover(function () {
$("#profile-groups-menu").show();
$("#profile-groups-menu").removeClass("hide");
}, function () {
$("#profile-groups-menu").hide();
$("#profile-groups-menu").addClass("hide");
});
});
});

function waitForElm(selector) {
return new Promise(resolve => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}
const observer = new MutationObserver(mutations => {
if (document.querySelector(selector)) {
resolve(document.querySelector(selector));
observer.disconnect();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
}
<!-- / TEMPLATE / -->
<style>
body,
html {
/* overflow-y: hidden; */
}

#profile-groups-menu {
position: fixed;
min-height: 200px;
min-width: 238px;
max-height: 800px;
width: 238px;
border-left: 1px solid transparent;
border-right: 1px solid transparent;
border-bottom: 1px solid #f9f9f9;
top: 5em;
right: 1em;
z-index: 10000 !important;
}

.dropdown-menu {
content: "";
}

.hide {
display: none;
}
</style>
<div id="profile-groups-menu" class="hide dropdown-menu dropdown-menu-right">
<a class="dropdown-item" href="#">
<h3><i class="m-icon m-icon-user-circle"></i>Group memberships</h3></a>
<div class="dropdown-divider"></div>
</div>

Sunday, September 17, 2023

Diving Deep into the Video Caption Editor's Integration with Azure Functions

Introduction

In our previous post, we introduced the Video Caption Editor for the Sitecore Content Hub and shed light on its importance in terms of efficiency and cost-saving. Today, we'll journey deeper into the technical aspects of this groundbreaking tool, especially its integration with Azure Functions.

Video Processing and Properties

Before sending video data to the Content Hub, it's crucial to obtain specific properties of the video that can aid in its correct representation and playback. This involves understanding the compression formats, such as the Apple ProRes 422 HQ.

Converting HTML to Images

One of the fascinating features of the Video Caption Editor is its capability to transform HTML content into images. This is particularly useful for generating overlays from HTML-based video captions. Leveraging powerful libraries such as SkiaSharp and HtmlAgilityPack, this feature provides high-fidelity conversions.

using HtmlAgilityPack;
using SkiaSharp;
using System.Collections.Generic;
using Topten.RichTextKit;

namespace Sitecore.CH.Implementation.AzFunctions.Model.Video
{
public class VideoHtmlToImage
{
public byte[] ConvertHtmlToImage(string html, int originalTextBoxWidth, int originalTextBoxHeight, int targetTextBoxWidth, int targetTextBoxHeight, SKColor backgroundColor, SKColor textColor, int videoWidth)
{
// Calculate aspect ratios
float originalAspectRatio = (float)originalTextBoxWidth / originalTextBoxHeight;
float targetAspectRatio = (float)targetTextBoxWidth / targetTextBoxHeight;

// Calculate scaled dimensions based on the aspect ratio
int scaledWidth, scaledHeight;
if (originalAspectRatio > targetAspectRatio)
{
scaledWidth = targetTextBoxWidth;
scaledHeight = (int)(targetTextBoxWidth / originalAspectRatio);
}
else
{
scaledWidth = (int)(targetTextBoxHeight * originalAspectRatio);
scaledHeight = targetTextBoxHeight;
}

// Create a new image surface
using (SKBitmap bitmap = new SKBitmap(targetTextBoxWidth, targetTextBoxHeight))
using (SKCanvas canvas = new SKCanvas(bitmap))
{
canvas.Clear(backgroundColor);
// Convert the HTML to a RichString
var richString = HtmlToRichString(html, textColor, videoWidth);
richString.MaxWidth = scaledWidth;
richString.MaxHeight = scaledHeight;
richString.Paint(canvas, new SKPoint(15, 20));

// Encode the bitmap as PNG
using (SKData data = SKImage.FromBitmap(bitmap). Encode(SKEncodedImageFormat.Png, 100))
{
return data.ToArray();
}
}
}

Dictionary<string, string> fontMap = new Dictionary<string, string>
{
{ "ql-font-timesnewroman", "Times New Roman" },
{ "ql-font-arial", "Arial" },
{ "ql-font-verdana", "Verdana" },
{ "ql-font-couriernew", "Courier New" },
{ "ql-font-georgia", "Georgia" },
{ "ql-font-comicsansms", "Comic Sans MS" },
{ "ql-font-consolas", "Consolas" },
{ "ql-font-impact", "Impact" },
{ "ql-font-lucidaconsole", "Lucida Console" },
{ "ql-font-lucidasansunicode", "Lucida Sans Unicode" },
{ "ql-font-microsoftsansserif", "Microsoft Sans Serif" },
{ "ql-font-palatinolinotype", "Palatino Linotype" },
{ "ql-font-segoe-ui", "Segoe UI" },
{ "ql-font-tahoma", "Tahoma" },
{ "ql-font-trebuchetms", "Trebuchet MS" },
{ "ql-font-symbol", "Symbol" },
{ "ql-font-webdings", "Webdings" },
{ "ql-font-wingdings", "Wingdings" }
};

// Additional methods and functionalities...
}
}

Asset Management in Content Hub

The core functionality revolves around managing video assets within the Content Hub. This encompasses creating, updating, and fetching assets. The Sitecore Content Hub SDK is at the heart of these operations, ensuring seamless integration.

Creating Asset Files

When introducing new video assets to the Content Hub, we utilize Azure Blob Storage for efficient storage and retrieval.

public async Task<(long ResponseId, long FileSize)> UploadVideoToContentHub(string videoBlobUrl, string videoExtension)
{
long responseId = 0;
long fileSize = 0;

try
{
_logger.LogInformation("Starting the video upload process...");
var uri = new Uri(videoBlobUrl, UriKind.Absolute);
var mClient = _mClientFactory.Client;
var cancellationTokenSource = new CancellationTokenSource();

using (var memoryStream = await MemoryStreamHelper. GetMemoryStreamFromUrlAsync(uri, cancellationTokenSource.Token))
{
string shortUniqueId = Guid.NewGuid().ToString().Substring(0, 7);
var contentType = VideoHelper.GetVideoContentType(videoExtension);
var name = $"video_caption_editor_{shortUniqueId}.{videoExtension}";
var uploadSource = new StreamUploadSource(memoryStream, contentType, name);
fileSize = memoryStream.Length;
var request = new UploadRequest(uploadSource, "AssetUploadConfiguration", "NewAsset")
{
ActionParameters = new Dictionary<string, object>
{
{ "FileSize", fileSize },
}
};

// Initiate upload and wait for its completion.
var response = await mClient.Uploads.UploadAsync(request, cancellationTokenSource.Token).ConfigureAwait(false);
responseId = (long)await mClient.LinkHelper.IdFromEntityAsync(response.Headers.Location).ConfigureAwait(false);
}
_logger.LogInformation("Video upload process completed successfully.");
return (responseId, fileSize);
}
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.Forbidden)
{
_logger.LogError(ex, "Authentication failed. Check your credentials or authorization token.");
throw new AuthenticationException("Authentication failed. Check your c redentials or authorization token.");
}
catch (Exception ex)
{
_logger.LogError(ex, $"File upload failed: {ex.Message}");
throw new VideoUploadException($"File upload failed: {ex.Message}");
}
}
}
}


// Additional methods and functionalities...

Updating Asset Files

The Video Caption Editor allows for updating existing assets. This is vital for making real-time edits to video captions and ensuring the Content Hub contains the most recent version.


public async Task<(long ResponseId, long FileSize)> UploadVideoToContentHub(IEntity asset, string fileName, string videoBlobUrl, string videoExtension)
{
long responseId = 0;
long fileSize = 0;
long assetId = asset.Id.Value;

try
{
_logger.LogInformation("Starting the video upload process...");
var uri = new Uri(videoBlobUrl, UriKind.Absolute);
var mClient = _mClientFactory.Client;
var cancellationTokenSource = new CancellationTokenSource();

using (var memoryStream = await MemoryStreamHelper.GetMemoryStreamFromUrlAsync(uri, cancellationTokenSource.Token))
{
var contentType = VideoHelper.GetVideoContentType(videoExtension);
var uploadSource = new StreamUploadSource(memoryStream, contentType, fileName);
fileSize = memoryStream.Length;
var request = new UploadRequest(uploadSource, "AssetUploadConfiguration", "NewMainFile")
{
ActionParameters = new Dictionary<string, object>
{
{ "AssetId", assetId},
{ "FileName", $"{assetId}_{fileName}" },
{ "FileSize", fileSize },
}
};

var response = await mClient.Uploads.UploadAsync(request, cancellationTokenSource.Token).ConfigureAwait(false);
responseId = (long)await mClient.LinkHelper.IdFromEntityAsync(response.Headers.Location).ConfigureAwait(false);
}
_logger.LogInformation("Video upload process completed successfully.");
return (responseId, fileSize);
}
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.Forbidden)
{
_logger.LogError(ex, "Authentication failed. Check your credentials or authorization token.");
throw new AuthenticationException("Authentication failed. Check your credentials or authorization token.");
}
catch (Exception ex)
{
_logger.LogError(ex, $"File upload failed: {ex.Message}");
throw new VideoUploadException($"File upload failed: {ex.Message}");
}
}


// Additional methods and functionalities...

Azure Batch Video Processing

For efficient video processing, especially when dealing with bulk operations, the Video Caption Editor integrates with Azure Batch. This provides scalability and ensures videos are processed in a timely manner without overloading resources.

BatchSharedKeyCredentials credentials = new BatchSharedKeyCredentials(batchAccountUrl, batchAccountName, batchAccountKey);
using (BatchClient batchClient = BatchClient.Open(credentials))
{
// Additional methods and functionalities...
            }

Conclusion

The Video Caption Editor, with its integration into the Sitecore Content Hub and Azure Functions, represents a significant advancement in DAM systems. Whether you're converting HTML captions to images, managing assets, or processing videos in batches, this tool ensures a streamlined and efficient workflow.

Stay tuned as we continue to explore more features and dive deeper into the world of DAM through the lens of our Video Caption Editor for Sitecore Content Hub.

Monday, September 11, 2023

Introducing the Video Caption Editor for Sitecore Content Hub - A Game-Changer for DAM


The "Why" Behind the Video Caption Editor

In the fast-paced world of Digital Asset Management (DAM), time is money. And when you're dealing with video content, this couldn't be truer. That's where our Video Caption Editor comes into play—a specialized tool built for Sitecore Content Hub, tailored to make your life easier, your workflow smoother, and your costs lower.

The Need for a Video Caption Editor in DAM

Videos are a powerful medium but managing them effectively within a DAM system like Sitecore Content Hub can be challenging. Editing captions, ensuring compliance, and enhancing accessibility are tasks that can consume a significant amount of time and resources. Our Video Caption Editor aims to simplify these tasks, reducing both time and financial expenditure.

Here are some of the benefits of using a Video Caption Editor in DAM:

  • Increased efficiency: The Video Caption Editor can help you edit captions more quickly and easily, freeing up your time for other tasks.
  • Improved compliance: The Video Caption Editor can help you ensure that your captions are compliant with accessibility standards, such as WCAG 2.1.
  • Added flexibility: The Video Caption Editor can be used to add additional text to videos, such as titles, copyrights, and other annotations.
  • Automated workflow: The Video Caption Editor is integrated with Azure Functions to automate the captioning process, freeing up your team to focus on other tasks.

Demo: See It to Believe It

Words can only say so much, and that's why we believe a demo is worth a thousand words.

From the intuitive UI to real-time caption editing, the demo offers a glimpse into how the Video Caption Editor can revolutionize your DAM operations.

Why We Built This

The motivation behind developing this Video Caption Editor was simple: Efficiency and Cost-Savings. We identified a gap in the market for a tool that could make the process of editing video captions within a DAM system more streamlined and less resource-intensive. And so, the Video Caption Editor was born.

Setting the Stage

This is just the tip of the iceberg. In our upcoming posts, we will delve into the technical aspects of this groundbreaking tool, discuss its integration with Azure Functions, and evaluate its pros and cons. So stay tuned for an enlightening journey into the world of DAM, all through the lens of our Video Caption Editor for Sitecore Content Hub.