@shivaedev/admin 0.12.3 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +38 -4
  3. package/dist/client/JobButton.d.ts +3 -0
  4. package/dist/client/JobButton.d.ts.map +1 -0
  5. package/dist/client/JobButton.js +5 -0
  6. package/dist/client/JobButton.js.map +1 -0
  7. package/dist/client/JobDetail.d.ts +12 -0
  8. package/dist/client/JobDetail.d.ts.map +1 -0
  9. package/dist/client/JobDetail.js +27 -0
  10. package/dist/client/JobDetail.js.map +1 -0
  11. package/dist/client/JobList.d.ts +8 -0
  12. package/dist/client/JobList.d.ts.map +1 -0
  13. package/dist/client/JobList.js +25 -0
  14. package/dist/client/JobList.js.map +1 -0
  15. package/dist/client/JobPagination.d.ts +8 -0
  16. package/dist/client/JobPagination.d.ts.map +1 -0
  17. package/dist/client/JobPagination.js +12 -0
  18. package/dist/client/JobPagination.js.map +1 -0
  19. package/dist/client/JobTable.d.ts +6 -0
  20. package/dist/client/JobTable.d.ts.map +1 -0
  21. package/dist/client/JobTable.js +13 -0
  22. package/dist/client/JobTable.js.map +1 -0
  23. package/dist/client/JobsMetrics.d.ts +9 -0
  24. package/dist/client/JobsMetrics.d.ts.map +1 -0
  25. package/dist/client/JobsMetrics.js +15 -0
  26. package/dist/client/JobsMetrics.js.map +1 -0
  27. package/dist/client/JobsQueryPanel.d.ts +7 -0
  28. package/dist/client/JobsQueryPanel.d.ts.map +1 -0
  29. package/dist/client/JobsQueryPanel.js +11 -0
  30. package/dist/client/JobsQueryPanel.js.map +1 -0
  31. package/dist/client/JobsQueueView.d.ts +12 -0
  32. package/dist/client/JobsQueueView.d.ts.map +1 -0
  33. package/dist/client/JobsQueueView.js +18 -0
  34. package/dist/client/JobsQueueView.js.map +1 -0
  35. package/dist/client/JobsView.d.ts +12 -0
  36. package/dist/client/JobsView.d.ts.map +1 -0
  37. package/dist/client/JobsView.js +17 -0
  38. package/dist/client/JobsView.js.map +1 -0
  39. package/dist/client/SessionConfirmation.d.ts +11 -0
  40. package/dist/client/SessionConfirmation.d.ts.map +1 -0
  41. package/dist/client/SessionConfirmation.js +15 -0
  42. package/dist/client/SessionConfirmation.js.map +1 -0
  43. package/dist/client/SessionListView.d.ts +11 -0
  44. package/dist/client/SessionListView.d.ts.map +1 -0
  45. package/dist/client/SessionListView.js +22 -0
  46. package/dist/client/SessionListView.js.map +1 -0
  47. package/dist/client/SessionPagination.d.ts +10 -0
  48. package/dist/client/SessionPagination.d.ts.map +1 -0
  49. package/dist/client/SessionPagination.js +9 -0
  50. package/dist/client/SessionPagination.js.map +1 -0
  51. package/dist/client/SessionsView.d.ts +21 -0
  52. package/dist/client/SessionsView.d.ts.map +1 -0
  53. package/dist/client/SessionsView.js +48 -0
  54. package/dist/client/SessionsView.js.map +1 -0
  55. package/dist/client/jobsTypes.d.ts +35 -0
  56. package/dist/client/jobsTypes.d.ts.map +1 -0
  57. package/dist/client/jobsTypes.js +2 -0
  58. package/dist/client/jobsTypes.js.map +1 -0
  59. package/dist/jobs/page.d.ts +49 -47
  60. package/dist/jobs/page.d.ts.map +1 -1
  61. package/dist/jobs/page.js +6 -1
  62. package/dist/jobs/page.js.map +1 -1
  63. package/dist/jobs/retryPage.d.ts +8 -6
  64. package/dist/jobs/retryPage.d.ts.map +1 -1
  65. package/dist/jobs/schemas.d.ts +3 -2
  66. package/dist/jobs/schemas.d.ts.map +1 -1
  67. package/dist/jobs/schemas.js +3 -2
  68. package/dist/jobs/schemas.js.map +1 -1
  69. package/dist/server/jobsHandlers.d.ts +4 -4
  70. package/dist/sessions/Page.d.ts +98 -0
  71. package/dist/sessions/Page.d.ts.map +1 -0
  72. package/dist/sessions/Page.js +10 -0
  73. package/dist/sessions/Page.js.map +1 -0
  74. package/dist/users/page.d.ts.map +1 -1
  75. package/dist/users/page.js +2 -1
  76. package/dist/users/page.js.map +1 -1
  77. package/package.json +10 -10
  78. package/src/client/JobButton.tsx +5 -0
  79. package/src/client/JobDetail.tsx +81 -0
  80. package/src/client/JobList.tsx +60 -0
  81. package/src/client/JobPagination.tsx +31 -0
  82. package/src/client/JobTable.tsx +48 -0
  83. package/src/client/JobsMetrics.tsx +37 -0
  84. package/src/client/JobsQueryPanel.tsx +23 -0
  85. package/src/client/JobsQueueView.tsx +74 -0
  86. package/src/client/JobsView.ts +34 -0
  87. package/src/client/SessionConfirmation.tsx +44 -0
  88. package/src/client/SessionListView.tsx +66 -0
  89. package/src/client/SessionPagination.tsx +40 -0
  90. package/src/client/SessionsView.ts +75 -0
  91. package/src/client/jobsTypes.ts +32 -0
  92. package/src/jobs/page.ts +6 -1
  93. package/src/jobs/schemas.ts +3 -2
  94. package/src/sessions/Page.ts +10 -0
  95. package/src/users/page.ts +2 -1
@@ -0,0 +1,66 @@
1
+ import { DateTime } from "effect";
2
+ import type { SignedInSession } from "@shivaedev/identity/sessions.ts";
3
+
4
+ interface SessionControls {
5
+ readonly currentSessionId: string | undefined;
6
+ readonly pending: boolean;
7
+ readonly select: (session: typeof SignedInSession.Type) => void;
8
+ }
9
+
10
+ export function SessionListView({ sessions, ...controls }: SessionControls & { readonly sessions: readonly (typeof SignedInSession.Type)[] }) {
11
+ if (sessions.length === 0) {
12
+ return <p>No signed-in sessions.</p>;
13
+ }
14
+ return (
15
+ <ul aria-label="Sessions" className="grid min-w-0 gap-4">
16
+ {sessions.map((session) => (
17
+ <SessionRow {...controls} key={session.id} session={session} />
18
+ ))}
19
+ </ul>
20
+ );
21
+ }
22
+
23
+ function SessionRow({ session, currentSessionId, pending, select }: SessionControls & { readonly session: typeof SignedInSession.Type }) {
24
+ const current = session.id === currentSessionId && <p className="font-medium">Your current session</p>;
25
+ return (
26
+ <li className="min-w-0 space-y-3 rounded-md border p-4 wrap-anywhere">
27
+ <div className="flex flex-wrap items-start justify-between gap-3">
28
+ <div className="min-w-0">
29
+ <p className="font-medium">{session.user.name}</p>
30
+ <p>{session.user.email}</p>
31
+ {current}
32
+ </div>
33
+ <button
34
+ aria-label={`Revoke session for ${session.user.email}`}
35
+ className="rounded-md border px-3 py-2 disabled:opacity-50"
36
+ disabled={pending}
37
+ onClick={() => select(session)}
38
+ type="button"
39
+ >
40
+ Revoke session
41
+ </button>
42
+ </div>
43
+ <SessionTimes session={session} />
44
+ </li>
45
+ );
46
+ }
47
+
48
+ function SessionTimes({ session }: { readonly session: typeof SignedInSession.Type }) {
49
+ const times = [
50
+ ["Started", session.startedAt],
51
+ ["Last activity", session.lastActivityAt],
52
+ ["Expires", session.expiresAt],
53
+ ] as const;
54
+ return (
55
+ <dl className="grid min-w-0 gap-3 text-sm sm:grid-cols-3">
56
+ {times.map(([label, time]) => (
57
+ <div key={label}>
58
+ <dt className="text-muted-foreground">{label}</dt>
59
+ <dd>
60
+ <time dateTime={DateTime.formatIso(time)}>{DateTime.formatIso(time)}</time>
61
+ </dd>
62
+ </div>
63
+ ))}
64
+ </dl>
65
+ );
66
+ }
@@ -0,0 +1,40 @@
1
+ import { Option } from "effect";
2
+ import type { SessionList, SessionListInput } from "@shivaedev/identity/sessions.ts";
3
+
4
+ type Cursor = NonNullable<typeof SessionListInput.Type.cursor>;
5
+
6
+ export function SessionPagination({
7
+ data,
8
+ cursors,
9
+ pending,
10
+ go,
11
+ }: {
12
+ readonly data: typeof SessionList.Type | undefined;
13
+ readonly cursors: readonly Cursor[];
14
+ readonly pending: boolean;
15
+ readonly go: (cursors: readonly Cursor[]) => void;
16
+ }) {
17
+ if (data === undefined) {
18
+ return null;
19
+ }
20
+ return (
21
+ <nav aria-label="Session pages" className="flex flex-wrap gap-3">
22
+ <button
23
+ className="rounded-md border px-3 py-2 disabled:opacity-50"
24
+ disabled={cursors.length === 0 || pending}
25
+ onClick={() => go(cursors.slice(0, -1))}
26
+ type="button"
27
+ >
28
+ Previous sessions
29
+ </button>
30
+ <button
31
+ className="rounded-md border px-3 py-2 disabled:opacity-50"
32
+ disabled={Option.isNone(data.next) || pending}
33
+ onClick={() => Option.map(data.next, (next) => go([...cursors, next]))}
34
+ type="button"
35
+ >
36
+ Next sessions
37
+ </button>
38
+ </nav>
39
+ );
40
+ }
@@ -0,0 +1,75 @@
1
+ import { Cause, type Effect, Option, Predicate } from "effect";
2
+ import type * as AsyncResult from "effect/reactivity/AsyncResult";
3
+ import type * as Atom from "effect/reactivity/Atom";
4
+ import { createElement, useState } from "react";
5
+ import type { SessionList, SessionListInput, SignedInSession } from "@shivaedev/identity/sessions.ts";
6
+ import { useAction, useQuery } from "@shivaedev/react/client/resultState.ts";
7
+ import type { AtomServices } from "@shivaedev/react/fieldRejection.ts";
8
+ import { SessionConfirmation } from "./SessionConfirmation.tsx";
9
+ import { SessionListView } from "./SessionListView.tsx";
10
+ import { SessionPagination } from "./SessionPagination.tsx";
11
+
12
+ export interface SessionsViewProps<R, E> {
13
+ readonly currentSessionId: string | undefined;
14
+ readonly list: (input: typeof SessionListInput.Type) => Atom.Atom<AsyncResult.AsyncResult<typeof SessionList.Type, unknown>>;
15
+ readonly onCurrentSessionRevoked: () => void;
16
+ readonly revoke: (input: { readonly id: string }) => Effect.Effect<void, unknown, AtomServices<R>>;
17
+ readonly runtime: Atom.AtomRuntime<R, E>;
18
+ }
19
+
20
+ function forbidden(cause: Option.Option<Cause.Cause<unknown>>) {
21
+ return Option.exists(Option.flatMap(cause, Cause.findErrorOption), (error) => Predicate.isTagged(error, "Forbidden"));
22
+ }
23
+
24
+ export function SessionsView<R, E>({ currentSessionId, list, onCurrentSessionRevoked, revoke, runtime }: SessionsViewProps<R, E>) {
25
+ const [cursors, setCursors] = useState<readonly NonNullable<typeof SessionListInput.Type.cursor>[]>([]);
26
+ const [selected, select] = useState<typeof SignedInSession.Type>();
27
+ const cursor = cursors.at(-1);
28
+ const state = useQuery(list({ ...(cursor === undefined ? {} : { cursor }), limit: 20 }));
29
+ const action = useAction(runtime, revoke, {
30
+ onSuccess: (_, { id }) => {
31
+ select(undefined);
32
+ if (id === currentSessionId) {
33
+ onCurrentSessionRevoked();
34
+ }
35
+ },
36
+ });
37
+ if (forbidden(state.cause)) {
38
+ return createElement("p", { role: "alert" }, "Permission required to manage sessions.");
39
+ }
40
+ const data = Option.getOrUndefined(state.data);
41
+ return createElement(
42
+ "section",
43
+ { "aria-label": "Signed-in sessions", className: "min-w-0 space-y-4" },
44
+ createElement(
45
+ "button",
46
+ {
47
+ className: "rounded-md border px-3 py-2 disabled:opacity-50",
48
+ disabled: state.pending || action.pending,
49
+ onClick: state.refresh,
50
+ type: "button",
51
+ },
52
+ "Refresh sessions",
53
+ ),
54
+ state.pending ? createElement("p", { role: "status" }, "Loading sessions…") : null,
55
+ Option.isSome(state.cause) ? createElement("p", { role: "alert" }, "Could not load sessions. Try refreshing.") : null,
56
+ Option.isSome(action.cause) ? createElement("p", { role: "alert" }, "Could not revoke the session. Refresh and try again.") : null,
57
+ createElement(SessionConfirmation, {
58
+ currentSessionId,
59
+ onCancel: () => select(undefined),
60
+ onConfirm: action.dispatch,
61
+ pending: action.pending,
62
+ selected,
63
+ }),
64
+ data === undefined ? null : createElement(SessionListView, { currentSessionId, pending: action.pending, select, sessions: data.sessions }),
65
+ createElement(SessionPagination, {
66
+ cursors,
67
+ data,
68
+ go: (next) => {
69
+ select(undefined);
70
+ setCursors(next);
71
+ },
72
+ pending: state.pending || action.pending,
73
+ }),
74
+ );
75
+ }
@@ -0,0 +1,32 @@
1
+ import type { Option, Schema } from "effect";
2
+ import type * as AsyncResult from "effect/reactivity/AsyncResult";
3
+ import type * as Atom from "effect/reactivity/Atom";
4
+ import type { deadLetteredSchema, JobsHealth, jobSchema, QueueCounts } from "#jobs/schemas.ts";
5
+
6
+ type Job = ReturnType<typeof jobSchema<typeof Schema.Unknown>>["Type"];
7
+ type DeadLetter = ReturnType<typeof deadLetteredSchema<typeof Schema.Unknown>>["Type"];
8
+ export type AdminJob = Job | DeadLetter;
9
+ interface JobId {
10
+ readonly id: string;
11
+ readonly queue: string;
12
+ }
13
+ interface PageInput {
14
+ readonly cursor?: string;
15
+ readonly limit: number;
16
+ readonly queue: string;
17
+ }
18
+ interface Page<A> {
19
+ readonly jobs: readonly A[];
20
+ readonly next: Option.Option<string>;
21
+ }
22
+ interface Query<TInput, A> {
23
+ readonly query: (input: TInput) => Atom.Atom<AsyncResult.AsyncResult<A, unknown>>;
24
+ }
25
+ export interface JobsViewClient {
26
+ readonly counts: Query<{ readonly queue: string }, typeof QueueCounts.Type>;
27
+ readonly deadLettered: Query<PageInput, Page<DeadLetter>>;
28
+ readonly failed: Query<PageInput, Page<Job>>;
29
+ readonly health: Query<void, typeof JobsHealth.Type>;
30
+ readonly job: Query<JobId, Option.Option<Job>>;
31
+ }
32
+ export type RetryJob = Atom.AtomResultFn<JobId, void, unknown>;
package/src/jobs/page.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Schema } from "effect";
1
+ import { Effect, Schema } from "effect";
2
2
  import { contract } from "@shivaedev/contract/contract.ts";
3
3
  import { query } from "@shivaedev/contract/operation.ts";
4
4
  import { type JobsEntry, type JobsPayload, jobsQueue } from "#jobs/entry.ts";
@@ -15,30 +15,35 @@ export function jobsPage<const TQueues extends readonly JobsEntry[]>(...queues:
15
15
  contract: contract("admin.jobs", {
16
16
  queries: [
17
17
  query("health", {
18
+ readPolicy: () => Effect.succeed(true),
18
19
  reads: () => [jobsKeys.health.list],
19
20
  rejections: JobsFailures,
20
21
  success: JobsHealth,
21
22
  }),
22
23
  query("counts", {
23
24
  payload: { queue: Schema.String },
25
+ readPolicy: () => Effect.succeed(true),
24
26
  reads: ({ queue }) => [jobsKeys.counts.item(queue)],
25
27
  rejections: JobsFailures,
26
28
  success: QueueCounts,
27
29
  }),
28
30
  query("failed", {
29
31
  payload: { ...PageFields, queue: Schema.String },
32
+ readPolicy: () => Effect.succeed(true),
30
33
  reads: ({ queue }) => [jobsKeys.failed.item(queue)],
31
34
  rejections: JobsFailures,
32
35
  success: jobPage(Job),
33
36
  }),
34
37
  query("deadLettered", {
35
38
  payload: { ...PageFields, queue: Schema.String },
39
+ readPolicy: () => Effect.succeed(true),
36
40
  reads: ({ queue }) => [jobsKeys.deadLettered.item(queue)],
37
41
  rejections: JobsFailures,
38
42
  success: jobPage(DeadLettered),
39
43
  }),
40
44
  query("job", {
41
45
  payload: { id: Schema.String, queue: Schema.String },
46
+ readPolicy: () => Effect.succeed(true),
42
47
  reads: ({ queue, id }) => [jobsKeys.job(queue).item(id)],
43
48
  rejections: JobsFailures,
44
49
  success: Schema.OptionFromNullOr(Job),
@@ -61,17 +61,18 @@ export const JobFields = {
61
61
  export const PageFields = { cursor: Schema.optionalKey(Schema.String), limit: Schema.Number };
62
62
 
63
63
  export function jobSchema<TPayload extends Schema.Top>(queue: { readonly name: string; readonly schema: TPayload }) {
64
- return Schema.Struct({ ...JobFields, data: queue.schema.annotate({ sensitive: true }), queue: Schema.Literal(queue.name) });
64
+ return Schema.Struct({ ...JobFields, data: Schema.Unknown.annotate({ sensitive: true }), queue: Schema.Literal(queue.name) });
65
65
  }
66
66
 
67
67
  export function deadLetteredSchema<TPayload extends Schema.Top>(queue: { readonly name: string; readonly schema: TPayload }) {
68
68
  const name: string = `${queue.name}-dlq`;
69
69
  return Schema.Struct({
70
70
  ...JobFields,
71
- data: queue.schema.annotate({ sensitive: true }),
71
+ data: Schema.Unknown.annotate({ sensitive: true }),
72
72
  queue: Schema.Literal(name),
73
73
  sourceId: Schema.OptionFromNullOr(Schema.String),
74
74
  sourceOutput: Schema.NullOr(JobFailure).annotate({ sensitive: true }),
75
+ sourceQueue: Schema.Literal(queue.name),
75
76
  });
76
77
  }
77
78
 
@@ -0,0 +1,10 @@
1
+ import { SessionsPermission } from "@shivaedev/identity/sessionPermissions.ts";
2
+ import { Sessions } from "@shivaedev/identity/sessions.ts";
3
+ import { adminPage } from "#page.ts";
4
+
5
+ export const SessionsPage = adminPage({
6
+ contract: Sessions,
7
+ id: "sessions",
8
+ permission: SessionsPermission,
9
+ route: { path: "/admin/sessions", title: "Sessions" },
10
+ });
package/src/users/page.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Schema } from "effect";
1
+ import { Effect, Schema } from "effect";
2
2
  import type { AccessPolicy } from "@shivaedev/access/roleSet.ts";
3
3
  import { LastRoleAssigner } from "@shivaedev/access/roles.ts";
4
4
  import { collection } from "@shivaedev/changes/keys.ts";
@@ -25,6 +25,7 @@ export function usersPage<const TRole extends string>(access: Pick<AccessPolicy<
25
25
  queries: [
26
26
  query("list", {
27
27
  payload: UserListInput.fields,
28
+ readPolicy: () => Effect.succeed(true),
28
29
  reads: () => [users.list],
29
30
  rejections: { AuthUnavailable, Forbidden },
30
31
  success: UsersList,