|
|
@@ -3,6 +3,7 @@ import React, { useEffect, useState } from 'react';
|
|
|
import type { UseFormRegisterReturn } from 'react-hook-form';
|
|
|
|
|
|
import { parseScopes } from '~/client/util/scope-util';
|
|
|
+import { useIsAdmin } from '~/stores-universal/context';
|
|
|
|
|
|
import type { Scope } from '../../../interfaces/scope';
|
|
|
import { SCOPE } from '../../../interfaces/scope';
|
|
|
@@ -15,15 +16,16 @@ import { AccessTokenScopeList } from './AccessTokenScopeList';
|
|
|
type AccessTokenScopeSelectProps = {
|
|
|
/** React Hook Form's register function for a field named "scopes" */
|
|
|
register: UseFormRegisterReturn<'scopes'>;
|
|
|
- watch: Scope[];
|
|
|
+ watch: string[];
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* Displays a list of permissions in a recursive, nested checkbox interface.
|
|
|
*/
|
|
|
export const AccessTokenScopeSelect: React.FC<AccessTokenScopeSelectProps> = ({ register, watch }) => {
|
|
|
- const [disabledScopes, setDisabledScopes] = useState<Set<string>>(new Set());
|
|
|
- const ScopesMap = parseScopes({ scopes: SCOPE, isAdmin: false });
|
|
|
+ const [disabledScopes, setDisabledScopes] = useState<Set<Scope>>(new Set());
|
|
|
+ const { data: isAdmin } = useIsAdmin();
|
|
|
+ const ScopesMap = parseScopes({ scopes: SCOPE, isAdmin });
|
|
|
|
|
|
const extractScopes = (obj: Record<string, any>): string[] => {
|
|
|
let result: string[] = [];
|
|
|
@@ -45,7 +47,7 @@ export const AccessTokenScopeSelect: React.FC<AccessTokenScopeSelectProps> = ({
|
|
|
const selectedScopes = watch || [];
|
|
|
|
|
|
// Create a set of scopes to disable based on prefixes
|
|
|
- const disabledSet = new Set<string>();
|
|
|
+ const disabledSet = new Set<Scope>();
|
|
|
|
|
|
selectedScopes.forEach((scope) => {
|
|
|
// Check if the scope is in the form `xxx:*`
|
|
|
@@ -54,7 +56,7 @@ export const AccessTokenScopeSelect: React.FC<AccessTokenScopeSelectProps> = ({
|
|
|
const prefix = scope.replace(':*', ':');
|
|
|
|
|
|
// Disable all scopes that start with the prefix (but are not the selected scope itself)
|
|
|
- Scopes.forEach((s) => {
|
|
|
+ Scopes.forEach((s:Scope) => {
|
|
|
if (s.startsWith(prefix) && s !== scope) {
|
|
|
disabledSet.add(s);
|
|
|
}
|
|
|
@@ -63,7 +65,7 @@ export const AccessTokenScopeSelect: React.FC<AccessTokenScopeSelectProps> = ({
|
|
|
});
|
|
|
|
|
|
setDisabledScopes(disabledSet);
|
|
|
- }, [watch]);
|
|
|
+ }, [watch, Scopes]);
|
|
|
|
|
|
return (
|
|
|
<div className="border rounded">
|