Mailspring/app/internal_packages/account-sidebar/lib/sidebar-item.ts
Ben Gotow 149b389508
Replace Babel with TypeScript compiler, switch entire app to TypeScript 🎉 (#1404)
* Switch to using Typescript instead of Babel

* Switch all es6 / jsx file extensions to ts / tsx

* Convert Utils to a TS module from module.exports style module

* Move everything from module.exports to typescript exports

* Define .d.ts files for mailspring-exports and component kit… Yes it seems this is the best option :(

* Load up on those @types

* Synthesize TS types from PropTypes for standard components

* Add types to Model classes and move constructor constants to instance vars

* 9800 => 7700 TS errors

* 7700 => 5600 TS errors

* 5600 => 5330 TS errors

* 5330 => 4866 TS errors

* 4866 => 4426 TS errors

* 4426 => 2411 TS errors

* 2411 > 1598 TS errors

* 1598 > 769 TS errors

* 769 > 129 TS errors

* 129 > 22 TS errors

* Fix runtime errors

* More runtime error fixes

* Remove support for custom .es6 file extension

* Remove a few odd remaining references to Nylas

* Don’t ship Typescript support in the compiled app for now

* Fix issues in compiled app - module resolution in TS is case sensitive?

* README updates

* Fix a few more TS errors

* Make “No Signature” option clickable + selectable

* Remove flicker when saving file and reloading keymaps

* Fix mail rule item height in preferences

* Fix missing spacing in thread sharing popover

* Fix scrollbar ticks being nested incorrectly

* Add Japanese as a manually reviewed language

* Prevent the thread list from “sticking”

* Re-use Sheet when switching root tabs, prevent sidebar from resetting

* Ensure specs run

* Update package configuration to avoid shpping types

* Turn eslint back on - we will opt-in to the TS rules one by one
2019-03-04 11:03:12 -08:00

215 lines
6.2 KiB
TypeScript

import _ from 'underscore';
import _str from 'underscore.string';
import { OutlineViewItem } from 'mailspring-component-kit';
import {
MailboxPerspective,
FocusedPerspectiveStore,
SyncbackCategoryTask,
DestroyCategoryTask,
CategoryStore,
Actions,
RegExpUtils,
} from 'mailspring-exports';
import * as SidebarActions from './sidebar-actions';
import { ISidebarItem } from './types';
const idForCategories = categories => _.pluck(categories, 'id').join('-');
const countForItem = function(perspective) {
const unreadCountEnabled = AppEnv.config.get('core.workspace.showUnreadForAllCategories');
if (perspective.isInbox() || unreadCountEnabled) {
return perspective.unreadCount();
}
return 0;
};
const isItemSelected = perspective => FocusedPerspectiveStore.current().isEqual(perspective);
const isItemCollapsed = function(id) {
if (AppEnv.savedState.sidebarKeysCollapsed[id] !== undefined) {
return AppEnv.savedState.sidebarKeysCollapsed[id];
} else {
return true;
}
};
const toggleItemCollapsed = function(item) {
if (!(item.children.length > 0)) {
return;
}
SidebarActions.setKeyCollapsed(item.id, !isItemCollapsed(item.id));
};
const onDeleteItem = function(item) {
// TODO Delete multiple categories at once
if (item.deleted === true) {
return;
}
const category = item.perspective.category();
if (!category) {
return;
}
Actions.queueTask(
new DestroyCategoryTask({
path: category.path,
accountId: category.accountId,
})
);
};
const onEditItem = function(item, value) {
let newDisplayName;
if (!value) {
return;
}
if (item.deleted === true) {
return;
}
const category = item.perspective.category();
if (!category) {
return;
}
const re = RegExpUtils.subcategorySplitRegex();
let match = re.exec(category.displayName);
let lastMatch = match;
while (match) {
lastMatch = match;
match = re.exec(category.displayName);
}
if (lastMatch) {
newDisplayName = category.displayName.slice(0, lastMatch.index + 1) + value;
} else {
newDisplayName = value;
}
if (newDisplayName === category.displayName) {
return;
}
Actions.queueTask(
SyncbackCategoryTask.forRenaming({
accountId: category.accountId,
path: category.path,
newName: newDisplayName,
})
);
};
export default class SidebarItem {
static forPerspective(id, perspective, opts: Partial<ISidebarItem> = {}): ISidebarItem {
let counterStyle;
if (perspective.isInbox()) {
counterStyle = OutlineViewItem.CounterStyles.Alt;
}
const collapsed = isItemCollapsed(id);
return Object.assign(
{
id,
name: perspective.name,
contextMenuLabel: perspective.name,
count: countForItem(perspective),
iconName: perspective.iconName,
children: [],
perspective,
selected: isItemSelected(perspective),
collapsed: collapsed != null ? collapsed : true,
counterStyle,
onDelete: opts.deletable ? onDeleteItem : undefined,
onEdited: opts.editable ? onEditItem : undefined,
onCollapseToggled: toggleItemCollapsed,
onDrop(item, event) {
const jsonString = event.dataTransfer.getData('mailspring-threads-data');
let jsonData = null;
try {
jsonData = JSON.parse(jsonString);
} catch (err) {
console.error(`JSON parse error: ${err}`);
}
if (!jsonData) {
return;
}
item.perspective.receiveThreadIds(jsonData.threadIds);
},
shouldAcceptDrop(item, event) {
const target = item.perspective;
const current = FocusedPerspectiveStore.current();
if (!event.dataTransfer.types.includes('mailspring-threads-data')) {
return false;
}
if (target.isEqual(current)) {
return false;
}
// We can't inspect the drag payload until drop, so we use a dataTransfer
// type to encode the account IDs of threads currently being dragged.
const accountsType = event.dataTransfer.types.find(t => t.startsWith('mailspring-accounts='));
const accountIds = (accountsType || '').replace('mailspring-accounts=', '').split(',');
return target.canReceiveThreadsFromAccountIds(accountIds);
},
onSelect(item) {
Actions.focusMailboxPerspective(item.perspective);
},
},
opts
);
}
static forCategories(categories = [], opts: Partial<ISidebarItem> = {}) {
const id = idForCategories(categories);
const contextMenuLabel = _str.capitalize(
categories[0] != null ? categories[0].displayType() : undefined
);
const perspective = MailboxPerspective.forCategories(categories);
if (opts.deletable == null) {
opts.deletable = true;
}
if (opts.editable == null) {
opts.editable = true;
}
opts.contextMenuLabel = contextMenuLabel;
return this.forPerspective(id, perspective, opts);
}
static forStarred(accountIds, opts: Partial<ISidebarItem> = {}) {
const perspective = MailboxPerspective.forStarred(accountIds);
let id = 'Starred';
if (opts.name) {
id += `-${opts.name}`;
}
return this.forPerspective(id, perspective, opts);
}
static forUnread(accountIds, opts: Partial<ISidebarItem> = {}) {
let categories = accountIds.map(accId => {
return CategoryStore.getCategoryByRole(accId, 'inbox');
});
// NOTE: It's possible for an account to not yet have an `inbox`
// category. Since the `SidebarStore` triggers on `AccountStore`
// changes, it'll trigger the exact moment an account is added to the
// config. However, the API has not yet come back with the list of
// `categories` for that account.
categories = _.compact(categories);
const perspective = MailboxPerspective.forUnread(categories);
let id = 'Unread';
if (opts.name) {
id += `-${opts.name}`;
}
return this.forPerspective(id, perspective, opts);
}
static forDrafts(accountIds, opts: Partial<ISidebarItem> = {}) {
const perspective = MailboxPerspective.forDrafts(accountIds);
const id = `Drafts-${opts.name}`;
return this.forPerspective(id, perspective, opts);
}
}