-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathdatabases.tsx
More file actions
135 lines (126 loc) · 4.99 KB
/
databases.tsx
File metadata and controls
135 lines (126 loc) · 4.99 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
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
import React, { useContext } from "react";
import { NavLink, useLoaderData, useNavigate } from "react-router-dom";
import { AppContext } from "../app-context.jsx";
import { SYNC_DB_NAME, truncateDbName } from "../helpers.js";
import { fireproof } from "@fireproof/core";
import { WithSidebar } from "../layouts/with-sidebar.jsx";
const reservedDbNames: string[] = [`fp.${SYNC_DB_NAME}`, "fp.petname_mappings", "fp.fp_sync"];
export async function databaseLoader(/*{ request }*/) {
const databases = await getIndexedDBNamesWithQueries();
return { databases };
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async function getIndexedDBNamesWithQueries(): Promise<{ name: string; queries: any[] }[]> {
try {
const databases = await indexedDB.databases();
const userDbs = databases
.filter((db) => db.name?.startsWith("fp.") && !db.name?.endsWith("_queries") && !reservedDbNames.includes(db.name))
.map((db) => db.name?.substring(3));
const dbsWithQueries = await Promise.all(
userDbs.map(async (dbName) => {
const queryDbName = `fp_${dbName}_queries`;
const queryDb = fireproof(queryDbName);
const allDocs = await queryDb.allDocs({ includeDocs: true });
const queries = allDocs.rows.map((row) => row.value);
return { name: dbName as string, queries };
}),
);
return dbsWithQueries;
} catch (error) {
console.error("Error fetching IndexedDB names and queries:", error);
return [];
}
}
export function Databases() {
return <WithSidebar sideBarComponent={<SidebarDatabases />} title="Databases" newUrl="/fp/databases/new" />;
}
const navLinks = [
{ to: "", label: "All Documents" },
{ to: "/history", label: "History" },
{ to: "/query", label: "Query" },
];
function SidebarDatabases() {
const { openMenu, toggleMenu, setIsSidebarOpen } = useContext(AppContext).sideBar;
const navigate = useNavigate();
const navigateToDatabase = (dbName: string) => {
navigate(`/fp/databases/${dbName}`);
setIsSidebarOpen(false); // Close sidebar on mobile after navigation
};
const { databases } = useLoaderData<{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
databases: { name: string; queries: any[] }[];
}>();
return (
<>
{databases.map((db) => (
<div key={db.name}>
<div className="flex items-center justify-between w-full">
<button
onClick={() => navigateToDatabase(db.name)}
className="flex-grow text-left rounded px-3 py-2 text-muted-foreground transition-colors hover:bg-muted hover:text-muted-foreground"
>
<span title={db.name}>{truncateDbName(db.name, 20)}</span>
</button>
<button
onClick={(e) => {
e.stopPropagation();
toggleMenu(db.name);
}}
className="flex items-center justify-center w-8 h-8 rounded transition-colors hover:bg-muted"
>
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className={`h-4 w-4 transition-transform duration-200 ${openMenu === db.name ? "rotate-180" : ""}`}
>
<polyline points="6 9 12 15 18 9"></polyline>
</svg>
</button>
</div>
<div
className={`pl-6 mt-2 space-y-2 overflow-hidden transition-all duration-200 ease-in-out ${
openMenu === db.name ? "max-h-[500px] opacity-100" : "max-h-0 opacity-0"
}`}
>
{navLinks.map((link) => (
<NavLink
end
key={link.to}
to={`/fp/databases/${db.name}${link.to}`}
className={({ isActive }) =>
`block rounded px-3 py-2 text-sm text-muted-foreground transition-colors hover:bg-muted hover:text-muted-foreground ${
isActive ? "font-bold" : ""
}`
}
onClick={() => setIsSidebarOpen(false)}
>
{link.label}
</NavLink>
))}
{db.queries.length > 0 && (
<div className="text-sm text-muted-foreground pl-3">
Saved Queries:
{db.queries.map((query, index) => (
<NavLink
key={index}
to={`/fp/databases/${db.name}/query/${query._id}`}
className="block rounded px-3 py-1 text-xs text-muted-foreground transition-colors hover:bg-muted hover:text-muted-foreground"
>
{query.name || `Query ${index + 1}`}
</NavLink>
))}
</div>
)}
</div>
</div>
))}
</>
);
}