Skip to content

Commit 55702c1

Browse files
committed
refactor: export into services api calls
1 parent c3227e3 commit 55702c1

4 files changed

Lines changed: 54 additions & 39 deletions

File tree

src/ui/components/CustomButtons/CodeActionButton.jsx

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, { useState, useEffect } from 'react';
2-
import axios from 'axios';
32
import Popper from '@material-ui/core/Popper';
43
import Paper from '@material-ui/core/Paper';
54
import {
@@ -10,8 +9,7 @@ import {
109
TerminalIcon,
1110
KeyIcon,
1211
} from '@primer/octicons-react';
13-
14-
const API_BASE = import.meta.env.VITE_API_URI ?? '';
12+
import { getSshConfig } from '../../services/ssh';
1513

1614
const CodeActionButton = ({ cloneURL }) => {
1715
const [anchorEl, setAnchorEl] = useState(null);
@@ -22,15 +20,14 @@ const CodeActionButton = ({ cloneURL }) => {
2220

2321
const [sshCfg, setSshCfg] = useState({ enabled: true, port: 22 });
2422
useEffect(() => {
25-
axios
26-
.get(`${API_BASE}/api/v1/config/ssh`, { withCredentials: true })
27-
.then((res) => {
28-
const { enabled = true, port = 22 } = res.data || {};
29-
setSshCfg({ enabled, port });
30-
})
31-
.catch((err) => {
32-
console.error('Failed to load SSH config:', err);
33-
});
23+
let active = true;
24+
(async () => {
25+
const cfg = await getSshConfig();
26+
if (active) setSshCfg(cfg);
27+
})();
28+
return () => {
29+
active = false;
30+
};
3431
}, []);
3532

3633
const getSSHUrl = () => {

src/ui/components/SSHKeysManager/SSHKeysManager.jsx

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import Alert from '@material-ui/lab/Alert';
1313
import { makeStyles } from '@material-ui/core/styles';
1414
import DeleteIcon from '@material-ui/icons/Delete';
1515
import VpnKeyIcon from '@material-ui/icons/VpnKey';
16-
import axios from 'axios';
17-
import dayjs from 'dayjs'; // npm i dayjs
16+
import dayjs from 'dayjs';
17+
18+
import { getSSHKeys, deleteSSHKey, addSSHKey } from '../../services/ssh';
1819

1920
const useStyles = makeStyles((theme) => ({
2021
root: { padding: theme.spacing(3), width: '100%' },
@@ -36,8 +37,6 @@ const useStyles = makeStyles((theme) => ({
3637
formField: { marginBottom: theme.spacing(2) },
3738
}));
3839

39-
const API_BASE = `${import.meta.env.VITE_API_URI}/api/v1/user`;
40-
4140
export default function SSHKeysManager({ username }) {
4241
const classes = useStyles();
4342

@@ -52,11 +51,8 @@ export default function SSHKeysManager({ username }) {
5251
const loadKeys = useCallback(async () => {
5352
if (!username) return;
5453
try {
55-
const res = await axios.get(`${API_BASE}/${username}/ssh-keys`, {
56-
withCredentials: true,
57-
});
58-
// API now returns [{name,fingerprint,addedAt}]
59-
setKeys(res.data.publicKeys || []);
54+
const data = await getSSHKeys(username);
55+
setKeys(data);
6056
} catch (err) {
6157
console.error(err);
6258
setBanner({ type: 'error', text: 'Failed to load SSH keys' });
@@ -71,10 +67,7 @@ export default function SSHKeysManager({ username }) {
7167
const handleDelete = async (index) => {
7268
const { fingerprint } = keys[index];
7369
try {
74-
await axios.delete(`${API_BASE}/${username}/ssh-keys/fingerprint`, {
75-
data: { fingerprint },
76-
withCredentials: true,
77-
});
70+
await deleteSSHKey(username, fingerprint);
7871
await loadKeys();
7972
setBanner({ type: 'success', text: 'SSH key removed' });
8073
} catch (err) {
@@ -90,16 +83,12 @@ export default function SSHKeysManager({ username }) {
9083
* Add new public key, then refresh list
9184
* --------------------------------------------------------- */
9285
const handleAddKey = async () => {
93-
const key = newKeyValue.trim();
86+
const publicKey = newKeyValue.trim();
9487
const name = newKeyName.trim();
95-
if (!key || !name) return;
88+
if (!publicKey || !name) return;
9689

9790
try {
98-
await axios.post(
99-
`${API_BASE}/${username}/ssh-keys`,
100-
{ publicKey: key, name },
101-
{ withCredentials: true },
102-
);
91+
await addSSHKey(username, { publicKey, name });
10392
await loadKeys();
10493
setBanner({ type: 'success', text: 'SSH key added' });
10594
setNewKeyValue('');
@@ -116,7 +105,6 @@ export default function SSHKeysManager({ username }) {
116105

117106
return (
118107
<div className={classes.root}>
119-
{/* ---------- Snackbar ---------- */}
120108
<Snackbar
121109
open={Boolean(banner)}
122110
autoHideDuration={4000}
@@ -169,7 +157,6 @@ export default function SSHKeysManager({ username }) {
169157
</Paper>
170158
))}
171159

172-
{/* ---------- Modal ---------- */}
173160
<Modal open={isModalOpen} onClose={() => setIsModalOpen(false)} className={classes.modal}>
174161
<div className={classes.modalContent}>
175162
<Typography variant='h6' gutterBottom>

src/ui/services/config.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,17 @@ const getUIRouteAuth = async (setData) => {
3232
});
3333
};
3434

35-
export {
36-
getAttestationConfig,
37-
getURLShortener,
38-
getEmailContact,
39-
getUIRouteAuth,
35+
const getSSHConfig = async (setData) => {
36+
const url = new URL(`${baseUrl}/config/ssh`);
37+
await axios(url.toString(), { withCredentials: true })
38+
.then((response) => {
39+
const { enabled = true, port = 22 } = response.data ?? {};
40+
setData({ enabled, port });
41+
})
42+
.catch((err) => {
43+
console.error('Failed to load SSH config:', err);
44+
setData({ enabled: true, port: 22 });
45+
});
4046
};
47+
48+
export { getAttestationConfig, getURLShortener, getEmailContact, getUIRouteAuth, getSSHConfig };

src/ui/services/ssh.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import axios from 'axios';
2+
3+
const BASE_URL = import.meta.env.VITE_API_URI
4+
? `${import.meta.env.VITE_API_URI}/api/v1/user`
5+
: `${location.origin}/api/v1/user`;
6+
7+
const AXIOS_CFG = { withCredentials: true };
8+
9+
export async function getSSHKeys(username) {
10+
const { data } = await axios.get(`${BASE_URL}/${username}/ssh-keys`, AXIOS_CFG);
11+
return data.publicKeys || [];
12+
}
13+
14+
export async function deleteSSHKey(username, fingerprint) {
15+
await axios.delete(`${BASE_URL}/${username}/ssh-keys/fingerprint`, {
16+
...AXIOS_CFG,
17+
data: { fingerprint },
18+
});
19+
}
20+
21+
export async function addSSHKey(username, key) {
22+
await axios.post(`${BASE_URL}/${username}/ssh-keys`, key, AXIOS_CFG);
23+
}

0 commit comments

Comments
 (0)