()
| 30 | } |
| 31 | |
| 32 | export function useSync() { |
| 33 | const adapter = useAdapter(); |
| 34 | const { user } = useAuthStore(); |
| 35 | const { sync: overleafSync, syncing, syncingProgress } = useSocketStore(); |
| 36 | |
| 37 | /** |
| 38 | * Sync document content to server based on platform |
| 39 | */ |
| 40 | const sync = useCallback( |
| 41 | async (options?: SyncOptions): Promise<SyncResult> => { |
| 42 | const projectId = adapter.getDocumentId?.() || getProjectId(); |
| 43 | const userId = user?.id || ""; |
| 44 | |
| 45 | googleAnalytics.fireEvent(userId, "sync_documents", { |
| 46 | projectId: projectId, |
| 47 | platform: adapter.platform, |
| 48 | }); |
| 49 | |
| 50 | try { |
| 51 | if (adapter.platform === "overleaf") { |
| 52 | // Overleaf: Use existing socket-based sync |
| 53 | const { session, gclb } = await getCookies(window.location.hostname); |
| 54 | await overleafSync( |
| 55 | userId, |
| 56 | projectId, |
| 57 | { |
| 58 | cookieOverleafSession2: session, |
| 59 | cookieGCLB: gclb, |
| 60 | }, |
| 61 | "unused", |
| 62 | ); |
| 63 | return { success: true }; |
| 64 | } else if (adapter.platform === "word") { |
| 65 | // Word: Get full text directly and upload to server |
| 66 | return await syncWordDocument(projectId, adapter, options); |
| 67 | } else { |
| 68 | // Browser or other platforms: Try to use adapter's getFullText |
| 69 | return await syncGenericDocument(projectId, adapter, options); |
| 70 | } |
| 71 | } catch (error) { |
| 72 | logError("Sync failed:", error); |
| 73 | return { success: false, error: error as Error }; |
| 74 | } |
| 75 | }, |
| 76 | [adapter, user?.id, overleafSync], |
| 77 | ); |
| 78 | |
| 79 | return { |
| 80 | sync, |
| 81 | syncing, |
| 82 | syncingProgress, |
| 83 | platform: adapter.platform, |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Sync Word document content to server |
no test coverage detected