|
| 1 | +import { useDiffViewerStore } from "@features/code-editor/stores/diffViewerStore"; |
| 2 | +import { useThemeStore } from "@stores/themeStore"; |
| 3 | +import type { Decorator, Meta, StoryObj } from "@storybook/react-vite"; |
| 4 | +import { CodeMirrorDiffEditor } from "./CodeMirrorDiffEditor"; |
| 5 | + |
| 6 | +const originalCode = `import { formatDate, parseJSON } from "./utils"; |
| 7 | +import { Logger } from "./logger"; |
| 8 | +
|
| 9 | +interface ProcessOptions { |
| 10 | + timeout: number; |
| 11 | + retries: number; |
| 12 | +} |
| 13 | +
|
| 14 | +const DEFAULT_TIMEOUT = 5000; |
| 15 | +
|
| 16 | +export function processItems(items: string[], options: ProcessOptions) { |
| 17 | + const log = new Logger("processor"); |
| 18 | + log.info("Starting processing", { count: items.length }); |
| 19 | +
|
| 20 | + const results: string[] = []; |
| 21 | + for (const item of items) { |
| 22 | + if (item.startsWith("#")) { |
| 23 | + continue; |
| 24 | + } |
| 25 | + const parsed = parseJSON(item); |
| 26 | + results.push(formatDate(parsed.timestamp)); |
| 27 | + } |
| 28 | +
|
| 29 | + log.info("Processing complete", { resultCount: results.length }); |
| 30 | + return results; |
| 31 | +} |
| 32 | +
|
| 33 | +export function validateInput(input: unknown): input is string { |
| 34 | + return typeof input === "string" && input.length > 0; |
| 35 | +} |
| 36 | +
|
| 37 | +export function deprecatedHelper(data: string): string { |
| 38 | + return data.trim().toLowerCase(); |
| 39 | +} |
| 40 | +
|
| 41 | +export function getVersion(): string { |
| 42 | + return "1.0.0"; |
| 43 | +} |
| 44 | +`; |
| 45 | + |
| 46 | +const modifiedCode = `import { formatDate, parseJSON, batchProcess } from "./utils"; |
| 47 | +import { Logger } from "./logger"; |
| 48 | +
|
| 49 | +interface ProcessOptions { |
| 50 | + timeout: number; |
| 51 | + retries: number; |
| 52 | + batchSize?: number; |
| 53 | +} |
| 54 | +
|
| 55 | +const DEFAULT_TIMEOUT = 10000; |
| 56 | +
|
| 57 | +export function processItems(entries: string[], options: ProcessOptions) { |
| 58 | + const log = new Logger("processor"); |
| 59 | + log.info("Starting processing", { count: entries.length }); |
| 60 | +
|
| 61 | + const results: string[] = []; |
| 62 | + for (const entry of entries) { |
| 63 | + if (entry.startsWith("#") || entry.startsWith("//")) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + const parsed = parseJSON(entry); |
| 67 | + const formatted = formatDate(parsed.timestamp); |
| 68 | + if (formatted) { |
| 69 | + results.push(formatted); |
| 70 | + } |
| 71 | + } |
| 72 | +
|
| 73 | + log.info("Processing complete", { resultCount: results.length }); |
| 74 | + return results; |
| 75 | +} |
| 76 | +
|
| 77 | +export function validateInput(input: unknown): input is string { |
| 78 | + return typeof input === "string" && input.length > 0; |
| 79 | +} |
| 80 | +
|
| 81 | +export function processBatch( |
| 82 | + entries: string[], |
| 83 | + options: ProcessOptions, |
| 84 | +): string[] { |
| 85 | + const batchSize = options.batchSize ?? 100; |
| 86 | + const batches: string[][] = []; |
| 87 | +
|
| 88 | + for (let i = 0; i < entries.length; i += batchSize) { |
| 89 | + batches.push(entries.slice(i, i + batchSize)); |
| 90 | + } |
| 91 | +
|
| 92 | + return batches.flatMap((batch) => batchProcess(batch)); |
| 93 | +} |
| 94 | +
|
| 95 | +export function getVersion(): string { |
| 96 | + return "2.0.0"; |
| 97 | +} |
| 98 | +`; |
| 99 | + |
| 100 | +function withDiffViewerState( |
| 101 | + state: Partial<{ |
| 102 | + viewMode: "split" | "unified"; |
| 103 | + wordWrap: boolean; |
| 104 | + loadFullFiles: boolean; |
| 105 | + wordDiffs: boolean; |
| 106 | + }>, |
| 107 | +): Decorator { |
| 108 | + return (Story) => { |
| 109 | + useDiffViewerStore.setState(state); |
| 110 | + return <Story />; |
| 111 | + }; |
| 112 | +} |
| 113 | + |
| 114 | +const meta: Meta<typeof CodeMirrorDiffEditor> = { |
| 115 | + title: "Features/CodeEditor/CodeMirrorDiffEditor", |
| 116 | + component: CodeMirrorDiffEditor, |
| 117 | + parameters: { |
| 118 | + layout: "fullscreen", |
| 119 | + }, |
| 120 | + decorators: [ |
| 121 | + (Story, context) => { |
| 122 | + const isDark = context.globals.theme !== "light"; |
| 123 | + useThemeStore.setState({ |
| 124 | + theme: isDark ? "dark" : "light", |
| 125 | + isDarkMode: isDark, |
| 126 | + }); |
| 127 | + return ( |
| 128 | + <div |
| 129 | + style={{ height: "600px", display: "flex", flexDirection: "column" }} |
| 130 | + > |
| 131 | + <Story /> |
| 132 | + </div> |
| 133 | + ); |
| 134 | + }, |
| 135 | + ], |
| 136 | + beforeEach: () => { |
| 137 | + useDiffViewerStore.setState({ |
| 138 | + viewMode: "unified", |
| 139 | + wordWrap: true, |
| 140 | + loadFullFiles: false, |
| 141 | + wordDiffs: true, |
| 142 | + }); |
| 143 | + }, |
| 144 | + argTypes: { |
| 145 | + onContentChange: { action: "content-changed" }, |
| 146 | + onRefresh: { action: "refresh" }, |
| 147 | + }, |
| 148 | + args: { |
| 149 | + originalContent: originalCode, |
| 150 | + modifiedContent: modifiedCode, |
| 151 | + }, |
| 152 | +}; |
| 153 | + |
| 154 | +export default meta; |
| 155 | +type Story = StoryObj<typeof CodeMirrorDiffEditor>; |
| 156 | + |
| 157 | +export const UnifiedView: Story = {}; |
| 158 | + |
| 159 | +export const SplitView: Story = { |
| 160 | + decorators: [withDiffViewerState({ viewMode: "split" })], |
| 161 | +}; |
| 162 | + |
| 163 | +export const UnifiedFullFile: Story = { |
| 164 | + decorators: [withDiffViewerState({ loadFullFiles: true })], |
| 165 | +}; |
| 166 | + |
| 167 | +export const SplitFullFile: Story = { |
| 168 | + decorators: [withDiffViewerState({ viewMode: "split", loadFullFiles: true })], |
| 169 | +}; |
| 170 | + |
| 171 | +export const WithoutWordDiffs: Story = { |
| 172 | + decorators: [withDiffViewerState({ wordDiffs: false })], |
| 173 | +}; |
| 174 | + |
| 175 | +export const Editable: Story = {}; |
| 176 | + |
| 177 | +export const WithRelativePath: Story = { |
| 178 | + args: { |
| 179 | + relativePath: "src/services/dataProcessor.ts", |
| 180 | + }, |
| 181 | +}; |
0 commit comments