chore: improve storybooks (#27306)

This commit is contained in:
非法操作
2025-10-23 11:00:45 +08:00
committed by GitHub
parent 4bb00b83d9
commit b198c9474a
32 changed files with 1077 additions and 24 deletions

View File

@@ -1,5 +1,8 @@
import type { StorybookConfig } from '@storybook/nextjs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const storybookDir = path.dirname(fileURLToPath(import.meta.url))
const config: StorybookConfig = {
stories: ['../app/components/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
@@ -32,9 +35,9 @@ const config: StorybookConfig = {
config.resolve.alias = {
...config.resolve.alias,
// Mock the plugin index files to avoid circular dependencies
[path.resolve(__dirname, '../app/components/base/prompt-editor/plugins/context-block/index.tsx')]: path.resolve(__dirname, '__mocks__/context-block.tsx'),
[path.resolve(__dirname, '../app/components/base/prompt-editor/plugins/history-block/index.tsx')]: path.resolve(__dirname, '__mocks__/history-block.tsx'),
[path.resolve(__dirname, '../app/components/base/prompt-editor/plugins/query-block/index.tsx')]: path.resolve(__dirname, '__mocks__/query-block.tsx'),
[path.resolve(storybookDir, '../app/components/base/prompt-editor/plugins/context-block/index.tsx')]: path.resolve(storybookDir, '__mocks__/context-block.tsx'),
[path.resolve(storybookDir, '../app/components/base/prompt-editor/plugins/history-block/index.tsx')]: path.resolve(storybookDir, '__mocks__/history-block.tsx'),
[path.resolve(storybookDir, '../app/components/base/prompt-editor/plugins/query-block/index.tsx')]: path.resolve(storybookDir, '__mocks__/query-block.tsx'),
}
return config
},

View File

@@ -0,0 +1,64 @@
import { AudioPlayerManager } from '@/app/components/base/audio-btn/audio.player.manager'
type PlayerCallback = ((event: string) => void) | null
class MockAudioPlayer {
private callback: PlayerCallback = null
private finishTimer?: ReturnType<typeof setTimeout>
public setCallback(callback: PlayerCallback) {
this.callback = callback
}
public playAudio() {
this.clearTimer()
this.callback?.('play')
this.finishTimer = setTimeout(() => {
this.callback?.('ended')
}, 2000)
}
public pauseAudio() {
this.clearTimer()
this.callback?.('paused')
}
private clearTimer() {
if (this.finishTimer)
clearTimeout(this.finishTimer)
}
}
class MockAudioPlayerManager {
private readonly player = new MockAudioPlayer()
public getAudioPlayer(
_url: string,
_isPublic: boolean,
_id: string | undefined,
_msgContent: string | null | undefined,
_voice: string | undefined,
callback: PlayerCallback,
) {
this.player.setCallback(callback)
return this.player
}
public resetMsgId() {
// No-op for the mock
}
}
export const ensureMockAudioManager = () => {
const managerAny = AudioPlayerManager as unknown as {
getInstance: () => AudioPlayerManager
__isStorybookMockInstalled?: boolean
}
if (managerAny.__isStorybookMockInstalled)
return
const mock = new MockAudioPlayerManager()
managerAny.getInstance = () => mock as unknown as AudioPlayerManager
managerAny.__isStorybookMockInstalled = true
}