-
-
Notifications
You must be signed in to change notification settings - Fork 23
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
Showing
10 changed files
with
146 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { DEFAULT_USER_AVATAR } from '@/constants/common'; | ||
import { DEFAULT_PRIMARY_COLOR } from '@/constants/theme'; | ||
import { Config } from '@/types/config'; | ||
|
||
export const DEFAULT_SETTINGS: Config = { | ||
backgroundEffect: 'glow', | ||
languageModel: { | ||
openAI: { | ||
apikey: '', | ||
endpoint: '', | ||
model: 'gpt-3.5-turbo', | ||
}, | ||
}, | ||
primaryColor: DEFAULT_PRIMARY_COLOR, | ||
avatar: DEFAULT_USER_AVATAR, | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export const VIDOL_THEME_APPEARANCE = 'VIDOL_THEME_APPEARANCE'; | ||
export const VIDOL_THEME_NEUTRAL_COLOR = 'VIDOL_THEME_NEUTRAL_COLOR'; | ||
export const VIDOL_THEME_PRIMARY_COLOR = 'VIDOL_THEME_PRIMARY_COLOR'; | ||
|
||
export const DEFAULT_PRIMARY_COLOR = 'blue'; |
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,70 @@ | ||
import { Upload } from 'antd'; | ||
import { createStyles } from 'antd-style'; | ||
import NextImage from 'next/image'; | ||
import { CSSProperties, memo, useCallback } from 'react'; | ||
|
||
import { DEFAULT_USER_AVATAR_URL } from '@/constants/common'; | ||
import { useConfigStore } from '@/store/config'; | ||
import { createUploadImageHandler } from '@/utils/common'; | ||
import { imageToBase64 } from '@/utils/imageToBase64'; | ||
|
||
const useStyle = createStyles( | ||
({ css, token }) => css` | ||
cursor: pointer; | ||
overflow: hidden; | ||
border-radius: 50%; | ||
transition: | ||
scale 400ms ${token.motionEaseOut}, | ||
box-shadow 100ms ${token.motionEaseOut}; | ||
&:hover { | ||
box-shadow: 0 0 0 3px ${token.colorText}; | ||
} | ||
&:active { | ||
scale: 0.8; | ||
} | ||
`, | ||
); | ||
|
||
interface AvatarWithUploadProps { | ||
compressSize?: number; | ||
id?: string; | ||
size?: number; | ||
style?: CSSProperties; | ||
} | ||
|
||
const AvatarWithUpload = memo<AvatarWithUploadProps>( | ||
({ size = 40, compressSize = 256, style, id }) => { | ||
const { styles } = useStyle(); | ||
const [avatar, setConfig] = useConfigStore((s) => [s.config.avatar, s.setConfig]); | ||
|
||
const handleUploadAvatar = useCallback( | ||
createUploadImageHandler((avatar) => { | ||
const img = new Image(); | ||
img.src = avatar; | ||
img.addEventListener('load', () => { | ||
const webpBase64 = imageToBase64({ img, size: compressSize }); | ||
setConfig({ avatar: webpBase64 }); | ||
}); | ||
}), | ||
[], | ||
); | ||
|
||
return ( | ||
<div className={styles} id={id} style={{ maxHeight: size, maxWidth: size, ...style }}> | ||
<Upload beforeUpload={handleUploadAvatar} itemRender={() => void 0} maxCount={1}> | ||
<NextImage | ||
alt={avatar ? 'userAvatar' : 'LobeChat'} | ||
height={size} | ||
src={!!avatar ? avatar : DEFAULT_USER_AVATAR_URL} | ||
unoptimized | ||
width={size} | ||
/> | ||
</Upload> | ||
</div> | ||
); | ||
}, | ||
); | ||
|
||
export default AvatarWithUpload; |
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
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 @@ | ||
export const createUploadImageHandler = | ||
(onUploadImage: (base64: string) => void) => (file: any) => { | ||
const reader = new FileReader(); | ||
reader.readAsDataURL(file); | ||
reader.addEventListener('load', () => { | ||
onUploadImage(String(reader.result)); | ||
}); | ||
}; |
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,37 @@ | ||
export const imageToBase64 = ({ | ||
size, | ||
img, | ||
type = 'image/webp', | ||
}: { | ||
img: HTMLImageElement; | ||
size: number; | ||
type?: string; | ||
}) => { | ||
const canvas = document.createElement('canvas'); | ||
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D; | ||
let startX = 0; | ||
let startY = 0; | ||
|
||
if (img.width > img.height) { | ||
startX = (img.width - img.height) / 2; | ||
} else { | ||
startY = (img.height - img.width) / 2; | ||
} | ||
|
||
canvas.width = size; | ||
canvas.height = size; | ||
|
||
ctx.drawImage( | ||
img, | ||
startX, | ||
startY, | ||
Math.min(img.width, img.height), | ||
Math.min(img.width, img.height), | ||
0, | ||
0, | ||
size, | ||
size, | ||
); | ||
|
||
return canvas.toDataURL(type); | ||
}; |