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

[Feature] Add GitHub as a new source type in Add Content flow #2167

Merged
merged 2 commits into from
Sep 22, 2024
Merged
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
1 change: 1 addition & 0 deletions public/github_default.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion src/components/AddContentModal/SourceTypeStep/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FC } from 'react'
import styled from 'styled-components'
import { Flex } from '~/components/common/Flex'
import { Text } from '~/components/common/Text'
import { RSS, TWITTER_HANDLE, YOUTUBE_CHANNEL } from '~/constants'
import { GITHUB_REPOSITORY, RSS, TWITTER_HANDLE, YOUTUBE_CHANNEL } from '~/constants'
import { colors } from '~/utils'
import { extractNameFromLink } from '../utils'

Expand All @@ -27,6 +27,10 @@ const CONTENT_TYPE_MAPPING: Record<string, { [k: string]: string }> = {
label: 'RSS Feed',
img: 'rss_feed.svg',
},
[GITHUB_REPOSITORY]: {
label: 'GitHub Repository',
img: 'github_default.svg',
},
}

export const SourceTypeStep: FC<Props> = ({ onNextStep, onPrevStep, type, value }) => (
Expand Down
5 changes: 3 additions & 2 deletions src/components/AddContentModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import * as sphinx from 'sphinx-bridge'
import { BaseModal } from '~/components/Modal'
import {
DOCUMENT,
GITHUB_REPOSITORY,
isE2E,
LINK,
NODE_ADD_ERROR,
RSS,
TWITTER_HANDLE,
TWITTER_SOURCE,
WEB_PAGE,
YOUTUBE_CHANNEL,
isE2E,
} from '~/constants'
import { api } from '~/network/api'
import { useModal } from '~/stores/useModalStore'
Expand Down Expand Up @@ -80,7 +81,7 @@ const handleSubmitForm = async (
} else {
return
}
} else if (sourceType === YOUTUBE_CHANNEL || sourceType === RSS) {
} else if (sourceType === YOUTUBE_CHANNEL || sourceType === RSS || sourceType === GITHUB_REPOSITORY) {
body.source = data.source
body.source_type = sourceType
}
Expand Down
15 changes: 14 additions & 1 deletion src/components/AddContentModal/utils/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { DOCUMENT, LINK, RSS, TWITTER_HANDLE, TWITTER_SOURCE, WEB_PAGE, YOUTUBE_CHANNEL } from '~/constants'
import {
DOCUMENT,
GITHUB_REPOSITORY,
LINK,
RSS,
TWITTER_HANDLE,
TWITTER_SOURCE,
WEB_PAGE,
YOUTUBE_CHANNEL,
} from '~/constants'
import { extractNameFromLink, getInputType } from '..'

describe('youtubeRegex', () => {
Expand Down Expand Up @@ -61,6 +70,10 @@ describe('youtubeRegex', () => {
it('should assert we can check for document regex', async () => {
expect(getInputType('some plain text')).toBe(DOCUMENT)
})

it('should assert we can check for GitHub repository regex', async () => {
expect(getInputType('https://github.com/stakwork/sphinx-nav-fiber')).toBe(GITHUB_REPOSITORY)
})
})

describe('extractNameFromLink', () => {
Expand Down
19 changes: 17 additions & 2 deletions src/components/AddContentModal/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { DOCUMENT, LINK, RSS, TWITTER_HANDLE, TWITTER_SOURCE, WEB_PAGE, YOUTUBE_CHANNEL } from '~/constants'
import {
DOCUMENT,
GITHUB_REPOSITORY,
LINK,
RSS,
TWITTER_HANDLE,
TWITTER_SOURCE,
WEB_PAGE,
YOUTUBE_CHANNEL,
} from '~/constants'

export const twitterHandlePattern = /\b(?:twitter\.com|x\.com)\/(?:@)?([\w_]+)(?:$|\?[^/]*$)/

Expand All @@ -14,6 +23,7 @@ const youtubeChannelPattern = /https?:\/\/(www\.)?youtube\.com\/(user\/)?(@)?([\

const genericUrlRegex = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/
const twitterBroadcastRegex = /https:\/\/twitter\.com\/i\/broadcasts\/([A-Za-z0-9_-]+)/
const githubRepoPattern = /https:\/\/github\.com\/[\w-]+\/[\w-]+/

export function getInputType(source: string) {
const linkPatterns = [
Expand Down Expand Up @@ -45,6 +55,10 @@ export function getInputType(source: string) {
return RSS
}

if (githubRepoPattern.test(source)) {
return GITHUB_REPOSITORY
}

if (genericUrlRegex.test(source)) {
return WEB_PAGE
}
Expand All @@ -60,4 +74,5 @@ export const extractNameFromLink = (inputString: string, type = ''): string | nu
return match ? match[1] : null
}

export const isSource = (type: string): boolean => !!type && [TWITTER_HANDLE, YOUTUBE_CHANNEL, RSS].includes(type)
export const isSource = (type: string): boolean =>
!!type && [TWITTER_HANDLE, YOUTUBE_CHANNEL, RSS, GITHUB_REPOSITORY].includes(type)
Loading