Spaces:
Sleeping
Sleeping
Commit ·
f58484f
1
Parent(s): 679aeba
Subscribe directly to folders and voices collections in code-by-dante
Browse files- services/firebase.ts +67 -19
services/firebase.ts
CHANGED
|
@@ -42,34 +42,82 @@ const cleanData = (data: any) => {
|
|
| 42 |
};
|
| 43 |
|
| 44 |
export const subscribeToItems = (callback: (items: DataItem[]) => void) => {
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
const data = doc.data();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
return {
|
| 61 |
id: doc.id,
|
| 62 |
...data,
|
| 63 |
-
blocks,
|
| 64 |
-
// Convert Firestore Timestamp to number (milliseconds) if necessary
|
| 65 |
createdAt: data.createdAt?.toMillis ? data.createdAt.toMillis() : Date.now(),
|
| 66 |
-
updatedAt: data.updatedAt?.toMillis ? data.updatedAt.toMillis() : (data.createdAt?.toMillis ? data.createdAt.toMillis() : Date.now()),
|
| 67 |
} as DataItem;
|
| 68 |
});
|
| 69 |
-
|
| 70 |
-
}, (
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
};
|
| 74 |
|
| 75 |
export const addItem = async (item: Omit<DataItem, 'id' | 'createdAt'>): Promise<string> => {
|
|
|
|
| 42 |
};
|
| 43 |
|
| 44 |
export const subscribeToItems = (callback: (items: DataItem[]) => void) => {
|
| 45 |
+
let foldersData: DataItem[] = [];
|
| 46 |
+
let voicesData: DataItem[] = [];
|
| 47 |
+
let neonItemsData: DataItem[] = [];
|
| 48 |
+
|
| 49 |
+
const updateAll = () => {
|
| 50 |
+
const combinedMap = new Map<string, DataItem>();
|
| 51 |
+
|
| 52 |
+
// Add folders
|
| 53 |
+
foldersData.forEach(item => combinedMap.set(item.id, item));
|
| 54 |
+
// Add voices
|
| 55 |
+
voicesData.forEach(item => combinedMap.set(item.id, item));
|
| 56 |
+
// Add neon items if any
|
| 57 |
+
neonItemsData.forEach(item => combinedMap.set(item.id, item));
|
| 58 |
+
|
| 59 |
+
callback(Array.from(combinedMap.values()));
|
| 60 |
+
};
|
| 61 |
+
|
| 62 |
+
// Subscribe to folders collection
|
| 63 |
+
const unsubFolders = onSnapshot(collection(db, 'folders'), (snapshot) => {
|
| 64 |
+
foldersData = snapshot.docs.map(doc => {
|
| 65 |
const data = doc.data();
|
| 66 |
+
return {
|
| 67 |
+
id: doc.id,
|
| 68 |
+
title: data.name || data.title || 'Carpeta',
|
| 69 |
+
summary: 'Carpeta',
|
| 70 |
+
content: '',
|
| 71 |
+
isFolder: true,
|
| 72 |
+
parentId: data.parentId || null,
|
| 73 |
+
tags: [],
|
| 74 |
+
categories: [],
|
| 75 |
+
createdAt: data.createdAt?.toMillis ? data.createdAt.toMillis() : Date.now(),
|
| 76 |
+
} as DataItem;
|
| 77 |
+
});
|
| 78 |
+
updateAll();
|
| 79 |
+
}, (err) => console.error("Error reading folders:", err));
|
| 80 |
|
| 81 |
+
// Subscribe to voices collection
|
| 82 |
+
const unsubVoices = onSnapshot(collection(db, 'voices'), (snapshot) => {
|
| 83 |
+
voicesData = snapshot.docs.map(doc => {
|
| 84 |
+
const data = doc.data();
|
| 85 |
+
return {
|
| 86 |
+
id: doc.id,
|
| 87 |
+
title: data.title || 'Voz',
|
| 88 |
+
summary: '',
|
| 89 |
+
content: '',
|
| 90 |
+
visitUrl: data.url || data.visitUrl || '',
|
| 91 |
+
downloadUrl: data.downloadUrl || '',
|
| 92 |
+
isFolder: false,
|
| 93 |
+
parentId: data.folderId || data.parentId || null,
|
| 94 |
+
tags: data.tags || [],
|
| 95 |
+
categories: data.categories || [],
|
| 96 |
+
linkGroups: data.linkGroups || [],
|
| 97 |
+
createdAt: data.createdAt?.toMillis ? data.createdAt.toMillis() : Date.now(),
|
| 98 |
+
} as DataItem;
|
| 99 |
+
});
|
| 100 |
+
updateAll();
|
| 101 |
+
}, (err) => console.error("Error reading voices:", err));
|
| 102 |
|
| 103 |
+
// Subscribe to neonvault_items collection (for backward compatibility)
|
| 104 |
+
const unsubNeon = onSnapshot(collection(db, 'neonvault_items'), (snapshot) => {
|
| 105 |
+
neonItemsData = snapshot.docs.map(doc => {
|
| 106 |
+
const data = doc.data();
|
| 107 |
return {
|
| 108 |
id: doc.id,
|
| 109 |
...data,
|
|
|
|
|
|
|
| 110 |
createdAt: data.createdAt?.toMillis ? data.createdAt.toMillis() : Date.now(),
|
|
|
|
| 111 |
} as DataItem;
|
| 112 |
});
|
| 113 |
+
updateAll();
|
| 114 |
+
}, (err) => console.error("Error reading neonvault_items:", err));
|
| 115 |
+
|
| 116 |
+
return () => {
|
| 117 |
+
unsubFolders();
|
| 118 |
+
unsubVoices();
|
| 119 |
+
unsubNeon();
|
| 120 |
+
};
|
| 121 |
};
|
| 122 |
|
| 123 |
export const addItem = async (item: Omit<DataItem, 'id' | 'createdAt'>): Promise<string> => {
|