mirror of
https://github.com/langgenius/dify.git
synced 2025-12-19 22:28:46 +00:00
chore: improve storybooks (#27306)
This commit is contained in:
@@ -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
|
||||
},
|
||||
|
||||
64
web/.storybook/utils/audio-player-manager.mock.ts
Normal file
64
web/.storybook/utils/audio-player-manager.mock.ts
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user