@tuturuuu/ui 0.8.0 → 0.9.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.
Files changed (182) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/biome.json +1 -1
  3. package/package.json +73 -71
  4. package/src/components/ui/accordion.tsx +1 -1
  5. package/src/components/ui/breadcrumb.tsx +1 -1
  6. package/src/components/ui/calendar-app/calendar-page-shell.tsx +4 -0
  7. package/src/components/ui/calendar-app/components/calendar-connections-settings-content.tsx +239 -33
  8. package/src/components/ui/calendar-app/components/load-smart-scheduling-tasks.tsx +143 -0
  9. package/src/components/ui/calendar-app/components/priority-view.tsx +10 -3
  10. package/src/components/ui/calendar-app/components/tasks-sidebar.tsx +4 -116
  11. package/src/components/ui/calendar-app/components/use-calendar-connections-manager.ts +67 -2
  12. package/src/components/ui/calendar.tsx +1 -1
  13. package/src/components/ui/carousel.tsx +1 -1
  14. package/src/components/ui/chat/chat-agent-details-external-thread-panel.test.tsx +1 -1
  15. package/src/components/ui/chat/chat-agent-details-external-thread-panel.tsx +1 -1
  16. package/src/components/ui/chat/chat-agent-details-operations-panel.test.tsx +1 -1
  17. package/src/components/ui/chat/chat-agent-details-operations-panel.tsx +1 -1
  18. package/src/components/ui/chat/chat-agent-details-setup-panel.tsx +1 -1
  19. package/src/components/ui/chat/chat-agent-details-sidebar.test.tsx +1 -1
  20. package/src/components/ui/chat/chat-agent-details-sidebar.tsx +2 -2
  21. package/src/components/ui/chat/chat-agent-details-utils.test.ts +1 -1
  22. package/src/components/ui/chat/chat-agent-details-utils.tsx +1 -1
  23. package/src/components/ui/chat/chat-agent-details-zalo-personal-panel.tsx +2 -2
  24. package/src/components/ui/checkbox.tsx +1 -1
  25. package/src/components/ui/color-picker.tsx +1 -1
  26. package/src/components/ui/command.tsx +1 -1
  27. package/src/components/ui/context-menu.tsx +5 -1
  28. package/src/components/ui/custom/__tests__/settings-dialog-shell.test.tsx +3 -0
  29. package/src/components/ui/custom/__tests__/workspace-select-helpers.test.ts +19 -0
  30. package/src/components/ui/custom/combobox.test.tsx +195 -0
  31. package/src/components/ui/custom/combobox.tsx +273 -156
  32. package/src/components/ui/custom/education/modules/youtube/delete-link-button.tsx +5 -13
  33. package/src/components/ui/custom/facebook-mockup/facebook-mockup.tsx +7 -1
  34. package/src/components/ui/custom/facebook-mockup/form.tsx +1 -1
  35. package/src/components/ui/custom/facebook-mockup/image-upload-field.tsx +1 -1
  36. package/src/components/ui/custom/facebook-mockup/preview.tsx +1 -1
  37. package/src/components/ui/custom/settings-dialog-shell.tsx +2 -1
  38. package/src/components/ui/custom/theme-toggle.tsx +1 -1
  39. package/src/components/ui/custom/workspace-select.tsx +8 -3
  40. package/src/components/ui/dialog.test.tsx +52 -0
  41. package/src/components/ui/dialog.tsx +6 -2
  42. package/src/components/ui/dropdown-menu.tsx +5 -1
  43. package/src/components/ui/finance/debts/debt-loan-form.tsx +12 -5
  44. package/src/components/ui/finance/debts/debt-loan-summary.tsx +3 -2
  45. package/src/components/ui/finance/debts/debts-page.test.tsx +54 -5
  46. package/src/components/ui/finance/debts/debts-page.tsx +15 -2
  47. package/src/components/ui/finance/invoices/components/subscription-group-selector.tsx +3 -5
  48. package/src/components/ui/finance/invoices/new-invoice-page.test.tsx +25 -5
  49. package/src/components/ui/finance/invoices/new-invoice-page.tsx +7 -2
  50. package/src/components/ui/finance/invoices/standard-invoice.tsx +4 -2
  51. package/src/components/ui/finance/invoices/subscription-invoice.tsx +4 -2
  52. package/src/components/ui/finance/invoices/utils.ts +3 -1
  53. package/src/components/ui/finance/transactions/form-content-dialog.tsx +3 -0
  54. package/src/components/ui/finance/transactions/form-types.ts +1 -0
  55. package/src/components/ui/finance/transactions/form.tsx +2 -0
  56. package/src/components/ui/finance/transactions/infinite-transactions-list.tsx +2 -0
  57. package/src/components/ui/finance/transactions/period-charts/category-breakdown-dialog.tsx +1 -1
  58. package/src/components/ui/finance/transactions/transaction-edit-dialog.tsx +1 -4
  59. package/src/components/ui/finance/transactions/transactions-create-summary.tsx +3 -0
  60. package/src/components/ui/finance/transactions/transactions-page.tsx +4 -1
  61. package/src/components/ui/finance/wallets/form.test.tsx +51 -3
  62. package/src/components/ui/finance/wallets/form.tsx +15 -4
  63. package/src/components/ui/finance/wallets/walletId/wallet-details-actions.tsx +4 -0
  64. package/src/components/ui/finance/wallets/walletId/wallet-details-page.tsx +4 -2
  65. package/src/components/ui/finance/wallets/wallets-data-table.tsx +1 -0
  66. package/src/components/ui/finance/wallets/wallets-page.tsx +5 -2
  67. package/src/components/ui/input-otp.tsx +1 -1
  68. package/src/components/ui/legacy/calendar/all-day-event-bar.tsx +28 -39
  69. package/src/components/ui/legacy/calendar/calendar-cell.tsx +2 -0
  70. package/src/components/ui/legacy/calendar/calendar-content.tsx +10 -6
  71. package/src/components/ui/legacy/calendar/calendar-header.tsx +23 -3
  72. package/src/components/ui/legacy/calendar/calendar-loading-skeleton.tsx +135 -0
  73. package/src/components/ui/legacy/calendar/calendar-matrix.tsx +175 -237
  74. package/src/components/ui/legacy/calendar/event-card.test.tsx +177 -0
  75. package/src/components/ui/legacy/calendar/event-card.tsx +220 -131
  76. package/src/components/ui/legacy/calendar/event-modal.tsx +17 -17
  77. package/src/components/ui/legacy/calendar/event-provider-display.tsx +69 -0
  78. package/src/components/ui/legacy/calendar/smart-calendar.test.tsx +86 -4
  79. package/src/components/ui/legacy/calendar/smart-calendar.tsx +32 -2
  80. package/src/components/ui/legacy/meet/create-plan-dialog.tsx +19 -10
  81. package/src/components/ui/navigation-menu.tsx +1 -1
  82. package/src/components/ui/pagination.tsx +1 -1
  83. package/src/components/ui/radio-group.tsx +1 -1
  84. package/src/components/ui/select.tsx +5 -1
  85. package/src/components/ui/sheet.tsx +1 -1
  86. package/src/components/ui/sidebar.tsx +1 -1
  87. package/src/components/ui/storefront/cart-popover.tsx +61 -0
  88. package/src/components/ui/storefront/cart-summary-parts.tsx +290 -0
  89. package/src/components/ui/storefront/cart-summary.tsx +93 -154
  90. package/src/components/ui/storefront/checkout-overlay.tsx +4 -5
  91. package/src/components/ui/storefront/listing-card.tsx +1 -1
  92. package/src/components/ui/storefront/merch-sections.tsx +70 -0
  93. package/src/components/ui/storefront/product-detail.tsx +1 -1
  94. package/src/components/ui/storefront/storefront-surface.test.tsx +106 -11
  95. package/src/components/ui/storefront/storefront-surface.tsx +101 -166
  96. package/src/components/ui/storefront/types.ts +4 -0
  97. package/src/components/ui/storefront/utils.ts +6 -0
  98. package/src/components/ui/text-editor/__tests__/extensions.test.ts +123 -0
  99. package/src/components/ui/text-editor/background-color-extension.ts +62 -0
  100. package/src/components/ui/text-editor/color-controls.tsx +284 -0
  101. package/src/components/ui/text-editor/editor.tsx +69 -14
  102. package/src/components/ui/text-editor/extensions.ts +8 -2
  103. package/src/components/ui/text-editor/highlight-extension.ts +22 -0
  104. package/src/components/ui/text-editor/tool-bar.tsx +9 -16
  105. package/src/components/ui/toast.tsx +1 -1
  106. package/src/components/ui/tu-do/boards/__tests__/board-share-dialog.test.tsx +270 -0
  107. package/src/components/ui/tu-do/boards/board-public-link-section.tsx +231 -0
  108. package/src/components/ui/tu-do/boards/board-share-dialog.tsx +222 -109
  109. package/src/components/ui/tu-do/boards/boardId/board-column.tsx +112 -43
  110. package/src/components/ui/tu-do/boards/boardId/kanban/bulk/bulk-mutations-clear-delete.ts +2 -0
  111. package/src/components/ui/tu-do/boards/boardId/kanban/bulk/bulk-mutations-move.ts +5 -0
  112. package/src/components/ui/tu-do/boards/boardId/kanban/bulk/bulk-mutations-updates.ts +3 -0
  113. package/src/components/ui/tu-do/boards/boardId/kanban/data/kanban-deadline-query.ts +50 -2
  114. package/src/components/ui/tu-do/boards/boardId/kanban/dnd/__tests__/column-reorder.test.ts +17 -0
  115. package/src/components/ui/tu-do/boards/boardId/kanban/dnd/column-reorder.ts +4 -1
  116. package/src/components/ui/tu-do/boards/boardId/kanban/dnd/task-drag-cache.ts +38 -9
  117. package/src/components/ui/tu-do/boards/boardId/kanban/dnd/task-drag-order.ts +2 -8
  118. package/src/components/ui/tu-do/boards/boardId/kanban/dnd/task-sort-key.ts +47 -0
  119. package/src/components/ui/tu-do/boards/boardId/kanban/dnd/use-kanban-dnd.ts +81 -30
  120. package/src/components/ui/tu-do/boards/boardId/kanban/planner/__tests__/kanban-planner-island.test.tsx +380 -0
  121. package/src/components/ui/tu-do/boards/boardId/kanban/planner/kanban-planner-dialog.tsx +204 -0
  122. package/src/components/ui/tu-do/boards/boardId/kanban/planner/planner-digest-panel.tsx +61 -0
  123. package/src/components/ui/tu-do/boards/boardId/kanban/planner/planner-item-strip.tsx +54 -0
  124. package/src/components/ui/tu-do/boards/boardId/kanban/planner/planner-plan-toolbar.tsx +251 -0
  125. package/src/components/ui/tu-do/boards/boardId/kanban/planner/planner-scope-badge.tsx +27 -0
  126. package/src/components/ui/tu-do/boards/boardId/kanban/planner/planner-section.tsx +58 -0
  127. package/src/components/ui/tu-do/boards/boardId/kanban/planner/planner-share-dialog.tsx +238 -0
  128. package/src/components/ui/tu-do/boards/boardId/kanban/planner/planner-target-controls.tsx +143 -0
  129. package/src/components/ui/tu-do/boards/boardId/kanban/planner/planner-utils.ts +65 -0
  130. package/src/components/ui/tu-do/boards/boardId/kanban/planner/use-kanban-planner-state.ts +234 -0
  131. package/src/components/ui/tu-do/boards/boardId/kanban/rendering/kanban-columns.test.tsx +397 -2
  132. package/src/components/ui/tu-do/boards/boardId/kanban/rendering/kanban-columns.tsx +103 -13
  133. package/src/components/ui/tu-do/boards/boardId/kanban/rendering/kanban-deadline-panels.tsx +443 -19
  134. package/src/components/ui/tu-do/boards/boardId/kanban/rendering/kanban-skeleton.tsx +94 -32
  135. package/src/components/ui/tu-do/boards/boardId/kanban.tsx +213 -106
  136. package/src/components/ui/tu-do/boards/boardId/task-board-server-page.test.tsx +26 -4
  137. package/src/components/ui/tu-do/boards/boardId/task-board-server-page.tsx +5 -2
  138. package/src/components/ui/tu-do/boards/boardId/task-card/measured-task-card.tsx +3 -0
  139. package/src/components/ui/tu-do/boards/boardId/task-card/task-card-comparator.ts +3 -0
  140. package/src/components/ui/tu-do/boards/boardId/task-card/task-card.tsx +191 -28
  141. package/src/components/ui/tu-do/boards/boardId/task-filter.test.tsx +152 -0
  142. package/src/components/ui/tu-do/boards/boardId/task-filter.tsx +555 -545
  143. package/src/components/ui/tu-do/boards/boardId/task-list.tsx +7 -0
  144. package/src/components/ui/tu-do/boards/share-section.tsx +100 -0
  145. package/src/components/ui/tu-do/drafts/draft-convert-dialog.tsx +10 -12
  146. package/src/components/ui/tu-do/drafts/drafts-page.tsx +33 -16
  147. package/src/components/ui/tu-do/initiatives/task-initiatives-client.tsx +56 -88
  148. package/src/components/ui/tu-do/my-tasks/my-tasks-content.tsx +26 -2
  149. package/src/components/ui/tu-do/my-tasks/use-my-tasks-state.ts +55 -8
  150. package/src/components/ui/tu-do/notes/note-edit-dialog.tsx +1 -4
  151. package/src/components/ui/tu-do/shared/__tests__/board-client.test.tsx +25 -0
  152. package/src/components/ui/tu-do/shared/__tests__/board-header.test.tsx +341 -38
  153. package/src/components/ui/tu-do/shared/__tests__/board-switcher.test.tsx +253 -0
  154. package/src/components/ui/tu-do/shared/__tests__/board-views.test.tsx +203 -2
  155. package/src/components/ui/tu-do/shared/__tests__/task-board-loading-state.test.tsx +17 -0
  156. package/src/components/ui/tu-do/shared/__tests__/task-legacy-route-recovery.test.tsx +16 -0
  157. package/src/components/ui/tu-do/shared/board-client.tsx +2 -7
  158. package/src/components/ui/tu-do/shared/board-config-storage.ts +7 -1
  159. package/src/components/ui/tu-do/shared/board-header.tsx +464 -975
  160. package/src/components/ui/tu-do/shared/board-layout-settings.tsx +165 -136
  161. package/src/components/ui/tu-do/shared/board-switcher.tsx +209 -217
  162. package/src/components/ui/tu-do/shared/board-views.tsx +587 -75
  163. package/src/components/ui/tu-do/shared/list-view.tsx +227 -1
  164. package/src/components/ui/tu-do/shared/recycle-bin-panel.tsx +142 -94
  165. package/src/components/ui/tu-do/shared/special-task-list-pins.ts +51 -0
  166. package/src/components/ui/tu-do/shared/task-board-loading-state.tsx +28 -0
  167. package/src/components/ui/tu-do/shared/task-edit-dialog/field-diff-viewer.tsx +3 -2
  168. package/src/components/ui/tu-do/shared/task-edit-dialog/selective-revert-panel.test.tsx +91 -0
  169. package/src/components/ui/tu-do/shared/task-edit-dialog/selective-revert-panel.tsx +123 -78
  170. package/src/components/ui/tu-do/shared/task-edit-dialog/task-activity-section.tsx +7 -1
  171. package/src/components/ui/tu-do/shared/task-edit-dialog/task-snapshot-dialog.tsx +8 -3
  172. package/src/components/ui/tu-do/shared/task-edit-dialog.tsx +2 -1
  173. package/src/components/ui/tu-do/shared/task-legacy-route-recovery.tsx +2 -9
  174. package/src/declarations.d.ts +1 -0
  175. package/src/hooks/__tests__/use-calendar-readonly.test.tsx +322 -2
  176. package/src/hooks/__tests__/use-calendar-sync.test.tsx +446 -0
  177. package/src/hooks/use-calendar-sync.tsx +247 -243
  178. package/src/hooks/use-calendar.tsx +323 -138
  179. package/src/hooks/use-task-actions.ts +24 -0
  180. package/src/hooks/use-user-workspace-config.ts +75 -0
  181. package/src/hooks/use-workspace-currency.ts +8 -3
  182. package/src/hooks/useBoardRealtimeEventHandler.ts +11 -0
@@ -0,0 +1,143 @@
1
+ 'use client';
2
+
3
+ import { Loader2 } from '@tuturuuu/icons';
4
+ import { Button } from '@tuturuuu/ui/button';
5
+ import { Combobox } from '@tuturuuu/ui/custom/combobox';
6
+ import { Input } from '@tuturuuu/ui/input';
7
+ import { useTranslations } from 'next-intl';
8
+
9
+ type WorkspaceOption = { id: string; name: string | null };
10
+ type BoardOption = {
11
+ id: string;
12
+ name: string | null;
13
+ task_lists?: Array<{ id: string; name: string | null }>;
14
+ };
15
+
16
+ interface PlannerTargetControlsProps {
17
+ addWorkspacePending: boolean;
18
+ boards: BoardOption[];
19
+ createItemPending: boolean;
20
+ lists: Array<{ id: string; name: string | null }>;
21
+ onAddWorkspace: () => void;
22
+ onCreateItem: () => void;
23
+ onPlannedDateChange: (value: string) => void;
24
+ onTargetBoardChange: (value: string) => void;
25
+ onTargetListChange: (value: string) => void;
26
+ onTargetWorkspaceChange: (value: string) => void;
27
+ onTaskTitleChange: (value: string) => void;
28
+ plannedDate: string;
29
+ targetBoardId: string;
30
+ targetIsIntended: boolean;
31
+ targetListId: string;
32
+ targetWorkspaceId: string;
33
+ taskTitle: string;
34
+ workspaces: WorkspaceOption[];
35
+ }
36
+
37
+ export function PlannerTargetControls({
38
+ addWorkspacePending,
39
+ boards,
40
+ createItemPending,
41
+ lists,
42
+ onAddWorkspace,
43
+ onCreateItem,
44
+ onPlannedDateChange,
45
+ onTargetBoardChange,
46
+ onTargetListChange,
47
+ onTargetWorkspaceChange,
48
+ onTaskTitleChange,
49
+ plannedDate,
50
+ targetBoardId,
51
+ targetIsIntended,
52
+ targetListId,
53
+ targetWorkspaceId,
54
+ taskTitle,
55
+ workspaces,
56
+ }: PlannerTargetControlsProps) {
57
+ const t = useTranslations('ws-task-plans');
58
+ const workspaceOptions = workspaces.map((workspace) => ({
59
+ value: workspace.id,
60
+ label: workspace.name ?? t('untitled_workspace'),
61
+ }));
62
+ const boardOptions = boards.map((board) => ({
63
+ value: board.id,
64
+ label: board.name ?? t('untitled_board'),
65
+ }));
66
+ const listOptions = lists.map((list) => ({
67
+ value: list.id,
68
+ label: list.name ?? t('target_list'),
69
+ }));
70
+
71
+ return (
72
+ <div className="grid gap-2 xl:grid-cols-[minmax(0,1fr)_22rem]">
73
+ <div className="grid gap-2 md:grid-cols-[1fr_1fr_1fr_auto]">
74
+ <Combobox
75
+ mode="single"
76
+ options={workspaceOptions}
77
+ selected={targetWorkspaceId}
78
+ onChange={(value) => onTargetWorkspaceChange(value as string)}
79
+ placeholder={t('target_workspace')}
80
+ searchPlaceholder={t('target_workspace')}
81
+ className="[&_button]:h-9"
82
+ />
83
+ <Combobox
84
+ mode="single"
85
+ options={boardOptions}
86
+ selected={targetBoardId}
87
+ onChange={(value) => onTargetBoardChange(value as string)}
88
+ placeholder={t('target_board')}
89
+ searchPlaceholder={t('target_board')}
90
+ className="[&_button]:h-9"
91
+ />
92
+ <Combobox
93
+ mode="single"
94
+ options={listOptions}
95
+ selected={targetListId}
96
+ onChange={(value) => onTargetListChange(value as string)}
97
+ placeholder={t('target_list')}
98
+ searchPlaceholder={t('target_list')}
99
+ disabled={!targetBoardId}
100
+ className="[&_button]:h-9"
101
+ />
102
+ <Button
103
+ type="button"
104
+ variant={targetIsIntended ? 'secondary' : 'outline'}
105
+ size="sm"
106
+ onClick={onAddWorkspace}
107
+ disabled={targetIsIntended || addWorkspacePending}
108
+ className="h-9"
109
+ >
110
+ {targetIsIntended ? t('intended_workspace') : t('add_workspace')}
111
+ </Button>
112
+ </div>
113
+
114
+ <div className="grid gap-2 md:grid-cols-[9rem_1fr_auto] xl:grid-cols-[8rem_1fr_auto]">
115
+ <Input
116
+ type="date"
117
+ value={plannedDate}
118
+ onChange={(event) => onPlannedDateChange(event.target.value)}
119
+ className="h-9"
120
+ aria-label={t('planned_date')}
121
+ />
122
+ <Input
123
+ value={taskTitle}
124
+ onChange={(event) => onTaskTitleChange(event.target.value)}
125
+ placeholder={t('task_title_placeholder')}
126
+ className="h-9"
127
+ />
128
+ <Button
129
+ type="button"
130
+ size="sm"
131
+ onClick={onCreateItem}
132
+ disabled={!taskTitle.trim() || createItemPending}
133
+ className="h-9 gap-2"
134
+ >
135
+ {createItemPending && <Loader2 className="h-4 w-4 animate-spin" />}
136
+ {targetIsIntended && targetListId
137
+ ? t('create_task')
138
+ : t('create_draft')}
139
+ </Button>
140
+ </div>
141
+ </div>
142
+ );
143
+ }
@@ -0,0 +1,65 @@
1
+ import type {
2
+ TaskPlan,
3
+ TaskPlanItem,
4
+ TaskPlanPeriod,
5
+ TaskPlanStatus,
6
+ } from '@tuturuuu/internal-api';
7
+
8
+ export const TASK_PLAN_PERIODS: TaskPlanPeriod[] = ['week', 'month', 'year'];
9
+ export const TASK_PLAN_STATUSES: TaskPlanStatus[] = [
10
+ 'draft',
11
+ 'active',
12
+ 'sent',
13
+ 'archived',
14
+ ];
15
+
16
+ function toDateInputValue(date: Date) {
17
+ return date.toISOString().slice(0, 10);
18
+ }
19
+
20
+ export function buildPlanWindow(period: TaskPlanPeriod, anchor = new Date()) {
21
+ const start = new Date(anchor);
22
+ start.setHours(0, 0, 0, 0);
23
+
24
+ const end = new Date(start);
25
+ if (period === 'week') {
26
+ const day = start.getDay();
27
+ const mondayOffset = day === 0 ? -6 : 1 - day;
28
+ start.setDate(start.getDate() + mondayOffset);
29
+ end.setTime(start.getTime());
30
+ end.setDate(start.getDate() + 6);
31
+ } else if (period === 'month') {
32
+ start.setDate(1);
33
+ end.setFullYear(start.getFullYear(), start.getMonth() + 1, 0);
34
+ } else {
35
+ start.setMonth(0, 1);
36
+ end.setFullYear(start.getFullYear(), 11, 31);
37
+ }
38
+
39
+ return {
40
+ start: toDateInputValue(start),
41
+ end: toDateInputValue(end),
42
+ };
43
+ }
44
+
45
+ export function getDefaultPlanTitle(period: TaskPlanPeriod) {
46
+ const window = buildPlanWindow(period);
47
+ return `${period[0]?.toUpperCase()}${period.slice(1)} plan ${window.start}`;
48
+ }
49
+
50
+ export function getPlanWindowLabel(
51
+ plan: Pick<TaskPlan, 'period_start' | 'period_end'>
52
+ ) {
53
+ return `${plan.period_start} -> ${plan.period_end}`;
54
+ }
55
+
56
+ export function getTaskPlanItemScope(
57
+ item: Pick<TaskPlanItem, 'task_id' | 'target_ws_id' | 'status'>,
58
+ personalWorkspaceId: string
59
+ ) {
60
+ if (item.status === 'draft' || !item.task_id) return 'draft';
61
+ if (!item.target_ws_id || item.target_ws_id === personalWorkspaceId) {
62
+ return 'personal';
63
+ }
64
+ return 'external';
65
+ }
@@ -0,0 +1,234 @@
1
+ 'use client';
2
+
3
+ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
4
+ import {
5
+ addWorkspaceTaskPlanWorkspace,
6
+ createWorkspaceTaskPlan,
7
+ createWorkspaceTaskPlanItem,
8
+ isTaskPlanSchemaUnavailable,
9
+ listWorkspaceBoardsWithLists,
10
+ listWorkspaces,
11
+ listWorkspaceTaskPlans,
12
+ type TaskPlanPeriod,
13
+ type TaskPlanStatus,
14
+ updateWorkspaceTaskPlan,
15
+ } from '@tuturuuu/internal-api';
16
+ import { toast } from '@tuturuuu/ui/sonner';
17
+ import { useTranslations } from 'next-intl';
18
+ import { useEffect, useMemo, useState } from 'react';
19
+ import { buildPlanWindow, getDefaultPlanTitle } from './planner-utils';
20
+
21
+ interface UseKanbanPlannerStateOptions {
22
+ boardId: string | null;
23
+ enabled?: boolean;
24
+ isPersonalWorkspace: boolean;
25
+ workspaceId: string;
26
+ }
27
+
28
+ export function useKanbanPlannerState({
29
+ boardId,
30
+ enabled = true,
31
+ isPersonalWorkspace,
32
+ workspaceId,
33
+ }: UseKanbanPlannerStateOptions) {
34
+ const tCommon = useTranslations('common');
35
+ const queryClient = useQueryClient();
36
+ const [mode, setMode] = useState<TaskPlanPeriod>('week');
37
+ const [planTitle, setPlanTitle] = useState('');
38
+ const [editMode, setEditMode] = useState<TaskPlanPeriod>('week');
39
+ const [editStatus, setEditStatus] = useState<TaskPlanStatus>('draft');
40
+ const [editTitle, setEditTitle] = useState('');
41
+ const [selectedPlanId, setSelectedPlanId] = useState<string | null>(null);
42
+ const [targetWorkspaceId, setTargetWorkspaceId] = useState(workspaceId);
43
+ const [targetBoardId, setTargetBoardId] = useState(boardId ?? '');
44
+ const [targetListId, setTargetListId] = useState('');
45
+ const [taskTitle, setTaskTitle] = useState('');
46
+ const [plannedDate, setPlannedDate] = useState(buildPlanWindow('week').start);
47
+
48
+ const plansQueryKey = ['task-plans', workspaceId] as const;
49
+ const plansQuery = useQuery({
50
+ enabled: isPersonalWorkspace && enabled,
51
+ queryKey: plansQueryKey,
52
+ queryFn: () => listWorkspaceTaskPlans(workspaceId),
53
+ staleTime: 20_000,
54
+ });
55
+ const schemaUnavailable =
56
+ plansQuery.data && isTaskPlanSchemaUnavailable(plansQuery.data);
57
+ const plans = schemaUnavailable ? [] : (plansQuery.data?.plans ?? []);
58
+ const selectedPlan = useMemo(
59
+ () => plans.find((plan) => plan.id === selectedPlanId) ?? plans[0] ?? null,
60
+ [plans, selectedPlanId]
61
+ );
62
+
63
+ const workspacesQuery = useQuery({
64
+ enabled: isPersonalWorkspace && enabled && !schemaUnavailable,
65
+ queryKey: ['task-plan-workspaces'],
66
+ queryFn: () => listWorkspaces({ limit: 100 }),
67
+ staleTime: 60_000,
68
+ });
69
+ const boardsQuery = useQuery({
70
+ enabled: enabled && Boolean(targetWorkspaceId) && !schemaUnavailable,
71
+ queryKey: ['task-plan-boards-with-lists', targetWorkspaceId],
72
+ queryFn: () => listWorkspaceBoardsWithLists(targetWorkspaceId),
73
+ staleTime: 30_000,
74
+ });
75
+ const targetWorkspace = workspacesQuery.data?.find(
76
+ (workspace) => workspace.id === targetWorkspaceId
77
+ );
78
+ const boards = boardsQuery.data?.boards ?? [];
79
+ const targetBoard = boards.find((board) => board.id === targetBoardId);
80
+ const lists = targetBoard?.task_lists ?? [];
81
+ const intendedWorkspaceIds = new Set(
82
+ selectedPlan?.workspaces?.map((workspace) => workspace.ws_id) ?? []
83
+ );
84
+ const targetIsIntended = intendedWorkspaceIds.has(targetWorkspaceId);
85
+
86
+ useEffect(() => {
87
+ if (!selectedPlan) return;
88
+ setEditMode(selectedPlan.period_type);
89
+ setEditStatus(selectedPlan.status);
90
+ setEditTitle(selectedPlan.title);
91
+ setPlannedDate(selectedPlan.period_start);
92
+ setTargetWorkspaceId(selectedPlan.default_target_ws_id ?? workspaceId);
93
+ setTargetBoardId(selectedPlan.default_target_board_id ?? boardId ?? '');
94
+ setTargetListId(selectedPlan.default_target_list_id ?? '');
95
+ }, [boardId, selectedPlan, workspaceId]);
96
+ useEffect(() => {
97
+ if (!targetBoardId && boards[0]?.id) setTargetBoardId(boards[0].id);
98
+ }, [boards, targetBoardId]);
99
+ useEffect(() => {
100
+ if (!targetListId && lists[0]?.id) setTargetListId(lists[0].id);
101
+ }, [lists, targetListId]);
102
+
103
+ const createPlanMutation = useMutation({
104
+ mutationFn: () => {
105
+ const window = buildPlanWindow(mode);
106
+ return createWorkspaceTaskPlan(workspaceId, {
107
+ title: planTitle.trim() || getDefaultPlanTitle(mode),
108
+ period_type: mode,
109
+ period_start: window.start,
110
+ period_end: window.end,
111
+ timezone: Intl.DateTimeFormat().resolvedOptions().timeZone || 'UTC',
112
+ default_target_ws_id: targetWorkspaceId,
113
+ default_target_board_id: targetBoardId || null,
114
+ default_target_list_id: targetListId || null,
115
+ intended_workspace_ids: [targetWorkspaceId],
116
+ });
117
+ },
118
+ onSuccess: (response) => {
119
+ if (isTaskPlanSchemaUnavailable(response)) return;
120
+ setSelectedPlanId(response.plan.id);
121
+ setPlanTitle('');
122
+ void queryClient.invalidateQueries({ queryKey: plansQueryKey });
123
+ },
124
+ onError: () => toast.error(tCommon('error')),
125
+ });
126
+ const updatePlanMutation = useMutation({
127
+ mutationFn: () => {
128
+ if (!selectedPlan) throw new Error('Missing plan');
129
+ const window = buildPlanWindow(editMode);
130
+ return updateWorkspaceTaskPlan(workspaceId, selectedPlan.id, {
131
+ title: editTitle.trim() || getDefaultPlanTitle(editMode),
132
+ period_type: editMode,
133
+ period_start: window.start,
134
+ period_end: window.end,
135
+ timezone: selectedPlan.timezone || 'UTC',
136
+ status: editStatus,
137
+ default_target_ws_id: targetWorkspaceId,
138
+ default_target_board_id: targetBoardId || null,
139
+ default_target_list_id: targetListId || null,
140
+ });
141
+ },
142
+ onSuccess: (response) => {
143
+ if (isTaskPlanSchemaUnavailable(response)) return;
144
+ void queryClient.invalidateQueries({ queryKey: plansQueryKey });
145
+ },
146
+ onError: () => toast.error(tCommon('error')),
147
+ });
148
+ const addWorkspaceMutation = useMutation({
149
+ mutationFn: () => {
150
+ if (!selectedPlan) throw new Error('Missing plan');
151
+ return addWorkspaceTaskPlanWorkspace(
152
+ workspaceId,
153
+ selectedPlan.id,
154
+ targetWorkspaceId
155
+ );
156
+ },
157
+ onSuccess: () => queryClient.invalidateQueries({ queryKey: plansQueryKey }),
158
+ onError: () => toast.error(tCommon('error')),
159
+ });
160
+ const createItemMutation = useMutation({
161
+ mutationFn: () => {
162
+ if (!selectedPlan || !taskTitle.trim()) throw new Error('Missing task');
163
+ const createSource = Boolean(targetIsIntended && targetListId);
164
+ return createWorkspaceTaskPlanItem(workspaceId, selectedPlan.id, {
165
+ target_ws_id: createSource ? targetWorkspaceId : null,
166
+ target_board_id: createSource ? targetBoardId : null,
167
+ target_list_id: createSource ? targetListId : null,
168
+ planned_start: plannedDate,
169
+ status: createSource ? 'planned' : 'draft',
170
+ snapshot_title: taskTitle.trim(),
171
+ source_task: createSource
172
+ ? {
173
+ name: taskTitle.trim(),
174
+ listId: targetListId,
175
+ end_date: plannedDate,
176
+ }
177
+ : undefined,
178
+ });
179
+ },
180
+ onSuccess: (response) => {
181
+ if (isTaskPlanSchemaUnavailable(response)) return;
182
+ setTaskTitle('');
183
+ void queryClient.invalidateQueries({ queryKey: plansQueryKey });
184
+ },
185
+ onError: () => toast.error(tCommon('error')),
186
+ });
187
+
188
+ return {
189
+ addWorkspaceMutation,
190
+ boards,
191
+ createItemMutation,
192
+ createPlanMutation,
193
+ editMode,
194
+ editStatus,
195
+ editTitle,
196
+ invalidatePlans: () =>
197
+ queryClient.invalidateQueries({ queryKey: plansQueryKey }),
198
+ lists,
199
+ mode,
200
+ planTitle,
201
+ plannedDate,
202
+ plans,
203
+ plansQuery,
204
+ plansQueryKey,
205
+ schemaUnavailable,
206
+ selectedPlan,
207
+ setEditMode,
208
+ setEditStatus,
209
+ setEditTitle,
210
+ setMode,
211
+ setPlannedDate,
212
+ setPlanTitle,
213
+ setSelectedPlanId,
214
+ setTargetBoardId: (value: string) => {
215
+ setTargetBoardId(value);
216
+ setTargetListId('');
217
+ },
218
+ setTargetListId,
219
+ setTargetWorkspaceId: (value: string) => {
220
+ setTargetWorkspaceId(value);
221
+ setTargetBoardId('');
222
+ setTargetListId('');
223
+ },
224
+ setTaskTitle,
225
+ targetBoardId,
226
+ targetIsIntended,
227
+ targetListId,
228
+ targetWorkspace,
229
+ targetWorkspaceId,
230
+ taskTitle,
231
+ updatePlanMutation,
232
+ workspaces: workspacesQuery.data ?? [],
233
+ };
234
+ }