38 lines
1.4 KiB
TypeScript
38 lines
1.4 KiB
TypeScript
import { getFeatureGate, isFeatureKey } from "@hivegrid/entitlements";
|
|
|
|
type SubscriptionFeatureTranslator = (key: string, options?: Record<string, unknown>) => unknown;
|
|
|
|
type SubscriptionFeatureTextInput = {
|
|
readonly name: string;
|
|
readonly title: string;
|
|
};
|
|
|
|
type SubscriptionFeatureBadgeInput = SubscriptionFeatureTextInput & {
|
|
readonly badge?: string | null;
|
|
};
|
|
|
|
export function getSubscriptionFeatureTitleKey(feature: SubscriptionFeatureTextInput): string {
|
|
return isFeatureKey(feature.name) ? `admin.connectors.feature.${feature.name}` : feature.title;
|
|
}
|
|
|
|
export function getSubscriptionFeatureTitle(
|
|
t: SubscriptionFeatureTranslator,
|
|
feature: SubscriptionFeatureTextInput,
|
|
values?: Record<string, unknown>
|
|
): string {
|
|
if (!isFeatureKey(feature.name)) return feature.title;
|
|
|
|
const key = getSubscriptionFeatureTitleKey(feature);
|
|
const translated = String(t(key, { defaultValue: feature.title, ...(values ?? {}) }));
|
|
return translated === key ? feature.title : translated;
|
|
}
|
|
|
|
export function getSubscriptionFeatureBadge(t: SubscriptionFeatureTranslator, feature: SubscriptionFeatureBadgeInput): string | null {
|
|
if (!feature.badge) return null;
|
|
if (!isFeatureKey(feature.name)) return feature.badge;
|
|
|
|
const registryFeature = getFeatureGate(feature.name);
|
|
if (!registryFeature || feature.badge !== registryFeature.title) return feature.badge;
|
|
return getSubscriptionFeatureTitle(t, { name: feature.name, title: feature.badge });
|
|
}
|