File size: 25,652 Bytes
16ab111
 
 
 
 
 
 
 
 
72f0edb
16ab111
72f0edb
 
 
16ab111
6bc7874
16ab111
 
 
72f0edb
16ab111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72f0edb
 
16ab111
 
 
 
72f0edb
 
 
 
 
 
 
 
6bc7874
72f0edb
 
16ab111
 
 
 
 
 
 
 
 
 
 
 
 
6bc7874
16ab111
 
 
 
 
 
 
 
 
 
 
 
6bc7874
16ab111
 
 
 
 
72f0edb
 
 
 
16ab111
 
72f0edb
 
 
 
 
 
 
 
 
 
6bc7874
16ab111
 
 
 
 
6bc7874
912e2d8
6bc7874
16ab111
6bc7874
16ab111
 
 
 
6bc7874
16ab111
 
 
 
 
6bc7874
16ab111
 
6bc7874
16ab111
 
6bc7874
16ab111
 
 
 
 
 
 
72f0edb
 
 
 
 
 
 
 
 
 
16ab111
 
 
 
 
72f0edb
 
 
16ab111
 
 
72f0edb
 
 
16ab111
72f0edb
16ab111
 
912e2d8
16ab111
 
 
6bc7874
16ab111
 
 
 
 
 
 
 
 
 
 
 
 
6bc7874
16ab111
 
 
 
 
 
 
6bc7874
16ab111
 
 
6bc7874
16ab111
 
72f0edb
 
 
 
 
 
 
 
 
 
 
 
 
 
16ab111
72f0edb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16ab111
 
6bc7874
16ab111
 
 
 
 
 
 
 
 
6bc7874
16ab111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bc7874
16ab111
 
 
6bc7874
 
16ab111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bc7874
16ab111
 
 
6bc7874
16ab111
 
6bc7874
16ab111
 
6bc7874
72f0edb
 
16ab111
 
 
6bc7874
16ab111
72f0edb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bc7874
72f0edb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bc7874
72f0edb
 
 
 
 
 
 
 
 
 
 
16ab111
 
6bc7874
16ab111
6bc7874
16ab111
 
 
 
 
 
 
 
 
 
72f0edb
16ab111
 
6bc7874
16ab111
 
 
6bc7874
16ab111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bc7874
16ab111
 
 
 
72f0edb
16ab111
 
 
 
 
 
 
 
 
 
 
72f0edb
16ab111
 
 
 
 
 
 
 
6bc7874
16ab111
 
 
 
 
 
 
 
 
6bc7874
16ab111
 
6bc7874
16ab111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bc7874
16ab111
 
 
 
 
 
 
6bc7874
16ab111
 
 
 
72f0edb
16ab111
6bc7874
16ab111
 
6bc7874
16ab111
 
6bc7874
 
16ab111
 
 
 
 
 
 
 
 
 
6bc7874
16ab111
 
6bc7874
16ab111
 
72f0edb
 
 
16ab111
 
72f0edb
 
6bc7874
72f0edb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bc7874
72f0edb
 
 
 
 
 
 
 
 
 
 
 
 
 
16ab111
6bc7874
16ab111
6bc7874
16ab111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bc7874
16ab111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72f0edb
 
 
 
 
 
 
16ab111
 
6bc7874
16ab111
6bc7874
16ab111
 
 
6bc7874
16ab111
 
 
 
 
 
 
6bc7874
16ab111
72f0edb
16ab111
6bc7874
16ab111
 
6bc7874
16ab111
 
 
 
 
6bc7874
16ab111
 
 
 
 
 
 
 
 
 
 
72f0edb
 
 
 
 
 
 
16ab111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72f0edb
16ab111
 
72f0edb
 
 
16ab111
 
72f0edb
 
 
 
16ab111
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
import React, {
  createContext,
  useState,
  useCallback,
  ReactNode,
  useRef,
  useEffect,
} from "react";
import { toast } from "sonner";
import { useUrdfParser } from "@/hooks/useUrdfParser";
import { UrdfProcessor, readUrdfFileContent } from "@/lib/UrdfDragAndDrop";
import { UrdfData, UrdfFileModel } from "@/lib/types";
import { useDefaultRobotData } from "@/hooks/useDefaultRobotData";
import { RobotAnimationConfig } from "@/lib/types";

// Define the result interface for Urdf detection
interface UrdfDetectionResult {
  hasUrdf: boolean;
  modelName?: string;
  parsedData?: UrdfData | null;
}

// Define the context type
export type UrdfContextType = {
  urdfProcessor: UrdfProcessor | null;
  registerUrdfProcessor: (processor: UrdfProcessor) => void;
  onUrdfDetected: (
    callback: (result: UrdfDetectionResult) => void
  ) => () => void;
  processUrdfFiles: (
    files: Record<string, File>,
    availableModels: string[]
  ) => Promise<void>;
  urdfBlobUrls: Record<string, string>;
  alternativeUrdfModels: string[];
  isSelectionModalOpen: boolean;
  setIsSelectionModalOpen: (isOpen: boolean) => void;
  urdfModelOptions: UrdfFileModel[];
  selectUrdfModel: (model: UrdfFileModel) => void;

  // Centralized robot data management
  currentRobotData: UrdfData | null;
  isDefaultModel: boolean;
  setIsDefaultModel: (isDefault: boolean) => void;
  resetToDefaultModel: () => void;
  urdfContent: string | null;

  // Animation configuration management
  currentAnimationConfig: RobotAnimationConfig | null;
  setCurrentAnimationConfig: (config: RobotAnimationConfig | null) => void;

  // These properties are kept for backward compatibility but are considered
  // implementation details and should not be used directly in components.
  // TODO: Remove these next three once the time is right
  parsedRobotData: UrdfData | null; // Data from parsed Urdf
  customModelName: string;
  customModelDescription: string;
};

// Create the context
export const UrdfContext = createContext<UrdfContextType | undefined>(
  undefined
);

// Props for the provider component
interface UrdfProviderProps {
  children: ReactNode;
}

export const UrdfProvider: React.FC<UrdfProviderProps> = ({ children }) => {
  // State for Urdf processor
  const [urdfProcessor, setUrdfProcessor] = useState<UrdfProcessor | null>(
    null
  );

  // State for blob URLs (replacing window.urdfBlobUrls)
  const [urdfBlobUrls, setUrdfBlobUrls] = useState<Record<string, string>>({});

  // State for alternative models (replacing window.alternativeUrdfModels)
  const [alternativeUrdfModels, setAlternativeUrdfModels] = useState<string[]>(
    []
  );

  // State for the Urdf selection modal
  const [isSelectionModalOpen, setIsSelectionModalOpen] = useState(false);
  const [urdfModelOptions, setUrdfModelOptions] = useState<UrdfFileModel[]>([]);

  // New state for centralized robot data management
  const [isDefaultModel, setIsDefaultModel] = useState(true);
  const [parsedRobotData, setParsedRobotData] = useState<UrdfData | null>(null);
  const [customModelName, setCustomModelName] = useState<string>("");
  const [customModelDescription, setCustomModelDescription] =
    useState<string>("");
  const [urdfContent, setUrdfContent] = useState<string | null>(null);

  // New state for animation configuration
  const [currentAnimationConfig, setCurrentAnimationConfig] =
    useState<RobotAnimationConfig | null>(null);

  // Get default robot data from our hook
  const { data: defaultRobotData } = useDefaultRobotData("T12");

  // Compute the current robot data based on model state
  const currentRobotData = isDefaultModel ? defaultRobotData : parsedRobotData;

  // Fetch the default Urdf content when the component mounts
  useEffect(() => {
    // Only fetch if we don't have content and we're using the default model
    if (isDefaultModel && !urdfContent) {
      const fetchDefaultUrdf = async () => {
        try {
          // Path to the default T12 Urdf file
          const defaultUrdfPath =
            "/urdf/SO_5DOF_ARM100_05d/urdf/SO_5DOF_ARM100_05d.Urdf";

          // Fetch the Urdf content
          const response = await fetch(defaultUrdfPath);

          if (!response.ok) {
            throw new Error(
              `Failed to fetch default Urdf: ${response.statusText}`
            );
          }

          const defaultUrdfContent = await response.text();
          console.log(
            `πŸ“„ Default Urdf content loaded, length: ${defaultUrdfContent.length} characters`
          );

          // Set the Urdf content in state
          setUrdfContent(defaultUrdfContent);
        } catch (error) {
          console.error("❌ Error loading default Urdf content:", error);
        }
      };

      fetchDefaultUrdf();
    }
  }, [isDefaultModel, urdfContent]);

  // Log data state changes for debugging
  useEffect(() => {
    console.log("πŸ€– Robot data context updated:", {
      isDefaultModel,
      hasDefaultData: !!defaultRobotData,
      hasParsedData: !!parsedRobotData,
      currentData: currentRobotData ? "available" : "null",
    });
  }, [isDefaultModel, defaultRobotData, parsedRobotData, currentRobotData]);

  // Reference for callbacks
  const urdfCallbacksRef = useRef<((result: UrdfDetectionResult) => void)[]>(
    []
  );

  // Get the parseUrdf function from the useUrdfParser hook
  const { parseUrdf } = useUrdfParser();

  // Reset to default model
  const resetToDefaultModel = useCallback(() => {
    setIsDefaultModel(true);
    setCustomModelName("");
    setCustomModelDescription("");
    setParsedRobotData(null);
    setUrdfContent(null);
    setCurrentAnimationConfig(null);

    toast.info("Switched to default model", {
      description: "The default ARM100 robot model is now displayed.",
    });
  }, []);

  // Register a callback for Urdf detection
  const onUrdfDetected = useCallback(
    (callback: (result: UrdfDetectionResult) => void) => {
      urdfCallbacksRef.current.push(callback);

      return () => {
        urdfCallbacksRef.current = urdfCallbacksRef.current.filter(
          (cb) => cb !== callback
        );
      };
    },
    []
  );

  // Register a Urdf processor
  const registerUrdfProcessor = useCallback((processor: UrdfProcessor) => {
    setUrdfProcessor(processor);
  }, []);

  // Internal function to notify callbacks and update central state
  const notifyUrdfCallbacks = useCallback(
    (result: UrdfDetectionResult) => {
      console.log("πŸ“£ Notifying Urdf callbacks with result:", result);

      // Update our internal state based on the result
      if (result.hasUrdf) {
        // Always ensure we set isDefaultModel to false when we have a Urdf
        setIsDefaultModel(false);

        if (result.parsedData) {
          // Create a copy of the parsed data with any missing fields filled from our state
          const enhancedParsedData: UrdfData = {
            ...result.parsedData,
          };

          // Set the name if available, or use the provided modelName as fallback
          if (result.parsedData.name) {
            setCustomModelName(result.parsedData.name);
          } else if (result.modelName) {
            setCustomModelName(result.modelName);
            // Also update the parsed data with this name to be consistent
            enhancedParsedData.name = result.modelName;
          }

          // Set description if available
          if (result.parsedData.description) {
            setCustomModelDescription(result.parsedData.description);
          } else {
            // If no description in parsed data, set a default one
            const defaultDesc =
              "A detailed 3D model of a robotic system with articulated joints and components.";
            enhancedParsedData.description = defaultDesc;
            setCustomModelDescription(defaultDesc);
          }

          // Update parsed data with the enhanced version
          setParsedRobotData(enhancedParsedData);
        } else if (result.modelName) {
          // Only have model name, no parsed data
          setCustomModelName(result.modelName);

          // Create a minimal UrdfData object with at least the name
          const minimalData: UrdfData = {
            name: result.modelName,
            description:
              "A detailed 3D model of a robotic system with articulated joints and components.",
          };
          setParsedRobotData(minimalData);
        }
      } else {
        // If no Urdf, reset to default
        resetToDefaultModel();
      }

      // Call all registered callbacks
      urdfCallbacksRef.current.forEach((callback) => callback(result));
    },
    [resetToDefaultModel]
  );

  // Helper function to process the selected Urdf model
  const processSelectedUrdf = useCallback(
    async (model: UrdfFileModel) => {
      if (!urdfProcessor) return;

      // Find the file in our files record
      const files = Object.values(urdfBlobUrls)
        .filter((url) => url === model.blobUrl)
        .map((url) => {
          const path = Object.keys(urdfBlobUrls).find(
            (key) => urdfBlobUrls[key] === url
          );
          return path ? { path, url } : null;
        })
        .filter((item) => item !== null);

      if (files.length === 0) {
        console.error("❌ Could not find file for selected Urdf model");
        return;
      }

      // Show a toast notification that we're parsing the Urdf
      const parsingToast = toast.loading("Analyzing Urdf model...", {
        description: "Extracting robot information",
        duration: 10000, // Long duration since we'll dismiss it manually
      });

      try {
        // Get the file from our record
        const filePath = files[0]?.path;
        if (!filePath || !urdfBlobUrls[filePath]) {
          throw new Error("File not found in records");
        }

        // Get the actual File object
        const response = await fetch(model.blobUrl);
        const blob = await response.blob();
        const file = new File(
          [blob],
          filePath.split("/").pop() || "model.urdf",
          {
            type: "application/xml",
          }
        );

        // Read the Urdf content
        const urdfContent = await readUrdfFileContent(file);

        console.log(
          `πŸ“ Urdf content read, length: ${urdfContent.length} characters`
        );

        // Store the Urdf content in state
        setUrdfContent(urdfContent);

        // Parse the Urdf
        const parseResult = await parseUrdf(urdfContent);

        // Dismiss the toast
        toast.dismiss(parsingToast);

        // Always set isDefaultModel to false when processing a custom Urdf
        setIsDefaultModel(false);

        if (parseResult) {
          // Success case - we have parsed data
          const modelDisplayName =
            model.name || model.path.split("/").pop() || "Unknown";

          // Create an enhanced version of the parsed data with any missing fields filled
          const enhancedParsedData: UrdfData = {
            ...parseResult,
          };

          // Update name in both state and enhanced data if needed
          if (parseResult.name) {
            setCustomModelName(parseResult.name);
          } else {
            setCustomModelName(modelDisplayName);
            enhancedParsedData.name = modelDisplayName;
          }

          // Update description in both state and enhanced data if needed
          if (parseResult.description) {
            setCustomModelDescription(parseResult.description);
          } else {
            const defaultDesc =
              "A detailed 3D model of a robotic system with articulated joints and components.";
            setCustomModelDescription(defaultDesc);
            enhancedParsedData.description = defaultDesc;
          }

          // Update the state with enhanced parsed data
          setParsedRobotData(enhancedParsedData);

          toast.success("Urdf model loaded successfully", {
            description: `Model: ${modelDisplayName}`,
            duration: 3000,
          });

          // Notify callbacks with the enhanced parsed data
          notifyUrdfCallbacks({
            hasUrdf: true,
            modelName: enhancedParsedData.name || modelDisplayName,
            parsedData: enhancedParsedData,
          });
        } else {
          // Partial success case - we loaded the model but couldn't fully parse it
          const modelDisplayName =
            model.name || model.path.split("/").pop() || "Unknown";

          // Create a minimal data structure with at least name and description
          const minimalData: UrdfData = {
            name: modelDisplayName,
            description:
              "A detailed 3D model of a robotic system with articulated joints and components.",
          };

          // Update our state
          setCustomModelName(modelDisplayName);
          setCustomModelDescription(minimalData.description);
          setParsedRobotData(minimalData);

          toast.warning("Urdf model loaded with limited information", {
            description: "Could not fully analyze the robot structure",
            duration: 3000,
          });

          // Notify callbacks with the minimal data
          notifyUrdfCallbacks({
            hasUrdf: true,
            modelName: modelDisplayName,
            parsedData: minimalData,
          });
        }
      } catch (error) {
        // Error case
        console.error("❌ Error processing selected Urdf:", error);
        toast.dismiss(parsingToast);
        toast.error("Error analyzing Urdf", {
          description: `Error: ${
            error instanceof Error ? error.message : String(error)
          }`,
          duration: 3000,
        });

        // Keep showing the custom model even if parsing failed
        // No need to reset to default unless user explicitly chooses to
      }
    },
    [urdfBlobUrls, urdfProcessor, parseUrdf, notifyUrdfCallbacks]
  );

  // Function to handle selecting a Urdf model from the modal
  const selectUrdfModel = useCallback(
    (model: UrdfFileModel) => {
      if (!urdfProcessor) {
        console.error("❌ No Urdf processor available");
        return;
      }

      console.log(`πŸ€– Selected model: ${model.name || model.path}`);

      // Close the modal
      setIsSelectionModalOpen(false);

      // Extract model name
      const modelName =
        model.name ||
        model.path
          .split("/")
          .pop()
          ?.replace(/\.urdf$/i, "") ||
        "Unknown";

      // Load the selected Urdf model
      urdfProcessor.loadUrdf(model.blobUrl);

      // Update our state immediately even before parsing
      setIsDefaultModel(false);
      setCustomModelName(modelName);

      // Show a toast notification that we're loading the model
      toast.info(`Loading model: ${modelName}`, {
        description: "Preparing 3D visualization",
        duration: 2000,
      });

      // Notify callbacks about the selection before parsing
      notifyUrdfCallbacks({
        hasUrdf: true,
        modelName,
        parsedData: undefined, // Will use parseUrdf later to get the data
      });

      // Try to parse the model - this will update the UI when complete
      processSelectedUrdf(model);
    },
    [urdfProcessor, notifyUrdfCallbacks, processSelectedUrdf]
  );

  // Process Urdf files - moved from DragAndDropContext
  const processUrdfFiles = useCallback(
    async (files: Record<string, File>, availableModels: string[]) => {
      // Clear previous blob URLs to prevent memory leaks
      Object.values(urdfBlobUrls).forEach(URL.revokeObjectURL);
      setUrdfBlobUrls({});
      setAlternativeUrdfModels([]);
      setUrdfModelOptions([]);

      try {
        // Check if we have any Urdf files
        if (availableModels.length > 0 && urdfProcessor) {
          console.log(
            `πŸ€– Found ${availableModels.length} Urdf models:`,
            availableModels
          );

          // Create blob URLs for all models
          const newUrdfBlobUrls: Record<string, string> = {};
          availableModels.forEach((path) => {
            if (files[path]) {
              newUrdfBlobUrls[path] = URL.createObjectURL(files[path]);
            }
          });
          setUrdfBlobUrls(newUrdfBlobUrls);

          // Save alternative models for reference
          setAlternativeUrdfModels(availableModels);

          // Create model options for the selection modal
          const modelOptions: UrdfFileModel[] = availableModels.map((path) => {
            const fileName = path.split("/").pop() || "";
            const modelName = fileName.replace(/\.urdf$/i, "");
            return {
              path,
              blobUrl: newUrdfBlobUrls[path],
              name: modelName,
            };
          });

          setUrdfModelOptions(modelOptions);

          // If there's only one model, use it directly
          if (availableModels.length === 1) {
            // Extract model name from the Urdf file
            const fileName = availableModels[0].split("/").pop() || "";
            const modelName = fileName.replace(/\.urdf$/i, "");
            console.log(`πŸ“„ Using model: ${modelName} (${fileName})`);

            // Use the blob URL instead of the file path
            const blobUrl = newUrdfBlobUrls[availableModels[0]];
            if (blobUrl) {
              console.log(`πŸ”— Using blob URL for Urdf: ${blobUrl}`);
              urdfProcessor.loadUrdf(blobUrl);

              // Immediately update model state
              setIsDefaultModel(false);
              setCustomModelName(modelName);

              // Process the Urdf file for parsing
              if (files[availableModels[0]]) {
                console.log(
                  "πŸ“„ Reading Urdf content for edge function parsing..."
                );

                // Show a toast notification that we're parsing the Urdf
                const parsingToast = toast.loading("Analyzing Urdf model...", {
                  description: "Extracting robot information",
                  duration: 10000, // Long duration since we'll dismiss it manually
                });

                try {
                  const urdfContent = await readUrdfFileContent(
                    files[availableModels[0]]
                  );

                  console.log(
                    `πŸ“ Urdf content read, length: ${urdfContent.length} characters`
                  );

                  // Store the Urdf content in state
                  setUrdfContent(urdfContent);

                  // Call the parseUrdf function from the hook
                  const parseResult = await parseUrdf(urdfContent);

                  // Dismiss the parsing toast
                  toast.dismiss(parsingToast);

                  if (parseResult) {
                    toast.success("Urdf model analyzed successfully", {
                      description: `Model: ${modelName}`,
                      duration: 3000,
                    });

                    // Create an enhanced version of the parsed data with any missing fields filled
                    const enhancedParsedData: UrdfData = {
                      ...parseResult,
                    };

                    // Update name in both state and enhanced data if needed
                    if (parseResult.name) {
                      setCustomModelName(parseResult.name);
                    } else {
                      setCustomModelName(modelName);
                      enhancedParsedData.name = modelName;
                    }

                    // Update description in both state and enhanced data if needed
                    if (parseResult.description) {
                      setCustomModelDescription(parseResult.description);
                    } else {
                      const defaultDesc =
                        "A detailed 3D model of a robotic system with articulated joints and components.";
                      setCustomModelDescription(defaultDesc);
                      enhancedParsedData.description = defaultDesc;
                    }

                    // Update the state with enhanced parsed data
                    setParsedRobotData(enhancedParsedData);

                    // Notify callbacks with all the information
                    notifyUrdfCallbacks({
                      hasUrdf: true,
                      modelName: enhancedParsedData.name || modelName,
                      parsedData: enhancedParsedData,
                    });
                  } else {
                    // Create a minimal data structure with at least name and description
                    const minimalData: UrdfData = {
                      name: modelName,
                      description:
                        "A detailed 3D model of a robotic system with articulated joints and components.",
                    };

                    // Update our state
                    setParsedRobotData(minimalData);

                    toast.warning(
                      "Urdf model loaded with limited information",
                      {
                        description:
                          "Could not fully analyze the robot structure",
                        duration: 3000,
                      }
                    );

                    // Still notify callbacks with minimal data
                    notifyUrdfCallbacks({
                      hasUrdf: true,
                      modelName,
                      parsedData: minimalData,
                    });
                  }
                } catch (parseError) {
                  console.error("❌ Error parsing Urdf:", parseError);
                  toast.dismiss(parsingToast);
                  toast.error("Error analyzing Urdf", {
                    description: `Error: ${
                      parseError instanceof Error
                        ? parseError.message
                        : String(parseError)
                    }`,
                    duration: 3000,
                  });

                  // Still notify callbacks without parsed data
                  notifyUrdfCallbacks({
                    hasUrdf: true,
                    modelName,
                  });
                }
              } else {
                console.error(
                  "❌ Could not find file for Urdf model:",
                  availableModels[0]
                );
                console.log("πŸ“¦ Available files:", Object.keys(files));

                // Still notify callbacks without parsed data
                notifyUrdfCallbacks({
                  hasUrdf: true,
                  modelName,
                });
              }
            } else {
              console.warn(
                `⚠️ No blob URL found for ${availableModels[0]}, using path directly`
              );
              urdfProcessor.loadUrdf(availableModels[0]);

              // Update the state even without a blob URL
              setIsDefaultModel(false);
              setCustomModelName(modelName);

              // Notify callbacks
              notifyUrdfCallbacks({
                hasUrdf: true,
                modelName,
              });
            }
          } else {
            // Multiple Urdf files found, show selection modal
            console.log(
              "πŸ“‹ Multiple Urdf files found, showing selection modal"
            );
            setIsSelectionModalOpen(true);

            // Notify that Urdf files are available but selection is needed
            notifyUrdfCallbacks({
              hasUrdf: true,
              modelName: "Multiple models available",
            });
          }
        } else {
          console.warn(
            "❌ No Urdf models found in dropped files or no processor available"
          );
          notifyUrdfCallbacks({ hasUrdf: false, parsedData: null });

          // Reset to default model when no Urdf files are found
          resetToDefaultModel();

          toast.error("No Urdf file found", {
            description: "Please upload a folder containing a .urdf file.",
            duration: 3000,
          });
        }
      } catch (error) {
        console.error("❌ Error processing Urdf files:", error);
        toast.error("Error processing files", {
          description: `Error: ${
            error instanceof Error ? error.message : String(error)
          }`,
          duration: 3000,
        });

        // Reset to default model on error
        resetToDefaultModel();
      }
    },
    [
      notifyUrdfCallbacks,
      parseUrdf,
      urdfBlobUrls,
      urdfProcessor,
      resetToDefaultModel,
    ]
  );

  // Clean up blob URLs when component unmounts
  React.useEffect(() => {
    return () => {
      Object.values(urdfBlobUrls).forEach(URL.revokeObjectURL);
    };
  }, [urdfBlobUrls]);

  // Create the context value
  const contextValue: UrdfContextType = {
    urdfProcessor,
    registerUrdfProcessor,
    onUrdfDetected,
    processUrdfFiles,
    urdfBlobUrls,
    alternativeUrdfModels,
    isSelectionModalOpen,
    setIsSelectionModalOpen,
    urdfModelOptions,
    selectUrdfModel,

    // New properties for centralized robot data management
    currentRobotData,
    isDefaultModel,
    setIsDefaultModel,
    parsedRobotData,
    customModelName,
    customModelDescription,
    resetToDefaultModel,
    urdfContent,

    // Animation configuration management
    currentAnimationConfig,
    setCurrentAnimationConfig,
  };

  return (
    <UrdfContext.Provider value={contextValue}>{children}</UrdfContext.Provider>
  );
};