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.
This commit is contained in:
Prakhar Pratyush 2024-06-03 12:41:07 +05:30 committed by Tim Abbott
parent 915503df10
commit 32391c3d06
5 changed files with 28 additions and 26 deletions

View File

@ -1,7 +1,21 @@
import {z} from "zod";
import * as blueslip from "./blueslip"; import * as blueslip from "./blueslip";
import * as channel from "./channel"; 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<typeof onboarding_step_schema>;
export const ONE_TIME_NOTICES_TO_DISPLAY = new Set<string>(); export const ONE_TIME_NOTICES_TO_DISPLAY = new Set<string>();
@ -31,6 +45,6 @@ export function update_onboarding_steps_to_display(onboarding_steps: OnboardingS
} }
} }
export function initialize(): void { export function initialize(params: {onboarding_steps: OnboardingStep[]}): void {
update_onboarding_steps_to_display(current_user.onboarding_steps); update_onboarding_steps_to_display(params.onboarding_steps);
} }

View File

@ -147,9 +147,6 @@ export function dispatch_normal_event(event) {
case "onboarding_steps": case "onboarding_steps":
onboarding_steps.update_onboarding_steps_to_display(event.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; break;
case "invites_changed": case "invites_changed":

View File

@ -21,20 +21,6 @@ export const narrow_term_schema = z.object({
export type NarrowTerm = z.output<typeof narrow_term_schema>; export type NarrowTerm = z.output<typeof narrow_term_schema>;
// Sync this with zerver.lib.events.do_events_register. // 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<typeof onboarding_step_schema>;
export const custom_profile_field_schema = z.object({ export const custom_profile_field_schema = z.object({
display_in_profile_summary: z.optional(z.boolean()), display_in_profile_summary: z.optional(z.boolean()),
field_data: z.string(), field_data: z.string(),
@ -57,7 +43,6 @@ export const current_user_schema = z.object({
is_guest: z.boolean(), is_guest: z.boolean(),
is_moderator: z.boolean(), is_moderator: z.boolean(),
is_owner: z.boolean(), is_owner: z.boolean(),
onboarding_steps: z.array(onboarding_step_schema),
user_id: z.number(), user_id: z.number(),
}); });
// Sync this with zerver.lib.events.do_events_register. // Sync this with zerver.lib.events.do_events_register.

View File

@ -510,6 +510,8 @@ export function initialize_everything(state_data) {
); );
const local_message_params = pop_fields("max_message_id"); const local_message_params = pop_fields("max_message_id");
const onboarding_steps_params = pop_fields("onboarding_steps");
const current_user_params = pop_fields( const current_user_params = pop_fields(
"avatar_source", "avatar_source",
"avatar_url", "avatar_url",
@ -529,7 +531,6 @@ export function initialize_everything(state_data) {
"is_guest", "is_guest",
"is_moderator", "is_moderator",
"is_owner", "is_owner",
"onboarding_steps",
"user_id", "user_id",
); );
@ -880,7 +881,7 @@ export function initialize_everything(state_data) {
}); });
drafts.initialize_ui(); drafts.initialize_ui();
drafts_overlay_ui.initialize(); drafts_overlay_ui.initialize();
onboarding_steps.initialize(); onboarding_steps.initialize(onboarding_steps_params);
typing.initialize(); typing.initialize();
starred_messages_ui.initialize(); starred_messages_ui.initialize();
user_status_ui.initialize(); user_status_ui.initialize();

View File

@ -126,6 +126,7 @@ const emoji = zrequire("emoji");
const message_store = zrequire("message_store"); const message_store = zrequire("message_store");
const people = zrequire("people"); const people = zrequire("people");
const user_status = zrequire("user_status"); const user_status = zrequire("user_status");
const onboarding_steps = zrequire("onboarding_steps");
const server_events_dispatch = zrequire("server_events_dispatch"); const server_events_dispatch = zrequire("server_events_dispatch");
@ -321,10 +322,14 @@ run_test("default_streams", ({override}) => {
}); });
run_test("onboarding_steps", () => { run_test("onboarding_steps", () => {
current_user.onboarding_steps = []; onboarding_steps.initialize({onboarding_steps: []});
const event = event_fixtures.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); 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}) => { run_test("invites_changed", ({override}) => {