Skip to content

Commit 7c0ff31

Browse files
authored
Merge pull request #48 from ut-code/refactor
リファクタリング
2 parents 4c3fac5 + 698dcc6 commit 7c0ff31

File tree

25 files changed

+1076
-873
lines changed

25 files changed

+1076
-873
lines changed

desktop/app.go

Lines changed: 0 additions & 389 deletions
Large diffs are not rendered by default.

desktop/auth.go

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"io"
8+
"net/http"
9+
10+
"github.com/ut-code/Raxcel/server/types"
11+
"github.com/zalando/go-keyring"
12+
)
13+
14+
type SignupResult struct {
15+
UserId string `json:"userId"`
16+
Error string `json:"error"`
17+
}
18+
19+
func (a *App) Signup(email, password string) SignupResult {
20+
postData := types.SignupRequest{
21+
Email: email,
22+
Password: password,
23+
}
24+
jsonData, err := json.Marshal(postData)
25+
if err != nil {
26+
return SignupResult{
27+
UserId: "",
28+
Error: fmt.Sprintf("failed to marshal request: %v", err),
29+
}
30+
}
31+
apiUrl := getAPIURL()
32+
33+
resp, err := http.Post(fmt.Sprintf("%s/auth/signup", apiUrl), "application/json", bytes.NewReader(jsonData))
34+
if err != nil {
35+
return SignupResult{
36+
UserId: "",
37+
Error: fmt.Sprintf("Failed to send request: %v", err),
38+
}
39+
}
40+
defer resp.Body.Close()
41+
body, err := io.ReadAll(resp.Body)
42+
if err != nil {
43+
return SignupResult{
44+
UserId: "",
45+
Error: fmt.Sprintf("Failed to read response: %v", err),
46+
}
47+
}
48+
var serverResponse types.SignupResponse
49+
err = json.Unmarshal(body, &serverResponse)
50+
if err != nil {
51+
return SignupResult{
52+
UserId: "",
53+
Error: fmt.Sprintf("Failed to parse response: %v", err),
54+
}
55+
}
56+
if resp.StatusCode != http.StatusCreated {
57+
return SignupResult{
58+
UserId: "",
59+
Error: serverResponse.Error,
60+
}
61+
}
62+
return SignupResult{
63+
UserId: serverResponse.UserId,
64+
Error: "",
65+
}
66+
}
67+
68+
type SigninResult struct {
69+
Token string `json:"token"`
70+
Error string `json:"error"`
71+
}
72+
73+
func (a *App) Signin(email, password string) SigninResult {
74+
postData := types.SigninRequest{
75+
Email: email,
76+
Password: password,
77+
}
78+
jsonData, err := json.Marshal(postData)
79+
if err != nil {
80+
return SigninResult{
81+
Token: "",
82+
Error: fmt.Sprintf("Failed to marshal request: %v", err),
83+
}
84+
}
85+
apiUrl := getAPIURL()
86+
87+
resp, err := http.Post(fmt.Sprintf("%s/auth/signin", apiUrl), "application/json", bytes.NewReader(jsonData))
88+
if err != nil {
89+
return SigninResult{
90+
Token: "",
91+
Error: fmt.Sprintf("Failed to send request: %v", err),
92+
}
93+
}
94+
defer resp.Body.Close()
95+
96+
body, err := io.ReadAll(resp.Body)
97+
if err != nil {
98+
return SigninResult{
99+
Token: "",
100+
Error: fmt.Sprintf("Failed to read response: %v", err),
101+
}
102+
}
103+
104+
var serverResponse types.SigninResponse
105+
err = json.Unmarshal(body, &serverResponse)
106+
if err != nil {
107+
return SigninResult{
108+
Token: "",
109+
Error: fmt.Sprintf("Failed to parse response %v", err),
110+
}
111+
}
112+
if resp.StatusCode != http.StatusOK {
113+
return SigninResult{
114+
Token: "",
115+
Error: serverResponse.Error,
116+
}
117+
}
118+
token := serverResponse.Token
119+
err = keyring.Set("Raxcel", "raxcel-user", token)
120+
if err != nil {
121+
return SigninResult{
122+
Token: "",
123+
Error: fmt.Sprintf("Failed to store token: %v", err),
124+
}
125+
}
126+
return SigninResult{
127+
Token: token,
128+
Error: "",
129+
}
130+
}
131+
132+
type GetCurrentUserResult struct {
133+
UserId string `json:"userId"`
134+
Error string `json:"error"`
135+
}
136+
137+
func (a *App) GetCurrentUser() GetCurrentUserResult {
138+
apiUrl := getAPIURL()
139+
token, _ := keyring.Get("Raxcel", "raxcel-user")
140+
req, _ := http.NewRequest("GET", fmt.Sprintf("%s/users/me", apiUrl), nil)
141+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
142+
client := &http.Client{}
143+
resp, err := client.Do(req)
144+
if err != nil {
145+
return GetCurrentUserResult{
146+
UserId: "",
147+
Error: fmt.Sprint(err),
148+
}
149+
}
150+
defer resp.Body.Close()
151+
body, err := io.ReadAll(resp.Body)
152+
if err != nil {
153+
return GetCurrentUserResult{
154+
UserId: "",
155+
Error: fmt.Sprint(err),
156+
}
157+
}
158+
var serverResponse types.GetCurrentUserResponse
159+
if err := json.Unmarshal(body, &serverResponse); err != nil {
160+
return GetCurrentUserResult{
161+
UserId: "",
162+
Error: fmt.Sprint(err),
163+
}
164+
}
165+
if serverResponse.AuthMiddlewareReturn != nil {
166+
return GetCurrentUserResult{
167+
UserId: "",
168+
// Error: serverResponse.AuthMiddlewareReturn.MiddlewareError, でも同じこと
169+
Error: serverResponse.MiddlewareError,
170+
}
171+
}
172+
if serverResponse.GetCurrentUserResponse.Error != "" {
173+
return GetCurrentUserResult{
174+
UserId: "",
175+
Error: serverResponse.Error,
176+
}
177+
}
178+
return GetCurrentUserResult{
179+
UserId: serverResponse.UserId,
180+
Error: "",
181+
}
182+
}
183+
184+
type SignOutResult struct {
185+
Error string `json:"error"`
186+
}
187+
188+
func (a *App) SignOut() SignOutResult {
189+
err := keyring.Delete("Raxcel", "raxcel-user")
190+
if err != nil {
191+
return SignOutResult{
192+
Error: fmt.Sprintf("Failed to sign out: %v", err),
193+
}
194+
}
195+
return SignOutResult{
196+
Error: "",
197+
}
198+
}

desktop/frontend/src/lib/chart.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,29 @@ type Vertex = {
88
export function setupPlot(values: number[]): ChartConfiguration {
99
const dataPoints: Vertex[] = [];
1010

11-
const xValues = []
12-
const yValues = []
11+
const xValues = [];
12+
const yValues = [];
1313

1414
for (let i = 0; i < values.length; i += 2) {
1515
const x = values[i];
1616
const y = values[i + 1];
17-
xValues.push(x)
18-
yValues.push(y)
17+
xValues.push(x);
18+
yValues.push(y);
1919
dataPoints.push({
2020
x,
2121
y,
2222
});
2323
}
24-
25-
xValues.sort()
26-
yValues.sort()
27-
const xRange = xValues[xValues.length - 1] - xValues[0]
28-
const yRange = yValues[yValues.length - 1] - yValues[0]
29-
const marginRatio = 0.1
30-
const xAxisMax = xValues[xValues.length - 1] + xRange * marginRatio
31-
const xAxisMin = xValues[0] - xRange * marginRatio
32-
const yAxisMax = yValues[yValues.length - 1] + yRange * marginRatio
33-
const yAxisMin = yValues[0] - yRange * marginRatio
24+
25+
xValues.sort();
26+
yValues.sort();
27+
const xRange = xValues[xValues.length - 1] - xValues[0];
28+
const yRange = yValues[yValues.length - 1] - yValues[0];
29+
const marginRatio = 0.1;
30+
const xAxisMax = xValues[xValues.length - 1] + xRange * marginRatio;
31+
const xAxisMin = xValues[0] - xRange * marginRatio;
32+
const yAxisMax = yValues[yValues.length - 1] + yRange * marginRatio;
33+
const yAxisMin = yValues[0] - yRange * marginRatio;
3434
const data = {
3535
datasets: [
3636
{
@@ -43,16 +43,16 @@ export function setupPlot(values: number[]): ChartConfiguration {
4343
};
4444

4545
const plugin: Plugin = {
46-
id: 'customCanvasBackgroundColor',
47-
beforeDraw: (chart, args, options) => {
48-
const {ctx} = chart;
49-
ctx.save();
50-
ctx.globalCompositeOperation = 'destination-over';
51-
ctx.fillStyle = options.color || '#FFFFFF';
52-
ctx.fillRect(0, 0, chart.width, chart.height);
53-
ctx.restore();
54-
}
55-
};
46+
id: "customCanvasBackgroundColor",
47+
beforeDraw: (chart, args, options) => {
48+
const { ctx } = chart;
49+
ctx.save();
50+
ctx.globalCompositeOperation = "destination-over";
51+
ctx.fillStyle = options.color || "#FFFFFF";
52+
ctx.fillRect(0, 0, chart.width, chart.height);
53+
ctx.restore();
54+
},
55+
};
5656
const config: ChartConfiguration = {
5757
type: "scatter",
5858
data: data,
@@ -61,12 +61,12 @@ export function setupPlot(values: number[]): ChartConfiguration {
6161
scales: {
6262
x: {
6363
max: xAxisMax,
64-
min: xAxisMin
64+
min: xAxisMin,
6565
},
6666
y: {
6767
max: yAxisMax,
68-
min: yAxisMin
69-
}
68+
min: yAxisMin,
69+
},
7070
},
7171
},
7272
};

desktop/frontend/src/lib/components/Chart.svelte

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,34 @@
7777

7878
<dialog class="modal" bind:this={dialogRef}>
7979
<div class="modal-box">
80-
<button aria-label="export as image" class="btn btn-sm absolute top-2 right-6" onclick={exportImage}>
81-
<svg class="w-5 h-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M5.25589 16C3.8899 15.0291 3 13.4422 3 11.6493C3 9.20008 4.8 6.9375 7.5 6.5C8.34694 4.48637 10.3514 3 12.6893 3C15.684 3 18.1317 5.32251 18.3 8.25C19.8893 8.94488 21 10.6503 21 12.4969C21 14.0582 20.206 15.4339 19 16.2417M12 21V11M12 21L9 18M12 21L15 18" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path> </g></svg>
80+
<button
81+
aria-label="export as image"
82+
class="btn btn-sm absolute top-2 right-6"
83+
onclick={exportImage}
84+
>
85+
<svg
86+
class="w-5 h-5"
87+
viewBox="0 0 24 24"
88+
fill="none"
89+
xmlns="http://www.w3.org/2000/svg"
90+
><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g
91+
id="SVGRepo_tracerCarrier"
92+
stroke-linecap="round"
93+
stroke-linejoin="round"
94+
></g><g id="SVGRepo_iconCarrier">
95+
<path
96+
d="M5.25589 16C3.8899 15.0291 3 13.4422 3 11.6493C3 9.20008 4.8 6.9375 7.5 6.5C8.34694 4.48637 10.3514 3 12.6893 3C15.684 3 18.1317 5.32251 18.3 8.25C19.8893 8.94488 21 10.6503 21 12.4969C21 14.0582 20.206 15.4339 19 16.2417M12 21V11M12 21L9 18M12 21L15 18"
97+
stroke="#000000"
98+
stroke-width="2"
99+
stroke-linecap="round"
100+
stroke-linejoin="round"
101+
></path>
102+
</g></svg
103+
>
82104
</button>
83105
<canvas bind:this={canvasRef} class="bg-white"></canvas>
84106
</div>
85-
<form method="dialog" class="modal-backdrop">
86-
<button>close</button>
87-
</form>
107+
<form method="dialog" class="modal-backdrop">
108+
<button>close</button>
109+
</form>
88110
</dialog>

desktop/frontend/src/lib/components/Chat.svelte

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
22
import { onMount } from "svelte";
3-
import { ChatWithAI, GetMessages } from "../wailsjs/go/main/App";
3+
import { ChatWithAI, LoadChatHistory } from "../wailsjs/go/main/App";
44
import Dialog from "$lib/components/Dialog.svelte";
55
import type { Cell } from "$lib/types";
66
import { gridToMarkdownTable } from "$lib/sheet";
@@ -38,14 +38,14 @@
3838
3939
onMount(async () => {
4040
// Load chat history when component mounts
41-
const res = await GetMessages();
42-
if (res.ok && res.messages) {
43-
messages = res.messages.map((msg) => ({
41+
const result = await LoadChatHistory();
42+
if (result.error === "") {
43+
messages = result.messages.map((msg) => ({
4444
author: msg.role === "user" ? "user" : "ai",
4545
message: msg.content,
4646
}));
47-
} else if (res.error) {
48-
console.error("Failed to load messages:", res.error);
47+
} else {
48+
console.error("Failed to load messages:", result.error);
4949
}
5050
});
5151
@@ -60,14 +60,14 @@
6060
// シート内容を含めるかどうかで分岐
6161
const spreadsheetContext = includeSheet ? gridToMarkdownTable(grid) : "";
6262
63-
const res = await ChatWithAI(userMessage, spreadsheetContext);
63+
const result = await ChatWithAI(userMessage, spreadsheetContext);
6464
userMessage = "";
65-
if (!res.ok) {
66-
showDialog(`Error: ${res.message}`, "AI Chat Error", "error");
65+
if (result.error !== "") {
66+
showDialog(`Error: ${result.error}`, "AI Chat Error", "error");
6767
}
6868
const newAiMessage: Message = {
6969
author: "ai",
70-
message: res.message,
70+
message: result.message,
7171
};
7272
messages.push(newAiMessage);
7373
isLoading = false;

desktop/frontend/src/lib/components/Toolbar.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
5252
async function handleSignOut() {
5353
const result = await SignOut();
54-
if (result.ok) {
54+
if (result.error === "") {
5555
authState.logout();
5656
} else {
5757
showDialog(`Sign out failed: ${result.error}`, "Sign Out Error", "error");

0 commit comments

Comments
 (0)