create-crm-tmp 1.1.3 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/create-crm-tmp.js +56 -35
- package/package.json +1 -1
- package/template/.prettierignore +2 -0
- package/template/README.md +230 -115
- package/template/components.json +22 -0
- package/template/eslint.config.mjs +13 -0
- package/template/exemple-contacts.csv +54 -0
- package/template/next.config.ts +41 -1
- package/template/package.json +63 -15
- package/template/prisma/migrations/20260318095700_init_db/migration.sql +978 -0
- package/template/prisma/schema.prisma +311 -67
- package/template/src/app/(auth)/invite/[token]/page.tsx +28 -29
- package/template/src/app/(auth)/layout.tsx +1 -1
- package/template/src/app/(auth)/reset-password/complete/page.tsx +21 -27
- package/template/src/app/(auth)/reset-password/page.tsx +14 -10
- package/template/src/app/(auth)/reset-password/verify/page.tsx +14 -10
- package/template/src/app/(auth)/signin/page.tsx +34 -23
- package/template/src/app/(dashboard)/agenda/page.tsx +3655 -2357
- package/template/src/app/(dashboard)/automatisation/[id]/page.tsx +10 -7
- package/template/src/app/(dashboard)/automatisation/_components/workflow-editor.tsx +609 -338
- package/template/src/app/(dashboard)/automatisation/new/page.tsx +11 -8
- package/template/src/app/(dashboard)/automatisation/page.tsx +463 -186
- package/template/src/app/(dashboard)/closing/page.tsx +517 -469
- package/template/src/app/(dashboard)/contacts/[id]/page.tsx +6151 -4210
- package/template/src/app/(dashboard)/contacts/companies/[id]/page.tsx +1702 -0
- package/template/src/app/(dashboard)/contacts/loading.tsx +13 -0
- package/template/src/app/(dashboard)/contacts/page.tsx +4124 -2130
- package/template/src/app/(dashboard)/dashboard/page.tsx +119 -105
- package/template/src/app/(dashboard)/dev/page.tsx +1291 -0
- package/template/src/app/(dashboard)/error.tsx +37 -0
- package/template/src/app/(dashboard)/layout.tsx +6 -2
- package/template/src/app/(dashboard)/loading.tsx +5 -0
- package/template/src/app/(dashboard)/settings/loading.tsx +19 -0
- package/template/src/app/(dashboard)/settings/page.tsx +1773 -3362
- package/template/src/app/(dashboard)/templates/page.tsx +504 -303
- package/template/src/app/(dashboard)/users/list/page.tsx +364 -355
- package/template/src/app/(dashboard)/users/page.tsx +279 -310
- package/template/src/app/(dashboard)/users/permissions/page.tsx +104 -99
- package/template/src/app/(dashboard)/users/roles/page.tsx +169 -140
- package/template/src/app/api/agenda/google-events/route.ts +92 -0
- package/template/src/app/api/audit-logs/route.ts +1 -1
- package/template/src/app/api/auth/check-active/route.ts +3 -2
- package/template/src/app/api/auth/google/callback/route.ts +8 -5
- package/template/src/app/api/auth/google/disconnect/route.ts +2 -2
- package/template/src/app/api/auth/google/route.ts +2 -1
- package/template/src/app/api/auth/google/status/route.ts +7 -31
- package/template/src/app/api/companies/[id]/activities/route.ts +129 -0
- package/template/src/app/api/companies/[id]/route.ts +194 -0
- package/template/src/app/api/companies/export/route.ts +206 -0
- package/template/src/app/api/companies/route.ts +196 -0
- package/template/src/app/api/contact-views/[id]/pin/route.ts +69 -0
- package/template/src/app/api/contact-views/[id]/route.ts +197 -0
- package/template/src/app/api/contact-views/route.ts +146 -0
- package/template/src/app/api/contacts/[id]/files/[fileId]/preview/route.ts +55 -0
- package/template/src/app/api/contacts/[id]/files/[fileId]/route.ts +20 -48
- package/template/src/app/api/contacts/[id]/files/route.ts +125 -186
- package/template/src/app/api/contacts/[id]/interactions/[interactionId]/route.ts +27 -1
- package/template/src/app/api/contacts/[id]/interactions/route.ts +45 -8
- package/template/src/app/api/contacts/[id]/kyc/route.ts +81 -0
- package/template/src/app/api/contacts/[id]/meet/route.ts +55 -29
- package/template/src/app/api/contacts/[id]/route.ts +184 -21
- package/template/src/app/api/contacts/[id]/send-email/route.ts +33 -11
- package/template/src/app/api/contacts/[id]/workflows/run/route.ts +67 -0
- package/template/src/app/api/contacts/export/route.ts +22 -31
- package/template/src/app/api/contacts/import/route.ts +77 -44
- package/template/src/app/api/contacts/import-preview/route.ts +139 -0
- package/template/src/app/api/contacts/origins/route.ts +63 -0
- package/template/src/app/api/contacts/route.ts +322 -57
- package/template/src/app/api/cron/cleanup-editor-images/route.ts +166 -0
- package/template/src/app/api/dashboard/stats/route.ts +9 -292
- package/template/src/app/api/dashboard/widgets/[id]/route.ts +0 -3
- package/template/src/app/api/dashboard/widgets/route.ts +19 -19
- package/template/src/app/api/dev/reminders/test/route.ts +114 -0
- package/template/src/app/api/editor/upload-image/route.ts +61 -0
- package/template/src/app/api/integrations/google-sheet/jobs/[jobId]/route.ts +47 -0
- package/template/src/app/api/integrations/google-sheet/jobs/usage/route.ts +50 -0
- package/template/src/app/api/integrations/google-sheet/sync/route.ts +28 -542
- package/template/src/app/api/invite/complete/route.ts +20 -23
- package/template/src/app/api/jobs/google-sheet/process/route.ts +84 -0
- package/template/src/app/api/jobs/google-sheet/schedule/route.ts +50 -0
- package/template/src/app/api/reminders/clear/route.ts +120 -0
- package/template/src/app/api/reminders/clear/undo/route.ts +112 -0
- package/template/src/app/api/reminders/route.ts +165 -39
- package/template/src/app/api/reminders/state/route.ts +164 -0
- package/template/src/app/api/reset-password/complete/route.ts +11 -13
- package/template/src/app/api/reset-password/request/route.ts +1 -1
- package/template/src/app/api/reset-password/verify/route.ts +1 -1
- package/template/src/app/api/send/route.ts +25 -47
- package/template/src/app/api/settings/closing-reasons/[id]/route.ts +10 -21
- package/template/src/app/api/settings/closing-reasons/route.ts +10 -21
- package/template/src/app/api/settings/company/route.ts +19 -26
- package/template/src/app/api/settings/google-ads/[id]/route.ts +20 -23
- package/template/src/app/api/settings/google-ads/route.ts +34 -23
- package/template/src/app/api/settings/google-calendar/calendars/route.ts +97 -0
- package/template/src/app/api/settings/google-calendar/route.ts +124 -0
- package/template/src/app/api/settings/google-sheet/[id]/route.ts +48 -23
- package/template/src/app/api/settings/google-sheet/auto-map/route.ts +56 -32
- package/template/src/app/api/settings/google-sheet/preview/route.ts +110 -0
- package/template/src/app/api/settings/google-sheet/route.ts +34 -23
- package/template/src/app/api/settings/integrations/logs/route.ts +93 -0
- package/template/src/app/api/settings/integrations/notifications/route.ts +67 -0
- package/template/src/app/api/settings/meta-leads/[id]/route.ts +20 -24
- package/template/src/app/api/settings/meta-leads/route.ts +34 -25
- package/template/src/app/api/settings/smtp/route.ts +53 -6
- package/template/src/app/api/settings/statuses/[id]/route.ts +29 -32
- package/template/src/app/api/settings/statuses/route.ts +24 -22
- package/template/src/app/api/statuses/route.ts +2 -5
- package/template/src/app/api/tasks/[id]/attendees/route.ts +36 -13
- package/template/src/app/api/tasks/[id]/route.ts +357 -145
- package/template/src/app/api/tasks/meet/route.ts +37 -26
- package/template/src/app/api/tasks/route.ts +201 -96
- package/template/src/app/api/templates/[id]/route.ts +22 -13
- package/template/src/app/api/templates/route.ts +22 -5
- package/template/src/app/api/users/[id]/resend-invite/route.ts +95 -0
- package/template/src/app/api/users/[id]/route.ts +22 -16
- package/template/src/app/api/users/commercials/route.ts +38 -0
- package/template/src/app/api/users/for-agenda/route.ts +1 -2
- package/template/src/app/api/users/list/route.ts +57 -19
- package/template/src/app/api/users/route.ts +89 -34
- package/template/src/app/api/webhooks/google-ads/route.ts +40 -1
- package/template/src/app/api/webhooks/meta-leads/route.ts +38 -1
- package/template/src/app/api/workflows/[id]/route.ts +29 -6
- package/template/src/app/api/workflows/process/route.ts +505 -170
- package/template/src/app/api/workflows/route.ts +42 -4
- package/template/src/app/globals.css +512 -32
- package/template/src/app/layout.tsx +28 -9
- package/template/src/app/page.tsx +37 -7
- package/template/src/components/address-autocomplete.tsx +233 -0
- package/template/src/components/config-error-alert.tsx +46 -0
- package/template/src/components/contacts/filter-bar.tsx +190 -0
- package/template/src/components/contacts/filter-builder.tsx +574 -0
- package/template/src/components/contacts/save-view-dialog.tsx +160 -0
- package/template/src/components/contacts/views-tab-bar.tsx +449 -0
- package/template/src/components/dashboard/activity-chart.tsx +6 -1
- package/template/src/components/dashboard/add-widget-dialog.tsx +13 -17
- package/template/src/components/dashboard/color-picker.tsx +7 -8
- package/template/src/components/dashboard/recent-activity.tsx +2 -5
- package/template/src/components/dashboard/stat-card.tsx +1 -3
- package/template/src/components/dashboard/status-distribution-chart.tsx +0 -1
- package/template/src/components/dashboard/top-contacts-list.tsx +7 -13
- package/template/src/components/dashboard/upcoming-tasks-list.tsx +2 -5
- package/template/src/components/dashboard/widget-wrapper.tsx +3 -6
- package/template/src/components/date-picker.tsx +399 -0
- package/template/src/components/editor/upload-editor-image.ts +42 -0
- package/template/src/components/editor.tsx +188 -35
- package/template/src/components/email-template.tsx +4 -2
- package/template/src/components/global-search.tsx +360 -0
- package/template/src/components/header.tsx +200 -107
- package/template/src/components/inactive-account-guard.tsx +58 -0
- package/template/src/components/integration-notifications-listener.tsx +12 -0
- package/template/src/components/invitation-email-template.tsx +4 -2
- package/template/src/components/lazy-editor.tsx +11 -0
- package/template/src/components/meet-cancellation-email-template.tsx +11 -3
- package/template/src/components/meet-confirmation-email-template.tsx +10 -3
- package/template/src/components/meet-update-email-template.tsx +10 -3
- package/template/src/components/page-header.tsx +19 -15
- package/template/src/components/protected-page.tsx +94 -0
- package/template/src/components/reset-password-email-template.tsx +4 -2
- package/template/src/components/settings/integrations/GoogleAdsIntegration.tsx +428 -0
- package/template/src/components/settings/integrations/GoogleSheetConfigMonitoringModal.tsx +680 -0
- package/template/src/components/settings/integrations/GoogleSheetIntegration.tsx +809 -0
- package/template/src/components/settings/integrations/ImportResultDialog.tsx +124 -0
- package/template/src/components/settings/integrations/IntegrationLogPanel.tsx +57 -0
- package/template/src/components/settings/integrations/IntegrationLogsTable.tsx +186 -0
- package/template/src/components/settings/integrations/MetaLeadIntegration.tsx +451 -0
- package/template/src/components/sidebar.tsx +117 -100
- package/template/src/components/skeleton.tsx +128 -45
- package/template/src/components/ui/accordion.tsx +64 -0
- package/template/src/components/ui/alert-dialog.tsx +139 -0
- package/template/src/components/ui/button.tsx +71 -0
- package/template/src/components/ui/components.tsx +1 -1
- package/template/src/components/ui/date-picker.tsx +422 -0
- package/template/src/components/ui/datetime-picker.tsx +338 -0
- package/template/src/components/ui/status-select.tsx +271 -0
- package/template/src/components/ui/tooltip.tsx +37 -0
- package/template/src/components/view-as-banner.tsx +1 -1
- package/template/src/components/view-as-modal.tsx +30 -19
- package/template/src/config/nav-pages.ts +108 -0
- package/template/src/contexts/app-toast-context.tsx +362 -0
- package/template/src/contexts/dashboard-theme-context.tsx +2 -7
- package/template/src/contexts/sidebar-context.tsx +27 -53
- package/template/src/contexts/task-reminder-context.tsx +134 -160
- package/template/src/contexts/view-as-context.tsx +32 -10
- package/template/src/hooks/use-alert.tsx +65 -0
- package/template/src/hooks/use-confirm.tsx +87 -0
- package/template/src/hooks/use-contact-views.ts +140 -0
- package/template/src/hooks/use-contacts.ts +69 -0
- package/template/src/hooks/use-fetch.ts +17 -0
- package/template/src/hooks/use-focus-trap.ts +73 -0
- package/template/src/hooks/use-statuses.ts +22 -0
- package/template/src/hooks/useIntegrationNotifications.ts +49 -0
- package/template/src/lib/address-api.ts +155 -0
- package/template/src/lib/auth.ts +8 -1
- package/template/src/lib/cache.ts +73 -0
- package/template/src/lib/check-permission.ts +12 -177
- package/template/src/lib/config-links.ts +14 -0
- package/template/src/lib/contact-duplicate.ts +79 -61
- package/template/src/lib/contact-interactions.ts +24 -22
- package/template/src/lib/contact-view-filters.ts +301 -0
- package/template/src/lib/contacts-list-url.ts +190 -0
- package/template/src/lib/dashboard-stats.ts +282 -0
- package/template/src/lib/dashboard-themes.ts +0 -5
- package/template/src/lib/date-utils.ts +176 -0
- package/template/src/lib/default-widgets.ts +0 -2
- package/template/src/lib/editor-html-image-dimensions.ts +172 -0
- package/template/src/lib/editor-image-limits.ts +19 -0
- package/template/src/lib/email-html-sanitize.ts +19 -0
- package/template/src/lib/encryption.ts +9 -6
- package/template/src/lib/fr-geography.ts +192 -0
- package/template/src/lib/get-auth-user.ts +25 -0
- package/template/src/lib/google-calendar-agenda.ts +201 -0
- package/template/src/lib/google-calendar.ts +309 -17
- package/template/src/lib/google-fetch.ts +63 -0
- package/template/src/lib/google-sheet-sync-jobs.ts +96 -0
- package/template/src/lib/google-sheet-sync-runner.ts +514 -0
- package/template/src/lib/integration-import-log.ts +21 -0
- package/template/src/lib/local-storage.ts +34 -0
- package/template/src/lib/permissions.ts +268 -40
- package/template/src/lib/prisma.ts +15 -12
- package/template/src/lib/qstash.ts +65 -0
- package/template/src/lib/reminder-state-server.ts +80 -0
- package/template/src/lib/reminder-state.ts +29 -0
- package/template/src/lib/roles.ts +12 -15
- package/template/src/lib/supabase-storage.ts +113 -0
- package/template/src/lib/template-variables.ts +204 -29
- package/template/src/lib/utils.ts +71 -11
- package/template/src/lib/widget-registry.ts +0 -4
- package/template/src/lib/workflow-executor.ts +391 -228
- package/template/src/proxy.ts +35 -73
- package/template/src/types/contact-views.ts +351 -0
- package/template/vercel.json +5 -0
- package/template/WORKFLOWS_CRON.md +0 -185
- package/template/prisma/migrations/20251126144728_init/migration.sql +0 -78
- package/template/prisma/migrations/20251126155204_add_user_roles/migration.sql +0 -5
- package/template/prisma/migrations/20251128095126_add_company_info/migration.sql +0 -19
- package/template/prisma/migrations/20251128123321_add_smtp_config/migration.sql +0 -22
- package/template/prisma/migrations/20251128132303_add_status/migration.sql +0 -23
- package/template/prisma/migrations/20251201102207_add_user_active/migration.sql +0 -75
- package/template/prisma/migrations/20251201105507_add_email_signature/migration.sql +0 -2
- package/template/prisma/migrations/20251201151122_add_tasks/migration.sql +0 -45
- package/template/prisma/migrations/20251202111854_add_task_reminder/migration.sql +0 -2
- package/template/prisma/migrations/20251202135859_add_google_meet_integration/migration.sql +0 -27
- package/template/prisma/migrations/20251203103317_add_meta_lead_integration/migration.sql +0 -20
- package/template/prisma/migrations/20251203104002_add_google_ads_integration/migration.sql +0 -18
- package/template/prisma/migrations/20251203112122_add_google_sheet_integration/migration.sql +0 -32
- package/template/prisma/migrations/20251203153853_allow_multiple_integration_configs/migration.sql +0 -20
- package/template/prisma/migrations/20251205141705_update_user_roles/migration.sql +0 -12
- package/template/prisma/migrations/20251205150000_add_commercial_and_telepro_assignment/migration.sql +0 -21
- package/template/prisma/migrations/20251205160000_add_interaction_logging/migration.sql +0 -11
- package/template/prisma/migrations/20251208090314_add_automatic_interaction_types/migration.sql +0 -12
- package/template/prisma/migrations/20251208094843_mg/migration.sql +0 -14
- package/template/prisma/migrations/20251208100000_add_company_support/migration.sql +0 -14
- package/template/prisma/migrations/20251208110000_add_templates/migration.sql +0 -26
- package/template/prisma/migrations/20251208141304_add_video_conference_task_type/migration.sql +0 -2
- package/template/prisma/migrations/20251209104759_add_internal_note_to_task/migration.sql +0 -2
- package/template/prisma/migrations/20251209134803_add_company_field/migration.sql +0 -2
- package/template/prisma/migrations/20251209150000_rename_company_to_company_name/migration.sql +0 -3
- package/template/prisma/migrations/20251209150016_add_email_tracking/migration.sql +0 -21
- package/template/prisma/migrations/20251209155908_add_notify_contact_to_task/migration.sql +0 -2
- package/template/prisma/migrations/20251210110019_add_appointment_types/migration.sql +0 -10
- package/template/prisma/migrations/20251210113928_add_contact_files/migration.sql +0 -26
- package/template/prisma/migrations/20251212132339_add_custom_roles/migration.sql +0 -24
- package/template/prisma/migrations/20251215104448_add_file_interaction_types/migration.sql +0 -11
- package/template/prisma/migrations/20251215145616_add_closing_reasons/migration.sql +0 -12
- package/template/prisma/migrations/20251216140850_add_log_users/migration.sql +0 -25
- package/template/prisma/migrations/20251216151000_rename_perdu_to_ferme/migration.sql +0 -8
- package/template/prisma/migrations/20251216162318_add_column_mappings_to_google_sheet/migration.sql +0 -2
- package/template/prisma/migrations/20251216185127_add_workflows/migration.sql +0 -80
- package/template/prisma/migrations/20251216192237_add_scheduled_workflow_actions/migration.sql +0 -32
- package/template/prisma/migrations/20251220000000_add_task_interaction_type/migration.sql +0 -4
- package/template/prisma/migrations/20251221000000_add_task_type/migration.sql +0 -3
- package/template/prisma/migrations/20251221000001_add_event_color/migration.sql +0 -23
- package/template/prisma/migrations/20260210114913_add_dashboard_widget/migration.sql +0 -20
- package/template/prisma/migrations/20260226093949_fix_cascade_on_user_delete/migration.sql +0 -69
- package/template/src/app/(dashboard)/users/layout.tsx +0 -30
- package/template/src/lib/google-drive.ts +0 -380
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useState, useEffect, useRef, useMemo, useCallback } from 'react';
|
|
4
|
+
import { useRouter } from 'next/navigation';
|
|
5
|
+
import useSWR from 'swr';
|
|
6
|
+
import { Search, Users, Building2, FileText, Loader2 } from 'lucide-react';
|
|
7
|
+
import { cn } from '@/lib/utils';
|
|
8
|
+
import { NAV_PAGES } from '@/config/nav-pages';
|
|
9
|
+
import { useUserRole } from '@/hooks/use-user-role';
|
|
10
|
+
|
|
11
|
+
interface ContactResult {
|
|
12
|
+
id: string;
|
|
13
|
+
firstName: string | null;
|
|
14
|
+
lastName: string | null;
|
|
15
|
+
email: string | null;
|
|
16
|
+
phone: string | null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface CompanyResult {
|
|
20
|
+
id: string;
|
|
21
|
+
name: string;
|
|
22
|
+
email: string | null;
|
|
23
|
+
phone: string | null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface SearchResults {
|
|
27
|
+
contacts: ContactResult[];
|
|
28
|
+
companies: CompanyResult[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const fetcher = async (url: string): Promise<SearchResults> => {
|
|
32
|
+
const params = new URL(url, globalThis.location?.origin);
|
|
33
|
+
const q = params.searchParams.get('q') || '';
|
|
34
|
+
const searchParam = encodeURIComponent(q);
|
|
35
|
+
|
|
36
|
+
const [contactsRes, companiesRes] = await Promise.all([
|
|
37
|
+
fetch(`/api/contacts?search=${searchParam}&limit=5`),
|
|
38
|
+
fetch(`/api/companies?search=${searchParam}&limit=5`),
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
const [contactsData, companiesData] = await Promise.all([
|
|
42
|
+
contactsRes.ok ? contactsRes.json() : { contacts: [] },
|
|
43
|
+
companiesRes.ok ? companiesRes.json() : { companies: [] },
|
|
44
|
+
]);
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
contacts: contactsData.contacts ?? [],
|
|
48
|
+
companies: companiesData.companies ?? companiesData ?? [],
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
function useDebounce(value: string, delay: number) {
|
|
53
|
+
const [debouncedValue, setDebouncedValue] = useState(value);
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
const timer = setTimeout(() => setDebouncedValue(value), delay);
|
|
56
|
+
return () => clearTimeout(timer);
|
|
57
|
+
}, [value, delay]);
|
|
58
|
+
return debouncedValue;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function GlobalSearch() {
|
|
62
|
+
const [query, setQuery] = useState('');
|
|
63
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
64
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
65
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
66
|
+
const router = useRouter();
|
|
67
|
+
const { hasPermission } = useUserRole();
|
|
68
|
+
|
|
69
|
+
const debouncedQuery = useDebounce(query.trim(), 300);
|
|
70
|
+
const shouldFetchCRM = debouncedQuery.length >= 2;
|
|
71
|
+
|
|
72
|
+
const { data: crmResults, isLoading: crmLoading } = useSWR(
|
|
73
|
+
shouldFetchCRM ? `/api/_search?q=${debouncedQuery}` : null,
|
|
74
|
+
fetcher,
|
|
75
|
+
{ keepPreviousData: true, revalidateOnFocus: false },
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
const filteredPages = useMemo(() => {
|
|
79
|
+
const q = query.trim().toLowerCase();
|
|
80
|
+
if (q.length === 0) return [];
|
|
81
|
+
return NAV_PAGES.filter(
|
|
82
|
+
(page) =>
|
|
83
|
+
page.permissions.some((p) => hasPermission(p)) &&
|
|
84
|
+
`${page.parentLabel ?? ''} ${page.name}`.toLowerCase().includes(q),
|
|
85
|
+
);
|
|
86
|
+
}, [query, hasPermission]);
|
|
87
|
+
|
|
88
|
+
const flatOptions = useMemo(() => {
|
|
89
|
+
const opts: { href: string }[] = [];
|
|
90
|
+
filteredPages.forEach((p) => opts.push({ href: p.href }));
|
|
91
|
+
if (crmResults) {
|
|
92
|
+
crmResults.contacts.forEach((c) => opts.push({ href: `/contacts/${c.id}` }));
|
|
93
|
+
crmResults.companies.forEach((c) => opts.push({ href: `/contacts/companies/${c.id}` }));
|
|
94
|
+
}
|
|
95
|
+
return opts;
|
|
96
|
+
}, [filteredPages, crmResults]);
|
|
97
|
+
|
|
98
|
+
const [focusedIndex, setFocusedIndex] = useState(0);
|
|
99
|
+
const optionRefs = useRef<(HTMLButtonElement | null)[]>([]);
|
|
100
|
+
const showDropdown = isOpen && query.trim().length > 0;
|
|
101
|
+
|
|
102
|
+
useEffect(() => {
|
|
103
|
+
setFocusedIndex(0);
|
|
104
|
+
}, [flatOptions.length]);
|
|
105
|
+
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
if (!showDropdown || focusedIndex < 0 || focusedIndex >= flatOptions.length) return;
|
|
108
|
+
optionRefs.current[focusedIndex]?.scrollIntoView({ block: 'nearest', behavior: 'smooth' });
|
|
109
|
+
}, [showDropdown, focusedIndex, flatOptions.length]);
|
|
110
|
+
|
|
111
|
+
const hasResults =
|
|
112
|
+
filteredPages.length > 0 ||
|
|
113
|
+
(crmResults && (crmResults.contacts.length > 0 || crmResults.companies.length > 0));
|
|
114
|
+
|
|
115
|
+
const navigate = useCallback(
|
|
116
|
+
(href: string) => {
|
|
117
|
+
setIsOpen(false);
|
|
118
|
+
setQuery('');
|
|
119
|
+
router.push(href);
|
|
120
|
+
},
|
|
121
|
+
[router],
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
useEffect(() => {
|
|
125
|
+
const handleClickOutside = (e: MouseEvent) => {
|
|
126
|
+
if (containerRef.current && !containerRef.current.contains(e.target as Node)) {
|
|
127
|
+
setIsOpen(false);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
const handleEscape = (e: KeyboardEvent) => {
|
|
131
|
+
if (e.key === 'Escape') {
|
|
132
|
+
setIsOpen(false);
|
|
133
|
+
inputRef.current?.blur();
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
document.addEventListener('mousedown', handleClickOutside);
|
|
137
|
+
document.addEventListener('keydown', handleEscape);
|
|
138
|
+
return () => {
|
|
139
|
+
document.removeEventListener('mousedown', handleClickOutside);
|
|
140
|
+
document.removeEventListener('keydown', handleEscape);
|
|
141
|
+
};
|
|
142
|
+
}, []);
|
|
143
|
+
|
|
144
|
+
const handleKeyDown = useCallback(
|
|
145
|
+
(e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
146
|
+
if (!showDropdown || flatOptions.length === 0) return;
|
|
147
|
+
if (e.key === 'ArrowDown') {
|
|
148
|
+
e.preventDefault();
|
|
149
|
+
setFocusedIndex((i) => Math.min(i + 1, flatOptions.length - 1));
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (e.key === 'ArrowUp') {
|
|
153
|
+
e.preventDefault();
|
|
154
|
+
setFocusedIndex((i) => Math.max(i - 1, 0));
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (e.key === 'Enter') {
|
|
158
|
+
e.preventDefault();
|
|
159
|
+
navigate(flatOptions[focusedIndex].href);
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
[showDropdown, flatOptions, focusedIndex, navigate],
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
let optionIndex = 0;
|
|
166
|
+
|
|
167
|
+
return (
|
|
168
|
+
<div ref={containerRef} className="relative hidden w-full max-w-xl sm:block">
|
|
169
|
+
<div className="relative">
|
|
170
|
+
<Search aria-hidden="true" className="text-muted-foreground pointer-events-none absolute top-1/2 left-3 h-4 w-4 -translate-y-1/2" />
|
|
171
|
+
<input
|
|
172
|
+
ref={inputRef}
|
|
173
|
+
type="text"
|
|
174
|
+
value={query}
|
|
175
|
+
onChange={(e) => {
|
|
176
|
+
setQuery(e.target.value);
|
|
177
|
+
if (!isOpen) setIsOpen(true);
|
|
178
|
+
}}
|
|
179
|
+
onFocus={() => {
|
|
180
|
+
if (query.trim().length > 0) setIsOpen(true);
|
|
181
|
+
}}
|
|
182
|
+
onKeyDown={handleKeyDown}
|
|
183
|
+
placeholder="Rechercher..."
|
|
184
|
+
className="border-border bg-muted text-foreground placeholder:text-muted-foreground focus-visible:border-primary/40 focus-visible:bg-background focus-visible:ring-primary/40 w-full rounded-lg border py-2 pr-4 pl-9 text-sm transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/50"
|
|
185
|
+
role="combobox"
|
|
186
|
+
aria-expanded={showDropdown}
|
|
187
|
+
aria-controls="global-search-results"
|
|
188
|
+
aria-activedescendant={
|
|
189
|
+
showDropdown && flatOptions.length > 0
|
|
190
|
+
? `global-search-option-${focusedIndex}`
|
|
191
|
+
: undefined
|
|
192
|
+
}
|
|
193
|
+
autoComplete="off"
|
|
194
|
+
/>
|
|
195
|
+
</div>
|
|
196
|
+
|
|
197
|
+
{showDropdown && (
|
|
198
|
+
<div
|
|
199
|
+
id="global-search-results"
|
|
200
|
+
role="listbox"
|
|
201
|
+
className="border-border bg-popover ui-dropdown-enter absolute top-full left-0 z-50 mt-1 max-h-112 w-full overflow-y-auto rounded-xl border shadow-(--shadow-dropdown)"
|
|
202
|
+
>
|
|
203
|
+
{/* Pages */}
|
|
204
|
+
{filteredPages.length > 0 && (
|
|
205
|
+
<div>
|
|
206
|
+
<div className="px-3 pt-3 pb-1">
|
|
207
|
+
<span className="text-muted-foreground text-xs font-semibold tracking-wider uppercase">
|
|
208
|
+
Pages
|
|
209
|
+
</span>
|
|
210
|
+
</div>
|
|
211
|
+
{filteredPages.map((page) => {
|
|
212
|
+
const Icon = page.icon;
|
|
213
|
+
const idx = optionIndex++;
|
|
214
|
+
return (
|
|
215
|
+
<button
|
|
216
|
+
key={page.href}
|
|
217
|
+
ref={(el) => {
|
|
218
|
+
optionRefs.current[idx] = el;
|
|
219
|
+
}}
|
|
220
|
+
id={`global-search-option-${idx}`}
|
|
221
|
+
onClick={() => navigate(page.href)}
|
|
222
|
+
className={cn(
|
|
223
|
+
'text-popover-foreground hover:bg-accent flex w-full cursor-pointer items-center gap-3 px-3 py-2 text-left text-sm transition-colors duration-200',
|
|
224
|
+
focusedIndex === idx && 'bg-accent',
|
|
225
|
+
)}
|
|
226
|
+
role="option"
|
|
227
|
+
aria-selected={focusedIndex === idx}
|
|
228
|
+
>
|
|
229
|
+
<Icon aria-hidden="true" className="text-muted-foreground h-4 w-4 shrink-0" />
|
|
230
|
+
<div className="min-w-0 flex-1">
|
|
231
|
+
<span className="text-popover-foreground block truncate font-medium">
|
|
232
|
+
{page.name}
|
|
233
|
+
</span>
|
|
234
|
+
{page.parentLabel && (
|
|
235
|
+
<span className="text-muted-foreground block truncate text-xs">
|
|
236
|
+
{page.parentLabel} {'>'} {page.name}
|
|
237
|
+
</span>
|
|
238
|
+
)}
|
|
239
|
+
</div>
|
|
240
|
+
</button>
|
|
241
|
+
);
|
|
242
|
+
})}
|
|
243
|
+
</div>
|
|
244
|
+
)}
|
|
245
|
+
|
|
246
|
+
{/* CRM loading */}
|
|
247
|
+
{shouldFetchCRM && crmLoading && !crmResults && (
|
|
248
|
+
<div className="text-muted-foreground flex items-center justify-center gap-2 px-3 py-6 text-sm">
|
|
249
|
+
<Loader2 className="h-4 w-4 animate-spin" />
|
|
250
|
+
Recherche...
|
|
251
|
+
</div>
|
|
252
|
+
)}
|
|
253
|
+
|
|
254
|
+
{/* Contacts */}
|
|
255
|
+
{crmResults && crmResults.contacts.length > 0 && (
|
|
256
|
+
<div>
|
|
257
|
+
<div
|
|
258
|
+
className={cn(
|
|
259
|
+
'px-3 pt-3 pb-1',
|
|
260
|
+
filteredPages.length > 0 && 'border-border border-t',
|
|
261
|
+
)}
|
|
262
|
+
>
|
|
263
|
+
<span className="text-muted-foreground text-xs font-semibold tracking-wider uppercase">
|
|
264
|
+
Contacts
|
|
265
|
+
</span>
|
|
266
|
+
</div>
|
|
267
|
+
{crmResults.contacts.map((contact) => {
|
|
268
|
+
const name =
|
|
269
|
+
[contact.firstName, contact.lastName].filter(Boolean).join(' ') ||
|
|
270
|
+
'Contact sans nom';
|
|
271
|
+
const idx = optionIndex++;
|
|
272
|
+
return (
|
|
273
|
+
<button
|
|
274
|
+
key={contact.id}
|
|
275
|
+
ref={(el) => {
|
|
276
|
+
optionRefs.current[idx] = el;
|
|
277
|
+
}}
|
|
278
|
+
id={`global-search-option-${idx}`}
|
|
279
|
+
onClick={() => navigate(`/contacts/${contact.id}`)}
|
|
280
|
+
className={cn(
|
|
281
|
+
'hover:bg-accent flex w-full cursor-pointer items-center gap-3 px-3 py-2 text-left text-sm transition-colors duration-200',
|
|
282
|
+
focusedIndex === idx && 'bg-accent',
|
|
283
|
+
)}
|
|
284
|
+
role="option"
|
|
285
|
+
aria-selected={focusedIndex === idx}
|
|
286
|
+
>
|
|
287
|
+
<Users aria-hidden="true" className="text-muted-foreground h-4 w-4 shrink-0" />
|
|
288
|
+
<div className="min-w-0 flex-1">
|
|
289
|
+
<span className="text-popover-foreground font-medium">{name}</span>
|
|
290
|
+
{(contact.email || contact.phone) && (
|
|
291
|
+
<span className="text-muted-foreground ml-2 truncate text-xs">
|
|
292
|
+
{contact.email || contact.phone}
|
|
293
|
+
</span>
|
|
294
|
+
)}
|
|
295
|
+
</div>
|
|
296
|
+
</button>
|
|
297
|
+
);
|
|
298
|
+
})}
|
|
299
|
+
</div>
|
|
300
|
+
)}
|
|
301
|
+
|
|
302
|
+
{/* Entreprises */}
|
|
303
|
+
{crmResults && crmResults.companies.length > 0 && (
|
|
304
|
+
<div>
|
|
305
|
+
<div className="border-border border-t px-3 pt-3 pb-1">
|
|
306
|
+
<span className="text-muted-foreground text-xs font-semibold tracking-wider uppercase">
|
|
307
|
+
Entreprises
|
|
308
|
+
</span>
|
|
309
|
+
</div>
|
|
310
|
+
{crmResults.companies.map((company) => {
|
|
311
|
+
const idx = optionIndex++;
|
|
312
|
+
return (
|
|
313
|
+
<button
|
|
314
|
+
key={company.id}
|
|
315
|
+
ref={(el) => {
|
|
316
|
+
optionRefs.current[idx] = el;
|
|
317
|
+
}}
|
|
318
|
+
id={`global-search-option-${idx}`}
|
|
319
|
+
onClick={() => navigate(`/contacts/companies/${company.id}`)}
|
|
320
|
+
className={cn(
|
|
321
|
+
'hover:bg-accent flex w-full cursor-pointer items-center gap-3 px-3 py-2 text-left text-sm transition-colors duration-200',
|
|
322
|
+
focusedIndex === idx && 'bg-accent',
|
|
323
|
+
)}
|
|
324
|
+
role="option"
|
|
325
|
+
aria-selected={focusedIndex === idx}
|
|
326
|
+
>
|
|
327
|
+
<Building2 aria-hidden="true" className="text-muted-foreground h-4 w-4 shrink-0" />
|
|
328
|
+
<div className="min-w-0 flex-1">
|
|
329
|
+
<span className="text-popover-foreground font-medium">{company.name}</span>
|
|
330
|
+
{company.email && (
|
|
331
|
+
<span className="text-muted-foreground ml-2 truncate text-xs">
|
|
332
|
+
{company.email}
|
|
333
|
+
</span>
|
|
334
|
+
)}
|
|
335
|
+
</div>
|
|
336
|
+
</button>
|
|
337
|
+
);
|
|
338
|
+
})}
|
|
339
|
+
</div>
|
|
340
|
+
)}
|
|
341
|
+
|
|
342
|
+
{/* Aucun résultat */}
|
|
343
|
+
{!crmLoading && !hasResults && query.trim().length > 0 && (
|
|
344
|
+
<div className="text-muted-foreground px-3 py-6 text-center text-sm">
|
|
345
|
+
Aucun résultat pour "{query.trim()}"
|
|
346
|
+
</div>
|
|
347
|
+
)}
|
|
348
|
+
|
|
349
|
+
{/* Recherche trop courte pour le CRM */}
|
|
350
|
+
{query.trim().length === 1 && filteredPages.length === 0 && (
|
|
351
|
+
<div className="text-muted-foreground px-3 py-6 text-center text-sm">
|
|
352
|
+
<FileText aria-hidden="true" className="text-muted-foreground/70 mx-auto mb-1 h-5 w-5" />
|
|
353
|
+
Tapez au moins 2 caractères pour rechercher dans le CRM
|
|
354
|
+
</div>
|
|
355
|
+
)}
|
|
356
|
+
</div>
|
|
357
|
+
)}
|
|
358
|
+
</div>
|
|
359
|
+
);
|
|
360
|
+
}
|