Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/apps/web/components/credentials/AllowedApplications.tsx
blob: ec7b857e652ef178af6d71404ee02970f454d5f7 (plain)
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
// 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,
  signInPath,
  type AllowedApplication,
} from "@/lib/credentials";
import { format, getCredentialsMessages } from "@/lib/credentials-messages";
import type { Locale } from "@/lib/localization";

type AllowedState =
  | { kind: "loading" }
  | { kind: "failed" }
  | { kind: "ready"; allowed: AllowedApplication[] };

/** The applications the signed-in account has allowed, each with a Withdraw button. */
export function AllowedApplications({ locale }: { locale: Locale }) {
  const { allowed: text, common } = getCredentialsMessages(locale);
  const [state, setState] = useState<AllowedState>({ kind: "loading" });

  useEffect(() => {
    const controller = new AbortController();
    void callOAuthApi<AllowedApplication[]>("/allowed", {
      signal: controller.signal,
    }).then((result) => {
      if (result.kind === "signedOut") {
        window.location.replace(signInPath("/account/applications"));
      } else if (result.kind === "ok") {
        setState({ kind: "ready", allowed: result.value });
      } else {
        setState({ kind: "failed" });
      }
    });
    return () => controller.abort();
  }, []);

  async function withdraw(authorizationId: string) {
    const result = await callOAuthApi(
      `/allowed/${encodeURIComponent(authorizationId)}`,
      { method: "DELETE" },
    );
    if (result.kind === "ok" || result.kind === "notFound") {
      setState((current) =>
        current.kind === "ready"
          ? {
              kind: "ready",
              allowed: current.allowed.filter(
                (entry) => entry.authorizationId !== authorizationId,
              ),
            }
          : current,
      );
    } else if (result.kind === "signedOut") {
      window.location.assign(signInPath("/account/applications"));
    }
  }

  if (state.kind === "loading") {
    return <p className="credentials-note">{common.loading}</p>;
  }
  if (state.kind === "failed") {
    return (
      <div className="form-message form-message-error" role="alert">
        {common.unreachable}
      </div>
    );
  }

  return (
    <div className="credentials-panel">
      {state.allowed.length === 0 ? (
        <p>{text.empty}</p>
      ) : (
        <ul className="credentials-list">
          {state.allowed.map((entry) => (
            <li key={entry.authorizationId}>
              <div>
                <strong>{entry.displayName ?? text.deleted}</strong>
                <span className="credentials-meta">
                  {entry.createdAt && (
                    <span>
                      {format(text.since, {
                        date: new Date(entry.createdAt).toLocaleDateString(
                          locale,
                        ),
                      })}
                    </span>
                  )}
                  {entry.scopes.map((scope) => (
                    <code className="credentials-code" key={scope}>
                      {scope}
                    </code>
                  ))}
                </span>
              </div>
              <button
                className="button button-secondary button-small"
                type="button"
                onClick={() => withdraw(entry.authorizationId)}
              >
                {text.withdraw}
              </button>
            </li>
          ))}
        </ul>
      )}
    </div>
  );
}