mirror of
https://github.com/chase-manning/pokemon-js.git
synced 2025-11-14 22:28:06 +00:00
✨ add hook to get lates save
This commit is contained in:
parent
e693ea98b9
commit
03ea9e5192
1 changed files with 24 additions and 0 deletions
24
src/app/use-latest-save.ts
Normal file
24
src/app/use-latest-save.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import { useEffect, useState } from "react";
|
||||
import { db } from "./db";
|
||||
import { collection, getDocs } from "firebase/firestore";
|
||||
|
||||
const useLatestSave = () => {
|
||||
const [save, setSave] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const getSave = async () => {
|
||||
const collection_ = collection(db, "lien");
|
||||
const saves = await getDocs(collection_);
|
||||
const saveData = saves.docs.map((doc) => doc.data());
|
||||
if (saveData.length === 0) return;
|
||||
const latestSave = saveData.sort((a, b) => b.timestamp - a.timestamp)[0];
|
||||
setSave(latestSave.gameState);
|
||||
};
|
||||
|
||||
getSave();
|
||||
}, []);
|
||||
|
||||
return save;
|
||||
};
|
||||
|
||||
export default useLatestSave;
|
||||
Loading…
Add table
Add a link
Reference in a new issue