-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbadges-options.tsx
88 lines (77 loc) · 2.53 KB
/
badges-options.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { useState } from 'react'
import { LIST_BADGES } from '@/badges'
import { DEFAULT_REPOSITORY_DATA } from '@/default-git-data'
import { type Editor } from '@tiptap/core'
import { Plus } from 'lucide-react'
import { useBuilder } from '@/store'
import { Button } from '@/components/ui/button'
import { Separator } from '@/components/ui/separator'
import { ComboboxBadges } from '@/components/combobox-badges'
type BadgeItemProps = {
name: string
url: string
isGithub: boolean
}
const addSingleBadge = ({ editor, data }: { editor: Editor; data: any }) => {
const { name, badgeUrl } = data
const endPos = editor.state.selection.head
// get the current active node
const parentNode = editor.state.selection.$head.parent
const {
attrs: { id }
} = parentNode
editor.commands.insertContentAt(endPos, {
type: 'image',
attrs: {
src: badgeUrl,
alt: id === 'badges' ? `Badge ${name}` : name
}
})
}
function BadgeItem({ name, url, isGithub }: BadgeItemProps) {
const gitRepositoryData = useBuilder((state) => state.gitRepositoryData)
const readmeEditor = useBuilder((state) => state.readmeEditor)
const { owner, repoName } = gitRepositoryData ?? DEFAULT_REPOSITORY_DATA
const badgeUrl = isGithub ? `${url}/${owner}/${repoName}` : url
const addBadge = () => {
addSingleBadge({
editor: readmeEditor!,
data: {
name,
badgeUrl
}
})
}
return (
<div className='w-full flex border border-dashed border-gray-600/30 rounded-md items-center overflow-hidden h-8'>
<div className='w-full border-none flex'>
<span className='font-medium justify-center text-[0.77rem] ml-2'>{name}</span>
</div>
<Button
size='icon'
variant='ghost'
className='rounded-none h-full border-none p-0 m-0'
onClick={addBadge}
>
<Plus className='size-4' />
</Button>
</div>
)
}
const defaultValues = LIST_BADGES.find((badge) => badge.id === 'frontend')!
export function BadgesOptions() {
const [badgeOptionData, setBadgeOptionData] = useState(defaultValues)
const isGithub = badgeOptionData.id === 'github'
const listBadges = badgeOptionData.data
return (
<div className='w-full min-h-[265px]'>
<ComboboxBadges setBadgeOptionData={setBadgeOptionData} />
<Separator className='my-4' />
<div className='grid gap-2 grid-cols-3 w-full'>
{listBadges?.map(({ id, name, url }) => {
return <BadgeItem key={id} name={name} url={url} isGithub={isGithub} />
})}
</div>
</div>
)
}