-
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
// SPDX-FileCopyrightText: 2025 Shota FUJI <pockawoooh@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-only
import "../../../../polyfill.ts";
import { type MessageInitShape } from "@bufbuild/protobuf";
import { createClient } from "@connectrpc/connect";
import { Button, Box, Container } from "@radix-ui/themes";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { type UpdateUserRequestSchema } from "@yamori/proto/yamori/workspace/v2/update_user_request_pb.js";
import { type User } from "@yamori/proto/yamori/workspace/v2/user_pb.js";
import { WorkspaceService } from "@yamori/proto/yamori/workspace/v2/workspace_service_pb.js";
import { type FC, use } from "react";
import * as Empty from "../../../../components/Empty.ts";
import { ManagedErrorCallout } from "../../../../components/ErrorCallout.ts";
import { UpdateForm } from "../../../../components/UserEditForm.tsx";
import { useConnectTransport } from "../../../../contexts/ConnectTransport.tsx";
import { NavigationContext } from "../../../../contexts/Router.tsx";
import { useToast } from "../../../../contexts/Toast.tsx";
import { IllegalMessageError } from "../../../../errors/IllegalMessageError.ts";
import { UserInputError } from "../../../../errors/UserInputError.ts";
import { LoggedInLayout } from "../../../LoggedInLayout.tsx";
import { userFetchQuery } from "../page.tsx";
import * as details from "../Details.tsx";
export const Title: FC = () => "ユーザ編集";
export function hasAccess(loginUser: User, user: User): boolean {
return !!(loginUser.id?.value === user.id?.value
? loginUser.permissions?.canUpdateSelfProfile
: user.isAdmin
? loginUser.isAdmin
: loginUser.permissions?.canUpdateOtherRegularUserProfile);
}
interface NoAccessProps {
user: User;
}
const NoAccess: FC<NoAccessProps> = ({ user }) => {
return (
<Empty.Root>
<Empty.Title>編集権限がありません</Empty.Title>
<Empty.Description>
「{user.displayName}」に対する編集権限がないためページを表示できません。
</Empty.Description>
<Empty.Actions>
<Button asChild>
<a href={details.createHref({ user })}>ユーザ詳細画面へ</a>
</Button>
</Empty.Actions>
</Empty.Root>
);
};
interface BodyProps {
loginUser: User;
user: User;
onUpdated?(): void;
}
const Body: FC<BodyProps> = ({ user, loginUser, onUpdated }) => {
const toast = useToast();
const transport = useConnectTransport();
const client = createClient(WorkspaceService, transport);
const update = useMutation({
async mutationFn(req: MessageInitShape<typeof UpdateUserRequestSchema>) {
const resp = await client.updateUser(req);
if (resp.result.case === "ok") {
return resp.result.value;
}
if (resp.result.case === "duplicatedName") {
throw new UserInputError("ユーザ名が同一の別ユーザが既に存在します");
}
if (typeof resp.result.case === "string") {
throw resp.result.value;
}
throw new IllegalMessageError(resp);
},
onSuccess(user) {
onUpdated?.();
toast.open({
severity: "success",
title: `ユーザ「${user.displayName}」を更新しました`,
dismissible: true,
type: "foreground",
});
},
});
return (
<Box mt="3">
{update.error ? (
<Box position="sticky" top="0" pt="2" mb="2" style={{ zIndex: 10 }}>
<ManagedErrorCallout
style={{ backdropFilter: "blur(2em)" }}
role="alert"
error={update.error}
actions={
<Button
size="1"
variant="outline"
type="button"
onClick={() => void update.reset()}
>
閉じる
</Button>
}
title="更新に失敗しました"
/>
</Box>
) : (
<Box mb="2" />
)}
<UpdateForm
pending={update.isPending}
loginUser={loginUser}
user={user}
onUpdate={({ name, displayName, isAdmin, ...permissions }) => {
update.mutate({ id: user.id, name, displayName, isAdmin, permissions });
}}
/>
</Box>
);
};
export interface PageProps {
loginUser: User;
user: User;
}
export const Page: FC<PageProps> = ({ loginUser, user }) => {
const navigation = use(NavigationContext);
const client = useQueryClient();
return (
<LoggedInLayout title={<Title />} user={loginUser}>
<Container p="2" size="2">
{hasAccess(loginUser, user) ? (
<Body
loginUser={loginUser}
user={user}
onUpdated={() => {
navigation.push(details.createHref({ user }));
client.refetchQueries({ queryKey: userFetchQuery(user.id?.value ?? "") });
}}
/>
) : (
<NoAccess user={user} />
)}
</Container>
</LoggedInLayout>
);
};
export interface CreateHrefInput {
user: User;
}
export function createHref({ user }: CreateHrefInput): string {
if (!user.id) {
return "/users";
}
return `/users/${user.id.value}/edit`;
}
export const pattern = new URLPattern({
pathname: "/users/:user/edit",
});