This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
Files
scoresaber-reloadedv3/apps/frontend/src/components/loaders/database-loader.tsx

40 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-09-08 22:35:32 +01:00
"use client";
2024-10-04 16:43:12 +01:00
import { createContext, useEffect, useState } from "react";
import Database, { db } from "../../common/database/database";
2024-09-08 22:35:32 +01:00
import FullscreenLoader from "./fullscreen-loader";
import { useToast } from "@/hooks/use-toast";
2024-09-08 22:35:32 +01:00
/**
* The context for the database. This is used to access the database from within the app.
*/
export const DatabaseContext = createContext<Database | undefined>(undefined);
2024-10-04 16:43:12 +01:00
type Props = {
children: React.ReactNode;
2024-09-08 22:35:32 +01:00
};
2024-10-04 16:43:12 +01:00
export default function DatabaseLoader({ children }: Props) {
const { toast } = useToast();
2024-09-08 22:35:32 +01:00
const [database, setDatabase] = useState<Database | undefined>(undefined);
useEffect(() => {
const before = performance.now();
setDatabase(db);
2024-10-04 16:43:12 +01:00
console.log(`Loaded database in ${performance.now() - before}ms`);
2024-10-04 16:43:12 +01:00
db.on("ready", err => {
toast({
title: "Database loaded",
2024-10-04 16:43:12 +01:00
description: "The database was loaded successfully.",
});
});
2024-09-30 22:20:48 +01:00
}, [toast]);
2024-09-08 22:35:32 +01:00
return (
<DatabaseContext.Provider value={database}>
{database == undefined ? <FullscreenLoader reason="Loading database..." /> : children}
2024-09-08 22:35:32 +01:00
</DatabaseContext.Provider>
);
}