feat(frontend): add usePermission hook, PermissionButton, PermissionRoute, and 403 handling

This commit is contained in:
2026-07-02 11:59:36 +08:00
parent b047dbc5d8
commit 14d81045e6
5 changed files with 68 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
import { useMemo } from 'react';
export function usePermission() {
const permissions: string[] = useMemo(() => {
try {
return JSON.parse(localStorage.getItem('permissions') || '[]');
} catch {
return [];
}
}, []);
const hasPermission = (code: string): boolean => permissions.includes(code);
const hasAnyPermission = (...codes: string[]): boolean =>
codes.some(c => permissions.includes(c));
const hasAllPermissions = (...codes: string[]): boolean =>
codes.every(c => permissions.includes(c));
return { permissions, hasPermission, hasAnyPermission, hasAllPermissions };
}