1 import { FetchError } from "@medusajs/js-sdk"
2 import { HttpTypes } from "@medusajs/types"
3 import { toast } from "@medusajs/ui"
4 import { MutateOptions } from "@tanstack/react-query"
5 import { useCallback, useMemo } from "react"
6 import { useTranslation } from "react-i18next"
7 import type { LayoutPreference } from "../components/layout-composer/types"
8 import {
9 useLayoutConfiguration,
10 useSetLayoutConfiguration,
11 } from "./api/layouts"
12
13 const EMPTY_PREFERENCE: LayoutPreference = { widgets: {} }
14
15 export type LayoutScope = "personal" | "default"
16
17 export type SetPreferenceOptions = {
18 /**
19 * Persist as the zone's system default (applies to all users) instead of the
20 * current user's personal configuration.
21 */
22 asDefault?: boolean
23 }
24
25 /**
26 * Per-call react-query mutate options for `setPreference`, used to run
27 * caller-specific side effects (e.g. exiting edit mode) once the save settles.
28 * Generic toasts are already handled inside the hook.
29 */
30 export type SetPreferenceMutateOptions = MutateOptions<
31 HttpTypes.AdminLayoutConfigurationResponse,
32 FetchError,
33 HttpTypes.AdminSetLayoutConfiguration
34 >
35
36 export type UseLayoutPreferenceReturn = {
37 /** The current user's personal configuration, seeded from the default when unset. */
38 personalPreference: LayoutPreference
39 /** The zone's system default configuration. */
40 defaultPreference: LayoutPreference
41 /** The persisted scope the user is actively viewing for this zone. */
42 activeScope: LayoutScope
43 /**
44 * Which scope actually has a saved configuration for this zone, or `null`
45 * when neither the user nor the system has defined one. Personal takes
46 * precedence since it overrides the system default for the current user.
47 */
48 definedScope: LayoutScope | null
49 setPreference: (
50 next: LayoutPreference,
51 options?: SetPreferenceOptions,
52 mutateOptions?: SetPreferenceMutateOptions
53 ) => void
54 isSaving: boolean
55 }
56
57 function toPreference(
58 configuration?: { widgets?: LayoutPreference["widgets"] } | null
59 ): LayoutPreference | null {
60 const widgets = configuration?.widgets
61 return widgets ? { widgets } : null
62 }
63
64 export function useLayoutPreference(zone: string): UseLayoutPreferenceReturn {
65 const { t } = useTranslation()
66
67 const { personal_configuration, default_configuration, active_scope } =
68 useLayoutConfiguration(zone)
69
70 const { mutate: setLayoutConfiguration, isPending: isSaving } =
71 useSetLayoutConfiguration(zone, {
72 onSuccess: () => toast.success(t("layout.saveSuccess")),
73 onError: (error) => toast.error(error.message),
74 })
75
76 const defaultPreference = useMemo(
77 () =>
78 toPreference(default_configuration?.configuration) ?? EMPTY_PREFERENCE,
79 [default_configuration]
80 )
81
82 const personalPreference = useMemo(
83 () =>
84 toPreference(personal_configuration?.configuration) ?? defaultPreference,
85 [personal_configuration, defaultPreference]
86 )
87
88 const activeScope: LayoutScope = active_scope ?? "personal"
89
90 // A configuration record only exists once a user or admin has saved one, so
91 // its presence is the signal that a layout has been deliberately defined.
92 // Personal wins over default since it's what the current user actually sees.
93 const definedScope: LayoutScope | null = personal_configuration
94 ? "personal"
95 : default_configuration
96 ? "default"
97 : null
98
99 const setPreference = useCallback(
100 (
101 next: LayoutPreference,
102 options?: SetPreferenceOptions,
103 mutateOptions?: SetPreferenceMutateOptions
104 ) => {
105 setLayoutConfiguration(
106 {
107 is_default: options?.asDefault ?? false,
108 configuration: { widgets: next.widgets },
109 },
110 mutateOptions
111 )
112 },
113 [setLayoutConfiguration]
114 )
115
116 return {
117 personalPreference,
118 defaultPreference,
119 activeScope,
120 definedScope,
121 setPreference,
122 isSaving,
123 }
124 }