Skip to content

Commit

Permalink
discover: youtube
Browse files Browse the repository at this point in the history
  • Loading branch information
vantage-ola committed Aug 26, 2024
1 parent f638fba commit 87b8be0
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 17 deletions.
3 changes: 3 additions & 0 deletions tracknow/web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { UserLogin } from "./components/User/UserLogin";
import { UserSignUp } from "./components/User/UserSignUp";
import { Welcome } from "./components/Welcome/Welcome";
import { Home } from "./components/Home/Home";
import Discover from "./components/Discover/Discover";

import UserAddLaptimes from "./components/User/UserAddLaptimes";
import UserAccountSettings from "./components/User/UserAccountSettings";
Expand Down Expand Up @@ -55,6 +56,7 @@ export const App = () => {
<Route path="/create-user" element={<UserSignUp />} />
</Route>


{/*user is logged in */}
<Route element={
<UserProvider>
Expand All @@ -67,6 +69,7 @@ export const App = () => {
</Flex>
}>

<Route path="/discover" element={<Discover />} />

<Route
path="/home"
Expand Down
55 changes: 45 additions & 10 deletions tracknow/web/src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ export interface Laptime {
platform?: string;
youtube_link?: string;
comment: string;
}
};

export interface CreateLaptimeResponse {
"Laptime Added Successfully": Laptime;
by: string;
}
};

export interface GetUserLaptimesResponse {
title: string;
Expand All @@ -54,31 +54,31 @@ export interface GetUserLaptimesResponse {
id: number;
by: string;
date_created: string;
}
};

export interface identity {
"message": "User Found";
id: number;
name: string; // username
pp: string; // profile_pic
}
};

export interface identityProfile {
name: string; // username
pp: string; // profile_pic
onOpen: () => void;
}
};

export interface EditUser {
username?: string;
password?: string;
nationality?: string;

}
};

export interface EditUserPic {
profile_picture_url?: string;
}
};

export interface UserData {
userId: number;
Expand All @@ -87,7 +87,7 @@ export interface UserData {
editProfile: (editProfile: EditUser) => Promise<void>;
editProfilePic: (editProfilePic: EditUserPic) => Promise<void>;
loading: boolean
}
};

export interface F1DriverStanding {
position: string;
Expand All @@ -97,12 +97,47 @@ export interface F1DriverStanding {
};
Constructors: Array<{ name: string }>;
points: string;
}
};

export interface F1ConstructorStanding {
position: string;
Constructor: {
name: string;
};
points: string;
}
};

export interface YoutubeSearchResult {
kind: string;
etag: string;
id: {
kind: string;
videoId: string;
};
snippet: {
publishedAt: string;
channelId: string;
title: string;
description: string;
thumbnails: {
default: {
url: string;
width: number;
height: number;
};
medium: {
url: string;
width: number;
height: number;
};
high: {
url: string;
width: number;
height: number;
};
};
channelTitle: string;
liveBroadcastContent: string;
publishTime: string;
};
};
11 changes: 11 additions & 0 deletions tracknow/web/src/components/Discover/Discover.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Youtube from "./Youtube";
const Discover: React.FC = () => {

Check warning on line 2 in tracknow/web/src/components/Discover/Discover.tsx

View check run for this annotation

Codecov / codecov/patch

tracknow/web/src/components/Discover/Discover.tsx#L2

Added line #L2 was not covered by tests

return (

Check warning on line 4 in tracknow/web/src/components/Discover/Discover.tsx

View check run for this annotation

Codecov / codecov/patch

tracknow/web/src/components/Discover/Discover.tsx#L4

Added line #L4 was not covered by tests
<>
<Youtube />
</>
);
};

export default Discover;
99 changes: 99 additions & 0 deletions tracknow/web/src/components/Discover/Youtube.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import * as React from "react";
import { Box, Card, CardBody, Flex, HStack, Icon, Stack, Text } from "@chakra-ui/react";
import useMiscFunctions from "../../misc/miscFunctions";
import { FaYoutube, FaHashtag } from "react-icons/fa";
import { formatDistanceToNow } from "date-fns";

import getInternetData from "../../hooks/useInternet";
import { YoutubeSearchResult } from "../../Types";

const Youtube: React.FC = () => {

Check warning on line 10 in tracknow/web/src/components/Discover/Youtube.tsx

View check run for this annotation

Codecov / codecov/patch

tracknow/web/src/components/Discover/Youtube.tsx#L10

Added line #L10 was not covered by tests

const { LazyLoadYoutubeEmbed } = useMiscFunctions();
const { fetchYoutube } = getInternetData();

Check warning on line 13 in tracknow/web/src/components/Discover/Youtube.tsx

View check run for this annotation

Codecov / codecov/patch

tracknow/web/src/components/Discover/Youtube.tsx#L12-L13

Added lines #L12 - L13 were not covered by tests

const [youtube, setYoutube] = React.useState<YoutubeSearchResult[]>([]);

Check warning on line 15 in tracknow/web/src/components/Discover/Youtube.tsx

View check run for this annotation

Codecov / codecov/patch

tracknow/web/src/components/Discover/Youtube.tsx#L15

Added line #L15 was not covered by tests

React.useEffect(() => {
const fetchData = async () => {
const youtubeData = await fetchYoutube();
setYoutube(youtubeData);

Check warning on line 20 in tracknow/web/src/components/Discover/Youtube.tsx

View check run for this annotation

Codecov / codecov/patch

tracknow/web/src/components/Discover/Youtube.tsx#L17-L20

Added lines #L17 - L20 were not covered by tests
};
fetchData();

Check warning on line 22 in tracknow/web/src/components/Discover/Youtube.tsx

View check run for this annotation

Codecov / codecov/patch

tracknow/web/src/components/Discover/Youtube.tsx#L22

Added line #L22 was not covered by tests
})
return (

Check warning on line 24 in tracknow/web/src/components/Discover/Youtube.tsx

View check run for this annotation

Codecov / codecov/patch

tracknow/web/src/components/Discover/Youtube.tsx#L24

Added line #L24 was not covered by tests
<>
{youtube.map((video) => (
<Card key={video.id.videoId}>

Check warning on line 27 in tracknow/web/src/components/Discover/Youtube.tsx

View check run for this annotation

Codecov / codecov/patch

tracknow/web/src/components/Discover/Youtube.tsx#L27

Added line #L27 was not covered by tests
<CardBody>
<Flex justifyContent={"space-between"} p={2}>
<Text as="b">{video.snippet.title}</Text>
<Text fontSize="smaller" color={"GrayText"}>
{formatDistanceToNow(new Date(video.snippet.publishedAt), { addSuffix: true })}
</Text>
</Flex>
<Box>
<LazyLoadYoutubeEmbed youtubeLink={video.id.videoId} />
</Box>
<Box p={2}>
<Stack direction={"row"} spacing={2} align="flex-start" flexShrink="0">
<Box
display={"inline-block"}
px={2}
py={1}
color="white"
mb={2}
>
<Flex justifyContent={"space-between"}>
<HStack>
<Icon color="red" as={FaYoutube} />
<Text color={"GrayText"} fontSize={"xs"} fontWeight="medium">
{video.snippet.channelTitle}
</Text>
</HStack>
</Flex>
</Box>
<Box
display={"inline-block"}
px={2}
py={1}
color="white"
mb={2}
>
<Flex justifyContent={"space-between"}>
<HStack>
<Icon color="red" as={FaHashtag} />
<Text color={"GrayText"} fontSize={"xs"} fontWeight="medium">
simracing
</Text>
</HStack>
</Flex>
</Box>
<Box
display={"inline-block"}
px={2}
py={1}
color="white"
mb={2}
>
<Flex justifyContent={"space-between"}>
<HStack>
<Icon color="red" as={FaHashtag} />
<Text color={"GrayText"} fontSize={"xs"} fontWeight="medium">
motorsports
</Text>
</HStack>
</Flex>
</Box>
</Stack>
</Box>
<Text fontSize={"smaller"}>{video.snippet.description}</Text>
</CardBody>
</Card>
))}
</>
);

};

export default Youtube;
4 changes: 2 additions & 2 deletions tracknow/web/src/components/SideBar/LeftSideBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface SideItemProps extends FlexProps {

const LinkItems: Array<LinkItemProps> = [
{ name: 'Home', icon: FiHome, disabled: false },
{ name: 'Discover', icon: FiGlobe, disabled: true },
{ name: 'Discover', icon: FiGlobe, disabled: false },
{ name: 'Events', icon: FiCalendar, disabled: true },
{ name: 'Leaderboard', icon: FiAward, disabled: true },
{ name: 'Setups', icon: FiSettings, disabled: true },
Expand All @@ -31,8 +31,8 @@ const LinkItems: Array<LinkItemProps> = [

const linkMap: { [key: string]: string } = {
Home: "/home",
Discover: "/discover",

// TODO make this items open in a new page.
Donate: 'https://www.buymeacoffee.com/vantageola',
Contribute: 'https://github.com/vantage-ola',

Expand Down
25 changes: 23 additions & 2 deletions tracknow/web/src/hooks/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ const endpoints = {

// formula 1 standings
FORMULA_1_TEAMS: `${API_PREFIX_URL}/f1/teams`,
FORMULA_1_DRIVERS: `${API_PREFIX_URL}/f1/drivers`
FORMULA_1_DRIVERS: `${API_PREFIX_URL}/f1/drivers`,

// content from internet
YOUTUBE: (date: string) => `${API_PREFIX_URL}/internet/youtube?date=${date}`

Check warning on line 43 in tracknow/web/src/hooks/API.ts

View check run for this annotation

Codecov / codecov/patch

tracknow/web/src/hooks/API.ts#L43

Added line #L43 was not covered by tests

};

Expand Down Expand Up @@ -331,6 +334,23 @@ async function fetchF1Drivers() {
return data;
};

// function to fetch daily youtube data
async function fetchYoutube(date: string) {
const response = await fetch(endpoints.YOUTUBE(date), {

Check warning on line 339 in tracknow/web/src/hooks/API.ts

View check run for this annotation

Codecov / codecov/patch

tracknow/web/src/hooks/API.ts#L338-L339

Added lines #L338 - L339 were not covered by tests
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
});

if (!response.ok) {
throw new Error(`Failed to fetch youtube data`)

Check warning on line 347 in tracknow/web/src/hooks/API.ts

View check run for this annotation

Codecov / codecov/patch

tracknow/web/src/hooks/API.ts#L347

Added line #L347 was not covered by tests
};

const data = await response.json();
return data;

Check warning on line 351 in tracknow/web/src/hooks/API.ts

View check run for this annotation

Codecov / codecov/patch

tracknow/web/src/hooks/API.ts#L350-L351

Added lines #L350 - L351 were not covered by tests
};

const API = {
fetchUser,
fetchUsers,
Expand All @@ -345,7 +365,8 @@ const API = {
fetchAUserLaptime,
fetchUsersLaptimes,
fetchF1Drivers,
fetchF1Teams
fetchF1Teams,
fetchYoutube
};


Expand Down
23 changes: 23 additions & 0 deletions tracknow/web/src/hooks/useInternet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import API from "./API";

const getInternetData = () => {

Check warning on line 3 in tracknow/web/src/hooks/useInternet.ts

View check run for this annotation

Codecov / codecov/patch

tracknow/web/src/hooks/useInternet.ts#L3

Added line #L3 was not covered by tests

const fetchYoutube = async () => {

Check warning on line 5 in tracknow/web/src/hooks/useInternet.ts

View check run for this annotation

Codecov / codecov/patch

tracknow/web/src/hooks/useInternet.ts#L5

Added line #L5 was not covered by tests

const currentDate = new Date().toLocaleDateString('en-CA'); //YYYY-MM-DD

Check warning on line 7 in tracknow/web/src/hooks/useInternet.ts

View check run for this annotation

Codecov / codecov/patch

tracknow/web/src/hooks/useInternet.ts#L7

Added line #L7 was not covered by tests

try {
const response = await API.fetchYoutube(currentDate);
return response;

Check warning on line 11 in tracknow/web/src/hooks/useInternet.ts

View check run for this annotation

Codecov / codecov/patch

tracknow/web/src/hooks/useInternet.ts#L9-L11

Added lines #L9 - L11 were not covered by tests

} catch (error) {
throw new Error("Error Fetching Youtube Data !");

Check warning on line 14 in tracknow/web/src/hooks/useInternet.ts

View check run for this annotation

Codecov / codecov/patch

tracknow/web/src/hooks/useInternet.ts#L14

Added line #L14 was not covered by tests
}


};

return { fetchYoutube }

Check warning on line 20 in tracknow/web/src/hooks/useInternet.ts

View check run for this annotation

Codecov / codecov/patch

tracknow/web/src/hooks/useInternet.ts#L20

Added line #L20 was not covered by tests
};

export default getInternetData;
7 changes: 4 additions & 3 deletions tracknow/web/src/misc/miscFunctions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ const useMiscFunctions = () => {
overflow: "hidden",
};

const getYouTubeId = (url: string) => {
const getYouTubeId = (urlOrId: string) => {

Check warning on line 30 in tracknow/web/src/misc/miscFunctions.tsx

View check run for this annotation

Codecov / codecov/patch

tracknow/web/src/misc/miscFunctions.tsx#L30

Added line #L30 was not covered by tests
const regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
const match = url.match(regExp);
return (match && match[7].length === 11) ? match[7] : false;
const match = urlOrId.match(regExp);

Check warning on line 32 in tracknow/web/src/misc/miscFunctions.tsx

View check run for this annotation

Codecov / codecov/patch

tracknow/web/src/misc/miscFunctions.tsx#L32

Added line #L32 was not covered by tests
return (match && match[7].length === 11) ? match[7] : urlOrId;
}

const youtubeID = getYouTubeId(youtubeLink);
Expand All @@ -50,6 +50,7 @@ const useMiscFunctions = () => {
);
};


const dummyLaptimes = [
{
title: "Fast Lap at Brands Hatch",
Expand Down

0 comments on commit 87b8be0

Please sign in to comment.