Files
leocrm/frontend/src/store/pluginStore.ts
T
Agent Zero bdad91a649 feat(5.20): Custom Fields — plugin-defined fields in Contact UI
- Add CustomFieldDefinition to PluginManifest (text/number/date/select/multiselect/boolean)
- Add GET/PATCH /api/v1/contacts/{id}/custom-fields routes
- Merge plugin definitions with stored values in contacts.custom JSONB
- Add CustomFieldRenderer component (read + edit modes)
- Integrate into ContactDetail (read-only) and ContactEditModal (editable)
- Add custom_fields to pluginStore and active manifests API
- Add useCustomFields/useUpdateCustomFields React Query hooks
- Tests: test_custom_fields.py (9 tests) + CustomFieldRenderer.test.tsx (5 tests)
- i18n keys already present (contacts.customFields)
2026-07-23 23:35:35 +02:00

149 lines
3.6 KiB
TypeScript

import { create } from 'zustand';
export interface PluginMenuItem {
label_key: string;
label: string;
path: string;
icon: string;
group: string;
order: number;
badge_key: string;
}
export interface PluginPageRoute {
path: string;
component: string;
parent: string;
protected: boolean;
order: number;
}
export interface PluginDetailTab {
entity_type: string;
label_key: string;
label: string;
component: string;
icon: string;
order: number;
permission: string;
}
export interface PluginSettingsPage {
path: string;
label_key: string;
label: string;
component: string;
icon: string;
order: number;
permission: string;
}
export interface PluginDashboardWidget {
id: string;
label_key: string;
label: string;
component: string;
icon: string;
order: number;
col_span: number;
row_span: number;
permission: string;
}
export interface PluginCustomFieldDefinition {
name: string;
label: string;
label_key: string;
field_type: 'text' | 'number' | 'date' | 'select' | 'multiselect' | 'boolean';
options: string[];
default_value: any;
required: boolean;
entity: string;
}
export interface PluginUiManifest {
name: string;
display_name: string;
version: string;
is_core: boolean;
menu_items: PluginMenuItem[];
page_routes: PluginPageRoute[];
detail_tabs: PluginDetailTab[];
settings_pages: PluginSettingsPage[];
dashboard_widgets: PluginDashboardWidget[];
custom_fields: PluginCustomFieldDefinition[];
}
interface PluginState {
manifests: PluginUiManifest[];
loading: boolean;
error: string | null;
loaded: boolean;
setManifests: (manifests: PluginUiManifest[]) => void;
setLoading: (loading: boolean) => void;
setError: (error: string | null) => void;
reset: () => void;
// Computed selectors
getAllMenuItems: () => PluginMenuItem[];
getAllPageRoutes: () => PluginPageRoute[];
getDetailTabsForEntity: (entityType: string) => PluginDetailTab[];
getAllSettingsPages: () => PluginSettingsPage[];
getAllDashboardWidgets: () => PluginDashboardWidget[];
getCustomFieldsForEntity: (entityType: string) => PluginCustomFieldDefinition[];
}
export const usePluginStore = create<PluginState>((set, get) => ({
manifests: [],
loading: false,
error: null,
loaded: false,
setManifests: (manifests) => set({ manifests, loaded: true, loading: false, error: null }),
setLoading: (loading) => set({ loading }),
setError: (error) => set({ error, loading: false }),
reset: () => set({ manifests: [], loading: false, error: null, loaded: false }),
getAllMenuItems: () => {
const { manifests } = get();
return manifests
.flatMap((m) => m.menu_items)
.sort((a, b) => a.order - b.order);
},
getAllPageRoutes: () => {
const { manifests } = get();
return manifests
.flatMap((m) => m.page_routes)
.sort((a, b) => a.order - b.order);
},
getDetailTabsForEntity: (entityType: string) => {
const { manifests } = get();
return manifests
.flatMap((m) => m.detail_tabs)
.filter((t) => t.entity_type === entityType)
.sort((a, b) => a.order - b.order);
},
getAllSettingsPages: () => {
const { manifests } = get();
return manifests
.flatMap((m) => m.settings_pages)
.sort((a, b) => a.order - b.order);
},
getAllDashboardWidgets: () => {
const { manifests } = get();
return manifests
.flatMap((m) => m.dashboard_widgets)
.sort((a, b) => a.order - b.order);
},
getCustomFieldsForEntity: (entityType: string) => {
const { manifests } = get();
return manifests
.flatMap((m) => m.custom_fields || [])
.filter((cf) => cf.entity === entityType);
},
}));