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

Dev to main #70

Closed
wants to merge 2 commits into from
Closed
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
39 changes: 27 additions & 12 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { initializeApp } from "firebase-admin/app";
import { getFirestore } from "firebase-admin/firestore";
import { onSchedule } from "firebase-functions/v2/scheduler";
import { onRequest } from "firebase-functions/v2/https";
import { Streamer } from "../types";
import { YoutubeClient, TwitchClient, TwitCastingClient } from "./api";
import { defineConfig, sortStreams } from "./utils";
import { createMaster } from "./createMater";

initializeApp();
const config = defineConfig();
Expand All @@ -14,20 +16,24 @@ const getStreamerMaster = async () => {
const youtubeChannelIdMap = new Map<string, string>();
const twitchChannelIdMap = new Map<string, string>();
const twitCastingChannelIdMap = new Map<string, string>();
const baseStreamers: Record<string, { order: number }> = {};

const master = await db.collection(config.collection.master.value()).get();
master.forEach((doc) => {
const { youtube, twitch, twitCasting } = doc.data();
const id = doc.id;
const { youtube, twitch, twitCasting, order } = doc.data();

if (youtube) youtubeChannelIdMap.set(youtube, doc.id);
if (twitch) twitchChannelIdMap.set(twitch, doc.id);
if (twitCasting) twitCastingChannelIdMap.set(twitCasting, doc.id);
if (youtube) youtubeChannelIdMap.set(youtube, id);
if (twitch) twitchChannelIdMap.set(twitch, id);
if (twitCasting) twitCastingChannelIdMap.set(twitCasting, id);
if (Number.isFinite(order)) baseStreamers[id] = { order };
});

return {
youtube: youtubeChannelIdMap,
twitch: twitchChannelIdMap,
twitCasting: twitCastingChannelIdMap,
baseStreamers,
};
};

Expand All @@ -43,7 +49,7 @@ export const getStreamers = onSchedule(
},
async () => {
// init
const master = await getStreamerMaster();
const { baseStreamers, ...idMap } = await getStreamerMaster();
const tokenDoc = db
.collection(config.collection.secrets.value())
.doc(config.document.token.value());
Expand All @@ -53,22 +59,21 @@ export const getStreamers = onSchedule(

// get channels
const getChannels = [
youtubeClient.getChannels([...master.youtube.keys()]),
twitchClient.getChannels([...master.twitch.keys()]),
twitClient.getChannels([...master.twitCasting.keys()]),
youtubeClient.getChannels([...idMap.youtube.keys()]),
twitchClient.getChannels([...idMap.twitch.keys()]),
twitClient.getChannels([...idMap.twitCasting.keys()]),
];
const channels = (await Promise.all(getChannels)).flat();

const streamers = channels.reduce(
(result, ch) => {
const key = master[ch.platform].get(ch.id);
if (!key) return result;
const key = idMap[ch.platform].get(ch.id);

result[key] = { ...result[key], [ch.platform]: ch };
if (key) result[key] = { ...result[key], [ch.platform]: ch };

return result;
},
{} as Record<string, Streamer>,
baseStreamers as Record<string, Streamer>,
);

// create and update db
Expand Down Expand Up @@ -142,3 +147,13 @@ export const getStreams = onSchedule(
await batch.commit();
},
);

export const createStreamerMaster = onRequest(
{
region: "asia-northeast1",
},
async (_, res) => {
createMaster(db, config.collection.master.value());
res.status(200).send("updated");
},
);
2 changes: 1 addition & 1 deletion functions/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type Channel = {
platform: Platform;
};

export type Streamer = { [k in Platform]: Channel };
export type Streamer = { [k in Platform]: Channel } & { order: number };

export type BaseStream = {
id: string;
Expand Down
9 changes: 6 additions & 3 deletions src/components/displayHistoryButton/styles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ export const Button = styled.button<{ state: boolean }>`
state
? theme.displayHistoryButton.iconActive
: theme.displayHistoryButton.icon};
${({ state }) =>
${({ state, theme }) =>
state &&
css`
box-shadow:
inset 3px 3px 5px #bbbbbb,
inset -3px -3px 5px #ffffff;
inset 3px 3px 5px ${theme.displayHistoryButton.shadow[0]},
inset -3px -3px 5px ${theme.displayHistoryButton.shadow[1]};
`}
display: flex;
justify-content: center;
align-items: center;

&:hover {
background-color: ${({ theme }) => theme.displayHistoryButton.bg.hover};
Expand Down
2 changes: 1 addition & 1 deletion src/components/inViewContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const InViewContainer = <T,>({
setRenderDataIdx((n) => ++n);
});

observer.observe(ref.current);
setTimeout(() => observer.observe(ref.current), 0);

return () => {
observer.unobserve(ref.current);
Expand Down
1 change: 1 addition & 0 deletions src/components/inViewContainer/styles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import styled from "styled-components";

export const ObserverElement = styled.div<{ height?: number }>`
min-height: ${({ height }) => height ?? 0}px;
height: 100%;
width: 100%;
`;
1 change: 0 additions & 1 deletion src/components/mainContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export const MainContainer: FC = () => {
const resize = () => {
const style = window.getComputedStyle(ref);
const width = getPixel(style, "width") - 40;
console.log("onResize", width);
setGridProperties(
calcGridProperties(width, cardWidth, { gapRange: [x, x * 4] }),
);
Expand Down
3 changes: 3 additions & 0 deletions src/components/settingMenu/styles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export const Button = styled.button`
background-color: ${({ theme }) => theme.dropdown.input.bg.normal};
transition: 0.3s ease;
color: ${({ theme }) => theme.dropdown.input.icon};
display: flex;
justify-content: center;
align-items: center;

&:hover {
background-color: ${({ theme }) => theme.dropdown.input.bg.hover};
Expand Down
3 changes: 2 additions & 1 deletion src/components/streamCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ export const StreamCard: FC<Props> = ({ stream }) => {
<Card
{...hoverParams}
isExpand={isExpand}
onClick={() => window.open(stream.url)}
href={stream.url}
target="_blank"
>
<Thumbnail src={stream.thumbnail} loading="lazy" />
<StreamInfo isExpand={isExpand && !isEnded}>
Expand Down
2 changes: 1 addition & 1 deletion src/components/streamCard/styles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type StyleProps = {
isExpand: boolean;
};

export const Card = styled.div<StyleProps>`
export const Card = styled.a<StyleProps>`
width: ${mobileParams.width}px;
height: ${({ isExpand }) =>
isExpand ? mobileParams.expandedHeight : mobileParams.height}px;
Expand Down
2 changes: 1 addition & 1 deletion src/components/streamerFilter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const StreamerFilter: FC<Props> = ({
);

const sortedStreamers = useMemo(
() => [...streamers].sort((a, b) => a.id.localeCompare(b.id)),
() => [...streamers].sort((a, b) => a.order - b.order),
[streamers],
);

Expand Down
1 change: 1 addition & 0 deletions src/configs/theme/dark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ export const darkTheme: DefaultTheme = {
normal: "transparent",
hover: colors.grey[700],
},
shadow: [colors.grey[900], colors.grey[800]],
},
};
1 change: 1 addition & 0 deletions src/configs/theme/light.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ export const lightTheme: DefaultTheme = {
normal: "transparent",
hover: colors.grey[200],
},
shadow: [colors.grey[400], colors.grey[100]],
},
};
10 changes: 7 additions & 3 deletions src/providers/vspoStreamProvider/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "./context";
import {
Channel,
ChannelResponse,
Stream,
Streamer,
StreamerMap,
Expand Down Expand Up @@ -38,9 +39,12 @@ const parseToStream = (streamRes: StreamResponse, channel: Channel): Stream => {
};

const parseToStreamer = (streamerRes: StreamerResponse): Streamer => {
const entries = Object.entries(streamerRes).map(
([key, { id, name, icon }]) => [key, { id, name, icon }],
);
const entries = Object.entries(streamerRes).map(([key, data]) => {
if (key === "order") return [key, data];

const { id, name, icon } = data as ChannelResponse;
return [key, { id, name, icon }];
});

return Object.fromEntries(entries);
};
Expand Down
9 changes: 7 additions & 2 deletions types/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export type ChannelResponse = {
platform: Platform;
};

export type StreamerResponse = { [k in Platform]: ChannelResponse };
export type StreamerResponse = { [k in Platform]: ChannelResponse } & {
order: number;
};

export type StreamResponse = {
id: string;
Expand All @@ -29,7 +31,10 @@ export type Channel = {
icon: string;
};

export type Streamer = { [key in Platform]: Channel } & { id: string };
export type Streamer = { [key in Platform]: Channel } & {
id: string;
order: number;
};

export type StreamerMap = { [id in string]: Streamer };

Expand Down
1 change: 1 addition & 0 deletions types/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export type DropdownTheme = {

export type DisplayHistoryButtonTheme = ButtonTheme & {
iconActive: string;
shadow: [string, string];
};

declare module "styled-components" {
Expand Down
Loading