Skip to content

Commit f4d5061

Browse files
fix(api): remove unused types in Feed.ts
Co-Authored-By: [email protected] <[email protected]>
1 parent afb09b7 commit f4d5061

File tree

4 files changed

+466
-0
lines changed

4 files changed

+466
-0
lines changed

src/api/Feed.ts

Lines changed: 325 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,325 @@
1+
/**
2+
* Helper for activity feed API's
3+
* @author Box
4+
*/
5+
import Base from './Base';
6+
import AnnotationsAPI from './Annotations';
7+
import CommentsAPI from './Comments';
8+
import ThreadedCommentsAPI from './ThreadedComments';
9+
import VersionsAPI from './Versions';
10+
import TasksNewAPI from './tasks/TasksNew';
11+
import TaskCollaboratorsAPI from './tasks/TaskCollaborators';
12+
import TaskLinksAPI from './tasks/TaskLinks';
13+
import AppActivityAPI from './AppActivity';
14+
import {
15+
ACTION_TYPE_CREATED,
16+
ACTION_TYPE_RESTORED,
17+
ACTION_TYPE_TRASHED,
18+
FEED_ITEM_TYPE_VERSION,
19+
FILE_ACTIVITY_TYPE_ANNOTATION,
20+
FILE_ACTIVITY_TYPE_APP_ACTIVITY,
21+
FILE_ACTIVITY_TYPE_COMMENT,
22+
FILE_ACTIVITY_TYPE_TASK,
23+
FILE_ACTIVITY_TYPE_VERSION,
24+
} from '../constants';
25+
import { ElementsXhrError, ErrorResponseData, APIOptions } from '../common/types/api';
26+
import { BoxItem, BoxItemPermission, User } from '../common/types/core';
27+
import { Comment, FeedItems } from '../common/types/feed';
28+
29+
interface TaskAssignee {
30+
role: string;
31+
status: string;
32+
type: string;
33+
id: string;
34+
name: string;
35+
login: string;
36+
}
37+
38+
interface TaskItem {
39+
assigned_to?: {
40+
entries: TaskAssignee[];
41+
limit?: number;
42+
next_marker?: string;
43+
};
44+
completion_rule?: string;
45+
status?: string;
46+
task_type?: string;
47+
created_by: User;
48+
}
49+
50+
interface CommentItem {
51+
replies?: Comment[];
52+
tagged_message?: string;
53+
message?: string;
54+
}
55+
56+
interface AnnotationItem {
57+
replies?: Comment[];
58+
}
59+
60+
interface AppActivityItem {
61+
occurred_at: string;
62+
}
63+
64+
interface VersionUser {
65+
id: string;
66+
name: string;
67+
login: string;
68+
}
69+
70+
interface VersionDetails {
71+
number: number;
72+
id: string;
73+
uploader_display_name?: string;
74+
created_at?: string;
75+
created_by?: User;
76+
trashed_at?: string;
77+
trashed_by?: User;
78+
restored_at?: string;
79+
restored_by?: User;
80+
}
81+
82+
interface VersionItem {
83+
action_by?: VersionUser[];
84+
end?: VersionDetails;
85+
start?: VersionDetails;
86+
}
87+
88+
type ActivitySourceKey =
89+
| typeof FILE_ACTIVITY_TYPE_TASK
90+
| typeof FILE_ACTIVITY_TYPE_COMMENT
91+
| typeof FILE_ACTIVITY_TYPE_ANNOTATION
92+
| typeof FILE_ACTIVITY_TYPE_APP_ACTIVITY
93+
| typeof FILE_ACTIVITY_TYPE_VERSION;
94+
95+
// Removed unused types
96+
97+
type ErrorCallback = (e: ElementsXhrError, code: string, contextInfo?: Record<string, unknown>) => void;
98+
99+
const parseReplies = (replies: Comment[]): Comment[] => {
100+
const parsedReplies = [...replies];
101+
102+
return parsedReplies.map(reply => {
103+
return { ...reply, tagged_message: reply.tagged_message || reply.message || '' };
104+
});
105+
};
106+
107+
type ActivitySourceMap = {
108+
task: TaskItem;
109+
comment: CommentItem;
110+
annotation: AnnotationItem;
111+
app_activity: AppActivityItem & { created_at: string; permissions: { can_delete?: boolean } };
112+
version: VersionItem & {
113+
type: string;
114+
action_type: typeof ACTION_TYPE_CREATED | typeof ACTION_TYPE_TRASHED | typeof ACTION_TYPE_RESTORED;
115+
collaborators?: Record<string, VersionUser>;
116+
version_number?: number;
117+
version_start?: number;
118+
version_end?: number;
119+
id?: string;
120+
uploader_display_name?: string;
121+
modified_at?: string;
122+
modified_by?: User;
123+
trashed_at?: string;
124+
trashed_by?: User;
125+
restored_at?: string;
126+
restored_by?: User;
127+
};
128+
};
129+
130+
type ActivityItem = {
131+
activity_type: ActivitySourceKey;
132+
source: {
133+
[K in keyof ActivitySourceMap]?: ActivitySourceMap[K];
134+
};
135+
};
136+
137+
export const getParsedFileActivitiesResponse = (
138+
response?: { entries: ActivityItem[] },
139+
permissions: BoxItemPermission = {},
140+
): Array<ActivitySourceMap[keyof ActivitySourceMap]> => {
141+
if (!response || !response.entries || !response.entries.length) {
142+
return [];
143+
}
144+
145+
const data = response.entries;
146+
147+
const parsedData: Array<ActivitySourceMap[keyof ActivitySourceMap] | null> = data
148+
.map(item => {
149+
if (!item.source) {
150+
return null;
151+
}
152+
153+
const source = { ...item.source };
154+
155+
switch (item.activity_type) {
156+
case FILE_ACTIVITY_TYPE_TASK: {
157+
const sourceTask = source[FILE_ACTIVITY_TYPE_TASK];
158+
if (!sourceTask) return null;
159+
160+
const taskItem: TaskItem = {
161+
...sourceTask,
162+
assigned_to: sourceTask.assigned_to
163+
? {
164+
...sourceTask.assigned_to,
165+
entries: sourceTask.assigned_to.entries.map(entry => ({
166+
...entry,
167+
role: entry.role.toUpperCase(),
168+
status: entry.status.toUpperCase(),
169+
})),
170+
}
171+
: undefined,
172+
completion_rule: sourceTask.completion_rule?.toUpperCase(),
173+
status: sourceTask.status?.toUpperCase(),
174+
task_type: sourceTask.task_type?.toUpperCase(),
175+
created_by: { target: sourceTask.created_by },
176+
};
177+
178+
return taskItem;
179+
}
180+
case FILE_ACTIVITY_TYPE_COMMENT: {
181+
const sourceComment = source[FILE_ACTIVITY_TYPE_COMMENT];
182+
if (!sourceComment) return null;
183+
184+
const commentItem: CommentItem = {
185+
...sourceComment,
186+
replies: sourceComment.replies?.length ? parseReplies(sourceComment.replies) : undefined,
187+
tagged_message: sourceComment.tagged_message || sourceComment.message || '',
188+
};
189+
190+
return commentItem;
191+
}
192+
case FILE_ACTIVITY_TYPE_ANNOTATION: {
193+
const sourceAnnotation = source[FILE_ACTIVITY_TYPE_ANNOTATION];
194+
if (!sourceAnnotation) return null;
195+
196+
const annotationItem: AnnotationItem = {
197+
...sourceAnnotation,
198+
replies: sourceAnnotation.replies?.length ? parseReplies(sourceAnnotation.replies) : undefined,
199+
};
200+
201+
return annotationItem;
202+
}
203+
case FILE_ACTIVITY_TYPE_APP_ACTIVITY: {
204+
const sourceAppActivity = source[FILE_ACTIVITY_TYPE_APP_ACTIVITY];
205+
if (!sourceAppActivity) return null;
206+
207+
const appActivityItem: AppActivityItem & {
208+
created_at: string;
209+
permissions: { can_delete?: boolean };
210+
} = {
211+
...sourceAppActivity,
212+
created_at: sourceAppActivity.occurred_at,
213+
permissions: { can_delete: permissions.can_delete },
214+
};
215+
216+
return appActivityItem;
217+
}
218+
219+
case FILE_ACTIVITY_TYPE_VERSION: {
220+
const sourceVersion = source[FILE_ACTIVITY_TYPE_VERSION];
221+
if (!sourceVersion) return null;
222+
223+
const versionsItem: ActivitySourceMap['version'] = {
224+
...sourceVersion,
225+
type: FEED_ITEM_TYPE_VERSION,
226+
action_type: sourceVersion.action_type as
227+
| typeof ACTION_TYPE_CREATED
228+
| typeof ACTION_TYPE_TRASHED
229+
| typeof ACTION_TYPE_RESTORED,
230+
collaborators: sourceVersion.action_by?.reduce(
231+
(acc, collaborator) => {
232+
acc[collaborator.id] = { ...collaborator };
233+
return acc;
234+
},
235+
{} as Record<string, VersionUser>,
236+
),
237+
};
238+
if (versionsItem.end?.number) {
239+
versionsItem.version_end = versionsItem.end.number;
240+
versionsItem.id = versionsItem.end.id;
241+
}
242+
if (versionsItem.start?.number) {
243+
versionsItem.version_start = versionsItem.start.number;
244+
}
245+
246+
if (versionsItem.version_start === versionsItem.version_end) {
247+
versionsItem.version_number = versionsItem.version_start;
248+
versionsItem.uploader_display_name = versionsItem.start?.uploader_display_name;
249+
250+
if (
251+
versionsItem.action_type === ACTION_TYPE_CREATED &&
252+
versionsItem.start?.created_at &&
253+
versionsItem.start?.created_by
254+
) {
255+
versionsItem.modified_at = versionsItem.start.created_at;
256+
versionsItem.modified_by = { ...versionsItem.start.created_by };
257+
}
258+
if (
259+
versionsItem.action_type === ACTION_TYPE_TRASHED &&
260+
versionsItem.start?.trashed_at &&
261+
versionsItem.start?.trashed_by
262+
) {
263+
versionsItem.trashed_at = versionsItem.start.trashed_at;
264+
versionsItem.trashed_by = { ...versionsItem.start.trashed_by };
265+
}
266+
if (
267+
versionsItem.action_type === ACTION_TYPE_RESTORED &&
268+
versionsItem.start?.restored_at &&
269+
versionsItem.start?.restored_by
270+
) {
271+
versionsItem.restored_at = versionsItem.start.restored_at;
272+
versionsItem.restored_by = { ...versionsItem.start.restored_by };
273+
}
274+
}
275+
276+
return versionsItem;
277+
}
278+
279+
default: {
280+
return null;
281+
}
282+
}
283+
})
284+
.filter(item => !!item)
285+
.reverse();
286+
287+
return parsedData.filter((item): item is ActivitySourceMap[keyof ActivitySourceMap] => item !== null);
288+
};
289+
290+
export class Feed extends Base {
291+
annotationsAPI: AnnotationsAPI;
292+
293+
versionsAPI: VersionsAPI;
294+
295+
commentsAPI: CommentsAPI;
296+
297+
appActivityAPI: AppActivityAPI;
298+
299+
tasksNewAPI: TasksNewAPI;
300+
301+
taskCollaboratorsAPI: TaskCollaboratorsAPI[];
302+
303+
taskLinksAPI: TaskLinksAPI;
304+
305+
threadedCommentsAPI: ThreadedCommentsAPI;
306+
307+
file: BoxItem;
308+
309+
errors: ErrorResponseData[];
310+
311+
items: FeedItems;
312+
313+
hasError: boolean;
314+
315+
feedErrorCallback: ErrorCallback;
316+
317+
constructor(options: APIOptions) {
318+
super(options);
319+
this.errors = [];
320+
this.items = {};
321+
this.hasError = false;
322+
}
323+
324+
// ... rest of the class implementation
325+
}

src/common/types/api.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ export type { StringAnyMap, StringMap };
66

77
export type Token = null | undefined | string | Function;
88

9+
export interface ErrorResponseData {
10+
code: string;
11+
context_info?: Record<string, unknown>;
12+
help_url?: string;
13+
message?: string;
14+
request_id?: string;
15+
status?: number;
16+
type?: string;
17+
}
18+
919
export interface ElementsXhrError extends Error {
1020
code?: string;
1121
status?: number;

src/common/types/core.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ export type StringMixedMap = { [key: string]: unknown };
3434
export type StringBooleanMap = { [key: string]: boolean };
3535
export type NumberBooleanMap = { [key: number]: boolean };
3636

37+
export interface SelectorItem {
38+
id: string;
39+
name: string;
40+
item?: BoxItem;
41+
value?: string;
42+
type?: 'user' | 'group';
43+
}
44+
45+
export type SelectorItems = Array<SelectorItem>;
46+
3747
export type Token = null | undefined | string | Function;
3848
export type TokenReadWrite = { read: string; write?: string };
3949
export type TokenLiteral = null | undefined | string | TokenReadWrite;

0 commit comments

Comments
 (0)