Skip to content

Commit

Permalink
Merge pull request #2167 from MirzaHanan/Add-github-source-type
Browse files Browse the repository at this point in the history
[Feature] Add `GitHub` as a new source type in `Add Content flow`
  • Loading branch information
Rassl authored Sep 22, 2024
2 parents 4827100 + b30b4b8 commit 7b59db9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
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)

0 comments on commit 7b59db9

Please sign in to comment.