From 8686628ea2ab5c541e136cea000ec3b11cf1b20c Mon Sep 17 00:00:00 2001 From: Arman Date: Fri, 15 Dec 2023 12:03:03 +0100 Subject: [PATCH] fix: unit functions --- src/lib/helpers/unit.ts | 13 ++++++++++--- src/lib/stores/billing.ts | 6 +++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/lib/helpers/unit.ts b/src/lib/helpers/unit.ts index 80b7e4882..394782ea2 100644 --- a/src/lib/helpers/unit.ts +++ b/src/lib/helpers/unit.ts @@ -44,11 +44,18 @@ export function createValueUnitPair(initialValue = 0, units: Unit unit.subscribe((newValue, prev) => { value.update((v) => { - const unitInBase = v * units.find((u) => u.name === prev).value; - return unitInBase / units.find((u) => u.name === newValue).value; + const prevUnit = units.find((u) => u.name === prev); + const newUnit = units.find((u) => u.name === newValue); + + // If either unit is not found, just return the current value + if (!prevUnit || !newUnit) { + return v; + } + + const unitInBase = v * prevUnit.value; + return unitInBase / newUnit.value; }); }); - return { value, unit, diff --git a/src/lib/stores/billing.ts b/src/lib/stores/billing.ts index 44e492ffd..8d3c28abf 100644 --- a/src/lib/stores/billing.ts +++ b/src/lib/stores/billing.ts @@ -70,9 +70,9 @@ export type PlanServices = export function getServiceLimit(serviceId: PlanServices, tier: Tier = null): number { if (!isCloud) return 0; if (!serviceId) return 0; - const plan = get(plansInfo).plans.find( - (p) => p.$id === (tier ?? get(organization)?.billingPlan) - ); + const info = get(plansInfo); + if (!info?.plans) return 0; + const plan = info.plans.find((p) => p.$id === (tier ?? get(organization)?.billingPlan)); return plan?.[serviceId]; }