// Staff permission editor (plan step 2.6e, APPS_PLATFORM D-7): every permission of the // licensed modules, grouped by module, from GET /api/waiters/permission-catalog. // - Saved as the whole key set: PUT /api/waiters/{id}/permissions. Keys of modules that // aren't licensed aren't shown and are kept as they are. // - You can only give permissions you hold yourself (backend: access.cannot_grant). Those // switches are disabled with the reason; removing is always allowed. // - "Reset to role defaults" fills the draft with the role's template (minus what you // can't give, which it names); nothing is saved until Save. // - A superadmin always has everything: read-only. import { useEffect, useMemo, useState } from 'react' import toast from 'react-hot-toast' import { useQueryClient } from '@tanstack/react-query' import { errorMessage, parseError } from '@xenia/api' import { useAccess } from '@xenia/access' import { useTranslation } from '@xenia/i18n' import { Button, InfoTip, Sheet, Spinner, Switch } from '@xenia/ui' import client from '../../api/client' import { canGive, diff, editorGroups, permLabelKey, resetToRole } from './permissionsLogic' export default function PermissionEditor({ member, onClose }) { const { t } = useTranslation('manager') const access = useAccess() const qc = useQueryClient() const actorIsSuperadmin = access.user?.role === 'superadmin' const [data, setData] = useState(null) // { catalog, modules, target, grantable } const [draft, setDraft] = useState(null) const [error, setError] = useState('') const [note, setNote] = useState('') const [busy, setBusy] = useState(false) useEffect(() => { let alive = true Promise.all([ client.get('/api/waiters/permission-catalog'), client.get('/api/platform/modules'), client.get(`/api/waiters/${member.id}/permissions`), actorIsSuperadmin ? null : client.get(`/api/waiters/${access.user.id}/permissions`), ]).then(([c, m, p, mine]) => { if (!alive) return setData({ catalog: c.data.modules, modules: m.data.modules, target: p.data, original: new Set(p.data.keys), grantable: mine ? new Set(mine.data.keys) : null, }) setDraft(new Set(p.data.keys)) }).catch(err => alive && setError(errorMessage(err, t))) return () => { alive = false } }, [member.id, actorIsSuperadmin, access.user.id, t]) const groups = useMemo(() => (data ? editorGroups(data.catalog, data.modules, { actorIsSuperadmin }) : []), [data, actorIsSuperadmin]) const name = member.full_name || member.username const label = key => t(permLabelKey(key)) const changes = data && draft ? diff(draft, data.original) : { added: [], removed: [] } const changed = changes.added.length + changes.removed.length const readOnly = data?.target.superadmin function toggle(key, on) { setNote('') setError('') setDraft(prev => { const next = new Set(prev) if (on) next.add(key) else next.delete(key) return next }) } function reset() { const { keys, skipped } = resetToRole(data.target.role_template, data.original, data.grantable) setDraft(keys) setError('') setNote(skipped.length ? t('permissions.resetSkipped', { list: skipped.map(label).join(', ') }) : t('permissions.resetDone')) } async function save() { setBusy(true) setError('') try { await client.put(`/api/waiters/${member.id}/permissions`, { keys: [...draft].sort() }) toast.success(t('permissions.saved', { name })) qc.invalidateQueries({ queryKey: ['staff'] }) if (member.id === access.user.id) access.refresh() onClose() } catch (err) { const x = parseError(err) setError(x.code === 'access.cannot_grant' ? t('permissions.cannotGrant', { label: label(x.params.key) }) : errorMessage(err, t)) } finally { setBusy(false) } } const footer = readOnly || !data ? null : ( <> ) return ( {!data && !error &&
} {!data && error &&

{error}

} {readOnly &&

{t('permissions.superadmin')}

} {data && !readOnly && (
{note &&

{note}

} {error &&

{error}

} {groups.map(g => (

{t(`modules:${g.key}.name`, { defaultValue: g.key })} {!g.on && {t('permissions.moduleOff')}}

{g.permissions.map(p => { const on = draft.has(p.key) const allowed = on || canGive(p.key, data.original, data.grantable) const fromRole = data.target.role_template.includes(p.key) const hints = [] if (!allowed) hints.push(t('permissions.notYours')) if (on !== fromRole) hints.push(fromRole ? t('permissions.roleHas') : t('permissions.roleHasNot')) return (
toggle(p.key, v)} label={label(p.key)} description={hints.join(' ยท ') || undefined} /> {p.overridable && {t('permissions.overridable')}}
) })}
))}
)}
) }