fix(org): Fix generate a social media image
This commit is contained in:
parent
adf83eda8c
commit
b40a38d8dd
1 changed files with 9 additions and 3 deletions
|
@ -1,5 +1,6 @@
|
||||||
import { nsMerge } from 'shared/utils.mjs'
|
import { nsMerge } from 'shared/utils.mjs'
|
||||||
import { useState } from 'react'
|
import { useState, useContext } from 'react'
|
||||||
|
import { LoadingStatusContext } from 'shared/context/loading-status-context.mjs'
|
||||||
import { useBackend } from 'shared/hooks/use-backend.mjs'
|
import { useBackend } from 'shared/hooks/use-backend.mjs'
|
||||||
import { useTranslation } from 'next-i18next'
|
import { useTranslation } from 'next-i18next'
|
||||||
import { StringInput, ListInput, ns as inputNs } from 'shared/components/inputs.mjs'
|
import { StringInput, ListInput, ns as inputNs } from 'shared/components/inputs.mjs'
|
||||||
|
@ -8,6 +9,7 @@ export const ns = nsMerge('genimg', inputNs)
|
||||||
|
|
||||||
export const GenerateImage = () => {
|
export const GenerateImage = () => {
|
||||||
const backend = useBackend()
|
const backend = useBackend()
|
||||||
|
const { setLoadingStatus } = useContext(LoadingStatusContext)
|
||||||
const { t } = useTranslation(ns)
|
const { t } = useTranslation(ns)
|
||||||
|
|
||||||
const [title, setTitle] = useState('')
|
const [title, setTitle] = useState('')
|
||||||
|
@ -19,12 +21,16 @@ export const GenerateImage = () => {
|
||||||
const generate = async () => {
|
const generate = async () => {
|
||||||
let result
|
let result
|
||||||
try {
|
try {
|
||||||
|
setLoadingStatus([true, 'status:contactingBackend'])
|
||||||
result = await backend.img({ title, intro, type, site })
|
result = await backend.img({ title, intro, type, site })
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
const uint8Array = new Uint8Array(result.data)
|
const uint8Array = new Uint8Array(result.data)
|
||||||
const base64String = btoa(String.fromCharCode.apply(null, uint8Array))
|
let uint8String = ''
|
||||||
|
uint8Array.map((byte) => (uint8String += String.fromCharCode(byte)))
|
||||||
|
setLoadingStatus([true, 'status:nailedIt', true, true])
|
||||||
|
const base64String = btoa(uint8String)
|
||||||
setPreview(`data:image/png;base64,${base64String}`)
|
setPreview(`data:image/png;base64,${base64String}`)
|
||||||
}
|
} else setLoadingStatus([true, 'status:backendError', true, false])
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err)
|
console.log(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue