-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.tsx
More file actions
178 lines (160 loc) · 4.77 KB
/
Copy pathindex.tsx
File metadata and controls
178 lines (160 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import * as Haptics from 'expo-haptics';
import { useCallback, useState } from 'react';
import { Dimensions, Platform, ScrollView } from 'react-native';
import { useAnimatedRef, useSharedValue } from 'react-native-reanimated';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import type {
OrderChangeCallback,
SortableGridRenderItem
} from 'react-native-sortables';
import Sortable from 'react-native-sortables';
import { spacing } from '@/theme';
import { SEPARATOR } from './constants';
import PlannedTasksHeader from './PlannedTasksHeader';
import SectionHeader from './SectionHeader';
import TaskCard from './TaskCard';
import type { Task } from './types';
import { calculateTotalDurations } from './utils';
const START_TIME_MINUTES = 9 * 60; // 9:00 AM
// Assume this is the initial data from the backend
export const DATA: Array<Task | typeof SEPARATOR> = [
// scheduled block
{ duration: 30, icon: '🍳', id: 'e1a4b3d2', title: 'Breakfast' },
SEPARATOR, // separates scheduled tasks from inbox tasks
// inbox tasks
{
duration: 45,
icon: '🔍',
id: 'c7f5e201',
title: 'Code review pull requests'
},
{
duration: 180,
icon: '💻',
id: 'd9a8c602',
title: 'Implement new auth feature'
},
{ duration: 30, icon: '🎨', id: 'ab34d591', title: 'Sync with UX designer' },
{ duration: 30, icon: '🚀', id: 'f3c6b820', title: 'Deploy staging build' },
{
duration: 60,
icon: '🤝',
id: 'bb9d4e71',
title: 'Interview frontend candidate'
},
{
duration: 45,
icon: '📋',
id: 'a482c930',
title: 'Sprint backlog grooming'
},
{
duration: 60,
icon: '📊',
id: 'ce57af12',
title: 'Prepare monthly HR metrics'
},
{ duration: 40, icon: '🔐', id: 'd0e74623', title: 'Review security patch' },
{ duration: 60, icon: '🍱', id: 'f72e1c88', title: 'Lunch with team' },
{
duration: 60,
icon: '🏢',
id: 'a2b5d7e4',
title: 'Company all-hands meeting'
}
];
const ITEM_DURATIONS = DATA.reduce(
(acc, item) => {
if (item === SEPARATOR) {
return acc;
}
acc[item.id] = item.duration;
return acc;
},
{} as Record<string, number>
);
const INITIAL_TOTAL_DURATIONS = calculateTotalDurations(
(DATA.slice(0, DATA.indexOf(SEPARATOR)) as Array<Task>).map(({ id }) => id),
ITEM_DURATIONS
);
const SCREEN_HEIGHT = Dimensions.get('window').height;
type Item = (typeof DATA)[number];
export default function TaskPlanner() {
const [data, setData] = useState(DATA);
const insets = useSafeAreaInsets();
const scrollableRef = useAnimatedRef<ScrollView>();
const totalDurations = useSharedValue<Record<string, number>>(
INITIAL_TOTAL_DURATIONS
);
const handleOrderChange = useCallback<OrderChangeCallback>(
({ fromIndex, indexToKey, keyToIndex, toIndex }) => {
totalDurations.value = calculateTotalDurations(
indexToKey,
ITEM_DURATIONS
);
const separatorIndex = keyToIndex[SEPARATOR];
// If the active item becomes selected
if (toIndex === separatorIndex) {
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Heavy);
}
// If the active item is deselected
else if (fromIndex === separatorIndex) {
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Heavy);
} else {
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
}
},
[totalDurations]
);
const renderItem = useCallback<SortableGridRenderItem<Item>>(
({ item }) => {
if (item === SEPARATOR) {
return <SectionHeader title='Inbox Tasks' />;
}
return (
<TaskCard
{...item}
startTimeMinutes={START_TIME_MINUTES}
totalDurations={totalDurations}
/>
);
},
[totalDurations]
);
return (
<ScrollView
ref={scrollableRef}
style={Platform.OS === 'web' && { overflowY: 'scroll' }}
contentContainerStyle={{
paddingBottom: insets.bottom + spacing.md,
paddingHorizontal: spacing.md
}}>
<PlannedTasksHeader
startTimeMinutes={START_TIME_MINUTES}
totalDurations={totalDurations}
/>
<Sortable.Grid
activeItemScale={1.03}
data={data}
dragActivationDelay={0}
overDrag='vertical'
renderItem={renderItem}
rowGap={spacing.sm}
scrollableRef={scrollableRef}
autoScrollActivationOffset={[
0.2 * SCREEN_HEIGHT,
0.075 * SCREEN_HEIGHT
]}
customHandle
onOrderChange={handleOrderChange}
onDragEnd={({ data: newData }) => {
setData(newData);
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
}}
onDragStart={() =>
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium)
}
/>
</ScrollView>
);
}