-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added integration with analysis endpoint
- Loading branch information
Showing
8 changed files
with
128 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export const KEY_ANALYSIS_LANDING = 'gptube-analysis' | ||
export const KEY_ANALYSIS_LANDING = 'gptube-analysis' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export const DEFAULT_PAGE_PAGINATION = 1 | ||
|
||
export const DEFAULT_PAGE_SIZE_PAGINATION = 10 | ||
|
||
export const CONTACT_SUPPORT_X = '@node_srojas1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import type { ModelsYoutubeAnalyzerReqBody } from "@/gptube-api"; | ||
import type { FormEvent } from "react"; | ||
|
||
import { useMutation, useQueryClient } from "@tanstack/react-query"; | ||
import { useEffect } from "react"; | ||
import toast from "react-hot-toast"; | ||
|
||
import { apiClient } from "@/gptube-api"; | ||
import { getAnalysisLandingFromCache } from "@/services/get-analysis-landing-from-cache.service"; | ||
import { extractYTVideoID } from "@/utils"; | ||
|
||
import { videoQueryKeys } from "./video-query-keys"; | ||
import { | ||
DEFAULT_PAGE_PAGINATION, | ||
DEFAULT_PAGE_SIZE_PAGINATION, | ||
CONTACT_SUPPORT_X, | ||
} from "@/constants/general.constants"; | ||
|
||
export function useDashboardAnalysis() { | ||
const queryClient = useQueryClient(); | ||
const mutation = useMutation({ | ||
mutationFn: (data: ModelsYoutubeAnalyzerReqBody) => { | ||
if (!data.email || data.email === "") { | ||
toast.success("Analysis started, please wait..."); | ||
} else { | ||
toast.success("Analysis started, the results will be sent to your email."); | ||
} | ||
return apiClient.apiYoutubeAnalysisPost({ | ||
video: data, | ||
}); | ||
}, | ||
onSuccess: (data) => { | ||
queryClient.invalidateQueries( | ||
videoQueryKeys.videosAnalyzed({ | ||
// TODO: implement authentication | ||
userId: "1", | ||
page: DEFAULT_PAGE_PAGINATION, | ||
pageSize: DEFAULT_PAGE_SIZE_PAGINATION, | ||
}), | ||
{ exact: true }, | ||
); | ||
queryClient.setQueryData(videoQueryKeys.videoAnalysis(data.videoId || ""), data); | ||
}, | ||
onError: (error) => { | ||
if (error instanceof Error) { | ||
toast.error(error.message); | ||
return; | ||
} | ||
toast.error( | ||
`Something went wrong. Please try again later or contact ${CONTACT_SUPPORT_X} on X.`, | ||
); | ||
}, | ||
}); | ||
|
||
const handleAnalysis = async (userId: string, videoId: string, email?: string) => { | ||
const data: ModelsYoutubeAnalyzerReqBody = { | ||
userId: userId, | ||
videoId: videoId, | ||
}; | ||
if (email && email !== "") { | ||
data["email"] = email; | ||
} | ||
await mutation.mutateAsync(data); | ||
}; | ||
|
||
useEffect(() => { | ||
if (mutation.error instanceof Error) { | ||
toast.error(mutation.error.message); | ||
} | ||
}, [mutation.error, mutation.isError]); | ||
|
||
return { | ||
handleAnalysis, | ||
isLoading: mutation.isLoading, | ||
dataAnalysis: mutation.data, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
export const videoQueryKeys = { | ||
videoPreview: (videoURL: string) => ['video-preview', videoURL] as const, | ||
videoPreview: (videoURL: string) => ["video-preview", videoURL] as const, | ||
videoAnalysis: (videoId: string) => ["video-analysis", videoId] as const, | ||
videosAnalyzed: (args: { userId: string; page: number; pageSize: number }) => | ||
['videos-analyzed', { ...args }] as const, | ||
videoStats: (args: { userId: string; videoId: string }) => ['video-stats', { ...args }] as const, | ||
["videos-analyzed", { ...args }] as const, | ||
videoStats: (args: { userId: string; videoId: string }) => ["video-stats", { ...args }] as const, | ||
videoNegativeComments: (args: { | ||
userId: string | ||
videoId: string | ||
page: number | ||
pageSize: number | ||
}) => ['video-negative-comments', { ...args }] as const, | ||
videoLanding: () => ['video-landing'] as const, | ||
} | ||
userId: string; | ||
videoId: string; | ||
page: number; | ||
pageSize: number; | ||
}) => ["video-negative-comments", { ...args }] as const, | ||
videoLanding: () => ["video-landing"] as const, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
import { YOUTUBE_URL_REGEX } from '@/constants/youtube.constants' | ||
import { YOUTUBE_URL_REGEX } from "@/constants/youtube.constants"; | ||
|
||
const EMAIL_REGEX = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$/i | ||
const EMAIL_REGEX = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$/i; | ||
|
||
export function isValidYoutubeUrl(url: string) { | ||
if (url.length === 0) return true | ||
|
||
return url.match(YOUTUBE_URL_REGEX) !== null | ||
if (url.length === 0) return false; | ||
return url.match(YOUTUBE_URL_REGEX) !== null; | ||
} | ||
|
||
export function isValidEmail(email: string) { | ||
return EMAIL_REGEX.exec(email) !== null | ||
return EMAIL_REGEX.exec(email) !== null; | ||
} |