@autobusal/providers 1.43.2 → 1.43.3

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/services.ts +42 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autobusal/providers",
3
- "version": "1.43.2",
3
+ "version": "1.43.3",
4
4
  "author": "Ferjolt Ozuni",
5
5
  "type": "module",
6
6
  "main": "index.ts",
package/services.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { useEffect, useRef } from 'react';
1
2
  import { useSuspenseQuery, UseSuspenseQueryResult } from '@tanstack/react-query';
2
3
  import i18next from 'i18next';
3
4
  import { useTranslation } from 'react-i18next';
@@ -55,16 +56,53 @@ export const useGetMenu = (): UseSuspenseQueryResult<MenuData> => {
55
56
  */
56
57
  const { i18n } = useTranslation();
57
58
 
58
- return useSuspenseQuery({
59
- queryKey: ['get-menu', i18n.resolvedLanguage ?? i18next.language],
59
+ const language = i18n.resolvedLanguage ?? i18next.language;
60
+
61
+ /*
62
+ * Claude - 2026-09-07 (Ferjolt: "when we switch to a language and
63
+ * immediately switch to another one, this does not work").
64
+ *
65
+ * A STABLE KEY, REFETCHED when the language changes - not a key that
66
+ * contains the language.
67
+ *
68
+ * With the language in the key, every switch handed this suspense query a
69
+ * key it had never loaded, and a suspense query with no data SUSPENDS.
70
+ * This one is mounted at the app's root (Setup), so the whole tree fell
71
+ * back to the loading screen - the "refresh" a visitor sees on every
72
+ * switch - and remounted when the labels arrived. Anything open at that
73
+ * moment was gone: measured on production, a language chosen while the
74
+ * previous switch's labels were still loading closed the picker and ran
75
+ * nothing, because the button it was clicked on no longer belonged to a
76
+ * mounted tree.
77
+ *
78
+ * Refetching an already-loaded key never suspends. The store keeps the
79
+ * previous language's labels for the few hundred milliseconds the new ones
80
+ * take, the page stays put, and a second choice lands on a live picker.
81
+ * `resolvedLanguage` for the reason the 2026-08-29 note gives.
82
+ */
83
+ const query = useSuspenseQuery<MenuData>({
84
+ queryKey: ['get-menu'],
60
85
  queryFn: async () => (
61
86
  await apiClient
62
87
  .get('/api/menus/get')
63
88
  .then(response => {
64
89
  menuStore.save(response.data);
65
90
 
66
- return {};
91
+ // the store is the reader; the query's own data is a token (as before)
92
+ return {} as MenuData;
67
93
  })
68
94
  )
69
- })
95
+ });
96
+
97
+ const loaded = useRef<string>(language);
98
+
99
+ useEffect(() => {
100
+ if (loaded.current !== language) {
101
+ loaded.current = language;
102
+
103
+ query.refetch();
104
+ }
105
+ }, [ language, query ]);
106
+
107
+ return query;
70
108
  };