Skip to content

Commit 78eb3fb

Browse files
author
jiangtao.yang
committed
feat: add home page
1 parent 4496e20 commit 78eb3fb

16 files changed

Lines changed: 1979 additions & 179 deletions

File tree

.env.defaults

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# create .env file to override the defaults
2+
API_KEY=an-open-ai-key-for-call-chatgpt
3+
MODEL=deepseek-chat

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# an API KEY for call chatgpt
2+
API_KEY=your-api-key
3+
4+
# a llm model name
5+
MODEL=deepseek-chat

apps/dsv/package.json

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,24 @@
88
"format": "prettier --write ."
99
},
1010
"dependencies": {
11+
"@ant-design/icons": "5.x",
12+
"@types/dotenv-webpack": "^7.0.8",
1113
"@visactor/vstory": "0.0.23",
14+
"antd": "^5.24.9",
15+
"assert": "^2.1.0",
16+
"buffer": "^6.0.3",
17+
"data-structure": "workspace:*",
18+
"dotenv-webpack": "^8.1.0",
19+
"openai": "^4.97.0",
20+
"parser": "workspace:*",
21+
"path-browserify": "^1.0.1",
22+
"process": "^0.11.10",
1223
"react": "^18.2.0",
1324
"react-dom": "^18.2.0",
14-
"parser": "workspace:*",
15-
"data-structure": "workspace:*",
1625
"schema": "workspace:*",
17-
"process": "^0.11.10",
18-
"assert": "^2.1.0",
19-
"path-browserify": "^1.0.1",
20-
"buffer": "^6.0.3",
26+
"styled-components": "^6.1.17",
2127
"util": "^0.12.5",
22-
"zustand": "^5.0.3"
28+
"zustand": "5.0.4"
2329
},
2430
"devDependencies": {
2531
"@rspack/cli": "^1.2.1",

apps/dsv/rspack.config.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { defineConfig } from '@rspack/cli';
22
import { rspack } from '@rspack/core';
33
import * as RefreshPlugin from '@rspack/plugin-react-refresh';
4+
const Dotenv = require('dotenv-webpack');
45

56
const isDev = process.env.NODE_ENV === 'development';
6-
77
// Target browsers, see: https://github.com/browserslist/browserslist
88
const targets = ['chrome >= 87', 'edge >= 88', 'firefox >= 78', 'safari >= 14'];
99

@@ -52,6 +52,12 @@ export default defineConfig({
5252
},
5353
],
5454
},
55+
{
56+
test: /\.m?js/,
57+
resolve: {
58+
fullySpecified: false,
59+
},
60+
},
5561
],
5662
},
5763
plugins: [
@@ -63,6 +69,11 @@ export default defineConfig({
6369
process: 'process/browser',
6470
Buffer: ['buffer', 'Buffer'],
6571
}),
72+
new Dotenv({
73+
path: '../../.env',
74+
safe: true,
75+
defaults: true,
76+
}),
6677
].filter(Boolean),
6778
optimization: {
6879
minimizer: [

apps/dsv/src/App.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
html,
2+
body {
3+
padding: 0;
4+
margin: 0;
5+
background: #fff;
6+
}

apps/dsv/src/App.tsx

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
1-
import { Chart } from './Chart';
2-
import { StructureType } from 'schema';
3-
import { CodeEditor } from './CodeEditor/CodeEditor';
1+
import { Home } from './components/Home/Home';
2+
import './App.css';
43

54
function App() {
6-
return (
7-
<div
8-
style={{
9-
display: 'flex',
10-
justifyContent: 'start',
11-
alignItems: 'center',
12-
height: '100vh',
13-
}}
14-
>
15-
<CodeEditor />
16-
<Chart />
17-
</div>
18-
);
5+
return <Home />;
196
}
207

218
export default App;

apps/dsv/src/CodeEditor/CodeEditor.tsx

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useRef } from 'react';
44
import { FC } from 'react';
55
import { ArrayBar } from 'data-structure';
66
import { Schema } from 'schema';
7-
import { useDsv } from './model';
7+
import { useDsv } from '../model';
88
VStory.registerAll();
99

1010
export const Chart: FC = () => {
@@ -21,17 +21,20 @@ export const Chart: FC = () => {
2121
}
2222
const arrayBar = new ArrayBar<number>([...schema.structures[0].array], {
2323
id: schema.structures[0].id,
24-
interval: 1000,
24+
interval: 400,
2525
structure: {},
2626
});
2727
schema.actions.forEach((action) => {
2828
if (action.structureId === arrayBar.id) {
2929
if (action.type === 'set') {
3030
arrayBar.set(action.args[0], action.args[1]);
3131
}
32-
if (action.type === 'get') {
33-
arrayBar.get(action.args[0]);
32+
if (action.type === 'highlight') {
33+
arrayBar.highlight(action.args[0]);
3434
}
35+
// if (action.type === 'get') {
36+
// arrayBar.get(action.args[0]);
37+
// }
3538
if (action.type === 'compare') {
3639
arrayBar.compare(action.args[0], action.args[1], action.args[2]);
3740
}
@@ -69,7 +72,7 @@ export const Chart: FC = () => {
6972

7073
const story = new VStory.Story(dsl, {
7174
dom: ref.current,
72-
background: 'pink',
75+
// background: 'pink',
7376
});
7477
const player = new VStory.Player(story);
7578
story.init(player);
@@ -82,8 +85,14 @@ export const Chart: FC = () => {
8285
}, [schema]);
8386

8487
return (
85-
<>
86-
<div style={{ width: '600px', height: '600px' }} ref={ref}></div>
87-
</>
88+
<div
89+
style={{
90+
width: '100%',
91+
height: '100%',
92+
borderRadius: '8px',
93+
overflow: 'hidden',
94+
}}
95+
ref={ref}
96+
></div>
8897
);
8998
};
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import { useCallback, useState } from 'react';
2+
import { run } from 'parser';
3+
import { useDsv } from '../../model';
4+
import { OpenAI } from 'openai';
5+
import { Button, Input } from 'antd';
6+
import { systemPrompt } from './constant';
7+
import { CodeContainer, Toolbar, CodeInput } from './style';
8+
9+
const S = {
10+
CodeContainer,
11+
Toolbar,
12+
CodeInput,
13+
};
14+
15+
export const CodeEditor = () => {
16+
const setSchema = useDsv((state) => state.setSchema);
17+
18+
const [apiKey, setApiKey] = useState<string>(process.env.API_KEY as string);
19+
const [model, setModel] = useState<string>(process.env.MODEL as string);
20+
21+
const [userPrompt, setUserPrompt] = useState(`生成冒泡排序`);
22+
const [isGenerating, setIsGenerating] = useState(false);
23+
const [code, setCode] = useState(`function bubbleSort(arr) {
24+
Array.prototype.swap = function(i, j) {
25+
var temp = this[i];
26+
this[i] = this[j];
27+
this[j] = temp;
28+
};
29+
30+
var len = arr.length;
31+
for (var i = 0; i < len - 1; i++) {
32+
for (var j = 0; j < len - 1 - i; j++) {
33+
if (arr[j] > arr[j + 1]) {
34+
arr.swap(j, j + 1);
35+
}
36+
}
37+
}
38+
return arr;
39+
}
40+
41+
// 执行函数, 并给出测试用例
42+
const result = bubbleSort([5, 3, 8, 4, 2]);
43+
console.log(result);`);
44+
45+
const handleExec = useCallback(
46+
(code: string) => {
47+
const result = run(code);
48+
setSchema(result.schema);
49+
},
50+
[code],
51+
);
52+
53+
const handleGenerateCode = async () => {
54+
setIsGenerating(true);
55+
try {
56+
const client = new OpenAI({
57+
baseURL: 'https://api.deepseek.com',
58+
apiKey,
59+
dangerouslyAllowBrowser: true,
60+
});
61+
62+
const completion = await client.chat.completions.create({
63+
model,
64+
messages: [
65+
{
66+
role: 'system',
67+
content: systemPrompt,
68+
},
69+
{
70+
role: 'user',
71+
content: userPrompt,
72+
},
73+
],
74+
temperature: 0.7,
75+
});
76+
77+
setCode(completion.choices[0].message.content || '');
78+
} catch (error) {
79+
alert('生成失败,请检查API配置');
80+
} finally {
81+
setIsGenerating(false);
82+
}
83+
};
84+
85+
return (
86+
<S.CodeContainer>
87+
<S.Toolbar>
88+
<Input.TextArea
89+
autoSize={{ minRows: 1, maxRows: 6 }}
90+
value={userPrompt}
91+
onChange={(e) => setUserPrompt(e.target.value)}
92+
placeholder="输入算法需求..."
93+
style={{
94+
width: '100%',
95+
}}
96+
/>
97+
98+
<Button
99+
onClick={handleGenerateCode}
100+
disabled={isGenerating}
101+
loading={isGenerating}
102+
>
103+
{isGenerating ? '生成中...' : '智能生成'}
104+
</Button>
105+
106+
<Button onClick={() => handleExec(code)}>执行</Button>
107+
</S.Toolbar>
108+
<S.CodeInput>
109+
<Input.TextArea
110+
size="small"
111+
style={{
112+
height: 'auto',
113+
fontFamily: 'monospace',
114+
}}
115+
value={code}
116+
onChange={(e) => setCode(e.target.value)}
117+
/>
118+
</S.CodeInput>
119+
</S.CodeContainer>
120+
);
121+
};

apps/dsv/src/components/CodeEditor/Toolbar.tsx

Whitespace-only changes.

0 commit comments

Comments
 (0)