39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
import React from 'react';
|
|
import { Navigate } from 'react-router-dom';
|
|
import { pb } from './pocketbase';
|
|
import { useSettingsStore } from '../store/settings';
|
|
|
|
export type UserRole = 'advisor' | 'technician' | 'mobile';
|
|
|
|
function roleFromModel(): UserRole | null {
|
|
const role = pb.authStore.model?.role as string | undefined;
|
|
if (role === 'advisor' || role === 'technician' || role === 'mobile') return role;
|
|
return null;
|
|
}
|
|
|
|
function roleFromSettings(): UserRole {
|
|
const { businessType } = useSettingsStore.getState().settings;
|
|
if (businessType === 'advisor' || businessType === 'shop') return 'advisor';
|
|
if (businessType === 'mobile' || businessType === 'home') return 'mobile';
|
|
return 'advisor';
|
|
}
|
|
|
|
export function getCurrentUserRole(): UserRole {
|
|
return roleFromModel() ?? roleFromSettings();
|
|
}
|
|
|
|
export function isOwnerRole(role: UserRole): boolean {
|
|
return role === 'advisor' || role === 'mobile';
|
|
}
|
|
|
|
export function isTechnicianRole(role: UserRole): boolean {
|
|
return role === 'technician';
|
|
}
|
|
|
|
export function RequireOwnerRole({ children }: { children: React.ReactNode }) {
|
|
if (isTechnicianRole(getCurrentUserRole())) {
|
|
return React.createElement(Navigate, { to: '/technician', replace: true });
|
|
}
|
|
return React.createElement(React.Fragment, null, children);
|
|
}
|