From 32391c3d0655a4bc22115f2cecd80ea896c9b831 Mon Sep 17 00:00:00 2001 From: Prakhar Pratyush Date: Mon, 3 Jun 2024 12:41:07 +0530 Subject: [PATCH] ui_init: Don't store onboarding_steps state_data as current_user_param. Earlier, onboarding_steps field of state_data was stored as current_user_params. Now, we store it separately in a data structure initialized in onboarding_steps.ts Reason: All the other state_data fields stored in current_user_params are attributes of UserProfile. So, it makes sense to store it separately. Fixes part of #30043. --- web/src/onboarding_steps.ts | 22 ++++++++++++++++++---- web/src/server_events_dispatch.js | 3 --- web/src/state_data.ts | 15 --------------- web/src/ui_init.js | 5 +++-- web/tests/dispatch.test.js | 9 +++++++-- 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/web/src/onboarding_steps.ts b/web/src/onboarding_steps.ts index 48b2a07520..2727c08e95 100644 --- a/web/src/onboarding_steps.ts +++ b/web/src/onboarding_steps.ts @@ -1,7 +1,21 @@ +import {z} from "zod"; + import * as blueslip from "./blueslip"; import * as channel from "./channel"; -import {current_user} from "./state_data"; -import type {OnboardingStep} from "./state_data"; + +const one_time_notice_schema = z.object({ + name: z.string(), + type: z.literal("one_time_notice"), +}); + +/* We may introduce onboarding step of types other than 'one time notice' +in future. Earlier, we had 'hotspot' and 'one time notice' as the two +types. We can simply do: +const onboarding_step_schema = z.union([one_time_notice_schema, other_type_schema]); +to avoid major refactoring when new type is introduced in the future. */ +const onboarding_step_schema = one_time_notice_schema; + +export type OnboardingStep = z.output; export const ONE_TIME_NOTICES_TO_DISPLAY = new Set(); @@ -31,6 +45,6 @@ export function update_onboarding_steps_to_display(onboarding_steps: OnboardingS } } -export function initialize(): void { - update_onboarding_steps_to_display(current_user.onboarding_steps); +export function initialize(params: {onboarding_steps: OnboardingStep[]}): void { + update_onboarding_steps_to_display(params.onboarding_steps); } diff --git a/web/src/server_events_dispatch.js b/web/src/server_events_dispatch.js index 96d8498dcd..1cd47b5a69 100644 --- a/web/src/server_events_dispatch.js +++ b/web/src/server_events_dispatch.js @@ -147,9 +147,6 @@ export function dispatch_normal_event(event) { case "onboarding_steps": onboarding_steps.update_onboarding_steps_to_display(event.onboarding_steps); - current_user.onboarding_steps = current_user.onboarding_steps - ? [...current_user.onboarding_steps, ...event.onboarding_steps] - : event.onboarding_steps; break; case "invites_changed": diff --git a/web/src/state_data.ts b/web/src/state_data.ts index fa74aef8f0..30d6af343e 100644 --- a/web/src/state_data.ts +++ b/web/src/state_data.ts @@ -21,20 +21,6 @@ export const narrow_term_schema = z.object({ export type NarrowTerm = z.output; // Sync this with zerver.lib.events.do_events_register. -const one_time_notice_schema = z.object({ - name: z.string(), - type: z.literal("one_time_notice"), -}); - -/* We may introduce onboarding step of types other than 'one time notice' -in future. Earlier, we had 'hotspot' and 'one time notice' as the two -types. We can simply do: -const onboarding_step_schema = z.union([one_time_notice_schema, other_type_schema]); -to avoid major refactoring when new type is introduced in the future. */ -const onboarding_step_schema = one_time_notice_schema; - -export type OnboardingStep = z.output; - export const custom_profile_field_schema = z.object({ display_in_profile_summary: z.optional(z.boolean()), field_data: z.string(), @@ -57,7 +43,6 @@ export const current_user_schema = z.object({ is_guest: z.boolean(), is_moderator: z.boolean(), is_owner: z.boolean(), - onboarding_steps: z.array(onboarding_step_schema), user_id: z.number(), }); // Sync this with zerver.lib.events.do_events_register. diff --git a/web/src/ui_init.js b/web/src/ui_init.js index 33959d5813..f724376179 100644 --- a/web/src/ui_init.js +++ b/web/src/ui_init.js @@ -510,6 +510,8 @@ export function initialize_everything(state_data) { ); const local_message_params = pop_fields("max_message_id"); + const onboarding_steps_params = pop_fields("onboarding_steps"); + const current_user_params = pop_fields( "avatar_source", "avatar_url", @@ -529,7 +531,6 @@ export function initialize_everything(state_data) { "is_guest", "is_moderator", "is_owner", - "onboarding_steps", "user_id", ); @@ -880,7 +881,7 @@ export function initialize_everything(state_data) { }); drafts.initialize_ui(); drafts_overlay_ui.initialize(); - onboarding_steps.initialize(); + onboarding_steps.initialize(onboarding_steps_params); typing.initialize(); starred_messages_ui.initialize(); user_status_ui.initialize(); diff --git a/web/tests/dispatch.test.js b/web/tests/dispatch.test.js index 2cb8163c74..a9c569b1be 100644 --- a/web/tests/dispatch.test.js +++ b/web/tests/dispatch.test.js @@ -126,6 +126,7 @@ const emoji = zrequire("emoji"); const message_store = zrequire("message_store"); const people = zrequire("people"); const user_status = zrequire("user_status"); +const onboarding_steps = zrequire("onboarding_steps"); const server_events_dispatch = zrequire("server_events_dispatch"); @@ -321,10 +322,14 @@ run_test("default_streams", ({override}) => { }); run_test("onboarding_steps", () => { - current_user.onboarding_steps = []; + onboarding_steps.initialize({onboarding_steps: []}); const event = event_fixtures.onboarding_steps; + const one_time_notices = new Set(); + for (const onboarding_step of event.onboarding_steps) { + one_time_notices.add(onboarding_step.name); + } dispatch(event); - assert_same(current_user.onboarding_steps, event.onboarding_steps); + assert_same(onboarding_steps.ONE_TIME_NOTICES_TO_DISPLAY, one_time_notices); }); run_test("invites_changed", ({override}) => {