GroupEditPage.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import { useState, useMemo } from 'react';
  2. import { useParams, useNavigate } from 'react-router-dom';
  3. import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
  4. import { useTranslation } from 'react-i18next';
  5. import { ArrowLeft, Save, Loader2, Search, Check, Minus, Shield, AlertTriangle } from 'lucide-react';
  6. import { api } from '../api/client';
  7. import type { Permission, PermissionCategory } from '../api/client';
  8. import { Button } from '../components/Button';
  9. import { Card } from '../components/Card';
  10. import { useToast } from '../contexts/ToastContext';
  11. export function GroupEditPage() {
  12. const { id } = useParams<{ id: string }>();
  13. const navigate = useNavigate();
  14. const queryClient = useQueryClient();
  15. const { t } = useTranslation();
  16. const { showToast } = useToast();
  17. const isEditing = Boolean(id);
  18. const [name, setName] = useState('');
  19. const [description, setDescription] = useState('');
  20. const [permissions, setPermissions] = useState<Permission[]>([]);
  21. const [search, setSearch] = useState('');
  22. const [initialized, setInitialized] = useState(false);
  23. const { data: groupData, isLoading: groupLoading } = useQuery({
  24. queryKey: ['group', id],
  25. queryFn: () => api.getGroup(Number(id)),
  26. enabled: isEditing,
  27. });
  28. const { data: permissionsData, isLoading: permissionsLoading } = useQuery({
  29. queryKey: ['permissions'],
  30. queryFn: () => api.getPermissions(),
  31. });
  32. // Initialize form from fetched group data (once)
  33. if (isEditing && groupData && !initialized) {
  34. setName(groupData.name);
  35. setDescription(groupData.description || '');
  36. setPermissions(groupData.permissions);
  37. setInitialized(true);
  38. }
  39. const createMutation = useMutation({
  40. mutationFn: (data: { name: string; description?: string; permissions: Permission[] }) =>
  41. api.createGroup(data),
  42. onSuccess: () => {
  43. queryClient.invalidateQueries({ queryKey: ['groups'] });
  44. queryClient.invalidateQueries({ queryKey: ['group'] });
  45. showToast(t('groups.toast.created'));
  46. navigate('/settings?tab=users');
  47. },
  48. onError: (error: Error) => {
  49. showToast(error.message, 'error');
  50. },
  51. });
  52. const updateMutation = useMutation({
  53. mutationFn: (data: { name?: string; description?: string; permissions: Permission[] }) =>
  54. api.updateGroup(Number(id), data),
  55. onSuccess: (updatedGroup) => {
  56. queryClient.invalidateQueries({ queryKey: ['groups'] });
  57. // Prime the single-group detail cache with the PATCH response body so
  58. // reopening the editor within the 60s default staleTime shows the
  59. // newly-saved permissions instead of the stale pre-update snapshot
  60. // (#1083). setQueryData alone is enough — we intentionally do NOT also
  61. // invalidate ['group', id] because that would trigger an immediate
  62. // background refetch that could race with / overwrite this primed value
  63. // in test environments where the GET handler is a static mock; in
  64. // production the server's GET would match this payload anyway.
  65. if (updatedGroup) {
  66. queryClient.setQueryData(['group', id], updatedGroup);
  67. }
  68. showToast(t('groups.toast.updated'));
  69. navigate('/settings?tab=users');
  70. },
  71. onError: (error: Error) => {
  72. showToast(error.message, 'error');
  73. },
  74. });
  75. const isSaving = createMutation.isPending || updateMutation.isPending;
  76. const handleSave = () => {
  77. if (!name.trim()) {
  78. showToast(t('groups.toast.enterGroupName'), 'error');
  79. return;
  80. }
  81. if (isEditing) {
  82. updateMutation.mutate({
  83. name: name !== groupData?.name ? name : undefined,
  84. description,
  85. permissions,
  86. });
  87. } else {
  88. createMutation.mutate({
  89. name,
  90. description: description || undefined,
  91. permissions,
  92. });
  93. }
  94. };
  95. const togglePermission = (perm: Permission) => {
  96. setPermissions((prev) =>
  97. prev.includes(perm) ? prev.filter((p) => p !== perm) : [...prev, perm]
  98. );
  99. };
  100. const toggleCategoryPermissions = (category: PermissionCategory, checked: boolean) => {
  101. const categoryPerms = category.permissions.map((p) => p.value);
  102. setPermissions((prev) => {
  103. const otherPerms = prev.filter((p) => !categoryPerms.includes(p));
  104. return checked ? [...otherPerms, ...categoryPerms] : otherPerms;
  105. });
  106. };
  107. const isCategoryFullySelected = (category: PermissionCategory) =>
  108. category.permissions.every((p) => permissions.includes(p.value));
  109. const isCategoryPartiallySelected = (category: PermissionCategory) => {
  110. const count = category.permissions.filter((p) => permissions.includes(p.value)).length;
  111. return count > 0 && count < category.permissions.length;
  112. };
  113. const selectAll = () => {
  114. if (permissionsData) {
  115. setPermissions(permissionsData.all_permissions);
  116. }
  117. };
  118. const clearAll = () => {
  119. setPermissions([]);
  120. };
  121. const searchLower = search.toLowerCase();
  122. const filteredCategories = useMemo(() => {
  123. if (!permissionsData) return [];
  124. if (!searchLower) return permissionsData.categories;
  125. return permissionsData.categories
  126. .map((cat) => ({
  127. ...cat,
  128. permissions: cat.permissions.filter((p) =>
  129. p.label.toLowerCase().includes(searchLower)
  130. ),
  131. }))
  132. .filter((cat) => cat.permissions.length > 0);
  133. }, [permissionsData, searchLower]);
  134. const totalPermissions = permissionsData?.all_permissions.length ?? 0;
  135. if (groupLoading || permissionsLoading) {
  136. return (
  137. <div className="flex items-center justify-center py-16">
  138. <Loader2 className="w-8 h-8 text-bambu-green animate-spin" />
  139. </div>
  140. );
  141. }
  142. return (
  143. <div className="space-y-6 max-w-5xl mx-auto">
  144. {/* Header */}
  145. <div className="flex items-center gap-3">
  146. <button
  147. onClick={() => navigate('/settings?tab=users')}
  148. className="p-2 rounded-lg hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white transition-colors"
  149. >
  150. <ArrowLeft className="w-5 h-5" />
  151. </button>
  152. <h1 className="text-xl font-bold text-white">
  153. {isEditing ? t('groups.editor.title') : t('groups.editor.createTitle')}
  154. </h1>
  155. </div>
  156. {/* System group warning */}
  157. {isEditing && groupData?.is_system && (
  158. <div className="flex items-center gap-3 px-4 py-3 rounded-lg bg-yellow-500/10 border border-yellow-500/20 text-yellow-400 text-sm">
  159. <AlertTriangle className="w-4 h-4 shrink-0" />
  160. {t('groups.form.systemGroupWarning')}
  161. </div>
  162. )}
  163. {/* Name + Description */}
  164. <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
  165. <div>
  166. <label className="block text-sm font-medium text-white mb-2">{t('groups.form.groupName')}</label>
  167. <input
  168. type="text"
  169. value={name}
  170. onChange={(e) => setName(e.target.value)}
  171. disabled={isEditing && groupData?.is_system}
  172. className="w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors disabled:opacity-50"
  173. placeholder={t('groups.form.groupNamePlaceholder')}
  174. />
  175. </div>
  176. <div>
  177. <label className="block text-sm font-medium text-white mb-2">{t('groups.form.description')}</label>
  178. <input
  179. type="text"
  180. value={description}
  181. onChange={(e) => setDescription(e.target.value)}
  182. className="w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors"
  183. placeholder={t('groups.form.descriptionPlaceholder')}
  184. />
  185. </div>
  186. </div>
  187. {/* Toolbar */}
  188. <div className="flex items-center justify-between flex-wrap gap-3">
  189. <div className="flex items-center gap-3">
  190. <span className="text-sm text-bambu-gray">
  191. {t('groups.editor.permissionsSelected', { count: permissions.length })} / {totalPermissions}
  192. </span>
  193. <Button size="sm" variant="ghost" onClick={selectAll}>
  194. {t('groups.editor.selectAll')}
  195. </Button>
  196. <Button size="sm" variant="ghost" onClick={clearAll}>
  197. {t('groups.editor.clearAll')}
  198. </Button>
  199. </div>
  200. <div className="relative">
  201. <Search className="w-4 h-4 absolute left-3 top-1/2 -translate-y-1/2 text-bambu-gray" />
  202. <input
  203. type="text"
  204. value={search}
  205. onChange={(e) => setSearch(e.target.value)}
  206. placeholder={t('groups.editor.search')}
  207. className="pl-9 pr-4 py-2 text-sm bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors w-64"
  208. />
  209. </div>
  210. </div>
  211. {/* Permission grid */}
  212. {filteredCategories.length === 0 ? (
  213. <div className="text-center py-12 text-bambu-gray">
  214. {t('groups.editor.noResults')}
  215. </div>
  216. ) : (
  217. <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
  218. {filteredCategories.map((category) => {
  219. // Use the full (unfiltered) category for selection logic
  220. const fullCategory = permissionsData!.categories.find((c) => c.name === category.name)!;
  221. const selectedCount = fullCategory.permissions.filter((p) => permissions.includes(p.value)).length;
  222. const totalCount = fullCategory.permissions.length;
  223. const fullySelected = isCategoryFullySelected(fullCategory);
  224. const partiallySelected = isCategoryPartiallySelected(fullCategory);
  225. return (
  226. <Card key={category.name}>
  227. <div className="sticky top-0 z-10 flex items-center justify-between px-4 py-3 bg-bambu-dark-secondary border-b border-bambu-dark-tertiary rounded-t-xl">
  228. <div className="flex items-center gap-3">
  229. <button
  230. type="button"
  231. onClick={() => toggleCategoryPermissions(fullCategory, !fullySelected)}
  232. className={`w-5 h-5 rounded border flex items-center justify-center transition-colors shrink-0 ${
  233. fullySelected
  234. ? 'bg-bambu-green border-bambu-green'
  235. : partiallySelected
  236. ? 'bg-bambu-green/50 border-bambu-green'
  237. : 'border-bambu-gray hover:border-white'
  238. }`}
  239. >
  240. {fullySelected && <Check className="w-3 h-3 text-white" />}
  241. {partiallySelected && !fullySelected && <Minus className="w-3 h-3 text-white" />}
  242. </button>
  243. <Shield className="w-4 h-4 text-bambu-gray shrink-0" />
  244. <span className="text-white font-medium text-sm">{category.name}</span>
  245. </div>
  246. <span className="text-xs text-bambu-gray tabular-nums">
  247. {selectedCount}/{totalCount}
  248. </span>
  249. </div>
  250. <div className="p-3 space-y-1">
  251. {category.permissions.map((perm) => (
  252. <label
  253. key={perm.value}
  254. className="flex items-center gap-3 px-2 py-1.5 rounded hover:bg-bambu-dark-secondary cursor-pointer"
  255. >
  256. <input
  257. type="checkbox"
  258. checked={permissions.includes(perm.value)}
  259. onChange={() => togglePermission(perm.value)}
  260. className="w-4 h-4 rounded border-bambu-gray text-bambu-green focus:ring-bambu-green focus:ring-offset-0 bg-bambu-dark-secondary"
  261. />
  262. <span className="text-sm text-bambu-gray">{perm.label}</span>
  263. </label>
  264. ))}
  265. </div>
  266. </Card>
  267. );
  268. })}
  269. </div>
  270. )}
  271. {/* Spacer for fixed bottom bar */}
  272. <div className="h-16" />
  273. {/* Fixed bottom bar */}
  274. <div className="fixed bottom-0 left-0 right-0 z-20 px-6 py-3 bg-bambu-dark-secondary border-t border-bambu-dark-tertiary flex items-center justify-center gap-3">
  275. <Button variant="secondary" onClick={() => navigate('/settings?tab=users')}>
  276. {t('common.cancel')}
  277. </Button>
  278. <Button onClick={handleSave} disabled={isSaving || !name.trim()}>
  279. {isSaving ? (
  280. <>
  281. <Loader2 className="w-4 h-4 animate-spin" />
  282. {t('common.saving')}
  283. </>
  284. ) : (
  285. <>
  286. <Save className="w-4 h-4" />
  287. {t('common.save')}
  288. </>
  289. )}
  290. </Button>
  291. </div>
  292. </div>
  293. );
  294. }