Skip to content

Commit d6a61e7

Browse files
committed
Refactor | Add JSDocs for the new dialog system
1 parent be382a7 commit d6a61e7

File tree

3 files changed

+234
-4
lines changed

3 files changed

+234
-4
lines changed

contexts/dialog-context/dialog-context.tsx

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,41 @@ const DialogContext = React.createContext<DialogContextValue | undefined>(
1313
undefined,
1414
);
1515

16+
/**
17+
* Dialog provider component for the global dialog system
18+
* @description Must be placed at the root of your app to enable dialog functionality
19+
* @param {React.ReactNode} children - Your app components
20+
* @example
21+
* ```tsx
22+
* function App() {
23+
* return (
24+
* <DialogProvider>
25+
* <YourAppContent />
26+
* </DialogProvider>
27+
* );
28+
* }
29+
* ```
30+
*/
1631
export function DialogProvider({ children }: { children: React.ReactNode }) {
1732
const queueRef = useRef(new DialogQueue());
1833
const [activeDialog, setActiveDialog] = useState<DialogConfig | null>(null);
1934
const timerRef = useRef<number | null>(null);
35+
const activeDialogIdRef = useRef<string | null>(null);
2036

2137
const processQueue = useCallback(() => {
2238
const next = queueRef.current.getNext();
2339
setActiveDialog(next);
40+
activeDialogIdRef.current = next?.id || null;
2441

2542
if (next?.autoHideDuration && next.autoHideDuration > 0) {
2643
timerRef.current = setTimeout(() => {
27-
hideDialog(next.id);
44+
if (next?.id) {
45+
const dialog = queueRef.current.get(next.id);
46+
queueRef.current.removeById(next.id);
47+
dialog?.onDismiss?.();
48+
}
49+
activeDialogIdRef.current = null;
50+
processQueue();
2851
}, next.autoHideDuration);
2952
}
3053
}, []);
@@ -185,16 +208,19 @@ export function DialogProvider({ children }: { children: React.ReactNode }) {
185208
(id?: string) => {
186209
clearTimer();
187210

188-
const targetId = id || activeDialog?.id;
211+
const targetId = id || activeDialogIdRef.current;
189212
if (targetId) {
190213
const dialog = queueRef.current.get(targetId);
191214
queueRef.current.removeById(targetId);
192215
dialog?.onDismiss?.();
216+
if (targetId === activeDialogIdRef.current) {
217+
activeDialogIdRef.current = null;
218+
}
193219
}
194220

195221
processQueue();
196222
},
197-
[activeDialog, clearTimer, processQueue],
223+
[clearTimer, processQueue],
198224
);
199225

200226
const clearDialogsByIds = useCallback(
@@ -279,6 +305,47 @@ export function DialogProvider({ children }: { children: React.ReactNode }) {
279305
);
280306
}
281307

308+
/**
309+
* Hook for accessing the dialog context API
310+
* @description Provides all dialog management methods
311+
* @returns {DialogContextValue} Dialog context with all management methods
312+
* @throws {Error} If used outside of DialogProvider
313+
* @example
314+
* ```tsx
315+
* const { showError, showSuccess, showPersistent, clearDialogsByIds } = useDialog();
316+
*
317+
* // Show error with icon
318+
* showError("Network error", {
319+
* title: "Connection Failed",
320+
* icon: () => <WifiOff size={50} color="#ef4444" />
321+
* });
322+
*
323+
* // Show dialog with custom content
324+
* showDialog({
325+
* title: "Confirm Action",
326+
* content: () => (
327+
* <View className="py-4">
328+
* <Text className="text-center mb-4">Are you sure?</Text>
329+
* <ProgressBar progress={75} />
330+
* </View>
331+
* )
332+
* });
333+
*
334+
* // Show persistent dialog with progress
335+
* const controller = showPersistent({
336+
* message: "Uploading...",
337+
* customId: "upload-progress"
338+
* });
339+
* controller.updateProgress(50);
340+
*
341+
* // Clear specific dialogs by custom ID
342+
* clearDialogsByIds(["upload-progress", "toast-notification"]);
343+
*
344+
* // Priority queue: HIGH will interrupt LOW
345+
* showInfo("This will be queued");
346+
* showError("This shows immediately and interrupts");
347+
* ```
348+
*/
282349
export function useDialog(): DialogContextValue {
283350
const context = React.useContext(DialogContext);
284351
if (!context) {

contexts/dialog-context/dialog-templates.tsx

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,40 @@
1-
import React from "react";
21
import {
32
AlertCircle,
43
CheckCircle,
54
Info,
65
Loader2,
76
XCircle,
87
} from "lucide-react-native";
8+
import React from "react";
99
import { DialogConfig, DialogPriority } from "./dialog-types";
1010

11+
/**
12+
* Pre-configured dialog templates for common use cases
13+
* @description Ready-to-use dialog configurations with appropriate icons, priorities, and behaviors
14+
* @example
15+
* ```tsx
16+
* const { showError, showSuccess } = useDialog();
17+
*
18+
* // Use error template
19+
* showError(DialogTemplates.error("Failed to save file"));
20+
*
21+
* // Use success template with custom title
22+
* showSuccess(DialogTemplates.success("File uploaded successfully", {
23+
* title: "Upload Complete"
24+
* }));
25+
* ```
26+
*/
1127
export const DialogTemplates = {
28+
/**
29+
* Error dialog template with high priority and red X icon
30+
* @param {string} message - Error message to display
31+
* @param {Partial<DialogConfig>} options - Additional configuration overrides
32+
* @returns {DialogConfig} Complete error dialog configuration
33+
* @example
34+
* ```tsx
35+
* showError(DialogTemplates.error("Network connection failed"));
36+
* ```
37+
*/
1238
error: (message: string, options?: Partial<DialogConfig>): DialogConfig => ({
1339
type: "error",
1440
priority: DialogPriority.HIGH,
@@ -20,6 +46,16 @@ export const DialogTemplates = {
2046
timestamp: Date.now(),
2147
}),
2248

49+
/**
50+
* Success dialog template with green checkmark and auto-hide
51+
* @param {string} message - Success message to display
52+
* @param {Partial<DialogConfig>} options - Additional configuration overrides
53+
* @returns {DialogConfig} Complete success dialog configuration
54+
* @example
55+
* ```tsx
56+
* showSuccess(DialogTemplates.success("File uploaded successfully!"));
57+
* ```
58+
*/
2359
success: (
2460
message: string,
2561
options?: Partial<DialogConfig>,
@@ -35,6 +71,16 @@ export const DialogTemplates = {
3571
timestamp: Date.now(),
3672
}),
3773

74+
/**
75+
* Info dialog template with blue info icon and low priority
76+
* @param {string} message - Information message to display
77+
* @param {Partial<DialogConfig>} options - Additional configuration overrides
78+
* @returns {DialogConfig} Complete info dialog configuration
79+
* @example
80+
* ```tsx
81+
* showInfo(DialogTemplates.info("New features available"));
82+
* ```
83+
*/
3884
info: (message: string, options?: Partial<DialogConfig>): DialogConfig => ({
3985
type: "info",
4086
priority: DialogPriority.LOW,
@@ -47,6 +93,16 @@ export const DialogTemplates = {
4793
timestamp: Date.now(),
4894
}),
4995

96+
/**
97+
* Warning dialog template with amber warning icon
98+
* @param {string} message - Warning message to display
99+
* @param {Partial<DialogConfig>} options - Additional configuration overrides
100+
* @returns {DialogConfig} Complete warning dialog configuration
101+
* @example
102+
* ```tsx
103+
* showAlert(DialogTemplates.warning("Battery is low"));
104+
* ```
105+
*/
50106
warning: (
51107
message: string,
52108
options?: Partial<DialogConfig>,
@@ -61,6 +117,16 @@ export const DialogTemplates = {
61117
timestamp: Date.now(),
62118
}),
63119

120+
/**
121+
* Loading dialog template with spinner and persistent priority
122+
* @param {string} message - Loading message to display
123+
* @param {Partial<DialogConfig>} options - Additional configuration overrides
124+
* @returns {DialogConfig} Complete loading dialog configuration
125+
* @example
126+
* ```tsx
127+
* const controller = showPersistent(DialogTemplates.loading("Processing data..."));
128+
* ```
129+
*/
64130
loading: (
65131
message: string,
66132
options?: Partial<DialogConfig>,
@@ -75,6 +141,16 @@ export const DialogTemplates = {
75141
timestamp: Date.now(),
76142
}),
77143

144+
/**
145+
* Critical error dialog template with highest priority and cannot be dismissed
146+
* @param {string} message - Critical error message to display
147+
* @param {Partial<DialogConfig>} options - Additional configuration overrides
148+
* @returns {DialogConfig} Complete critical error dialog configuration
149+
* @example
150+
* ```tsx
151+
* showDialog(DialogTemplates.critical("System failure - app must restart"));
152+
* ```
153+
*/
78154
critical: (
79155
message: string,
80156
options?: Partial<DialogConfig>,

0 commit comments

Comments
 (0)