Skip to content
Snippets Groups Projects
Commit d5b0074c authored by Piotrek's avatar Piotrek
Browse files

Fix: Fixed a major issue that was causing too many unnecessary reads from the database

parent a61548dd
No related branches found
No related tags found
1 merge request!2Fix: Multiple fixes across the app with the main being the popup now always...
......@@ -9,7 +9,7 @@ export {}
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyAIXCufoNziwwkDEj6pF1pFs1b2ogqtITw",
apiKey: process.env.KEY,
authDomain: "leaf-0001.firebaseapp.com",
projectId: "leaf-0001",
storageBucket: "leaf-0001.appspot.com",
......
import dataset from "assets/dataset_small.json"
import { collection, getDocs, type DocumentData } from "firebase/firestore/lite"
import {
collection,
getDocs,
limit,
query,
where,
type DocumentData,
} from "firebase/firestore/lite"
import type { PlasmoMessaging } from "@plasmohq/messaging"
// import initialized db instance
import { db } from "~background"
// Export it!
interface data_small {
brand: string
country: string
environmental_total: number
supplychain_total: number
social_total: number
}
import type { data_small } from "~interfaces/data_small"
interface url_data {
name: string
......@@ -22,11 +21,24 @@ interface url_data {
const data: data_small[] = dataset
async function queryUrls() {
const urlsCollection = collection(db, "urls")
const urlsSnapshot = await getDocs(urlsCollection)
const urlsList = urlsSnapshot.docs.map((doc) => doc.data())
return urlsList
async function queryUrls(currentTab: chrome.tabs.Tab) {
let answer
const url = currentTab.url.split("/")[2]
const urlsCollection = query(
collection(db, "urls"),
where("url", "==", url),
limit(1),
)
const brandsSnapshot = await getDocs(urlsCollection)
if (brandsSnapshot.empty) {
return answer
} else {
brandsSnapshot.forEach((doc) => {
answer = doc.data()
console.log(answer)
})
return answer
}
}
function ifExists(tokens: string) {
......@@ -40,11 +52,13 @@ function getAnswers(tokens: string, validUrl: DocumentData) {
let secondAnswer
let mainAnswer: data_small = ifExists(tokens)
// chack if mainAnswer isnt undefined and identify any brands from our dataset in the url
if (mainAnswer && validUrl.name.toLowerCase().includes(mainAnswer.brand.toLowerCase())) {
if (
mainAnswer &&
validUrl.name.toLowerCase().includes(mainAnswer.brand.toLowerCase())
) {
secondAnswer = mainAnswer
mainAnswer = ifExists(tokens.replace(secondAnswer.brand, ""))
} else {
console.log("this must be the problem")
secondAnswer = ifExists(validUrl.name)
}
//make sure that if only second answer is found then it is displayed
......@@ -54,10 +68,9 @@ function getAnswers(tokens: string, validUrl: DocumentData) {
const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
let answer
const urls = await queryUrls()
const queryOptions = { active: true, lastFocusedWindow: true }
const [tab]: chrome.tabs.Tab[] = await chrome.tabs.query(queryOptions)
const validUrl = urls.find((doc) => tab.url.includes(doc.url))
const validUrl = await queryUrls(tab)
const tokens: string = await tab.title
if (validUrl) {
answer = getAnswers(tokens, validUrl)
......
......@@ -4,9 +4,9 @@ import {
limit,
query,
where,
type DocumentData
type DocumentData,
} from "firebase/firestore/lite"
import type data_advanced from "interfaces/data_advanced"
import type { BrandData } from "interfaces/data_advanced"
import type { PlasmoMessaging } from "@plasmohq/messaging"
......@@ -17,12 +17,11 @@ async function queryAdvancedData(brandName: string) {
const brandsCollection = query(
collection(db, "companies"),
where("brand", "==", brandName),
limit(1)
limit(1),
)
const brandsSnapshot = await getDocs(brandsCollection)
brandsSnapshot.forEach((doc) => {
brandsData = doc.data()
console.log(brandsData)
})
return brandsData
}
......@@ -31,7 +30,7 @@ const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
try {
const response = await queryAdvancedData(req.body.name)
res.send({
response
response,
})
} catch (e) {
console.log(e)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment