This commit is contained in:
archer
2023-07-28 20:47:20 +08:00
parent f764d81cdd
commit 950dffaedf
8 changed files with 62 additions and 63 deletions

View File

@@ -1,18 +1,32 @@
import React from 'react';
import React, { useMemo } from 'react';
import { Box } from '@chakra-ui/react';
import Image from './img/Image';
const regex = /((http|https|ftp):\/\/[^\s\u4e00-\u9fa5\u3000-\u303f\uff00-\uffef]+)/gi;
const Link = (props: { href?: string; children?: React.ReactNode[] }) => {
const decText = decodeURIComponent(props.href || '');
const replaceText = decText.replace(regex, (match, p1) => {
const text = decText === props.children?.[0] ? p1 : props.children?.[0];
const isInternal = /^\/#/i.test(p1);
const target = isInternal ? '_self' : '_blank';
return `<a href="${p1}" target=${target}>${text}</a>`;
});
const Link = (props: { href?: string; children?: any }) => {
const Html = useMemo(() => {
const decText = decodeURIComponent(props.href || '');
return <Box as={'span'} dangerouslySetInnerHTML={{ __html: replaceText }} />;
return decText.replace(regex, (match, p1) => {
let text = decText === props.children?.[0] ? p1 : props.children?.[0];
const isInternal = /^\/#/i.test(p1);
const target = isInternal ? '_self' : '_blank';
if (props?.children?.[0]?.props?.node?.tagName === 'img') {
// eslint-disable-next-line @next/next/no-img-element
text = `<img src="${props?.children?.[0]?.props?.src}" />`;
}
return `<a href="${p1}" target=${target}>${text}</a>`;
});
}, [props.children, props.href]);
return typeof Html === 'string' ? (
<Box as={'span'} dangerouslySetInnerHTML={{ __html: Html }} />
) : (
Html
);
};
export default React.memo(Link);

View File

@@ -0,0 +1,27 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { authUser } from '@/service/utils/auth';
import { connectToDatabase, OutLink } from '@/service/mongo';
import { OutLinkTypeEnum } from '@/constants/chat';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
await authUser({ req, authRoot: true });
await connectToDatabase();
await OutLink.updateMany(
{},
{
$set: { type: OutLinkTypeEnum.share }
}
);
jsonRes(res, {});
} catch (error) {
jsonRes(res, {
code: 500,
error
});
}
}

View File

@@ -4,6 +4,7 @@ import { connectToDatabase, OutLink } from '@/service/mongo';
import { authApp, authUser } from '@/service/utils/auth';
import type { ShareChatEditType } from '@/types/app';
import { customAlphabet } from 'nanoid';
import { OutLinkTypeEnum } from '@/constants/chat';
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz1234567890', 24);
/* create a shareChat */
@@ -27,7 +28,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
shareId,
userId,
appId,
name
name,
type: OutLinkTypeEnum.share
});
jsonRes(res, {