-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac519d2
commit a359e24
Showing
8 changed files
with
193 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
nav: components | ||
group: shields | ||
title: Social | ||
order: 8 | ||
--- | ||
|
||
<code src="./index.tsx" inline></code> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { useControls, useCreateStore } from '@lobehub/ui'; | ||
import { folder } from 'leva'; | ||
import { pick } from 'lodash-es'; | ||
import { memo, useMemo } from 'react'; | ||
|
||
import MarkdownStorybook from '@/components/MarkdownStorybook'; | ||
import { shieldBaseControls } from '@/const/shieldBaseControls'; | ||
import { socialShieldControlsPickList } from '@/const/socialShieldControls'; | ||
import { genSocialShields } from '@/services/genSocialShield'; | ||
|
||
const idControls = { | ||
/* eslint-disable sort-keys-fix/sort-keys-fix */ | ||
qq: '40073838', | ||
wechat: '40073838', | ||
x: 'canisminor1990', | ||
weibo: 'Canis_Minor', | ||
discord: 'canisminor1990', | ||
steam: 'canisminor', | ||
/* eslint-enable */ | ||
}; | ||
|
||
const controls = { | ||
prefix: true, | ||
['⚒️']: folder( | ||
{ | ||
...pick(shieldBaseControls, ['style', 'labelColor', 'color']), | ||
logoColor: { | ||
...shieldBaseControls.logoColor, | ||
value: 'white', | ||
}, | ||
}, | ||
{ | ||
collapsed: true, | ||
}, | ||
), | ||
}; | ||
const pickControls = { ['✅']: folder(socialShieldControlsPickList, { collapsed: true }) }; | ||
|
||
const Social = memo(() => { | ||
const store = useCreateStore(); | ||
|
||
const idOptions = useControls(idControls, { store }); | ||
const options = useControls(controls, { store }); | ||
const pickOptions = useControls(pickControls, { store }); | ||
|
||
const md = useMemo( | ||
() => genSocialShields(options, idOptions, pickOptions), | ||
[options, idOptions, pickOptions], | ||
); | ||
|
||
return <MarkdownStorybook levaStore={store}>{md.join('\n\n')}</MarkdownStorybook>; | ||
}); | ||
|
||
export default Social; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import urlJoin from 'url-join'; | ||
|
||
import { colorOptions } from '@/const/shieldBaseControls'; | ||
import { ShieldsBaseOptions } from '@/types/shields'; | ||
import { genPickList } from '@/utils/genPickList'; | ||
|
||
export interface socialShieldControlsItem extends Partial<ShieldsBaseOptions> { | ||
genLink?: (id: string) => string | undefined; | ||
} | ||
|
||
export const socialShieldControls: { | ||
[key: string]: socialShieldControlsItem; | ||
} = { | ||
/* eslint-disable sort-keys-fix/sort-keys-fix */ | ||
qq: { | ||
logo: 'tencentqq', | ||
logoColor: 'white', | ||
color: colorOptions.blue, | ||
}, | ||
wechat: { | ||
logo: 'wechat', | ||
logoColor: 'white', | ||
color: colorOptions.lime, | ||
}, | ||
discord: { | ||
logo: 'discord', | ||
logoColor: 'white', | ||
color: colorOptions.purple, | ||
}, | ||
weibo: { | ||
logo: 'sinaweibo', | ||
logoColor: 'white', | ||
color: 'FF9F9F', | ||
genLink: (id) => urlJoin('https://weibo.com/n', id), | ||
}, | ||
steam: { | ||
logo: 'steam', | ||
logoColor: 'white', | ||
color: 'ABCAFF', | ||
genLink: (id) => urlJoin('https://steamcommunity.com/id', id), | ||
}, | ||
x: { | ||
logo: 'x', | ||
logoColor: 'white', | ||
color: colorOptions.white, | ||
genLink: (id) => urlJoin('https://x.com', id), | ||
}, | ||
/* eslint-enable */ | ||
}; | ||
|
||
export const socialShieldControlsPickList = genPickList(socialShieldControls); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { identity, pickBy } from 'lodash-es'; | ||
import qs from 'query-string'; | ||
import urlJoin from 'url-join'; | ||
|
||
import { socialShieldControls, socialShieldControlsItem } from '@/const/socialShieldControls'; | ||
import { SHIELD_BADGE_URL } from '@/const/url'; | ||
import { ShieldsBaseOptions } from '@/types/shields'; | ||
import { formatCustomLabel } from '@/utils/formatCustomLabel'; | ||
import { genShield } from '@/utils/genShield'; | ||
|
||
interface SpcialIdOptions { | ||
discord?: string; | ||
qq?: string; | ||
steam?: string; | ||
wechat?: string; | ||
weibo?: string; | ||
x?: string; | ||
} | ||
|
||
type SocialShieldOptions = ShieldsBaseOptions & | ||
socialShieldControlsItem & { | ||
id: string; | ||
name: string; | ||
prefix: boolean; | ||
}; | ||
|
||
export const genSocialShield = (options: SocialShieldOptions) => { | ||
const { name, genLink, id, color, prefix, ...config } = options; | ||
|
||
const defShield = qs.stringifyUrl({ | ||
query: pickBy(config, identity) as any, | ||
url: urlJoin( | ||
SHIELD_BADGE_URL, | ||
formatCustomLabel({ | ||
color: (color as string) || 'black', | ||
label: `${prefix ? '@' : ''}${id}`, | ||
}), | ||
), | ||
}); | ||
const defLink = genLink?.(id); | ||
|
||
return genShield(`social-${name}`, defShield, defLink); | ||
}; | ||
|
||
export const genSocialShields = ( | ||
options: Partial<SocialShieldOptions> | any, | ||
idOptions: SpcialIdOptions, | ||
pickOptions: { [key: string]: boolean }, | ||
) => { | ||
const defShields: string[] = []; | ||
const defLinks: string[] = []; | ||
|
||
for (const [name, config] of Object.entries(socialShieldControls)) { | ||
if (!pickOptions[name]) continue; | ||
const data = genSocialShield({ | ||
name, | ||
...config, | ||
...options, | ||
color: options.color || config.color, | ||
// @ts-ignore | ||
id: idOptions?.[name], | ||
}); | ||
defShields.push(data[0]); | ||
defLinks.push(data[1]); | ||
} | ||
return [defShields.join('\n'), defLinks.join('\n')]; | ||
}; |