Skip to content
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

Keyword generation #17

Merged
merged 6 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion app/[postTitle]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Tiptap as TipTap } from "@/components/TipTap";
import AnsPost from "@/components/queAnsPage/AnsPost";
import RecentFeed from "@/components/queAnsPage/RecentFeed";
import imageCompression from 'browser-image-compression';
import Image from "next/image";

import { auth, db, storage } from "@/utils/firebase";
import {
Expand Down Expand Up @@ -54,6 +55,8 @@ import { Tiptap } from "@/components/TipTapAns";
import { z } from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
import { AnswerDescriptionType } from "@/schemas/answer";
import { Table, TableBody, TableHead, TableHeader, TableRow } from "@/components/ui/table";
import { Separator } from "@/components/ui/separator";

type Input = z.infer<typeof AnswerDescriptionType>;

Expand Down Expand Up @@ -87,6 +90,7 @@ type QuestionType = {
shares: number;
questionImageURL: string;
createdAt: string;
keywords: string;
anonymity: boolean;
// Add any other fields as necessary
};
Expand Down Expand Up @@ -127,10 +131,27 @@ const PostPage = ({ params: { postTitle } }: Props) => {

const router = useRouter();
const [user, loading] = useAuthState(auth);
const [previewImg, setPreviewImg] = useState<any>(null);

const uploadImage = async(file: any) => {
if (file == null) return;

if (file) {
const reader = new FileReader();
reader.onload = (event) => {
if (event.target) {
const imageUrl = event.target.result;
setPreviewImg(imageUrl);
} else {
console.error('Error reading file:', file);
setPreviewImg(null);
}
};
reader.readAsDataURL(file);
} else {
setPreviewImg(null);
}

const storageRef = ref(storage, `answers/${file.name}`);

try {
Expand Down Expand Up @@ -266,6 +287,7 @@ const PostPage = ({ params: { postTitle } }: Props) => {
}, [postTitleWithSpaces, postTitle]);

const [description, setDescription] = useState("");
//console.log("Keywords ", queObject.keywords);

return (

Expand Down Expand Up @@ -309,7 +331,14 @@ const PostPage = ({ params: { postTitle } }: Props) => {
</FormItem>
)}
/>

{(progress||0)>0&&<span className='pt-3'>{`${Math.ceil((progress||0))} % Uploaded`}</span>}
<div>
{
previewImg&&<div className="w-full flex items-center justify-center">
<Image src={previewImg} alt="previewImage" width={250} height={250}/>
</div>
}
</div>
{/* anonymity toggle */}
<FormField
control={form.control}
Expand Down Expand Up @@ -359,9 +388,29 @@ const PostPage = ({ params: { postTitle } }: Props) => {
</div>
</div>
</div>

<div className=" sm:block hidden col-span-2 sticky overflow-hidden h-fit rounded-lg border border-gray-300 order-last ">
<div>
<RecentFeed />
</div>
<Separator className="h-2"/>
<div className="bg-[#FFFFFF] dark:bg-[#262626] order-last">
<Table>
<TableHeader>
<TableRow>
<TableHead className="text-left font-[590] text-base text-black dark:text-white">HashTags</TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableRow className="">
<div className="bg-[#FFFFFF] dark:bg-[#262626] p-3">
{queObject.keywords?queObject.keywords:"No tags Available"}
</div>
</TableRow>
</TableBody>
</Table>
</div>
</div>
</div>
);
};
Expand Down
61 changes: 58 additions & 3 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import { auth , db , storage } from "@/utils/firebase";
import { useAuthState } from "react-firebase-hooks/auth";
import { useRouter , useSearchParams } from "next/navigation";

import { addDoc, collection, serverTimestamp } from "firebase/firestore";
import { addDoc, collection, doc, onSnapshot, serverTimestamp, updateDoc } from "firebase/firestore";
import { ref , uploadBytes, uploadBytesResumable , getDownloadURL} from "firebase/storage";
import { DialogClose } from "@radix-ui/react-dialog";

Expand All @@ -79,10 +79,27 @@ export default function Home() {
const [imageUpload , setImageUpload] = useState<File | null>(null);
const [imageUrl, setImageUrl] = useState<string | null>(null);
const [progress , setProgress] = useState<number | null>(0);
const [previewImg, setPreviewImg] = useState<any>(null);

const uploadImage = async(file: any) => {
if(file == null) return;

if (file) {
const reader = new FileReader();
reader.onload = (event) => {
if (event.target) {
const imageUrl = event.target.result;
setPreviewImg(imageUrl);
} else {
console.error('Error reading file:', file);
setPreviewImg(null);
}
};
reader.readAsDataURL(file);
} else {
setPreviewImg(null);
}

const storageRef = ref(storage, `questions/${file.name}`);

try {
Expand Down Expand Up @@ -159,6 +176,8 @@ export default function Home() {
});

async function createQuestionPost(data: Input) {

console.log("creating");

const docRef = await addDoc(collection(db, "questions"), {
title: data.title,
Expand All @@ -172,13 +191,42 @@ export default function Home() {
// ansNumbers: 0,
});

const quesId = docRef.id;

toast({
title: "Question Posted",
description: "Your question has been posted successfully.",
});

try {
console.log("keyword Gen.....")
const docRef = await addDoc(collection(db, 'keywords'), {
prompt: `Generate some keywords and hashtags on topic ${data.title}`,
});
console.log('Keyword Document written with ID: ', docRef.id);

// Listen for changes to the document
const unsubscribe = onSnapshot(doc(db, 'keywords', docRef.id), async(snap) => {
const data = snap.data();
if (data && data.response) {
console.log('RESPONSE: ' + data.response);
const keywordsString = `${data.response}`;

const questionDocRef = doc(db, 'questions', quesId);
await updateDoc(questionDocRef, {
keywords: keywordsString, // Add your keywords here
});
}
});

// Stop listening after some time (for demonstration purposes)
setTimeout(() => unsubscribe(), 60000);
} catch (error) {
console.error('Error adding document: ', error);
}

console.log("Document written with ID: ", docRef.id);
console.log(data);
//console.log(data);
}

function onSubmit(data: Input) {
Expand Down Expand Up @@ -236,7 +284,6 @@ export default function Home() {
else
{


return (
<Suspense>
<>
Expand Down Expand Up @@ -353,6 +400,14 @@ export default function Home() {
{(progress||0)>0&&<span className='pt-3'>{`${Math.ceil((progress||0))} % Uploaded`}</span>}
{/* "0" to make upload percentage invisible when no image is selected */}
{/* anonymity toggle */}

<div>
{
previewImg&&<div className="w-full flex items-center justify-center">
<Image src={previewImg} alt="previewImage" width={250} height={250}/>
</div>
}
</div>
<FormField
control={form.control}
name="anonymity"
Expand Down
55 changes: 54 additions & 1 deletion components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ import { QuestionType } from "@/schemas/question";
import { db , storage } from "@/utils/firebase";
import { useSearchParams } from "next/navigation";

import { addDoc, collection, serverTimestamp } from "firebase/firestore";
import { addDoc, collection, doc, onSnapshot, serverTimestamp, updateDoc } from "firebase/firestore";
import { ref , uploadBytes, uploadBytesResumable , getDownloadURL} from "firebase/storage";
import { DialogClose } from "@radix-ui/react-dialog";
import MobileSidebar from "./MobileSidebar";
Expand Down Expand Up @@ -111,6 +111,7 @@ const Navbar = ({}: Props) => {
const [imageUpload , setImageUpload] = useState<File | null>(null);
const [imageUrl, setImageUrl] = useState<string | null>(null);
const [progress , setProgress] = useState<number | null>(0);
const [previewImg, setPreviewImg] = useState<any>(null);


const dispatch = useDispatch();
Expand All @@ -131,6 +132,22 @@ const Navbar = ({}: Props) => {
const uploadImage = async(file: any) => {
if(file == null) return;

if (file) {
const reader = new FileReader();
reader.onload = (event) => {
if (event.target) {
const imageUrl = event.target.result;
setPreviewImg(imageUrl);
} else {
console.error('Error reading file:', file);
setPreviewImg(null);
}
};
reader.readAsDataURL(file);
} else {
setPreviewImg(null);
}

const storageRef = ref(storage, `questions/${file.name}`);

try {
Expand Down Expand Up @@ -187,11 +204,40 @@ const Navbar = ({}: Props) => {
// ansNumbers: 0,
});

const quesId = docRef.id;

toast({
title: "Question Posted",
description: "Try refreshing in case you don't see your question",
})

try {
console.log("keyword Gen.....")
const docRef = await addDoc(collection(db, 'keywords'), {
prompt: `Generate some keywords and hashtags on topic ${data.title}`,
});
console.log('Keyword Document written with ID: ', docRef.id);

// Listen for changes to the document
const unsubscribe = onSnapshot(doc(db, 'keywords', docRef.id), async(snap) => {
const data = snap.data();
if (data && data.response) {
console.log('RESPONSE: ' + data.response);
const keywordsString = `${data.response}`;

const questionDocRef = doc(db, 'questions', quesId);
await updateDoc(questionDocRef, {
keywords: keywordsString, // Add your keywords here
});
}
});

// Stop listening after some time (for demonstration purposes)
setTimeout(() => unsubscribe(), 60000);
} catch (error) {
console.error('Error adding document: ', error);
}

console.log("Document written with ID: ", docRef.id);
console.log(data);
}
Expand Down Expand Up @@ -379,6 +425,13 @@ const Navbar = ({}: Props) => {
{(progress||0)>0&&<span className='pt-3'>{`${Math.ceil((progress||0))} % Uploaded`}</span>}
{/* "0" to make upload percentage invisible when no image is selected */}
{/* anonymity toggle */}
<div>
{
previewImg&&<div className="w-full flex items-center justify-center">
<Image src={previewImg} alt="previewImage" width={250} height={250}/>
</div>
}
</div>
<FormField
control={form.control}
name="anonymity"
Expand Down
Loading