Skip to content

Commit 1445984

Browse files
committed
migrate to the new insights structure
1 parent a3b6126 commit 1445984

File tree

5 files changed

+74
-47
lines changed

5 files changed

+74
-47
lines changed

app/components/insight-text.tsx

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,41 @@
11
import { memo } from 'react';
22

33
import { Link } from '@/components/link/link';
4-
import { Insight } from '@/types/generated/graphql';
5-
6-
import { RANK_DESCRIPTIONS } from '../app.consts';
4+
import { InsightsQuery, SegmentType } from '@/types/generated/graphql';
75

86
type InsightTextProps = {
9-
insight: Insight;
7+
insight: NonNullable<InsightsQuery['insights']>[number];
108
};
119

12-
const DEFAULT_RANKING_LINKS = {
13-
[`${RANK_DESCRIPTIONS.s.title.toLowerCase()}ing`]: '/by/stars/1',
14-
[`${RANK_DESCRIPTIONS.c.title.toLowerCase()}ing`]: '/by/contributions/1',
15-
[`${RANK_DESCRIPTIONS.f.title.toLowerCase()}ing`]: '/by/followers/1',
16-
} as const;
17-
18-
/** Escapes RegExp metacharacters so the username is treated literally */
19-
const escapeRegExp = (str: string) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
20-
21-
// stars ranking|contributor ranking|followers ranking
22-
const rankingPattern = Object.values(RANK_DESCRIPTIONS)
23-
.map((data) => `${data.title.toLowerCase()}ing`)
24-
.join('|');
25-
2610
const InsightText: React.FC<InsightTextProps> = ({ insight }) => {
27-
const { text, data } = insight;
28-
const login = data?.user?.login;
11+
const { segments, entities } = insight;
2912

30-
let combinedRegex: RegExp;
31-
if (login) {
32-
combinedRegex = new RegExp(`\\b(${escapeRegExp(login)}|${rankingPattern})\\b`, 'gi');
33-
} else {
34-
combinedRegex = new RegExp(`\\b(${rankingPattern})\\b`, 'gi');
35-
}
36-
const pieces = text.split(combinedRegex);
13+
return segments.map((segment) => {
14+
if (!segment) return null;
3715

38-
return pieces.map((piece, idx) => {
39-
if (!piece) return null;
40-
41-
if (login && login === piece) {
16+
if (segment.type === SegmentType.Mention) {
17+
const githubHandle = entities.mentions[segment.entityKey!]?.handles?.github;
4218
return (
43-
<Link key={idx} href={`/profile/${login}`} className="text-foreground hover:text-foreground/80">
44-
{piece}
19+
<Link key={segment.text} href={`/profile/${githubHandle}`} className="text-foreground hover:text-foreground/80">
20+
{segment.text}
4521
</Link>
4622
);
4723
}
4824

49-
const key = piece.toLowerCase();
50-
if (key in DEFAULT_RANKING_LINKS) {
25+
if (segment.type === SegmentType.Link) {
26+
const link = entities.links[segment.entityKey!]?.url;
5127
return (
52-
<Link key={idx} href={DEFAULT_RANKING_LINKS[key]!} className="text-foreground hover:text-foreground/80">
53-
{piece}
28+
<Link key={segment.text} href={link} className="text-foreground hover:text-foreground/80">
29+
{segment.text}
5430
</Link>
5531
);
5632
}
5733

58-
return piece;
34+
if (segment.type === SegmentType.Text) {
35+
return segment.text;
36+
}
37+
38+
return null;
5939
});
6040
};
6141

app/components/insights-carousel.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from '@/components/ui/carousel';
1414
import { Skeleton } from '@/components/ui/skeleton';
1515
import { fetchInsights } from '@/graphql/helpers/fetch-insights';
16-
import { Insight, InsightCategory } from '@/types/generated/graphql';
16+
import { InsightCategory, InsightsQuery } from '@/types/generated/graphql';
1717

1818
import InsightText from './insight-text';
1919

@@ -36,7 +36,7 @@ const InsightsCarousel = () => {
3636
const [api, setApi] = useState<CarouselApi>();
3737
const [current, setCurrent] = useState(0);
3838
const [count, setCount] = useState(0);
39-
const [insights, setInsights] = useState<Insight[]>([]);
39+
const [insights, setInsights] = useState<InsightsQuery['insights']>([]);
4040
const [loading, setLoading] = useState(true);
4141

4242
useEffect(() => {
@@ -85,7 +85,7 @@ const InsightsCarousel = () => {
8585
<div className="relative mb-4">
8686
<div className="hidden md:block absolute inset-0 rotate-[-6deg] scale-y-[1.6] -translate-x-10 pointer-events-none bg-landing-page-gradient-start-color rounded-[100%]" />
8787
<CarouselContent>
88-
{insights.map((insight, index) => (
88+
{insights?.map((insight, index) => (
8989
<CarouselItem key={index} className="flex items-center">
9090
<Card className="bg-transparent border-0 shadow-none py-0">
9191
<CardContent className="flex flex-col justify-center px-0">

graphql/insights.gql

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
query Insights {
22
insights {
33
category
4-
data
5-
createdAt
6-
updatedAt
7-
text
4+
segments {
5+
type
6+
text
7+
entityKey
8+
}
9+
entities {
10+
mentions
11+
links
12+
}
813
}
914
}

types/generated/graphql.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,19 @@ export type CountryTopUsers = {
8888
s?: Maybe<UserBasic>;
8989
};
9090

91+
export type Entities = {
92+
__typename?: 'Entities';
93+
links?: Maybe<Scalars['JSON']['output']>;
94+
mentions?: Maybe<Scalars['JSON']['output']>;
95+
};
96+
9197
export type Insight = {
9298
__typename?: 'Insight';
9399
category: InsightCategory;
94100
createdAt: Scalars['DateTime']['output'];
95101
data?: Maybe<Scalars['JSON']['output']>;
102+
entities: Entities;
103+
segments: Array<Segment>;
96104
text: Scalars['String']['output'];
97105
updatedAt: Scalars['DateTime']['output'];
98106
};
@@ -320,6 +328,20 @@ export enum RepositorySortField {
320328
Stargazers = 'STARGAZERS'
321329
}
322330

331+
export type Segment = {
332+
__typename?: 'Segment';
333+
display?: Maybe<Scalars['String']['output']>;
334+
entityKey?: Maybe<Scalars['String']['output']>;
335+
text?: Maybe<Scalars['String']['output']>;
336+
type: SegmentType;
337+
};
338+
339+
export enum SegmentType {
340+
Link = 'LINK',
341+
Mention = 'MENTION',
342+
Text = 'TEXT'
343+
}
344+
323345
export type SocialAccount = {
324346
__typename?: 'SocialAccount';
325347
nodes?: Maybe<Array<SocialAccountNodeEntity>>;
@@ -461,7 +483,7 @@ export type GlobalRankingsQuery = { __typename?: 'Query', globalRankings: Array<
461483
export type InsightsQueryVariables = Exact<{ [key: string]: never; }>;
462484

463485

464-
export type InsightsQuery = { __typename?: 'Query', insights?: Array<{ __typename?: 'Insight', category: InsightCategory, data?: any | null, createdAt: any, updatedAt: any, text: string }> | null };
486+
export type InsightsQuery = { __typename?: 'Query', insights?: Array<{ __typename?: 'Insight', category: InsightCategory, segments: Array<{ __typename?: 'Segment', type: SegmentType, text?: string | null, entityKey?: string | null }>, entities: { __typename?: 'Entities', mentions?: any | null, links?: any | null } }> | null };
465487

466488
export type ProfileContributionsQueryVariables = Exact<{
467489
login: Scalars['String']['input'];
@@ -530,7 +552,7 @@ export const CountryRankingsDocument = {"kind":"Document","definitions":[{"kind"
530552
export const CountrySummaryDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"CountrySummary"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"order"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"CountrySummaryOrder"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"countrySummary"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"order"},"value":{"kind":"Variable","name":{"kind":"Name","value":"order"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"country"}},{"kind":"Field","name":{"kind":"Name","value":"usersCount"}},{"kind":"Field","name":{"kind":"Name","value":"date"}},{"kind":"Field","name":{"kind":"Name","value":"s"}},{"kind":"Field","name":{"kind":"Name","value":"c"}},{"kind":"Field","name":{"kind":"Name","value":"f"}},{"kind":"Field","name":{"kind":"Name","value":"topUsers"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"s"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"login"}},{"kind":"Field","name":{"kind":"Name","value":"avatarUrl"}}]}},{"kind":"Field","name":{"kind":"Name","value":"c"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"login"}},{"kind":"Field","name":{"kind":"Name","value":"avatarUrl"}}]}},{"kind":"Field","name":{"kind":"Name","value":"f"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"login"}},{"kind":"Field","name":{"kind":"Name","value":"avatarUrl"}}]}}]}},{"kind":"Field","name":{"kind":"Name","value":"countryData"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"code"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"flag"}}]}}]}}]}}]} as unknown as DocumentNode<CountrySummaryQuery, CountrySummaryQueryVariables>;
531553
export const GlobalRankByLoginDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GlobalRankByLogin"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"login"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"globalRankByLogin"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"login"},"value":{"kind":"Variable","name":{"kind":"Name","value":"login"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"githubId"}},{"kind":"Field","name":{"kind":"Name","value":"c"}},{"kind":"Field","name":{"kind":"Name","value":"cM"}},{"kind":"Field","name":{"kind":"Name","value":"f"}},{"kind":"Field","name":{"kind":"Name","value":"fM"}},{"kind":"Field","name":{"kind":"Name","value":"s"}},{"kind":"Field","name":{"kind":"Name","value":"sM"}},{"kind":"Field","name":{"kind":"Name","value":"user"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"login"}},{"kind":"Field","name":{"kind":"Name","value":"avatarUrl"}},{"kind":"Field","name":{"kind":"Name","value":"s"}},{"kind":"Field","name":{"kind":"Name","value":"c"}},{"kind":"Field","name":{"kind":"Name","value":"f"}},{"kind":"Field","name":{"kind":"Name","value":"location"}}]}}]}}]}}]} as unknown as DocumentNode<GlobalRankByLoginQuery, GlobalRankByLoginQueryVariables>;
532554
export const GlobalRankingsDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GlobalRankings"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"order"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"RankOrder"}}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"offset"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"Int"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"globalRankings"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"order"},"value":{"kind":"Variable","name":{"kind":"Name","value":"order"}}},{"kind":"Argument","name":{"kind":"Name","value":"offset"},"value":{"kind":"Variable","name":{"kind":"Name","value":"offset"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"githubId"}},{"kind":"Field","name":{"kind":"Name","value":"c"}},{"kind":"Field","name":{"kind":"Name","value":"cM"}},{"kind":"Field","name":{"kind":"Name","value":"f"}},{"kind":"Field","name":{"kind":"Name","value":"fM"}},{"kind":"Field","name":{"kind":"Name","value":"s"}},{"kind":"Field","name":{"kind":"Name","value":"sM"}},{"kind":"Field","name":{"kind":"Name","value":"user"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"login"}},{"kind":"Field","name":{"kind":"Name","value":"avatarUrl"}},{"kind":"Field","name":{"kind":"Name","value":"s"}},{"kind":"Field","name":{"kind":"Name","value":"c"}},{"kind":"Field","name":{"kind":"Name","value":"f"}},{"kind":"Field","name":{"kind":"Name","value":"location"}},{"kind":"Field","name":{"kind":"Name","value":"country"}}]}}]}}]}}]} as unknown as DocumentNode<GlobalRankingsQuery, GlobalRankingsQueryVariables>;
533-
export const InsightsDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"Insights"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"insights"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"category"}},{"kind":"Field","name":{"kind":"Name","value":"data"}},{"kind":"Field","name":{"kind":"Name","value":"createdAt"}},{"kind":"Field","name":{"kind":"Name","value":"updatedAt"}},{"kind":"Field","name":{"kind":"Name","value":"text"}}]}}]}}]} as unknown as DocumentNode<InsightsQuery, InsightsQueryVariables>;
555+
export const InsightsDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"Insights"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"insights"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"category"}},{"kind":"Field","name":{"kind":"Name","value":"segments"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"type"}},{"kind":"Field","name":{"kind":"Name","value":"text"}},{"kind":"Field","name":{"kind":"Name","value":"entityKey"}}]}},{"kind":"Field","name":{"kind":"Name","value":"entities"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"mentions"}},{"kind":"Field","name":{"kind":"Name","value":"links"}}]}}]}}]}}]} as unknown as DocumentNode<InsightsQuery, InsightsQueryVariables>;
534556
export const ProfileContributionsDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"ProfileContributions"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"login"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"user"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"login"},"value":{"kind":"Variable","name":{"kind":"Name","value":"login"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"contributions"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"year"}},{"kind":"Field","name":{"kind":"Name","value":"prsCount"}},{"kind":"Field","name":{"kind":"Name","value":"mergedPrsCount"}},{"kind":"Field","name":{"kind":"Name","value":"linesAdded"}},{"kind":"Field","name":{"kind":"Name","value":"linesRemoved"}},{"kind":"Field","name":{"kind":"Name","value":"repository"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"githubId"}},{"kind":"Field","name":{"kind":"Name","value":"createdAt"}},{"kind":"Field","name":{"kind":"Name","value":"pushedAt"}},{"kind":"Field","name":{"kind":"Name","value":"url"}},{"kind":"Field","name":{"kind":"Name","value":"forkCount"}},{"kind":"Field","name":{"kind":"Name","value":"isArchived"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"releasesCount"}},{"kind":"Field","name":{"kind":"Name","value":"stargazerCount"}}]}}]}}]}}]}}]} as unknown as DocumentNode<ProfileContributionsQuery, ProfileContributionsQueryVariables>;
535557
export const ProfileFetchingStatusDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"ProfileFetchingStatus"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"login"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"user"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"login"},"value":{"kind":"Variable","name":{"kind":"Name","value":"login"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"fetchingStatus"}},{"kind":"Field","name":{"kind":"Name","value":"fetchingUpdatedAt"}}]}}]}}]} as unknown as DocumentNode<ProfileFetchingStatusQuery, ProfileFetchingStatusQueryVariables>;
536558
export const ProfileIdByLoginDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"ProfileIdByLogin"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"login"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"globalRankByLogin"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"login"},"value":{"kind":"Variable","name":{"kind":"Name","value":"login"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"githubId"}}]}}]}}]} as unknown as DocumentNode<ProfileIdByLoginQuery, ProfileIdByLoginQueryVariables>;

types/generated/schema.graphql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,17 @@ A date-time string at UTC, such as 2019-12-03T09:54:33Z, compliant with the date
7070
"""
7171
scalar DateTime
7272

73+
type Entities {
74+
links: JSON
75+
mentions: JSON
76+
}
77+
7378
type Insight {
7479
category: InsightCategory!
7580
createdAt: DateTime!
7681
data: JSON
82+
entities: Entities!
83+
segments: [Segment!]!
7784
text: String!
7885
updatedAt: DateTime!
7986
}
@@ -245,6 +252,19 @@ enum RepositorySortField {
245252
STARGAZERS
246253
}
247254

255+
type Segment {
256+
display: String
257+
entityKey: String
258+
text: String
259+
type: SegmentType!
260+
}
261+
262+
enum SegmentType {
263+
LINK
264+
MENTION
265+
TEXT
266+
}
267+
248268
type SocialAccount {
249269
nodes: [SocialAccountNodeEntity!]
250270
totalCount: Int!

0 commit comments

Comments
 (0)