+ {/* {JSON.stringify(globalState)}
*/}
{getText('title_keywords')}
@@ -67,13 +92,21 @@ export default function Keywords() {
+
+
+ {showExplore && (
+
+ )}
+
+ >
);
}
\ No newline at end of file
diff --git a/src/utils/firebase.js b/src/utils/firebase.js
index f21dcf9..cb73ae5 100644
--- a/src/utils/firebase.js
+++ b/src/utils/firebase.js
@@ -1,6 +1,6 @@
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
-import { getFirestore, collection, doc, writeBatch, setDoc } from "firebase/firestore";
+import { getFirestore, collection, doc, writeBatch, setDoc, onSnapshot, getDoc } from "firebase/firestore";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
@@ -22,31 +22,38 @@ const app = initializeApp(firebaseConfig);
export function addKeywords(keywords) {
- console.log('Adding keywords to Firestore:', keywords);
+ // console.log('Adding keywords to Firestore:', keywords);
- const sortedKeywords = [...keywords].sort((a, b) => a.id - b.id);
+ // const sortedKeywords = [...keywords].sort((a, b) => a.id - b.id);
- const db = getFirestore(app);
- const batch = writeBatch(db);
+ // const db = getFirestore(app);
+ // const batch = writeBatch(db);
- sortedKeywords.forEach((keyword) => {
- const keywordRef = doc(db, "keywords", keyword.id.toString());
+ // sortedKeywords.forEach((keyword) => {
+ // const keywordRef = doc(db, "keywords", keyword.id.toString());
- const keywordDoc = {
- ...keyword,
- updatedTime: new Date(),
- };
-
- batch.set(keywordRef, keywordDoc);
- });
-
- batch.commit()
- .then(() => {
- console.log("Keywords added successfully!");
- })
- .catch((error) => {
- console.error("Error adding keywords: ", error);
+ // const keywordDoc = {
+ // ...keyword,
+ // updatedTime: new Date(),
+ // };
+
+ // batch.set(keywordRef, keywordDoc);
+ // });
+
+ // batch.commit()
+ // .then(() => {
+ // console.log("Keywords added successfully!");
+ // })
+ // .catch((error) => {
+ // console.error("Error adding keywords: ", error);
+ // });
+
+
+ setState({
+ state: 'keywords',
+ keywords: keywords,
+ updatedTime: new Date(),
});
}
@@ -64,4 +71,48 @@ export function lookatKeyword(keyword) {
title: keyword.title,
updatedTime: new Date(),
});
-}
\ No newline at end of file
+}
+
+export function setState(state) {
+
+ console.log('Setting state in Firestore:', state);
+
+ const db = getFirestore(app);
+ const stateRef = doc(collection(db, "state"), 'latest_page');
+
+ setDoc(stateRef, {
+ ...state,
+ updatedTime: new Date(),
+ });
+}
+
+export function onStateChange(callback) {
+
+ const db = getFirestore(app);
+ const stateRef = doc(collection(db, "state"), 'latest_page');
+
+ const unsubscribe = onSnapshot(stateRef, (doc) => {
+ if (doc.exists()) {
+ callback(doc.data());
+ } else {
+ console.log("No such document!");
+ }
+ });
+
+ return unsubscribe;
+}
+
+
+export async function getState(){
+ const db = getFirestore(app);
+ const stateRef = doc(collection(db, "state"), 'latest_page');
+
+ const state_doc=await getDoc(stateRef);
+ if (state_doc.exists()) {
+ return state_doc.data();
+ } else {
+ console.log("No such document!");
+ return null;
+ }
+}
+
diff --git a/src/utils/usetext.jsx b/src/utils/usetext.jsx
index 5be94c0..85de720 100644
--- a/src/utils/usetext.jsx
+++ b/src/utils/usetext.jsx
@@ -1,5 +1,6 @@
import { createContext, useContext, useEffect, useState } from "react";
import useSWR from "swr";
+import { onStateChange } from "./firebase";
const TextContent=createContext();
@@ -20,9 +21,10 @@ export function TextProvider({children}) {
const [nextLang, setNextLang]=useState('en');
const [selectedKeyword, setSelectedKeyword] = useState([]);
+ const [globalState, setGlobalState] = useState(null);
function getText(key) {
- console.log("getText", key, lang, data?.[key]?.[lang]);
+ // console.log("getText", key, lang, data?.[key]?.[lang]);
return data?.[key]?.[lang] || "";
}
@@ -55,13 +57,56 @@ export function TextProvider({children}) {
setSelectedKeyword([]);
}
+ function onGlobalKeywordChange(new_state){
+ if(new_state.state!=='keywords') return;
+
+ console.log('Global keyword change detected', new_state);
+
+
+ setGlobalState(new_state);
+
+ if(globalStateAvailable(new_state)){
+ const keywords=new_state.keywords || [];
+ setSelectedKeyword(keywords);
+ }
+ }
+
+ function globalStateAvailable(new_state){
+
+ const state=new_state || globalState;
+
+ if(!state || !state.state) return false;
+
+
+ const time=new Date(state?.updatedTime.seconds*1000 + state?.updatedTime.nanoseconds/1000000);
+ const now=new Date();
+ const diff=(now.getTime()-time.getTime())/1000; // in seconds
+
+ console.log('Checking global state availability', state, diff);
+ return state && state.state==='keywords' && diff<30;
+ }
+
+
+
useEffect(()=>{
console.log("selectedKeyword changed", selectedKeyword);
}, [selectedKeyword]);
+ useEffect(()=>{
+
+ const unsubscribe=onStateChange(onGlobalKeywordChange);
+
+ return () => {
+ unsubscribe();
+ };
+
+ },[]);
+
return (
-