1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1
"use client";
import { useEffect, useState } from "react";
import {
callOAuthApi,
type ApplicationCredentials,
type ApplicationView,
} from "@/lib/credentials";
import { format, getCredentialsMessages } from "@/lib/credentials-messages";
import type { Locale } from "@/lib/localization";
import { AccountSignInNotice } from "./AccountSignInNotice";
import { ApplicationForm } from "./ApplicationForm";
import { CredentialsCard } from "./CredentialsCard";
type DetailsState =
| { kind: "loading" }
| { kind: "signedOut" }
| { kind: "notFound" }
| { kind: "failed" }
| { kind: "ready"; application: ApplicationView };
/** One application of the signed-in account: its settings, its secret and its deletion. */
export function ApplicationDetails({
locale,
clientId,
}: {
locale: Locale;
clientId: string;
}) {
const { common, details, form, kinds } = getCredentialsMessages(locale);
const [state, setState] = useState<DetailsState>({ kind: "loading" });
const [saved, setSaved] = useState(false);
const [secret, setSecret] = useState<ApplicationCredentials | null>(null);
const path = `/applications/${encodeURIComponent(clientId)}`;
useEffect(() => {
const controller = new AbortController();
void callOAuthApi<ApplicationView>(
`/applications/${encodeURIComponent(clientId)}`,
{ signal: controller.signal },
).then((result) => {
if (result.kind === "signedOut") {
setState({ kind: "signedOut" });
} else if (result.kind === "ok") {
setState({ kind: "ready", application: result.value });
} else {
setState({ kind: result.kind === "notFound" ? "notFound" : "failed" });
}
});
return () => controller.abort();
}, [clientId]);
if (state.kind === "loading") {
return <p className="credentials-note">{common.loading}</p>;
}
if (state.kind === "signedOut") {
return <AccountSignInNotice locale={locale} />;
}
if (state.kind !== "ready") {
return (
<div className="credentials-panel">
<p>
{state.kind === "notFound" ? common.notFound : common.unreachable}
</p>
<div className="credentials-actions">
<a className="button button-secondary" href="/credentials">
{common.back}
</a>
</div>
</div>
);
}
const { application } = state;
async function rotate() {
const result = await callOAuthApi<ApplicationCredentials>(
`${path}/secret`,
{ method: "POST" },
);
if (result.kind === "ok") {
setSecret(result.value);
} else if (result.kind === "signedOut") {
setState({ kind: "signedOut" });
}
}
async function remove() {
if (!window.confirm(details.confirmDelete)) {
return;
}
const result = await callOAuthApi(path, { method: "DELETE" });
if (result.kind === "ok" || result.kind === "notFound") {
window.location.replace("/credentials");
} else if (result.kind === "signedOut") {
setState({ kind: "signedOut" });
}
}
return (
<>
<div className="credentials-panel">
<p className="credentials-meta">
<span>{kinds[application.kind].title}</span>
<code className="credentials-code">{application.clientId}</code>
<span>
{format(details.created, {
date: new Date(application.createdAt).toLocaleDateString(locale),
})}
</span>
</p>
{saved && (
<div className="form-message form-message-success" role="status">
{form.saved}
</div>
)}
</div>
<ApplicationForm
key={application.clientId}
locale={locale}
kind={application.kind}
initial={application}
submitLabel={form.save}
onSubmit={async (submission) => {
setSaved(false);
const result = await callOAuthApi<ApplicationView>(path, {
method: "PUT",
body: submission,
});
switch (result.kind) {
case "ok":
setState({ kind: "ready", application: result.value });
setSaved(true);
return null;
case "signedOut":
setState({ kind: "signedOut" });
return null;
case "refused":
return result.errors;
default:
return { form: common.unreachable };
}
}}
/>
{secret ? (
<CredentialsCard
locale={locale}
title={details.newSecret}
credentials={secret}
/>
) : (
<div className="credentials-panel">
<h2>{details.secretHeading}</h2>
{application.isPublic ? (
<p className="credentials-note">{details.noSecret}</p>
) : (
<>
<p className="credentials-note">{details.rotateNote}</p>
<div className="credentials-actions">
<button
className="button button-secondary"
type="button"
onClick={rotate}
>
{details.rotate}
</button>
</div>
</>
)}
</div>
)}
<div className="credentials-panel">
<h2>{details.deleteHeading}</h2>
<p className="credentials-note">{details.deleteNote}</p>
<div className="credentials-actions">
<button
className="button button-danger"
type="button"
onClick={remove}
>
{details.delete}
</button>
</div>
</div>
</>
);
}
|