-
Notifications
You must be signed in to change notification settings - Fork 23
remove supabase dependency #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import { useEffect, useRef, useState } from "react"; | ||
|
|
||
| import { ThumbDownIcon, ThumbUpIcon } from "@/chakra/custom-chakra-icons"; | ||
| import { SupaBaseDatabase } from "@/database/database"; | ||
| import { isSupabaseInitialized, addFeedback } from "@/database/database"; | ||
| import { AnswerQuality, FeedbackPayload, Ratings } from "@/types"; | ||
| import { Button, Flex, Text } from "@chakra-ui/react"; | ||
|
|
||
|
|
@@ -52,15 +52,13 @@ const Rating = ({ isResponseGenerated, feedbackId }: RatingProps) => { | |
| if (feedback.rating === Ratings.NEGATIVE && !feedback.answerQuality) { | ||
| return; | ||
| } | ||
| const { status, error } = await SupaBaseDatabase.getInstance().addFeedback({ | ||
| ...feedback, | ||
| feedbackId, | ||
| }); | ||
|
|
||
| if (status >= 200 && status < 300 && !error) { | ||
| setIsFeedbackSubmitted(true); | ||
| console.log("Feedback sent successfully"); | ||
| if (isSupabaseInitialized) { | ||
| await addFeedback(feedback, feedbackId); | ||
| } else { | ||
| console.error('Cannot submit rating because supabase is not initialized'); | ||
| } | ||
| setIsFeedbackSubmitted(true); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't want to do this if the db isn't initialised
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's the only way that the page closes |
||
| }; | ||
|
|
||
| if (!isResponseGenerated) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -8,12 +8,23 @@ const SUPABASE_URL = publicRuntimeConfig.SUPABASE_URL; | |||||
| const SUPABASE_ANON_KEY = publicRuntimeConfig.SUPABASE_ANON_KEY; | ||||||
| const DB_NAME = publicRuntimeConfig.DB_NAME; | ||||||
|
|
||||||
| export const isSupabaseInitialized = SUPABASE_URL !== undefined && SUPABASE_ANON_KEY !== undefined && SUPABASE_URL !== "" && SUPABASE_ANON_KEY !== ""; | ||||||
adamjonas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| // Initialize Supabase client | ||||||
| let supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY); | ||||||
| let supabase = null; | ||||||
|
|
||||||
| if (SUPABASE_URL) { | ||||||
| supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY); | ||||||
| } else { | ||||||
| console.error('SUPABASE_URL is not defined in .env file'); | ||||||
| } | ||||||
|
|
||||||
| // Example usage: Fetch all rows from a table named "tasks" | ||||||
| export class SupaBaseDatabase { | ||||||
| static getInstance() { | ||||||
| if (!supabase) { | ||||||
| throw new Error('Supabase has not been initialized because SUPABASE_URL is not defined'); | ||||||
| } | ||||||
| return new SupaBaseDatabase(); | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -83,3 +94,56 @@ export class SupaBaseDatabase { | |||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| export const getCachedAnswer = async (question: string, author?: string) => { | ||||||
| question = question.toLowerCase(); | ||||||
| author = author?.toLocaleLowerCase(); | ||||||
adamjonas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| const answers = await SupaBaseDatabase.getInstance().getAnswerByQuestion( | ||||||
| question, | ||||||
| author | ||||||
| ); | ||||||
|
|
||||||
| if (!answers || answers.length === 0) { | ||||||
| console.error("Error fetching answer: No answers found."); | ||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| // Use JavaScript .find() method to get first element where answer is not an empty string | ||||||
| const nonEmptyAnswer = answers.find((item) => item.answer.trim() !== ""); | ||||||
|
|
||||||
| if (!nonEmptyAnswer) { | ||||||
| console.error("Error fetching answer: No non-empty answers found."); | ||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| // Return the nonEmptyAnswer directly as a string | ||||||
| return createReadableStream(nonEmptyAnswer.answer); | ||||||
| }; | ||||||
|
|
||||||
| export const addDocumentToSupabase = async (payload: any) => { | ||||||
| await SupaBaseDatabase.getInstance().insertData(payload); | ||||||
| }; | ||||||
|
|
||||||
| function createReadableStream(text: string) { | ||||||
| const encoder = new TextEncoder(); | ||||||
| const readable = new ReadableStream({ | ||||||
| start(controller) { | ||||||
| controller.enqueue(encoder.encode(text)); | ||||||
| controller.close(); | ||||||
| }, | ||||||
| }); | ||||||
| return readable; | ||||||
| } | ||||||
|
|
||||||
| export const addFeedback = async (feedback: FeedbackPayload, feedbackId: string) => { | ||||||
| const { status, error } = await SupaBaseDatabase.getInstance().addFeedback({ | ||||||
| ...feedback, | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to spread the feedback object. Pass it directly.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get this from my linter when I change to your suggestion:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay. its a type issue but this is fine too |
||||||
| feedbackId, | ||||||
| }); | ||||||
|
|
||||||
| if (status >= 200 && status < 300 && !error) { | ||||||
| console.log("Feedback sent successfully"); | ||||||
| return true; | ||||||
| } | ||||||
| return false; | ||||||
| }; | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this function returns true or false, we should assign the return value a variable and check if the feedback was submitted. Only then should we
setFeedBackSubmittedto trueThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, this is a hack to get the page to close.