@djangocfg/layouts 2.1.36 → 2.1.37

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 (40) hide show
  1. package/package.json +5 -5
  2. package/src/layouts/AppLayout/BaseApp.tsx +31 -25
  3. package/src/layouts/shared/types.ts +36 -0
  4. package/src/snippets/McpChat/context/ChatContext.tsx +9 -0
  5. package/src/snippets/PWA/@docs/research.md +576 -0
  6. package/src/snippets/PWA/@refactoring/ARCHITECTURE_ANALYSIS.md +1179 -0
  7. package/src/snippets/PWA/@refactoring/EXECUTIVE_SUMMARY.md +271 -0
  8. package/src/snippets/PWA/@refactoring/README.md +204 -0
  9. package/src/snippets/PWA/@refactoring/REFACTORING_PROPOSALS.md +1109 -0
  10. package/src/snippets/PWA/@refactoring2/COMPARISON-WITH-NEXTJS.md +718 -0
  11. package/src/snippets/PWA/@refactoring2/P1-FIXES-COMPLETED.md +188 -0
  12. package/src/snippets/PWA/@refactoring2/POST-P0-ANALYSIS.md +362 -0
  13. package/src/snippets/PWA/@refactoring2/README.md +85 -0
  14. package/src/snippets/PWA/@refactoring2/RECOMMENDATIONS.md +1321 -0
  15. package/src/snippets/PWA/@refactoring2/REMAINING-ISSUES.md +557 -0
  16. package/src/snippets/PWA/README.md +387 -0
  17. package/src/snippets/PWA/components/A2HSHint.tsx +226 -0
  18. package/src/snippets/PWA/components/IOSGuide.tsx +29 -0
  19. package/src/snippets/PWA/components/IOSGuideDrawer.tsx +101 -0
  20. package/src/snippets/PWA/components/IOSGuideModal.tsx +101 -0
  21. package/src/snippets/PWA/components/PushPrompt.tsx +165 -0
  22. package/src/snippets/PWA/config.ts +20 -0
  23. package/src/snippets/PWA/context/DjangoPushContext.tsx +105 -0
  24. package/src/snippets/PWA/context/InstallContext.tsx +118 -0
  25. package/src/snippets/PWA/context/PushContext.tsx +156 -0
  26. package/src/snippets/PWA/hooks/useDjangoPush.ts +277 -0
  27. package/src/snippets/PWA/hooks/useInstallPrompt.ts +164 -0
  28. package/src/snippets/PWA/hooks/useIsPWA.ts +115 -0
  29. package/src/snippets/PWA/hooks/usePushNotifications.ts +205 -0
  30. package/src/snippets/PWA/index.ts +95 -0
  31. package/src/snippets/PWA/types/components.ts +101 -0
  32. package/src/snippets/PWA/types/index.ts +26 -0
  33. package/src/snippets/PWA/types/install.ts +38 -0
  34. package/src/snippets/PWA/types/platform.ts +29 -0
  35. package/src/snippets/PWA/types/push.ts +21 -0
  36. package/src/snippets/PWA/utils/localStorage.ts +203 -0
  37. package/src/snippets/PWA/utils/logger.ts +149 -0
  38. package/src/snippets/PWA/utils/platform.ts +151 -0
  39. package/src/snippets/PWA/utils/vapid.ts +226 -0
  40. package/src/snippets/index.ts +30 -0
@@ -0,0 +1,203 @@
1
+ /**
2
+ * LocalStorage utilities for PWA install state persistence
3
+ */
4
+
5
+ import type { IOSGuideState } from '../types';
6
+
7
+ /**
8
+ * Storage keys
9
+ */
10
+ export const STORAGE_KEYS = {
11
+ IOS_GUIDE_DISMISSED: 'pwa_ios_guide_dismissed_at',
12
+ APP_INSTALLED: 'pwa_app_installed',
13
+ A2HS_DISMISSED: 'pwa_a2hs_dismissed_at',
14
+ PUSH_DISMISSED: 'pwa_push_dismissed_at',
15
+ } as const;
16
+
17
+ /**
18
+ * Check if iOS guide was dismissed recently
19
+ * @param resetDays Number of days before re-showing (default: 7)
20
+ */
21
+ export function isDismissedRecently(resetDays: number = 7): boolean {
22
+ if (typeof window === 'undefined') return false;
23
+
24
+ try {
25
+ const dismissed = localStorage.getItem(STORAGE_KEYS.IOS_GUIDE_DISMISSED);
26
+ if (!dismissed) return false;
27
+
28
+ const dismissedAt = parseInt(dismissed, 10);
29
+ const now = Date.now();
30
+ const daysSinceDismissed = (now - dismissedAt) / (1000 * 60 * 60 * 24);
31
+
32
+ return daysSinceDismissed < resetDays;
33
+ } catch {
34
+ return false;
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Mark iOS guide as dismissed
40
+ */
41
+ export function markIOSGuideDismissed(): void {
42
+ if (typeof window === 'undefined') return;
43
+
44
+ try {
45
+ localStorage.setItem(STORAGE_KEYS.IOS_GUIDE_DISMISSED, Date.now().toString());
46
+ } catch {
47
+ // Fail silently if localStorage is not available
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Clear iOS guide dismissal
53
+ */
54
+ export function clearIOSGuideDismissal(): void {
55
+ if (typeof window === 'undefined') return;
56
+
57
+ try {
58
+ localStorage.removeItem(STORAGE_KEYS.IOS_GUIDE_DISMISSED);
59
+ } catch {
60
+ // Fail silently
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Get iOS guide state
66
+ */
67
+ export function getIOSGuideState(resetDays: number = 7): IOSGuideState {
68
+ if (typeof window === 'undefined') {
69
+ return {
70
+ dismissed: false,
71
+ dismissedAt: null,
72
+ shouldShow: false,
73
+ };
74
+ }
75
+
76
+ try {
77
+ const dismissed = localStorage.getItem(STORAGE_KEYS.IOS_GUIDE_DISMISSED);
78
+ if (!dismissed) {
79
+ return {
80
+ dismissed: false,
81
+ dismissedAt: null,
82
+ shouldShow: true,
83
+ };
84
+ }
85
+
86
+ const dismissedAt = parseInt(dismissed, 10);
87
+ const now = Date.now();
88
+ const daysSinceDismissed = (now - dismissedAt) / (1000 * 60 * 60 * 24);
89
+
90
+ return {
91
+ dismissed: true,
92
+ dismissedAt,
93
+ shouldShow: daysSinceDismissed >= resetDays,
94
+ };
95
+ } catch {
96
+ return {
97
+ dismissed: false,
98
+ dismissedAt: null,
99
+ shouldShow: false,
100
+ };
101
+ }
102
+ }
103
+
104
+ /**
105
+ * Mark app as installed
106
+ */
107
+ export function markAppInstalled(): void {
108
+ if (typeof window === 'undefined') return;
109
+
110
+ try {
111
+ localStorage.setItem(STORAGE_KEYS.APP_INSTALLED, 'true');
112
+ // Clear dismissal state when app is installed
113
+ clearIOSGuideDismissal();
114
+ } catch {
115
+ // Fail silently
116
+ }
117
+ }
118
+
119
+ /**
120
+ * Check if app is marked as installed
121
+ */
122
+ export function isAppInstalled(): boolean {
123
+ if (typeof window === 'undefined') return false;
124
+
125
+ try {
126
+ return localStorage.getItem(STORAGE_KEYS.APP_INSTALLED) === 'true';
127
+ } catch {
128
+ return false;
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Check if A2HS hint was dismissed recently
134
+ * @param resetDays Number of days before re-showing (default: 3)
135
+ */
136
+ export function isA2HSDismissedRecently(resetDays: number = 3): boolean {
137
+ return isDismissedRecentlyHelper(resetDays, STORAGE_KEYS.A2HS_DISMISSED);
138
+ }
139
+
140
+ /**
141
+ * Mark A2HS hint as dismissed
142
+ */
143
+ export function markA2HSDismissed(): void {
144
+ if (typeof window === 'undefined') return;
145
+ try {
146
+ localStorage.setItem(STORAGE_KEYS.A2HS_DISMISSED, Date.now().toString());
147
+ } catch {
148
+ // Fail silently
149
+ }
150
+ }
151
+
152
+ /**
153
+ * Check if push prompt was dismissed recently
154
+ * @param resetDays Number of days before re-showing (default: 7)
155
+ */
156
+ export function isPushDismissedRecently(resetDays: number = 7): boolean {
157
+ return isDismissedRecentlyHelper(resetDays, STORAGE_KEYS.PUSH_DISMISSED);
158
+ }
159
+
160
+ /**
161
+ * Mark push prompt as dismissed
162
+ */
163
+ export function markPushDismissed(): void {
164
+ if (typeof window === 'undefined') return;
165
+ try {
166
+ localStorage.setItem(STORAGE_KEYS.PUSH_DISMISSED, Date.now().toString());
167
+ } catch {
168
+ // Fail silently
169
+ }
170
+ }
171
+
172
+ /**
173
+ * Helper: Check if a key was dismissed recently
174
+ * @internal
175
+ */
176
+ function isDismissedRecentlyHelper(resetDays: number, key: string): boolean {
177
+ if (typeof window === 'undefined') return false;
178
+ try {
179
+ const dismissed = localStorage.getItem(key);
180
+ if (!dismissed) return false;
181
+ const dismissedAt = parseInt(dismissed, 10);
182
+ const daysSince = (Date.now() - dismissedAt) / (1000 * 60 * 60 * 24);
183
+ return daysSince < resetDays;
184
+ } catch {
185
+ return false;
186
+ }
187
+ }
188
+
189
+ /**
190
+ * Clear all PWA install data
191
+ */
192
+ export function clearAllPWAData(): void {
193
+ if (typeof window === 'undefined') return;
194
+
195
+ try {
196
+ localStorage.removeItem(STORAGE_KEYS.IOS_GUIDE_DISMISSED);
197
+ localStorage.removeItem(STORAGE_KEYS.APP_INSTALLED);
198
+ localStorage.removeItem(STORAGE_KEYS.A2HS_DISMISSED);
199
+ localStorage.removeItem(STORAGE_KEYS.PUSH_DISMISSED);
200
+ } catch {
201
+ // Fail silently
202
+ }
203
+ }
@@ -0,0 +1,149 @@
1
+ /**
2
+ * PWA Logger with Conditional Logging
3
+ *
4
+ * Provides logging utilities that respect environment and debug settings:
5
+ * - In production: Only errors are logged
6
+ * - In development: All levels are logged
7
+ * - Debug mode: Can be enabled in production via localStorage
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { pwaLogger } from '../utils/logger';
12
+ *
13
+ * pwaLogger.info('Info message'); // Only in dev
14
+ * pwaLogger.warn('Warning message'); // Only in dev
15
+ * pwaLogger.error('Error message'); // Always logged
16
+ * pwaLogger.debug('Debug message'); // Only when debug enabled
17
+ * ```
18
+ *
19
+ * Enable debug mode in production:
20
+ * ```typescript
21
+ * import { enablePWADebug } from '../utils/logger';
22
+ * enablePWADebug();
23
+ * // or in console:
24
+ * localStorage.setItem('pwa_debug', 'true');
25
+ * ```
26
+ */
27
+
28
+ import { consola } from 'consola';
29
+
30
+ const isDevelopment = process.env.NODE_ENV === 'development';
31
+
32
+ /**
33
+ * Check if debug mode is enabled via localStorage
34
+ */
35
+ function isDebugEnabled(): boolean {
36
+ if (typeof window === 'undefined') return false;
37
+ try {
38
+ return localStorage.getItem('pwa_debug') === 'true';
39
+ } catch {
40
+ return false;
41
+ }
42
+ }
43
+
44
+ /**
45
+ * PWA Logger instance with conditional logging
46
+ */
47
+ export const pwaLogger = {
48
+ /**
49
+ * Info level logging
50
+ * Only logs in development or when debug is enabled
51
+ */
52
+ info: (...args: Parameters<typeof consola.info>): void => {
53
+ if (isDevelopment || isDebugEnabled()) {
54
+ consola.info(...args);
55
+ }
56
+ },
57
+
58
+ /**
59
+ * Warning level logging
60
+ * Only logs in development or when debug is enabled
61
+ */
62
+ warn: (...args: Parameters<typeof consola.warn>): void => {
63
+ if (isDevelopment || isDebugEnabled()) {
64
+ consola.warn(...args);
65
+ }
66
+ },
67
+
68
+ /**
69
+ * Error level logging
70
+ * Always logs (production + development)
71
+ */
72
+ error: (...args: Parameters<typeof consola.error>): void => {
73
+ consola.error(...args);
74
+ },
75
+
76
+ /**
77
+ * Debug level logging
78
+ * Only logs when debug is explicitly enabled
79
+ */
80
+ debug: (...args: Parameters<typeof consola.debug>): void => {
81
+ if (isDebugEnabled()) {
82
+ consola.debug(...args);
83
+ }
84
+ },
85
+
86
+ /**
87
+ * Success level logging
88
+ * Only logs in development or when debug is enabled
89
+ */
90
+ success: (...args: Parameters<typeof consola.success>): void => {
91
+ if (isDevelopment || isDebugEnabled()) {
92
+ consola.success(...args);
93
+ }
94
+ },
95
+ };
96
+
97
+ /**
98
+ * Enable debug mode
99
+ *
100
+ * This allows seeing debug logs in production.
101
+ * Call this function or set localStorage manually:
102
+ * `localStorage.setItem('pwa_debug', 'true')`
103
+ *
104
+ * @example
105
+ * ```typescript
106
+ * import { enablePWADebug } from '@djangocfg/layouts/snippets';
107
+ * enablePWADebug();
108
+ * // Reload page to see debug logs
109
+ * ```
110
+ */
111
+ export function enablePWADebug(): void {
112
+ if (typeof window !== 'undefined') {
113
+ try {
114
+ localStorage.setItem('pwa_debug', 'true');
115
+ consola.info('[PWA] Debug mode enabled. Reload page to see debug logs.');
116
+ } catch (e) {
117
+ consola.error('[PWA] Failed to enable debug mode:', e);
118
+ }
119
+ }
120
+ }
121
+
122
+ /**
123
+ * Disable debug mode
124
+ *
125
+ * @example
126
+ * ```typescript
127
+ * import { disablePWADebug } from '@djangocfg/layouts/snippets';
128
+ * disablePWADebug();
129
+ * ```
130
+ */
131
+ export function disablePWADebug(): void {
132
+ if (typeof window !== 'undefined') {
133
+ try {
134
+ localStorage.removeItem('pwa_debug');
135
+ consola.info('[PWA] Debug mode disabled.');
136
+ } catch (e) {
137
+ consola.error('[PWA] Failed to disable debug mode:', e);
138
+ }
139
+ }
140
+ }
141
+
142
+ /**
143
+ * Check if debug mode is currently active
144
+ *
145
+ * @returns true if debug mode is enabled
146
+ */
147
+ export function isPWADebugEnabled(): boolean {
148
+ return isDebugEnabled();
149
+ }
@@ -0,0 +1,151 @@
1
+ /**
2
+ * Platform Detection Utilities
3
+ *
4
+ * Centralized utilities for detecting PWA state, platform, and capabilities.
5
+ * Used by hooks to avoid code duplication.
6
+ */
7
+
8
+ /**
9
+ * Check if running as PWA (standalone mode)
10
+ *
11
+ * Checks if the app is running in standalone mode (added to home screen).
12
+ * This is the primary indicator that a PWA has been installed.
13
+ *
14
+ * @returns true if app is running in standalone mode
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * if (isStandalone()) {
19
+ * console.log('Running as PWA');
20
+ * }
21
+ * ```
22
+ */
23
+ export function isStandalone(): boolean {
24
+ if (typeof window === 'undefined') return false;
25
+
26
+ // Fallback for older browsers without matchMedia
27
+ if (!window.matchMedia) {
28
+ // Use legacy iOS check only
29
+ const nav = navigator as Navigator & { standalone?: boolean };
30
+ return nav.standalone === true;
31
+ }
32
+
33
+ // Check display-mode media query (modern approach)
34
+ const isStandaloneDisplay = window.matchMedia('(display-mode: standalone)').matches;
35
+
36
+ // Legacy iOS check (navigator.standalone)
37
+ const nav = navigator as Navigator & { standalone?: boolean };
38
+ const isStandaloneNavigator = nav.standalone === true;
39
+
40
+ return isStandaloneDisplay || isStandaloneNavigator;
41
+ }
42
+
43
+ /**
44
+ * Check if device is mobile
45
+ *
46
+ * @returns true if device is mobile (iOS, Android, etc.)
47
+ */
48
+ export function isMobileDevice(): boolean {
49
+ if (typeof window === 'undefined') return false;
50
+ return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
51
+ }
52
+
53
+ /**
54
+ * Check if web app manifest exists and is valid
55
+ *
56
+ * @returns true if manifest link exists in document head
57
+ */
58
+ export function hasValidManifest(): boolean {
59
+ if (typeof document === 'undefined') return false;
60
+ const manifestLink = document.querySelector('link[rel="manifest"]');
61
+ return !!manifestLink;
62
+ }
63
+
64
+ /**
65
+ * Reliable check for PWA mode with edge case handling
66
+ *
67
+ * This function provides additional validation for desktop browsers
68
+ * to avoid false positives (e.g., Safari macOS "Add to Dock").
69
+ *
70
+ * For mobile devices, standard standalone check is sufficient.
71
+ * For desktop, additionally validates that a manifest exists.
72
+ *
73
+ * @returns true if app is running as a genuine PWA
74
+ *
75
+ * @example
76
+ * ```typescript
77
+ * // Use this for more reliable detection
78
+ * if (isStandaloneReliable()) {
79
+ * console.log('Definitely running as PWA');
80
+ * }
81
+ * ```
82
+ */
83
+ export function isStandaloneReliable(): boolean {
84
+ const standalone = isStandalone();
85
+ if (!standalone) return false;
86
+
87
+ // For mobile devices, standalone check is sufficient
88
+ if (isMobileDevice()) return true;
89
+
90
+ // For desktop browsers, additionally check for valid manifest
91
+ // This prevents false positives like Safari macOS "Add to Dock"
92
+ return hasValidManifest();
93
+ }
94
+
95
+ /**
96
+ * Get display mode from media query
97
+ *
98
+ * @returns Current display mode: 'standalone', 'fullscreen', 'minimal-ui', or 'browser'
99
+ */
100
+ export function getDisplayMode(): 'standalone' | 'fullscreen' | 'minimal-ui' | 'browser' {
101
+ if (typeof window === 'undefined') return 'browser';
102
+
103
+ if (!window.matchMedia) return 'browser';
104
+
105
+ const modes: Array<'standalone' | 'fullscreen' | 'minimal-ui'> = [
106
+ 'fullscreen',
107
+ 'standalone',
108
+ 'minimal-ui',
109
+ ];
110
+
111
+ for (const mode of modes) {
112
+ if (window.matchMedia(`(display-mode: ${mode})`).matches) {
113
+ return mode;
114
+ }
115
+ }
116
+
117
+ return 'browser';
118
+ }
119
+
120
+ /**
121
+ * Create a media query listener for display-mode changes
122
+ *
123
+ * @param callback - Function to call when display mode changes
124
+ * @returns Cleanup function to remove listener
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * const cleanup = onDisplayModeChange((isStandalone) => {
129
+ * console.log('Display mode changed:', isStandalone);
130
+ * });
131
+ *
132
+ * // Later: cleanup();
133
+ * ```
134
+ */
135
+ export function onDisplayModeChange(callback: (isStandalone: boolean) => void): () => void {
136
+ if (typeof window === 'undefined' || !window.matchMedia) {
137
+ return () => {}; // No-op cleanup
138
+ }
139
+
140
+ const mediaQuery = window.matchMedia('(display-mode: standalone)');
141
+
142
+ const handleChange = (e: MediaQueryListEvent) => {
143
+ callback(e.matches);
144
+ };
145
+
146
+ mediaQuery.addEventListener('change', handleChange);
147
+
148
+ return () => {
149
+ mediaQuery.removeEventListener('change', handleChange);
150
+ };
151
+ }