Skip to content

Commit 5082853

Browse files
committed
feat: getting somewhere
1 parent 15e7b0b commit 5082853

8 files changed

Lines changed: 88 additions & 198 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ tsconfig.lsif.json
99
*.lsif
1010
*.db
1111
.vscode/
12+
.env

server/.gitignore

Lines changed: 0 additions & 12 deletions
This file was deleted.

server/src/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { PrismaClient } from "@prisma/client";
22
import express from "express";
33

4+
const PORT = process.env.PORT || 3001;
5+
46
const app = express();
57

68
app.use(express.json());
@@ -16,14 +18,16 @@ async function main() {
1618

1719
app.post(`/`, async (req, res) => {
1820
data = req.body;
19-
res.json({ msg: "SUCCESS" });
21+
console.log("server: save data=", Object.keys(data.diff));
22+
res.json({ success: true });
2023
});
2124

2225
app.get(`/`, async (req, res) => {
26+
console.log("server: get data=", Object.keys(data.diff));
2327
res.json(data);
2428
});
2529

26-
const server = app.listen(3000, () => {
30+
const server = app.listen(PORT, () => {
2731
main()
2832
.catch((e) => {
2933
throw e;
@@ -32,5 +36,5 @@ const server = app.listen(3000, () => {
3236
await prisma.$disconnect();
3337
});
3438

35-
console.log(`🚀 Server ready at: http://localhost:3000`);
39+
console.log(`🚀 Server ready at: http://localhost:${PORT}`);
3640
});

solid/src/App.tsx

Lines changed: 43 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,58 @@
1-
import {
2-
children,
3-
createEffect,
4-
createMemo,
5-
createSignal,
6-
For,
7-
PropsWithChildren,
8-
splitProps,
9-
} from "solid-js";
10-
import { ResolvedJSXElement } from "solid-js/types/reactive/signal";
11-
import styles from "./App.module.css";
1+
import { createResource, For } from "solid-js";
122

13-
type Todo = {
14-
id: number;
15-
title: string;
16-
finished: boolean;
17-
};
18-
19-
const formattedTodo = (todo: Todo) => {
20-
return createMemo(
21-
() => `${todo.title} - ${todo.finished ? "DONE" : "ACTIVE"}`
22-
);
23-
};
24-
25-
type TodoProps = {
26-
todo: Todo;
27-
updateTodo: (todo: Todo) => void;
28-
deleteTodo: (todoId: number) => void;
29-
};
30-
31-
const Todo = (props: TodoProps) => {
32-
const [editedTitle, setEditedTitle] = createSignal(props.todo.title);
33-
const [editMode, setEditMode] = createSignal(false);
34-
35-
const [data, actions] = splitProps(props, ["todo"]);
36-
37-
return (
38-
<div>
39-
{editMode() ? (
40-
<input
41-
value={editedTitle()}
42-
onInput={(e) => setEditedTitle(e.currentTarget.value)}
43-
/>
44-
) : (
45-
<p>{formattedTodo(data.todo)()}</p>
46-
)}
47-
48-
{editMode() ? (
49-
<button
50-
onClick={() =>
51-
actions.updateTodo({ ...data.todo, title: editedTitle() })
52-
}>
53-
Save
54-
</button>
55-
) : (
56-
<button onClick={() => setEditMode(true)}>Edit</button>
57-
)}
58-
59-
{!data.todo.finished && (
60-
<button
61-
onClick={() => actions.updateTodo({ ...data.todo, finished: true })}>
62-
Finish
63-
</button>
64-
)}
65-
66-
<button onClick={() => actions.deleteTodo(data.todo.id)}>Delete</button>
67-
</div>
68-
);
69-
};
70-
71-
const ListTodos = (props: PropsWithChildren) => {
72-
const memo = children(() => props.children);
73-
const [counter, setCounter] = createSignal(0);
74-
75-
createEffect(() =>
76-
setCounter(((memo() as ResolvedJSXElement[]) || []).length)
77-
);
78-
// Below code won't work (because props.children.length is not "reactive"?)
79-
// createEffect(() => setCounter(props.children.length));
80-
81-
return (
82-
<div class={styles.todo}>
83-
<div>{memo()}</div>
84-
{/* <div>{props.children}</div> */}
85-
<p>Total todos: {counter()}</p>
86-
</div>
87-
);
88-
};
89-
90-
type AddTodosProps = {
91-
addTodo: (todo: Todo) => void;
92-
count: number;
93-
};
94-
95-
const AddTodos = (props: AddTodosProps) => {
96-
const [title, setTitle] = createSignal("");
3+
interface Device {
4+
id: string;
5+
name: string;
6+
userId: string;
7+
}
978

98-
const addTodo = () => {
99-
props.addTodo({ id: props.count, title: title(), finished: false });
100-
setTitle("");
9+
function App() {
10+
const [devices, { mutate, refetch }] = createResource<Device[]>(async () => {
11+
const response = await fetch("http://localhost:3001/api/devices", {
12+
method: "GET",
13+
body: JSON.stringify({
14+
userId: 1,
15+
}),
16+
});
17+
const connectedDevices = await response.json();
18+
return connectedDevices;
19+
});
20+
21+
const onSend = () => {
22+
console.log("onSend");
23+
vscodeApi.postMessage({
24+
type: "send",
25+
});
10126
};
10227

103-
return (
104-
<div>
105-
<input
106-
type="text"
107-
onInput={(e) => setTitle(e.currentTarget.value)}
108-
value={title()}
109-
/>
110-
{title() && <button onClick={addTodo}>Add</button>}
111-
</div>
112-
);
113-
};
114-
115-
function App() {
116-
const [todos, setTodos] = createSignal<Todo[]>([]);
28+
const onReceive = () => {
29+
console.log("onReceive");
30+
vscodeApi.postMessage({
31+
type: "receive",
32+
});
33+
};
11734

11835
return (
11936
<div>
120-
<button
121-
onClick={() =>
122-
vscodeApi.postMessage({
123-
type: "fetch",
124-
text: "hooli",
125-
})
126-
}>
127-
PUSH SOME MESSAGE TO VSCODE
128-
</button>
37+
<p>Devices</p>
12938

13039
<div>
131-
<h4>Simple Todo App</h4>
132-
<AddTodos
133-
addTodo={(todo) => setTodos([...todos(), todo])}
134-
count={todos().length}
135-
/>
136-
137-
<ListTodos>
138-
<For each={todos()}>
139-
{(todo) => (
140-
<Todo
141-
todo={todo}
142-
updateTodo={(newTodo) =>
143-
setTodos(
144-
todos().map((t) => (t.id === newTodo.id ? newTodo : t))
145-
)
146-
}
147-
deleteTodo={(todoId) =>
148-
setTodos(todos().filter((t) => t.id !== todoId))
149-
}
150-
/>
151-
)}
152-
</For>
153-
</ListTodos>
40+
<button onClick={onSend}>SEND</button>
41+
<button onClick={onReceive}>RECEIVE</button>
15442
</div>
15543

15644
<div>
157-
<h4>(WIP)</h4>
45+
<For each={devices()} fallback={<span>Empty</span>}>
46+
{(device) => (
47+
<div>
48+
<span>{`${device.name} - ${device.id}`}</span>
49+
<div>
50+
<button onClick={onSend}>SEND</button>
51+
<button onClick={onReceive}>RECEIVE</button>
52+
</div>
53+
</div>
54+
)}
55+
</For>
15856
</div>
15957
</div>
16058
);

solid/src/index.css

Lines changed: 0 additions & 13 deletions
This file was deleted.

solid/src/index.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* @refresh reload */
2-
import { render } from 'solid-js/web';
2+
import { render } from "solid-js/web";
33

4-
import './index.css';
5-
import App from './App';
4+
import App from "./App";
65

7-
render(() => <App />, document.getElementById('root') as HTMLElement);
6+
render(() => <App />, document.getElementById("root") as HTMLElement);

src/extension.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@ export async function activate(context: vscode.ExtensionContext) {
7373
console.log("STAGE", diff.staged);
7474
console.log("UNSTAGE", diff.unstaged);
7575

76-
// const response = await axios.post("http://localhost:3000", {
77-
// diff: diff,
78-
// });
79-
// console.log("response", response.data);
80-
81-
vscode.window.showInformationMessage("Changes saved");
76+
const response = await axios.post("http://localhost:3001", {
77+
diff,
78+
});
79+
if (response.data.success) {
80+
vscode.window.showInformationMessage("Changes saved");
81+
} else {
82+
vscode.window.showErrorMessage("Error sending and saving message");
83+
}
8284
})
8385
);
8486

@@ -90,25 +92,31 @@ export async function activate(context: vscode.ExtensionContext) {
9092
const repo = await git?.init(repositories[0].rootUri);
9193

9294
const currentDir = vscode.workspace.workspaceFolders || [];
93-
const patcFilePath = `${currentDir[0].uri.fsPath}/temp.patch`;
9495

9596
try {
96-
const retrievedChanges = await axios.get("http://localhost:3000");
97+
const retrievedChanges = await axios.get("http://localhost:3001");
9798
const diff = retrievedChanges.data.diff;
9899

100+
const stagedPatch = `${currentDir[0].uri.fsPath}/staged.patch`;
101+
await vscode.workspace.fs.writeFile(
102+
vscode.Uri.file(stagedPatch),
103+
Buffer.from(diff.staged)
104+
);
105+
106+
const unstagedPatch = `${currentDir[0].uri.fsPath}/unstaged.patch`;
99107
await vscode.workspace.fs.writeFile(
100-
vscode.Uri.file(patcFilePath),
101-
Buffer.from(diff)
108+
vscode.Uri.file(unstagedPatch),
109+
Buffer.from(diff.unstaged)
102110
);
103111
} catch (error) {
104112
console.error(error);
105113
}
106114

107-
repo?.apply(patcFilePath);
115+
// repo?.apply(patcFilePath);
108116

109-
await vscode.workspace.fs.delete(vscode.Uri.file(patcFilePath));
117+
// await vscode.workspace.fs.delete(vscode.Uri.file(patcFilePath));
110118

111-
vscode.window.showInformationMessage("Changes are applied");
119+
vscode.window.showInformationMessage("Patch file created");
112120
})
113121
);
114122
}

src/webviewProvider.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,22 @@ export class CodesyncWebviewProvider implements vscode.WebviewViewProvider {
2323

2424
webviewView.webview.onDidReceiveMessage((data) => {
2525
console.log("onDidReceiveMessage", data);
26+
2627
switch (data.type) {
27-
case "incoming": {
28-
vscode.window.activeTextEditor?.insertSnippet(
29-
new vscode.SnippetString(`#${data.value}`)
30-
);
28+
case "send": {
29+
console.log("send");
30+
vscode.commands.executeCommand("codesync.sendChanges");
31+
// vscode.window.activeTextEditor?.insertSnippet(
32+
// new vscode.SnippetString(`#${data.value}`)
33+
// );
3134
break;
3235
}
33-
case "outcoming": {
34-
vscode.window.activeTextEditor?.insertSnippet(
35-
new vscode.SnippetString(`#${data.value}`)
36-
);
36+
case "receive": {
37+
console.log("receive");
38+
vscode.commands.executeCommand("codesync.applyChanges");
39+
// vscode.window.activeTextEditor?.insertSnippet(
40+
// new vscode.SnippetString(`#${data.value}`)
41+
// );
3742
break;
3843
}
3944
}

0 commit comments

Comments
 (0)