@iblai/web-utils 1.0.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import React__default, { useCallback, useDebugValue, useMemo, useState, useEffect, useRef, useLayoutEffect, createContext, useContext } from 'react';
3
- import { OpenAPI, CoreService, ReportsService, AiAnalyticsService, AiMentorService, SearchService, AiPromptService, AiIndexService, CatalogService, SkillsService, AiAccountService, CredentialsService, FeaturesService, CommerceService, NotificationsService } from '@iblai/iblai-api';
3
+ import { OpenAPI, CoreService, ReportsService, AiAnalyticsService, AiMentorService, SearchService, AiPromptService, AiIndexService, CatalogService, SkillsService, AiAccountService, CredentialsService, FeaturesService, CommerceService, NotificationsService, MentorVisibilityEnum } from '@iblai/iblai-api';
4
4
  import { jsx } from 'react/jsx-runtime';
5
5
  import axios from 'axios';
6
6
 
@@ -7395,10 +7395,11 @@ const stripeApiSlice = createApi({
7395
7395
  providesTags: ['stripe-context'],
7396
7396
  }),
7397
7397
  getStripePricingPageSession: builder.query({
7398
- query: ({ platform_key }) => ({
7398
+ query: ({ platform_key, params }) => ({
7399
7399
  url: STRIPE_ENDPOINTS.GET_STRIPE_PRICING_PAGE_SESSION.path(platform_key),
7400
7400
  service: STRIPE_ENDPOINTS.GET_STRIPE_PRICING_PAGE_SESSION.service,
7401
7401
  method: 'GET',
7402
+ params,
7402
7403
  }),
7403
7404
  providesTags: ['stripe-context'],
7404
7405
  }),
@@ -9831,6 +9832,7 @@ const useSubscriptionHandlerV2 = (subscriptionFlow) => {
9831
9832
  });
9832
9833
  }
9833
9834
  catch (error) {
9835
+ console.error("Error getting user active app:", JSON.stringify(error));
9834
9836
  return null;
9835
9837
  }
9836
9838
  };
@@ -9877,6 +9879,7 @@ const useSubscriptionHandlerV2 = (subscriptionFlow) => {
9877
9879
  return await getUserActiveApp();
9878
9880
  }
9879
9881
  catch (error) {
9882
+ console.error("Error updating trial status:", JSON.stringify(error));
9880
9883
  return null;
9881
9884
  }
9882
9885
  };
@@ -9969,6 +9972,7 @@ const useSubscriptionHandlerV2 = (subscriptionFlow) => {
9969
9972
  return stripeContext.payment_link_url;
9970
9973
  }
9971
9974
  catch (error) {
9975
+ console.error("Error getting top up URL:", JSON.stringify(error));
9972
9976
  return null;
9973
9977
  }
9974
9978
  };
@@ -10042,6 +10046,7 @@ const useSubscriptionHandlerV2 = (subscriptionFlow) => {
10042
10046
  return stripeCustomerPortal === null || stripeCustomerPortal === void 0 ? void 0 : stripeCustomerPortal.url;
10043
10047
  }
10044
10048
  catch (error) {
10049
+ console.error("Error getting billing URL:", JSON.stringify(error));
10045
10050
  return null;
10046
10051
  }
10047
10052
  };
@@ -10087,7 +10092,10 @@ const useSubscriptionHandlerV2 = (subscriptionFlow) => {
10087
10092
  try {
10088
10093
  subscriptionFlow.handleBeforePricingPageDisplayFlow();
10089
10094
  const { data: stripeContext } = await getStripePricingPageSession({
10090
- platform_key: subscriptionFlow.getCurrentTenantKey(),
10095
+ platform_key: subscriptionFlow.getMainTenantKey(),
10096
+ params: {
10097
+ source_platform_key: subscriptionFlow.getCurrentTenantKey(),
10098
+ },
10091
10099
  }, false);
10092
10100
  if (isStripeContextError ||
10093
10101
  !stripeContext ||
@@ -10100,6 +10108,7 @@ const useSubscriptionHandlerV2 = (subscriptionFlow) => {
10100
10108
  }, !hasCredits);
10101
10109
  }
10102
10110
  catch (error) {
10111
+ console.error("Error getting stripe pricing page session:", JSON.stringify(error));
10103
10112
  subscriptionFlow.handleFailureOnPaymentFlow();
10104
10113
  }
10105
10114
  };
@@ -11487,7 +11496,7 @@ const useTenantContext = () => useContext(TenantContext);
11487
11496
  * 4. Handles tenant-specific domain redirects
11488
11497
  * 5. Maintains tenant access state
11489
11498
  */
11490
- function TenantProvider({ children, fallback, onAuthSuccess, onAuthFailure, currentTenant, requestedTenant, saveCurrentTenant, saveUserTenants, handleTenantSwitch, saveVisitingTenant, removeVisitingTenant, saveUserTokens, saveTenant, onAutoJoinUserToTenant, redirectToAuthSpa, }) {
11499
+ function TenantProvider({ children, fallback, onAuthSuccess, onAuthFailure, currentTenant, requestedTenant, saveCurrentTenant, saveUserTenants, handleTenantSwitch, saveVisitingTenant, removeVisitingTenant, saveUserTokens, saveTenant, onAutoJoinUserToTenant, redirectToAuthSpa, username, }) {
11491
11500
  const { userIsAccessingPublicRoute, setUserIsAccessingPublicRoute } = useAuthContext();
11492
11501
  const [determineUserPath, setDetermineUserPath] = useState(false);
11493
11502
  const [isLoading, setIsLoading] = React__default.useState(true);
@@ -11685,6 +11694,11 @@ function TenantProvider({ children, fallback, onAuthSuccess, onAuthFailure, curr
11685
11694
  platform_name: data.platform_name,
11686
11695
  is_advertising: (_a = data.metadata) === null || _a === void 0 ? void 0 : _a.is_advertising,
11687
11696
  };
11697
+ if (!username) {
11698
+ saveVisitingTenant === null || saveVisitingTenant === void 0 ? void 0 : saveVisitingTenant(newCurrentTenant);
11699
+ // user is not authenticated so we don't need to do anything that concerns an authenticated user
11700
+ return;
11701
+ }
11688
11702
  const { data: tenants } = await fetchUserTenants();
11689
11703
  const enhancedTenants = await enhanceTenants(tenants, data);
11690
11704
  saveUserTenants(enhancedTenants);
@@ -11826,7 +11840,7 @@ function TenantProvider({ children, fallback, onAuthSuccess, onAuthFailure, curr
11826
11840
  * 3. Manages redirection based on mentor availability
11827
11841
  * 4. Integrates with tenant context for access control
11828
11842
  */
11829
- function MentorProvider({ children, fallback, onAuthSuccess, onAuthFailure, redirectToAuthSpa, redirectToMentor, onLoadMentorsPermissions, redirectToNoMentorsPage, redirectToCreateMentor, username, isAdmin, mainTenantKey, requestedMentorId, handleMentorNotFound, forceDetermineMentor = false, }) {
11843
+ function MentorProvider({ children, fallback, onAuthSuccess, onAuthFailure, redirectToAuthSpa, redirectToMentor, onLoadMentorsPermissions, redirectToNoMentorsPage, redirectToCreateMentor, username, isAdmin, mainTenantKey, requestedMentorId, handleMentorNotFound, forceDetermineMentor = false, onComplete, }) {
11830
11844
  const [isLoading, setIsLoading] = useState(true);
11831
11845
  const { userIsAccessingPublicRoute } = useAuthContext();
11832
11846
  const { determineUserPath, tenantKey, metadata } = useTenantContext();
@@ -11853,6 +11867,33 @@ function MentorProvider({ children, fallback, onAuthSuccess, onAuthFailure, redi
11853
11867
  }
11854
11868
  catch (_a) { }
11855
11869
  };
11870
+ /**
11871
+ * Gets the mentor database ID from public settings if not available on the mentor object
11872
+ */
11873
+ const getMentorDbId = async (mentor) => {
11874
+ if (mentor === null || mentor === void 0 ? void 0 : mentor.id) {
11875
+ return mentor.id;
11876
+ }
11877
+ if (mentor === null || mentor === void 0 ? void 0 : mentor.unique_id) {
11878
+ try {
11879
+ const response = await getMentorPublicSettings({
11880
+ // @ts-ignore
11881
+ mentor: mentor.unique_id,
11882
+ org: tenantKey,
11883
+ // @ts-ignore
11884
+ userId: username,
11885
+ }).unwrap();
11886
+ return response === null || response === void 0 ? void 0 : response.mentor_id;
11887
+ }
11888
+ catch (error) {
11889
+ console.log("failed to fetch mentor ID from public settings", {
11890
+ error: error instanceof Error ? error.message : String(error),
11891
+ mentorUniqueId: mentor.unique_id,
11892
+ });
11893
+ }
11894
+ }
11895
+ return undefined;
11896
+ };
11856
11897
  /**
11857
11898
  * Determines which mentor to redirect the user to by following this priority:
11858
11899
  * 1. Recently accessed mentors
@@ -11860,9 +11901,11 @@ function MentorProvider({ children, fallback, onAuthSuccess, onAuthFailure, redi
11860
11901
  * 3. Non-featured mentors
11861
11902
  * 4. Seeded mentors (for admin users)
11862
11903
  * 5. Creation flow or no mentors page
11904
+ *
11905
+ * Returns the mentor object if found, undefined otherwise
11863
11906
  */
11864
11907
  async function determineMentorToRedirectTo() {
11865
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
11908
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
11866
11909
  console.log("starting mentor determination process", JSON.stringify({
11867
11910
  tenantKey,
11868
11911
  username,
@@ -11883,13 +11926,7 @@ function MentorProvider({ children, fallback, onAuthSuccess, onAuthFailure, redi
11883
11926
  metadata.skills_embedded_mentor_name) {
11884
11927
  const defaultMentor = JSON.parse(metadata.skills_embedded_mentor_name);
11885
11928
  if (defaultMentor === null || defaultMentor === void 0 ? void 0 : defaultMentor.unique_id) {
11886
- if (defaultMentor === null || defaultMentor === void 0 ? void 0 : defaultMentor.id) {
11887
- const rbacPermissions = await loadMentorsPermissions(defaultMentor === null || defaultMentor === void 0 ? void 0 : defaultMentor.id);
11888
- onLoadMentorsPermissions === null || onLoadMentorsPermissions === void 0 ? void 0 : onLoadMentorsPermissions(rbacPermissions);
11889
- }
11890
- redirectToMentor(tenantKey, defaultMentor.unique_id);
11891
- onAuthSuccess === null || onAuthSuccess === void 0 ? void 0 : onAuthSuccess();
11892
- return;
11929
+ return defaultMentor;
11893
11930
  }
11894
11931
  }
11895
11932
  // Check for recently accessed mentors (second choice)
@@ -11909,21 +11946,13 @@ function MentorProvider({ children, fallback, onAuthSuccess, onAuthFailure, redi
11909
11946
  if (starredMentors.length > 0) {
11910
11947
  const starredMentor = starredMentors[0];
11911
11948
  if (starredMentor === null || starredMentor === void 0 ? void 0 : starredMentor.unique_id) {
11912
- const rbacPermissions = await loadMentorsPermissions(starredMentor === null || starredMentor === void 0 ? void 0 : starredMentor.id);
11913
- onLoadMentorsPermissions === null || onLoadMentorsPermissions === void 0 ? void 0 : onLoadMentorsPermissions(rbacPermissions);
11914
- redirectToMentor(tenantKey, starredMentor.unique_id);
11915
- onAuthSuccess === null || onAuthSuccess === void 0 ? void 0 : onAuthSuccess();
11916
- return;
11949
+ return starredMentor;
11917
11950
  }
11918
11951
  }
11919
11952
  if (recentMentors.length > 0) {
11920
11953
  const recentMentor = recentMentors[0];
11921
11954
  if (recentMentor === null || recentMentor === void 0 ? void 0 : recentMentor.unique_id) {
11922
- const rbacPermissions = await loadMentorsPermissions(recentMentor === null || recentMentor === void 0 ? void 0 : recentMentor.id);
11923
- onLoadMentorsPermissions === null || onLoadMentorsPermissions === void 0 ? void 0 : onLoadMentorsPermissions(rbacPermissions);
11924
- redirectToMentor(tenantKey, recentMentor.unique_id);
11925
- onAuthSuccess === null || onAuthSuccess === void 0 ? void 0 : onAuthSuccess();
11926
- return;
11955
+ return recentMentor;
11927
11956
  }
11928
11957
  }
11929
11958
  const recentMentorsResult = await fetchMentors({
@@ -11940,19 +11969,12 @@ function MentorProvider({ children, fallback, onAuthSuccess, onAuthFailure, redi
11940
11969
  }));
11941
11970
  // Check if we found recent mentors for the tenant
11942
11971
  if (recentMentors.length > 0) {
11943
- const selectedMentorId = ((_b = recentMentors[0]) === null || _b === void 0 ? void 0 : _b.unique_id) || "";
11944
- const selectedMentorDbId = ((_c = recentMentors[0]) === null || _c === void 0 ? void 0 : _c.id) || "";
11945
- console.log("redirecting to recent mentor", JSON.stringify({
11946
- selectedMentorId,
11947
- mentorName: (_d = recentMentors[0]) === null || _d === void 0 ? void 0 : _d.name,
11972
+ console.log("found recent mentor", JSON.stringify({
11973
+ selectedMentorId: (_b = recentMentors[0]) === null || _b === void 0 ? void 0 : _b.unique_id,
11974
+ mentorName: (_c = recentMentors[0]) === null || _c === void 0 ? void 0 : _c.name,
11948
11975
  tenantKey,
11949
11976
  }));
11950
- const rbacPermissions = await loadMentorsPermissions(selectedMentorDbId);
11951
- // Select the most recent mentor as current mentor
11952
- onLoadMentorsPermissions === null || onLoadMentorsPermissions === void 0 ? void 0 : onLoadMentorsPermissions(rbacPermissions);
11953
- redirectToMentor(tenantKey, selectedMentorId);
11954
- onAuthSuccess === null || onAuthSuccess === void 0 ? void 0 : onAuthSuccess();
11955
- return;
11977
+ return recentMentors[0];
11956
11978
  }
11957
11979
  // If no recent mentors, get featured mentors
11958
11980
  console.log("no recent mentors found, fetching featured mentors", {
@@ -11966,7 +11988,7 @@ function MentorProvider({ children, fallback, onAuthSuccess, onAuthFailure, redi
11966
11988
  limit: QUERY_LIMIT,
11967
11989
  orderBy: "recently_accessed_at",
11968
11990
  });
11969
- const featuredMentors = ((_e = featuredMentorsResult.data) === null || _e === void 0 ? void 0 : _e.results) || [];
11991
+ const featuredMentors = ((_d = featuredMentorsResult.data) === null || _d === void 0 ? void 0 : _d.results) || [];
11970
11992
  console.log("featured mentors fetched", {
11971
11993
  count: featuredMentors.length,
11972
11994
  mentorIds: featuredMentors.map((m) => m === null || m === void 0 ? void 0 : m.unique_id),
@@ -11986,16 +12008,11 @@ function MentorProvider({ children, fallback, onAuthSuccess, onAuthFailure, redi
11986
12008
  const defaultIblMentor = featuredMentors.find((mentor) => { var _a; return ((_a = mentor === null || mentor === void 0 ? void 0 : mentor.metadata) === null || _a === void 0 ? void 0 : _a.default) === true; });
11987
12009
  // Check if found default featured mentor
11988
12010
  if (defaultIblMentor) {
11989
- console.log("redirecting to default IBL mentor", {
12011
+ console.log("found default IBL mentor", {
11990
12012
  mentorId: defaultIblMentor === null || defaultIblMentor === void 0 ? void 0 : defaultIblMentor.unique_id,
11991
12013
  mentorName: defaultIblMentor === null || defaultIblMentor === void 0 ? void 0 : defaultIblMentor.name,
11992
12014
  });
11993
- const defaultIblMentorDbId = (defaultIblMentor === null || defaultIblMentor === void 0 ? void 0 : defaultIblMentor.id) || "";
11994
- const rbacPermissions = await loadMentorsPermissions(defaultIblMentorDbId);
11995
- onLoadMentorsPermissions === null || onLoadMentorsPermissions === void 0 ? void 0 : onLoadMentorsPermissions(rbacPermissions);
11996
- redirectToMentor === null || redirectToMentor === void 0 ? void 0 : redirectToMentor(tenantKey, (defaultIblMentor === null || defaultIblMentor === void 0 ? void 0 : defaultIblMentor.unique_id) || "");
11997
- onAuthSuccess === null || onAuthSuccess === void 0 ? void 0 : onAuthSuccess();
11998
- return;
12015
+ return defaultIblMentor;
11999
12016
  }
12000
12017
  }
12001
12018
  else {
@@ -12006,30 +12023,19 @@ function MentorProvider({ children, fallback, onAuthSuccess, onAuthFailure, redi
12006
12023
  // Check if there's a default featured mentor
12007
12024
  const defaultFeatureMentor = featuredMentors.find((mentor) => { var _a; return ((_a = mentor === null || mentor === void 0 ? void 0 : mentor.metadata) === null || _a === void 0 ? void 0 : _a.default) === true; });
12008
12025
  if (defaultFeatureMentor) {
12009
- console.log("redirecting to default featured mentor", {
12026
+ console.log("found default featured mentor", {
12010
12027
  mentorId: defaultFeatureMentor === null || defaultFeatureMentor === void 0 ? void 0 : defaultFeatureMentor.unique_id,
12011
12028
  mentorName: defaultFeatureMentor === null || defaultFeatureMentor === void 0 ? void 0 : defaultFeatureMentor.name,
12012
12029
  });
12013
- const defaultFeatureMentorDbId = (defaultFeatureMentor === null || defaultFeatureMentor === void 0 ? void 0 : defaultFeatureMentor.id) || "";
12014
- const rbacPermissions = await loadMentorsPermissions(defaultFeatureMentorDbId);
12015
- onLoadMentorsPermissions === null || onLoadMentorsPermissions === void 0 ? void 0 : onLoadMentorsPermissions(rbacPermissions);
12016
- redirectToMentor(tenantKey, (defaultFeatureMentor === null || defaultFeatureMentor === void 0 ? void 0 : defaultFeatureMentor.unique_id) || "");
12017
- onAuthSuccess === null || onAuthSuccess === void 0 ? void 0 : onAuthSuccess();
12018
- return;
12030
+ return defaultFeatureMentor;
12019
12031
  }
12020
12032
  }
12021
12033
  // If no default mentor, select the first featured mentor
12022
- const firstFeaturedMentorId = ((_f = featuredMentors[0]) === null || _f === void 0 ? void 0 : _f.unique_id) || "";
12023
- console.log("redirecting to first featured mentor", {
12024
- mentorId: firstFeaturedMentorId,
12025
- mentorName: (_g = featuredMentors[0]) === null || _g === void 0 ? void 0 : _g.name,
12034
+ console.log("found first featured mentor", {
12035
+ mentorId: (_e = featuredMentors[0]) === null || _e === void 0 ? void 0 : _e.unique_id,
12036
+ mentorName: (_f = featuredMentors[0]) === null || _f === void 0 ? void 0 : _f.name,
12026
12037
  });
12027
- const firstFeaturedMentorDbId = ((_h = featuredMentors[0]) === null || _h === void 0 ? void 0 : _h.id) || "";
12028
- const rbacPermissions = await loadMentorsPermissions(firstFeaturedMentorDbId);
12029
- onLoadMentorsPermissions === null || onLoadMentorsPermissions === void 0 ? void 0 : onLoadMentorsPermissions(rbacPermissions);
12030
- redirectToMentor(tenantKey, firstFeaturedMentorId);
12031
- onAuthSuccess === null || onAuthSuccess === void 0 ? void 0 : onAuthSuccess();
12032
- return;
12038
+ return featuredMentors[0];
12033
12039
  }
12034
12040
  // If no featured mentors, get non-featured mentors
12035
12041
  console.log("no featured mentors found, fetching non-featured mentors", {
@@ -12043,24 +12049,18 @@ function MentorProvider({ children, fallback, onAuthSuccess, onAuthFailure, redi
12043
12049
  limit: QUERY_LIMIT,
12044
12050
  orderBy: "recently_accessed_at",
12045
12051
  });
12046
- const nonFeaturedMentors = ((_j = nonFeaturedMentorsResult.data) === null || _j === void 0 ? void 0 : _j.results) || [];
12052
+ const nonFeaturedMentors = ((_g = nonFeaturedMentorsResult.data) === null || _g === void 0 ? void 0 : _g.results) || [];
12047
12053
  console.log("non-featured mentors fetched", {
12048
12054
  count: nonFeaturedMentors.length,
12049
12055
  mentorIds: nonFeaturedMentors.map((m) => m === null || m === void 0 ? void 0 : m.unique_id),
12050
12056
  });
12051
12057
  // Check if we found non-featured mentors
12052
12058
  if (nonFeaturedMentors.length > 0) {
12053
- const firstNonFeaturedMentorId = ((_k = nonFeaturedMentors[0]) === null || _k === void 0 ? void 0 : _k.unique_id) || "";
12054
- console.log("redirecting to first non-featured mentor", {
12055
- mentorId: firstNonFeaturedMentorId,
12056
- mentorName: (_l = nonFeaturedMentors[0]) === null || _l === void 0 ? void 0 : _l.name,
12059
+ console.log("found first non-featured mentor", {
12060
+ mentorId: (_h = nonFeaturedMentors[0]) === null || _h === void 0 ? void 0 : _h.unique_id,
12061
+ mentorName: (_j = nonFeaturedMentors[0]) === null || _j === void 0 ? void 0 : _j.name,
12057
12062
  });
12058
- const firstNonFeaturedMentorDbId = ((_m = nonFeaturedMentors[0]) === null || _m === void 0 ? void 0 : _m.id) || "";
12059
- const rbacPermissions = await loadMentorsPermissions(firstNonFeaturedMentorDbId);
12060
- onLoadMentorsPermissions === null || onLoadMentorsPermissions === void 0 ? void 0 : onLoadMentorsPermissions(rbacPermissions);
12061
- redirectToMentor(tenantKey, firstNonFeaturedMentorId);
12062
- onAuthSuccess === null || onAuthSuccess === void 0 ? void 0 : onAuthSuccess();
12063
- return;
12063
+ return nonFeaturedMentors[0];
12064
12064
  }
12065
12065
  // If no mentors found, check if user is admin
12066
12066
  console.log("no mentors found, checking admin status", {
@@ -12080,7 +12080,7 @@ function MentorProvider({ children, fallback, onAuthSuccess, onAuthFailure, redi
12080
12080
  // @ts-ignore
12081
12081
  userId: username,
12082
12082
  });
12083
- const seededMentorsDetails = (_o = seedMentorsResult.data) === null || _o === void 0 ? void 0 : _o.detail;
12083
+ const seededMentorsDetails = (_k = seedMentorsResult.data) === null || _k === void 0 ? void 0 : _k.detail;
12084
12084
  console.log("mentor seeding completed", {
12085
12085
  success: !!seededMentorsDetails,
12086
12086
  details: seededMentorsDetails,
@@ -12095,29 +12095,23 @@ function MentorProvider({ children, fallback, onAuthSuccess, onAuthFailure, redi
12095
12095
  username,
12096
12096
  limit: QUERY_LIMIT,
12097
12097
  });
12098
- const featuredMentorsAfterSeed = ((_p = featuredMentorsAfterSeedResult.data) === null || _p === void 0 ? void 0 : _p.results) || [];
12098
+ const featuredMentorsAfterSeed = ((_l = featuredMentorsAfterSeedResult.data) === null || _l === void 0 ? void 0 : _l.results) || [];
12099
12099
  console.log("featured mentors after seeding", {
12100
12100
  count: featuredMentorsAfterSeed.length,
12101
12101
  mentorIds: featuredMentorsAfterSeed.map((m) => m === null || m === void 0 ? void 0 : m.unique_id),
12102
12102
  });
12103
12103
  if (featuredMentorsAfterSeed.length > 0) {
12104
- const seededMentorId = ((_q = featuredMentorsAfterSeed[0]) === null || _q === void 0 ? void 0 : _q.unique_id) || "";
12105
- console.log("redirecting to seeded mentor", {
12106
- mentorId: seededMentorId,
12107
- mentorName: (_r = featuredMentorsAfterSeed[0]) === null || _r === void 0 ? void 0 : _r.name,
12104
+ console.log("found seeded mentor", {
12105
+ mentorId: (_m = featuredMentorsAfterSeed[0]) === null || _m === void 0 ? void 0 : _m.unique_id,
12106
+ mentorName: (_o = featuredMentorsAfterSeed[0]) === null || _o === void 0 ? void 0 : _o.name,
12108
12107
  });
12109
- const seededMentorDbId = ((_s = featuredMentorsAfterSeed[0]) === null || _s === void 0 ? void 0 : _s.id) || "";
12110
- const rbacPermissions = await loadMentorsPermissions(seededMentorDbId);
12111
- onLoadMentorsPermissions === null || onLoadMentorsPermissions === void 0 ? void 0 : onLoadMentorsPermissions(rbacPermissions);
12112
- redirectToMentor(tenantKey, seededMentorId);
12113
- onAuthSuccess === null || onAuthSuccess === void 0 ? void 0 : onAuthSuccess();
12114
- return;
12108
+ return featuredMentorsAfterSeed[0];
12115
12109
  }
12116
12110
  else {
12117
12111
  console.log("no seeded mentors found, redirecting to create mentor");
12118
12112
  redirectToCreateMentor();
12119
12113
  onAuthSuccess === null || onAuthSuccess === void 0 ? void 0 : onAuthSuccess();
12120
- return;
12114
+ return undefined;
12121
12115
  }
12122
12116
  }
12123
12117
  else {
@@ -12125,7 +12119,7 @@ function MentorProvider({ children, fallback, onAuthSuccess, onAuthFailure, redi
12125
12119
  // Prompt the user to create a mentor
12126
12120
  redirectToCreateMentor();
12127
12121
  onAuthSuccess === null || onAuthSuccess === void 0 ? void 0 : onAuthSuccess();
12128
- return;
12122
+ return undefined;
12129
12123
  }
12130
12124
  }
12131
12125
  else {
@@ -12136,7 +12130,7 @@ function MentorProvider({ children, fallback, onAuthSuccess, onAuthFailure, redi
12136
12130
  // Redirect to no mentors page for non-admin users
12137
12131
  redirectToNoMentorsPage();
12138
12132
  onAuthSuccess === null || onAuthSuccess === void 0 ? void 0 : onAuthSuccess();
12139
- return;
12133
+ return undefined;
12140
12134
  }
12141
12135
  }
12142
12136
  catch (error) {
@@ -12152,9 +12146,11 @@ function MentorProvider({ children, fallback, onAuthSuccess, onAuthFailure, redi
12152
12146
  error: errorMessage,
12153
12147
  });
12154
12148
  redirectToAuthSpa();
12149
+ return undefined;
12155
12150
  }
12156
12151
  finally {
12157
12152
  setIsLoading(false);
12153
+ onComplete === null || onComplete === void 0 ? void 0 : onComplete();
12158
12154
  }
12159
12155
  }
12160
12156
  async function mentorExistsInTenant(tenantKey, requestedMentorId) {
@@ -12190,6 +12186,7 @@ function MentorProvider({ children, fallback, onAuthSuccess, onAuthFailure, redi
12190
12186
  console.log("starting mentor check process", determineUserPath, requestedMentorId, tenantKey);
12191
12187
  if (userIsAccessingPublicRoute && !requestedMentorId) {
12192
12188
  setIsLoading(false);
12189
+ onComplete === null || onComplete === void 0 ? void 0 : onComplete();
12193
12190
  return;
12194
12191
  }
12195
12192
  setIsLoading(true);
@@ -12197,13 +12194,25 @@ function MentorProvider({ children, fallback, onAuthSuccess, onAuthFailure, redi
12197
12194
  // then this mentor provider will redirect the user to the correct mentor.
12198
12195
  if (determineUserPath || forceDetermineMentor) {
12199
12196
  console.log("determine user path is true, starting mentor determination");
12200
- await determineMentorToRedirectTo();
12197
+ const mentor = await determineMentorToRedirectTo();
12198
+ if (mentor === null || mentor === void 0 ? void 0 : mentor.unique_id) {
12199
+ // Get mentor DB ID - use existing ID or fetch from public settings
12200
+ let mentorDbId = mentor === null || mentor === void 0 ? void 0 : mentor.id;
12201
+ if (!mentorDbId) {
12202
+ mentorDbId = await getMentorDbId(mentor);
12203
+ }
12204
+ // Load permissions if we have a mentor DB ID
12205
+ if (mentorDbId) {
12206
+ const rbacPermissions = await loadMentorsPermissions(mentorDbId);
12207
+ onLoadMentorsPermissions === null || onLoadMentorsPermissions === void 0 ? void 0 : onLoadMentorsPermissions(rbacPermissions);
12208
+ }
12209
+ // Redirect to the mentor
12210
+ redirectToMentor(tenantKey, mentor.unique_id);
12211
+ onAuthSuccess === null || onAuthSuccess === void 0 ? void 0 : onAuthSuccess();
12212
+ }
12201
12213
  return;
12202
12214
  }
12203
12215
  else if (requestedMentorId) {
12204
- console.log(" ", {
12205
- requestedMentorId,
12206
- });
12207
12216
  const [mentorExists, requestedMentorDbId] = await mentorExistsInTenant(tenantKey, requestedMentorId);
12208
12217
  // If the mentor does not exist in the tenant, redirect the user to the correct mentor
12209
12218
  if (!mentorExists) {
@@ -12218,7 +12227,22 @@ function MentorProvider({ children, fallback, onAuthSuccess, onAuthFailure, redi
12218
12227
  }
12219
12228
  else {
12220
12229
  console.log("using default mentor determination fallback");
12221
- await determineMentorToRedirectTo();
12230
+ const mentor = await determineMentorToRedirectTo();
12231
+ if (mentor === null || mentor === void 0 ? void 0 : mentor.unique_id) {
12232
+ // Get mentor DB ID - use existing ID or fetch from public settings
12233
+ let mentorDbId = mentor === null || mentor === void 0 ? void 0 : mentor.id;
12234
+ if (!mentorDbId) {
12235
+ mentorDbId = await getMentorDbId(mentor);
12236
+ }
12237
+ // Load permissions if we have a mentor DB ID
12238
+ if (mentorDbId) {
12239
+ const rbacPermissions = await loadMentorsPermissions(mentorDbId);
12240
+ onLoadMentorsPermissions === null || onLoadMentorsPermissions === void 0 ? void 0 : onLoadMentorsPermissions(rbacPermissions);
12241
+ }
12242
+ // Redirect to the mentor
12243
+ redirectToMentor(tenantKey, mentor.unique_id);
12244
+ onAuthSuccess === null || onAuthSuccess === void 0 ? void 0 : onAuthSuccess();
12245
+ }
12222
12246
  }
12223
12247
  return;
12224
12248
  }
@@ -12229,16 +12253,24 @@ function MentorProvider({ children, fallback, onAuthSuccess, onAuthFailure, redi
12229
12253
  onLoadMentorsPermissions === null || onLoadMentorsPermissions === void 0 ? void 0 : onLoadMentorsPermissions(rbacPermissions);
12230
12254
  }
12231
12255
  setIsLoading(false);
12256
+ onComplete === null || onComplete === void 0 ? void 0 : onComplete();
12232
12257
  }
12233
12258
  // If the user is navigating to a specific mentor, check if it exists in the tenant
12234
12259
  }
12235
12260
  else {
12236
12261
  console.log("no specific mentor requested, loading complete");
12237
12262
  setIsLoading(false);
12263
+ onComplete === null || onComplete === void 0 ? void 0 : onComplete();
12238
12264
  }
12239
12265
  }
12240
12266
  checkMentor();
12241
- }, []);
12267
+ }, [
12268
+ requestedMentorId,
12269
+ determineUserPath,
12270
+ forceDetermineMentor,
12271
+ userIsAccessingPublicRoute,
12272
+ tenantKey,
12273
+ ]);
12242
12274
  // Show fallback component during mentor determination
12243
12275
  if (isLoading) {
12244
12276
  console.log("mentor provider showing fallback component");
@@ -13207,7 +13239,7 @@ const useChat = ({ wsUrl, wsToken, flowConfig, sessionId, stopGenerationWsUrl, e
13207
13239
  await hapticFeedback.impactAsync("medium");
13208
13240
  }
13209
13241
  catch (error) {
13210
- console.warn("Haptic feedback failed:", error);
13242
+ errorHandler === null || errorHandler === void 0 ? void 0 : errorHandler("Haptic feedback failed", error);
13211
13243
  }
13212
13244
  }
13213
13245
  }, [enableHaptics, hapticFeedback]);
@@ -13254,6 +13286,7 @@ const useChat = ({ wsUrl, wsToken, flowConfig, sessionId, stopGenerationWsUrl, e
13254
13286
  onStreamingChange === null || onStreamingChange === void 0 ? void 0 : onStreamingChange(false);
13255
13287
  });
13256
13288
  socket.addEventListener("message", (event) => {
13289
+ var _a, _b, _c;
13257
13290
  if (isWebSocketPaused.current)
13258
13291
  return;
13259
13292
  try {
@@ -13303,14 +13336,14 @@ const useChat = ({ wsUrl, wsToken, flowConfig, sessionId, stopGenerationWsUrl, e
13303
13336
  if (messageData.data !== undefined || messageData.eos !== undefined) {
13304
13337
  const messageText = messageData.data || "";
13305
13338
  // If we have content to add
13306
- if (messageText) {
13339
+ if (messageText && currentStreamingMessage.current) {
13307
13340
  currentStreamingMessage.current = {
13308
13341
  ...currentStreamingMessage.current,
13309
13342
  content: currentStreamingMessage.current.content + messageText,
13310
13343
  };
13311
13344
  onStreamingMessageUpdate === null || onStreamingMessageUpdate === void 0 ? void 0 : onStreamingMessageUpdate(currentStreamingMessage.current);
13312
13345
  // Update or add message in Redux store
13313
- if (currentStreamingMessage.current.id) {
13346
+ if ((_a = currentStreamingMessage.current) === null || _a === void 0 ? void 0 : _a.id) {
13314
13347
  dispatch(chatActions.appendMessageToActiveTab({
13315
13348
  id: currentStreamingMessage.current.id,
13316
13349
  content: currentStreamingMessage.current.content,
@@ -13320,8 +13353,8 @@ const useChat = ({ wsUrl, wsToken, flowConfig, sessionId, stopGenerationWsUrl, e
13320
13353
  // Handle end of stream
13321
13354
  if (messageData.eos) {
13322
13355
  // Final update to Redux store with complete message
13323
- if (currentStreamingMessage.current.id &&
13324
- currentStreamingMessage.current.content) {
13356
+ if (((_b = currentStreamingMessage.current) === null || _b === void 0 ? void 0 : _b.id) &&
13357
+ ((_c = currentStreamingMessage.current) === null || _c === void 0 ? void 0 : _c.content)) {
13325
13358
  dispatch(chatActions.appendMessageToActiveTab({
13326
13359
  id: currentStreamingMessage.current.id,
13327
13360
  content: currentStreamingMessage.current.content,
@@ -13336,13 +13369,13 @@ const useChat = ({ wsUrl, wsToken, flowConfig, sessionId, stopGenerationWsUrl, e
13336
13369
  }
13337
13370
  }
13338
13371
  catch (error) {
13339
- console.error("Error processing message:", error);
13372
+ errorHandler === null || errorHandler === void 0 ? void 0 : errorHandler("Error processing message", error);
13340
13373
  onStreamingChange === null || onStreamingChange === void 0 ? void 0 : onStreamingChange(false);
13341
13374
  onStatusChange("error");
13342
13375
  }
13343
13376
  });
13344
13377
  socket.addEventListener("error", (error) => {
13345
- console.error("WebSocket error:", error);
13378
+ errorHandler === null || errorHandler === void 0 ? void 0 : errorHandler("WebSocket error", error);
13346
13379
  onStreamingChange === null || onStreamingChange === void 0 ? void 0 : onStreamingChange(false);
13347
13380
  onStatusChange("error");
13348
13381
  });
@@ -13493,8 +13526,12 @@ const useChat = ({ wsUrl, wsToken, flowConfig, sessionId, stopGenerationWsUrl, e
13493
13526
  }
13494
13527
  };
13495
13528
  const _initiateServerStopGeneration = () => {
13496
- var _a;
13497
- (_a = stopGenerationSocket === null || stopGenerationSocket === void 0 ? void 0 : stopGenerationSocket.current) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify({
13529
+ var _a, _b;
13530
+ if (!((_a = currentStreamingMessage.current) === null || _a === void 0 ? void 0 : _a.id)) {
13531
+ console.warn("Cannot stop generation: no active generation ID");
13532
+ return;
13533
+ }
13534
+ (_b = stopGenerationSocket === null || stopGenerationSocket === void 0 ? void 0 : stopGenerationSocket.current) === null || _b === void 0 ? void 0 : _b.send(JSON.stringify({
13498
13535
  generation_id: currentStreamingMessage.current.id,
13499
13536
  name: flowConfig.name,
13500
13537
  tenant: flowConfig.tenant,
@@ -13554,7 +13591,7 @@ const useChat = ({ wsUrl, wsToken, flowConfig, sessionId, stopGenerationWsUrl, e
13554
13591
  };
13555
13592
  };
13556
13593
 
13557
- function useMentorSettings({ mentorId, tenantKey, username }) {
13594
+ function useMentorSettings({ mentorId, tenantKey, username, }) {
13558
13595
  var _a, _b, _c, _d;
13559
13596
  const isLoggedIn = username !== ANONYMOUS_USERNAME;
13560
13597
  const { data: mentorSettings } = useGetMentorSettingsQuery({
@@ -13590,6 +13627,9 @@ function useMentorSettings({ mentorId, tenantKey, username }) {
13590
13627
  mentorUniqueId: isLoggedIn
13591
13628
  ? mentorSettings === null || mentorSettings === void 0 ? void 0 : mentorSettings.mentor_unique_id
13592
13629
  : mentorPublicSettings === null || mentorPublicSettings === void 0 ? void 0 : mentorPublicSettings.mentor_unique_id,
13630
+ mentorVisibility: isLoggedIn
13631
+ ? mentorSettings === null || mentorSettings === void 0 ? void 0 : mentorSettings.mentor_visibility
13632
+ : mentorPublicSettings === null || mentorPublicSettings === void 0 ? void 0 : mentorPublicSettings.mentor_visibility,
13593
13633
  mentorName: (_a = mentorSettings === null || mentorSettings === void 0 ? void 0 : mentorSettings.mentor) !== null && _a !== void 0 ? _a : mentorPublicSettings === null || mentorPublicSettings === void 0 ? void 0 : mentorPublicSettings.mentor,
13594
13634
  enableGuidedPrompts: isLoggedIn
13595
13635
  ? mentorSettings === null || mentorSettings === void 0 ? void 0 : mentorSettings.enable_guided_prompts
@@ -13609,7 +13649,7 @@ function useMentorSettings({ mentorId, tenantKey, username }) {
13609
13649
  }
13610
13650
 
13611
13651
  function useAdvancedChat({ tenantKey, mentorId, username = ANONYMOUS_USERNAME, token, wsUrl, stopGenerationWsUrl, redirectToAuthSpa, errorHandler, sendMessageToParentWebsite, isPreviewMode, mentorShareableToken, on402Error, cachedSessionId, onStartNewChat, }) {
13612
- var _a, _b, _c, _d;
13652
+ var _a, _b, _c, _d, _e;
13613
13653
  const dispatch = useDispatch();
13614
13654
  const [createSessionId, { isLoading: isLoadingSessionIds }] = useCreateSessionIdMutation();
13615
13655
  const chats = useSelector(selectChats);
@@ -13647,7 +13687,7 @@ function useAdvancedChat({ tenantKey, mentorId, username = ANONYMOUS_USERNAME, t
13647
13687
  username,
13648
13688
  pathway: mentorId,
13649
13689
  },
13650
- sessionId: cachedSessionId !== null && cachedSessionId !== void 0 ? cachedSessionId : sessionIds[activeTab],
13690
+ sessionId: (_a = cachedSessionId === null || cachedSessionId === void 0 ? void 0 : cachedSessionId[mentorId]) !== null && _a !== void 0 ? _a : sessionIds[activeTab],
13651
13691
  activeTab,
13652
13692
  wsToken: token,
13653
13693
  errorHandler,
@@ -13661,17 +13701,17 @@ function useAdvancedChat({ tenantKey, mentorId, username = ANONYMOUS_USERNAME, t
13661
13701
  const [getSessionChats, { isError: isErrorGettingSessionChats }] = useLazyGetSessionIdQuery();
13662
13702
  useEffect(() => {
13663
13703
  const getChats = async () => {
13664
- var _a;
13704
+ var _a, _b;
13665
13705
  try {
13666
13706
  const { data } = await getSessionChats({
13667
- sessionId: cachedSessionId !== null && cachedSessionId !== void 0 ? cachedSessionId : "",
13707
+ sessionId: (_a = cachedSessionId === null || cachedSessionId === void 0 ? void 0 : cachedSessionId[mentorId]) !== null && _a !== void 0 ? _a : "",
13668
13708
  org: tenantKey,
13669
13709
  share: true,
13670
13710
  });
13671
13711
  let previousChats = [];
13672
13712
  try {
13673
13713
  previousChats =
13674
- ((_a = data === null || data === void 0 ? void 0 : data.results) === null || _a === void 0 ? void 0 : _a.map((result) => {
13714
+ ((_b = data === null || data === void 0 ? void 0 : data.results) === null || _b === void 0 ? void 0 : _b.map((result) => {
13675
13715
  return {
13676
13716
  ...result,
13677
13717
  role: result.type === "human" ? "user" : "assistant",
@@ -13687,14 +13727,21 @@ function useAdvancedChat({ tenantKey, mentorId, username = ANONYMOUS_USERNAME, t
13687
13727
  catch (error) {
13688
13728
  console.error(JSON.stringify(error));
13689
13729
  }
13730
+ // Ensures that the first AI response due to a Proactive prompt
13731
+ // doesn't show up as part of the chats
13732
+ if ((data === null || data === void 0 ? void 0 : data.proactive_prompt) &&
13733
+ previousChats.length > 0 &&
13734
+ previousChats[0].type === "ai") {
13735
+ previousChats.splice(0, 1);
13736
+ }
13690
13737
  dispatch(chatActions.setNewMessages(previousChats));
13691
13738
  }
13692
13739
  catch (error) {
13693
13740
  console.error(JSON.stringify(error));
13694
13741
  }
13695
13742
  };
13696
- if (cachedSessionId) {
13697
- dispatch(chatActions.updateSessionIds(cachedSessionId));
13743
+ if (cachedSessionId === null || cachedSessionId === void 0 ? void 0 : cachedSessionId[mentorId]) {
13744
+ dispatch(chatActions.updateSessionIds(cachedSessionId === null || cachedSessionId === void 0 ? void 0 : cachedSessionId[mentorId]));
13698
13745
  getChats();
13699
13746
  }
13700
13747
  }, [cachedSessionId]);
@@ -13732,9 +13779,12 @@ function useAdvancedChat({ tenantKey, mentorId, username = ANONYMOUS_USERNAME, t
13732
13779
  onStartNewChat === null || onStartNewChat === void 0 ? void 0 : onStartNewChat(response.session_id);
13733
13780
  }
13734
13781
  catch (error) {
13735
- errorHandler === null || errorHandler === void 0 ? void 0 : errorHandler(`Failed to start new chat: ${JSON.stringify(error)}`);
13736
- console.log("[auth-redirect] Failed to start new chat", JSON.stringify({ error }));
13737
- redirectToAuthSpa(undefined, undefined, true);
13782
+ if (mentorSettings.mentorVisibility !==
13783
+ MentorVisibilityEnum.VIEWABLE_BY_ANYONE) {
13784
+ errorHandler === null || errorHandler === void 0 ? void 0 : errorHandler(`Failed to start new chat: ${JSON.stringify(error)}`);
13785
+ console.log("[auth-redirect] Failed to start new chat", JSON.stringify({ error }));
13786
+ redirectToAuthSpa(undefined, undefined, true);
13787
+ }
13738
13788
  }
13739
13789
  }, [
13740
13790
  isPreviewMode,
@@ -13748,7 +13798,8 @@ function useAdvancedChat({ tenantKey, mentorId, username = ANONYMOUS_USERNAME, t
13748
13798
  ]);
13749
13799
  React__default.useEffect(() => {
13750
13800
  if (!showingSharedChat || mentorSettings.allowAnonymous) {
13751
- if (mentorSettings.allowAnonymous !== undefined && !cachedSessionId) {
13801
+ if (mentorSettings.allowAnonymous !== undefined &&
13802
+ !(cachedSessionId === null || cachedSessionId === void 0 ? void 0 : cachedSessionId[mentorId])) {
13752
13803
  startNewChat();
13753
13804
  }
13754
13805
  }
@@ -13820,9 +13871,9 @@ function useAdvancedChat({ tenantKey, mentorId, username = ANONYMOUS_USERNAME, t
13820
13871
  currentStreamingMessage,
13821
13872
  activeTab,
13822
13873
  uniqueMentorId: mentorId,
13823
- mentorName: (_a = mentorSettings === null || mentorSettings === void 0 ? void 0 : mentorSettings.mentorName) !== null && _a !== void 0 ? _a : "",
13824
- profileImage: (_b = mentorSettings === null || mentorSettings === void 0 ? void 0 : mentorSettings.profileImage) !== null && _b !== void 0 ? _b : "",
13825
- enabledGuidedPrompts: (_c = mentorSettings === null || mentorSettings === void 0 ? void 0 : mentorSettings.enableGuidedPrompts) !== null && _c !== void 0 ? _c : true,
13874
+ mentorName: (_b = mentorSettings === null || mentorSettings === void 0 ? void 0 : mentorSettings.mentorName) !== null && _b !== void 0 ? _b : "",
13875
+ profileImage: (_c = mentorSettings === null || mentorSettings === void 0 ? void 0 : mentorSettings.profileImage) !== null && _c !== void 0 ? _c : "",
13876
+ enabledGuidedPrompts: (_d = mentorSettings === null || mentorSettings === void 0 ? void 0 : mentorSettings.enableGuidedPrompts) !== null && _d !== void 0 ? _d : true,
13826
13877
  sendMessage,
13827
13878
  stopGenerating,
13828
13879
  setMessage,
@@ -13834,7 +13885,7 @@ function useAdvancedChat({ tenantKey, mentorId, username = ANONYMOUS_USERNAME, t
13834
13885
  isConnected,
13835
13886
  messageQueue,
13836
13887
  sessionIds,
13837
- enableSafetyDisclaimer: (_d = mentorSettings === null || mentorSettings === void 0 ? void 0 : mentorSettings.safetyDisclaimer) !== null && _d !== void 0 ? _d : false,
13888
+ enableSafetyDisclaimer: (_e = mentorSettings === null || mentorSettings === void 0 ? void 0 : mentorSettings.safetyDisclaimer) !== null && _e !== void 0 ? _e : false,
13838
13889
  resetConnection,
13839
13890
  };
13840
13891
  }