沉淀数字员工角色偏好状态
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { createContext, useContext, useState, useCallback, type ReactNode } from 'react';
|
||||
import { createContext, useContext, useState, useCallback, useEffect, type ReactNode } from 'react';
|
||||
import { readDigitalEmployeeUiState, saveDigitalEmployeeUiStatePatch } from '@/lib/digitalUiStateBridge';
|
||||
|
||||
export type ConsoleRole = 'finance' | 'engineering' | 'executive' | 'hr' | 'sales';
|
||||
export type MotionLevel = 'none' | 'minimal';
|
||||
@@ -136,12 +137,32 @@ function getStoredRole(): ConsoleRole {
|
||||
export function RoleProvider({ children }: { children: ReactNode }) {
|
||||
const [role, setRoleState] = useState<ConsoleRole>(getStoredRole);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
void readDigitalEmployeeUiState().then((state) => {
|
||||
if (cancelled) return;
|
||||
const storedRole = state?.consoleRole;
|
||||
if (storedRole && storedRole in ROLE_PROFILES) {
|
||||
setRoleState(storedRole as ConsoleRole);
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, storedRole);
|
||||
localStorage.removeItem(LEGACY_STORAGE_KEY);
|
||||
} catch { /* noop */ }
|
||||
}
|
||||
});
|
||||
return () => { cancelled = true; };
|
||||
}, []);
|
||||
|
||||
const setRole = useCallback((newRole: ConsoleRole) => {
|
||||
setRoleState(newRole);
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, newRole);
|
||||
localStorage.removeItem(LEGACY_STORAGE_KEY);
|
||||
} catch { /* noop */ }
|
||||
void saveDigitalEmployeeUiStatePatch(
|
||||
{ consoleRole: newRole },
|
||||
{ action: 'update_console_role', metadata: { role: newRole } },
|
||||
);
|
||||
}, []);
|
||||
|
||||
const profile = ROLE_PROFILES[role];
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
export interface DigitalEmployeeProfileState {
|
||||
name: string;
|
||||
company: string;
|
||||
position: string;
|
||||
currentStatus: string;
|
||||
}
|
||||
|
||||
export interface DigitalEmployeeUiState {
|
||||
selectedDutyIdsByDate: Record<string, string[]>;
|
||||
confirmedDates: Record<string, string>;
|
||||
reportScheduleTime: string;
|
||||
reportGeneratedAtByDate: Record<string, string>;
|
||||
decisionResultsByDate: Record<string, Record<string, string>>;
|
||||
profile: DigitalEmployeeProfileState;
|
||||
consoleRole?: string;
|
||||
rolePreferences?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
interface DigitalEmployeeBridgeWindow {
|
||||
QimingClawBridge?: {
|
||||
digital?: {
|
||||
getUiState?: () => Promise<DigitalEmployeeUiState>;
|
||||
saveUiState?: (update: {
|
||||
state: DigitalEmployeeUiState;
|
||||
action?: string;
|
||||
date?: string;
|
||||
metadata?: Record<string, unknown>;
|
||||
}) => Promise<DigitalEmployeeUiState>;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
export function defaultDigitalEmployeeUiState(): DigitalEmployeeUiState {
|
||||
return {
|
||||
selectedDutyIdsByDate: {},
|
||||
confirmedDates: {},
|
||||
reportScheduleTime: '18:00',
|
||||
reportGeneratedAtByDate: {},
|
||||
decisionResultsByDate: {},
|
||||
profile: {
|
||||
name: '飞天数字员工',
|
||||
company: 'qimingclaw 客户端',
|
||||
position: '客户端任务执行员',
|
||||
currentStatus: 'working',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export async function readDigitalEmployeeUiState(): Promise<DigitalEmployeeUiState | null> {
|
||||
try {
|
||||
return await (window as DigitalEmployeeBridgeWindow).QimingClawBridge?.digital?.getUiState?.() ?? null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveDigitalEmployeeUiStatePatch(
|
||||
patch: Partial<DigitalEmployeeUiState>,
|
||||
options: { action?: string; date?: string; metadata?: Record<string, unknown> } = {},
|
||||
): Promise<DigitalEmployeeUiState | null> {
|
||||
const bridge = (window as DigitalEmployeeBridgeWindow).QimingClawBridge?.digital;
|
||||
if (!bridge?.saveUiState) return null;
|
||||
try {
|
||||
const current = await bridge.getUiState?.().catch(() => null) ?? null;
|
||||
const state = {
|
||||
...defaultDigitalEmployeeUiState(),
|
||||
...(current ?? {}),
|
||||
...patch,
|
||||
};
|
||||
return await bridge.saveUiState({ state, ...options });
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -54,6 +54,8 @@ interface AdapterState {
|
||||
reportScheduleTime: string;
|
||||
reportGeneratedAtByDate: Record<string, string>;
|
||||
decisionResultsByDate: Record<string, Record<string, string>>;
|
||||
consoleRole?: string;
|
||||
rolePreferences?: Record<string, unknown>;
|
||||
profile: {
|
||||
name: string;
|
||||
company: string;
|
||||
@@ -484,6 +486,8 @@ function defaultState(): AdapterState {
|
||||
reportScheduleTime: DEFAULT_REPORT_TIME,
|
||||
reportGeneratedAtByDate: {},
|
||||
decisionResultsByDate: {},
|
||||
consoleRole: 'sales',
|
||||
rolePreferences: {},
|
||||
profile: {
|
||||
name: '飞天数字员工',
|
||||
company: 'qimingclaw 客户端',
|
||||
|
||||
@@ -2,6 +2,7 @@ import { useState, useEffect, useRef, type CSSProperties } from 'react';
|
||||
import GlassCard from '@/components/digital/GlassCard';
|
||||
import CardTitleBar from '@/components/digital/CardTitleBar';
|
||||
import { useRole, ROLE_PROFILES, type ConsoleRole } from '@/contexts/RoleContext';
|
||||
import { readDigitalEmployeeUiState, saveDigitalEmployeeUiStatePatch } from '@/lib/digitalUiStateBridge';
|
||||
import { t } from '@/lib/i18n';
|
||||
|
||||
const ROLES: ConsoleRole[] = ['sales', 'engineering', 'hr', 'finance', 'executive'];
|
||||
@@ -67,6 +68,11 @@ function loadPreferences(): RolePreferences {
|
||||
}
|
||||
}
|
||||
|
||||
function normalizePreferences(value: unknown): RolePreferences {
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) return defaultPreferences;
|
||||
return { ...defaultPreferences, ...value as Partial<RolePreferences> };
|
||||
}
|
||||
|
||||
export default function OpsSettings() {
|
||||
const { role, profile, setRole } = useRole();
|
||||
const [preferences, setPreferences] = useState<RolePreferences>(loadPreferences);
|
||||
@@ -77,6 +83,20 @@ export default function OpsSettings() {
|
||||
return () => { if (savedTimerRef.current) clearTimeout(savedTimerRef.current); };
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
void readDigitalEmployeeUiState().then((state) => {
|
||||
if (cancelled || !state?.rolePreferences) return;
|
||||
const next = normalizePreferences(state.rolePreferences);
|
||||
setPreferences(next);
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(next));
|
||||
localStorage.removeItem(LEGACY_STORAGE_KEY);
|
||||
} catch { /* localStorage blocked */ }
|
||||
});
|
||||
return () => { cancelled = true; };
|
||||
}, []);
|
||||
|
||||
const markSaved = () => {
|
||||
setSaved(true);
|
||||
if (savedTimerRef.current) clearTimeout(savedTimerRef.current);
|
||||
@@ -89,6 +109,10 @@ export default function OpsSettings() {
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(next));
|
||||
localStorage.removeItem(LEGACY_STORAGE_KEY);
|
||||
} catch { /* localStorage blocked */ }
|
||||
void saveDigitalEmployeeUiStatePatch(
|
||||
{ rolePreferences: next as unknown as Record<string, unknown> },
|
||||
{ action: 'update_role_preferences', metadata: { preferences: next } },
|
||||
);
|
||||
markSaved();
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user