From 0151192be09fdb8380b05100a5fb965f26c9ca52 Mon Sep 17 00:00:00 2001 From: Innei Date: Thu, 15 Jun 2023 17:03:26 +0800 Subject: [PATCH] feat: note redirect Signed-off-by: Innei --- src/app/notes/route.ts | 10 ++++++++++ .../layout/header/HeaderDataConfigureProvider.tsx | 13 +++---------- src/lib/_.ts | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 src/app/notes/route.ts diff --git a/src/app/notes/route.ts b/src/app/notes/route.ts new file mode 100644 index 0000000000..9f79fa7460 --- /dev/null +++ b/src/app/notes/route.ts @@ -0,0 +1,10 @@ +import type { NextRequest } from 'next/server' +import { NextResponse } from 'next/server' +import { apiClient } from '~/utils/request' + +export const GET = async (request: NextRequest) => { + const url = request.nextUrl.clone() + const { data: {nid}}= await apiClient.note.getLatest() + url.pathname = `/notes/${nid}` + return NextResponse.redirect(url) +} diff --git a/src/components/layout/header/HeaderDataConfigureProvider.tsx b/src/components/layout/header/HeaderDataConfigureProvider.tsx index a47759f1db..2dd17d82ea 100644 --- a/src/components/layout/header/HeaderDataConfigureProvider.tsx +++ b/src/components/layout/header/HeaderDataConfigureProvider.tsx @@ -1,15 +1,10 @@ /* eslint-disable @typescript-eslint/no-non-null-assertion */ 'use client' -import { - createContext, - useContext, - useEffect, - useMemo, - useState, -} from 'react' +import { createContext, useContext, useEffect, useMemo, useState } from 'react' import { useAggregation } from '~/hooks/data/use-aggregation' +import { cloneDeep } from '~/lib/_' import { headerMenuConfig as baseHeaderMenuConfig } from './config' @@ -24,9 +19,7 @@ export const HeaderDataConfigureProvider: Component = ({ children }) => { useEffect(() => { if (!data) return - const nextMenuConfig = JSON.parse( - JSON.stringify(baseHeaderMenuConfig), - ) as typeof baseHeaderMenuConfig + const nextMenuConfig = cloneDeep(baseHeaderMenuConfig) if (data.pageMeta) { const homeIndex = nextMenuConfig.findIndex((item) => item.type === 'Home') if (homeIndex !== -1) { diff --git a/src/lib/_.ts b/src/lib/_.ts index 785cc9d5bc..40f5d63ac7 100644 --- a/src/lib/_.ts +++ b/src/lib/_.ts @@ -58,3 +58,17 @@ export const throttle = any>( export const isUndefined = (val: any): val is undefined => typeof val === 'undefined' + +export const cloneDeep = (val: T): T => { + if (Array.isArray(val)) { + return val.map(cloneDeep) as any + } else if (typeof val === 'object' && val !== null) { + const result: any = {} + for (const key in val) { + result[key] = cloneDeep(val[key]) + } + return result + } else { + return val + } +}