allaw-ui 5.4.7 → 5.4.8

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.
@@ -13,12 +13,15 @@ import AvatarToggleGroup from "./AvatarToggleGroup";
13
13
  import ProSwitchModal from "./ProSwitchModal";
14
14
  import styles from "./proSwitch.module.css";
15
15
  var LOCALSTORAGE_KEY = "allaw.proSwitch.order";
16
+ var LOCALSTORAGE_SELECTED_KEY = "allaw.proSwitch.selected";
16
17
  var ProSwitch = function (_a) {
17
18
  var _b = _a.pros, pros = _b === void 0 ? [] : _b, _c = _a.selected, selected = _c === void 0 ? [] : _c, mode = _a.mode, _d = _a.disabled, disabled = _d === void 0 ? false : _d, maxVisible = _a.maxVisible, onSelect = _a.onSelect, onOrderChange = _a.onOrderChange, onOpenPortal = _a.onOpenPortal, onClosePortal = _a.onClosePortal, _e = _a.className, className = _e === void 0 ? "" : _e, _f = _a.showTooltips, showTooltips = _f === void 0 ? false : _f, _g = _a.tooltipId, tooltipId = _g === void 0 ? "pro-switch-tooltip" : _g;
18
19
  var containerRef = useRef(null);
19
20
  var _h = useState([]), order = _h[0], setOrder = _h[1];
20
21
  var _j = useState(selected), selectedIds = _j[0], setSelectedIds = _j[1];
21
22
  var _k = useState(false), modalOpen = _k[0], setModalOpen = _k[1];
23
+ var selectionHydrationAttemptedRef = useRef(false);
24
+ var didReceiveExternalSelectedRef = useRef(selected.length > 0);
22
25
  // Initialisation de l'ordre depuis localStorage ou props
23
26
  useEffect(function () {
24
27
  var initialOrder = pros.map(function (p) { return p.proUserId; });
@@ -39,10 +42,56 @@ var ProSwitch = function (_a) {
39
42
  }
40
43
  setOrder(initialOrder);
41
44
  }, [pros]);
45
+ // Hydrater la sélection depuis localStorage (une seule fois, quand pros est dispo)
46
+ useEffect(function () {
47
+ if (selectionHydrationAttemptedRef.current)
48
+ return;
49
+ if (!pros || pros.length === 0)
50
+ return;
51
+ if (didReceiveExternalSelectedRef.current) {
52
+ selectionHydrationAttemptedRef.current = true;
53
+ return;
54
+ }
55
+ selectionHydrationAttemptedRef.current = true;
56
+ try {
57
+ var stored = localStorage.getItem(LOCALSTORAGE_SELECTED_KEY);
58
+ if (!stored)
59
+ return;
60
+ var arr = JSON.parse(stored);
61
+ var filtered = arr.filter(function (id) { return pros.some(function (p) { return p.proUserId === id; }); });
62
+ var hydratedIds_1 = mode === "single" ? filtered.slice(0, 1) : filtered;
63
+ if (hydratedIds_1.length === 0)
64
+ return;
65
+ setSelectedIds(hydratedIds_1);
66
+ if (onSelect) {
67
+ var hydratedPros = pros.filter(function (p) {
68
+ return hydratedIds_1.includes(p.proUserId);
69
+ });
70
+ onSelect(hydratedPros);
71
+ }
72
+ }
73
+ catch (_a) {
74
+ // ignore
75
+ }
76
+ }, [pros, mode, onSelect]);
42
77
  // Sync selectedIds si props.selected change
43
78
  useEffect(function () {
79
+ if (!didReceiveExternalSelectedRef.current) {
80
+ if (selected.length === 0)
81
+ return;
82
+ didReceiveExternalSelectedRef.current = true;
83
+ }
44
84
  setSelectedIds(selected);
45
85
  }, [selected]);
86
+ // Persister la sélection (après tentative d'hydratation) pour éviter d'écraser le storage au 1er render
87
+ useEffect(function () {
88
+ if (!selectionHydrationAttemptedRef.current)
89
+ return;
90
+ try {
91
+ localStorage.setItem(LOCALSTORAGE_SELECTED_KEY, JSON.stringify(selectedIds));
92
+ }
93
+ catch (_a) { }
94
+ }, [selectedIds]);
46
95
  // Gestion de la sélection
47
96
  var handleSelect = useCallback(function (selectedPros) {
48
97
  var newSelectedIds = selectedPros.map(function (p) { return p.proUserId; });
@@ -54,6 +103,10 @@ var ProSwitch = function (_a) {
54
103
  return;
55
104
  }
56
105
  setSelectedIds(newSelectedIds);
106
+ try {
107
+ localStorage.setItem(LOCALSTORAGE_SELECTED_KEY, JSON.stringify(newSelectedIds));
108
+ }
109
+ catch (_a) { }
57
110
  if (onSelect)
58
111
  onSelect(selectedPros);
59
112
  }, [mode, selectedIds, onSelect]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "allaw-ui",
3
- "version": "5.4.7",
3
+ "version": "5.4.8",
4
4
  "description": "Composants UI pour l'application Allaw",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",