-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathconnect.tsx
More file actions
55 lines (49 loc) · 1.49 KB
/
connect.tsx
File metadata and controls
55 lines (49 loc) · 1.49 KB
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
import React from "react";
import { redirect } from "react-router-dom";
import { fireproof } from "@fireproof/core";
import { DEFAULT_ENDPOINT, SYNC_DB_NAME } from "../../helpers.js";
import { URI } from "@adviser/cement";
export async function connectDatabasesLoader({ request }: { request: Request }) {
const url = URI.from(request.url);
const localName = url.getParam("localName");
if (!localName) {
throw new Error("Local name is required");
}
const remoteName = url.getParam("remoteName");
const sanitizedRemoteName = remoteName?.replace(/^[^a-zA-Z0-9]+/g, "").replace(/[^a-zA-Z0-9]+/g, "_");
const endpoint = url.getParam("endpoint") || DEFAULT_ENDPOINT;
const syncDb = fireproof(SYNC_DB_NAME);
const keys = [localName];
if (remoteName) {
keys.push(remoteName);
}
const result = await syncDb.query<
{
localName: string;
remoteName: string;
},
[string, string]
>((doc) => [doc.localName, doc.remoteName], {
keys,
includeDocs: true,
});
if (result.rows.length === 0) {
const ok = await syncDb.put({
remoteName,
sanitizedRemoteName,
localName,
endpoint,
firstConnect: true,
});
console.log(ok);
} else {
const doc = result.rows[0].doc;
console.log(doc);
// TODO: Update the existing document if needed
// await syncDb.put({ ...doc, endpoint, lastConnect: new Date() });
}
return redirect(`/fp/databases/${sanitizedRemoteName}`);
}
export function DatabasesConnect() {
return <></>;
}