Skip to content

Commit

Permalink
chore: move sourceCountData instantiation to component
Browse files Browse the repository at this point in the history
  • Loading branch information
IgboPharaoh committed Dec 7, 2024
1 parent 7cf2fc1 commit a47d483
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { extractTranscripts } from "@/utils";
import { allTranscripts } from "contentlayer/generated";
import Wrapper from "@/components/layout/Wrapper";
import FeaturedTranscriptClient from "./FeaturedTranscriptClient";
import SourceCountData from "@/public/source-count-data.json";

const FeaturedTranscripts = () => {
const { latestTranscripts, featuredTranscripts } = extractTranscripts(allTranscripts);
const { latestTranscripts, featuredTranscripts } = extractTranscripts(allTranscripts, SourceCountData);

return (
<div className='flex items-center justify-center w-full bg-gray-custom-100'>
Expand Down
13 changes: 6 additions & 7 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Markdown, type Transcript } from "contentlayer/generated";
import { ContentTreeArray } from "./data";
import SourceCountData from "../../public/source-count-data.json";

export interface ContentTree {
[key: string]: ContentTree | Transcript[];
Expand Down Expand Up @@ -49,10 +48,10 @@ export function shuffle(data: Transcript[]) {
return data;
}

const getSourceFromTranscript = (data: Transcript) =>
const getSourceFromTranscript = (data: Transcript, SourceCountData:ContentData[]) =>
SourceCountData.find((source) => source.slug === data.slugAsParams[0])?.name ?? (data.slugAsParams as Array<string>)[0];

export const extractTranscripts = (allTranscripts: Transcript[]) => {
export const extractTranscripts = (allTranscripts: Transcript[],SourceCountData:ContentData[]) => {
const CURRENT_DAY = Date.now();
const ONE_DAY = 86_400_000; // 1000 * 3600 * 24

Expand All @@ -68,24 +67,24 @@ export const extractTranscripts = (allTranscripts: Transcript[]) => {
const transcriptDate = new Date(transcript.date as string).getTime();
const daysOpened = Math.floor((CURRENT_DAY - transcriptDate) / ONE_DAY);

acc.push({ ...transcript, daysOpened, sourceName: getSourceFromTranscript(transcript) });
acc.push({ ...transcript, daysOpened, sourceName: getSourceFromTranscript(transcript, SourceCountData) });
acc.sort((a, b) => new Date(b.date as string).getTime() - new Date(a.date as string).getTime());

if (acc.length > 3) acc.pop();
return acc;
}, [] as (Transcript & { daysOpened: number; sourceName: string })[]);

const featuredTranscripts = getFeaturedTranscripts(transcripts);
const featuredTranscripts = getFeaturedTranscripts(transcripts, SourceCountData);

return { latestTranscripts, featuredTranscripts };
};

export const getFeaturedTranscripts = (allTranscripts: Transcript[]) => {
export const getFeaturedTranscripts = (allTranscripts: Transcript[],SourceCountData:ContentData[]) => {
let featuredTranscripts: (Transcript & { sourceName: string })[] = [];

for (const transcript of allTranscripts) {
if (transcript.speakers) {
featuredTranscripts.push({ ...transcript, sourceName: getSourceFromTranscript(transcript) });
featuredTranscripts.push({ ...transcript, sourceName: getSourceFromTranscript(transcript, SourceCountData) });
}
}

Expand Down

0 comments on commit a47d483

Please sign in to comment.