Skip to content

Commit

Permalink
[MM-199] Convert files related to redux from js to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
ayusht2810 committed Feb 13, 2024
1 parent d8fb001 commit a7ba497
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 139 deletions.
File renamed without changes.
86 changes: 44 additions & 42 deletions webapp/src/actions/index.js → webapp/src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import {DispatchFunc, GetStateFunc} from 'mattermost-redux/types/actions';

import {GlobalState} from '../types/store';

import Client from '../client';
import ActionTypes from '../action_types';

import {id as pluginId} from '../manifest';

export function getConnected(reminder = false) {
return async (dispatch) => {
return async (dispatch: DispatchFunc) => {
let data;
try {
data = await Client.getConnected(reminder);
Expand All @@ -24,8 +26,8 @@ export function getConnected(reminder = false) {
};
}

function checkAndHandleNotConnected(data) {
return async (dispatch) => {
function checkAndHandleNotConnected(data: {id: string}) {
return async (dispatch: DispatchFunc) => {
if (data && data.id === 'not_connected') {
dispatch({
type: ActionTypes.RECEIVED_CONNECTED,
Expand All @@ -42,16 +44,16 @@ function checkAndHandleNotConnected(data) {
};
}

export function getReviewsDetails(prList) {
return async (dispatch, getState) => {
export function getReviewsDetails(prList: PrsDetailsData[]) {
return async (dispatch: DispatchFunc) => {
let data;
try {
data = await Client.getPrsDetails(prList);
} catch (error) {
return {error};
}

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
const connected = await checkAndHandleNotConnected(data)(dispatch);
if (!connected) {
return {error: data};
}
Expand All @@ -66,15 +68,15 @@ export function getReviewsDetails(prList) {
}

export function getRepos() {
return async (dispatch, getState) => {
return async (dispatch: DispatchFunc) => {
let data;
try {
data = await Client.getRepositories();
} catch (error) {
return {error: data};
}

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
const connected = await checkAndHandleNotConnected(data)(dispatch);
if (!connected) {
return {error: data};
}
Expand All @@ -89,15 +91,15 @@ export function getRepos() {
}

export function getSidebarContent() {
return async (dispatch, getState) => {
return async (dispatch: DispatchFunc) => {
let data;
try {
data = await Client.getSidebarContent();
} catch (error) {
return {error};
}

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
const connected = await checkAndHandleNotConnected(data)(dispatch);
if (!connected) {
return {error: data};
}
Expand All @@ -111,16 +113,16 @@ export function getSidebarContent() {
};
}

export function getYourPrsDetails(prList) {
return async (dispatch, getState) => {
export function getYourPrsDetails(prList: PrsDetailsData[]) {
return async (dispatch: DispatchFunc) => {
let data;
try {
data = await Client.getPrsDetails(prList);
} catch (error) {
return {error};
}

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
const connected = await checkAndHandleNotConnected(data)(dispatch);
if (!connected) {
return {error: data};
}
Expand All @@ -134,16 +136,16 @@ export function getYourPrsDetails(prList) {
};
}

export function getLabelOptions(repo) {
return async (dispatch, getState) => {
export function getLabelOptions(repo: string) {
return async (dispatch: DispatchFunc) => {
let data;
try {
data = await Client.getLabels(repo);
} catch (error) {
return {error};
}

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
const connected = await checkAndHandleNotConnected(data)(dispatch);
if (!connected) {
return {error: data};
}
Expand All @@ -152,16 +154,16 @@ export function getLabelOptions(repo) {
};
}

export function getAssigneeOptions(repo) {
return async (dispatch, getState) => {
export function getAssigneeOptions(repo: string) {
return async (dispatch: DispatchFunc) => {
let data;
try {
data = await Client.getAssignees(repo);
} catch (error) {
return {error};
}

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
const connected = await checkAndHandleNotConnected(data)(dispatch);
if (!connected) {
return {error: data};
}
Expand All @@ -170,16 +172,16 @@ export function getAssigneeOptions(repo) {
};
}

export function getMilestoneOptions(repo) {
return async (dispatch, getState) => {
export function getMilestoneOptions(repo: string) {
return async (dispatch: DispatchFunc) => {
let data;
try {
data = await Client.getMilestones(repo);
} catch (error) {
return {error};
}

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
const connected = await checkAndHandleNotConnected(data)(dispatch);
if (!connected) {
return {error: data};
}
Expand All @@ -189,15 +191,15 @@ export function getMilestoneOptions(repo) {
}

export function getMentions() {
return async (dispatch, getState) => {
return async (dispatch: DispatchFunc) => {
let data;
try {
data = await Client.getMentions();
} catch (error) {
return {error};
}

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
const connected = await checkAndHandleNotConnected(data)(dispatch);
if (!connected) {
return {error: data};
}
Expand All @@ -213,13 +215,13 @@ export function getMentions() {

const GITHUB_USER_GET_TIMEOUT_MILLISECONDS = 1000 * 60 * 60; // 1 hour

export function getGitHubUser(userID) {
return async (dispatch, getState) => {
export function getGitHubUser(userID: string) {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
if (!userID) {
return {};
}

const user = getState()[`plugins-${pluginId}`].githubUsers[userID];
const user = (getState() as GlobalState)['plugins-github'].githubUsers[userID];
if (user && user.last_try && Date.now() - user.last_try < GITHUB_USER_GET_TIMEOUT_MILLISECONDS) {
return {};
}
Expand All @@ -231,8 +233,8 @@ export function getGitHubUser(userID) {
let data;
try {
data = await Client.getGitHubUser(userID);
} catch (error) {
if (error.status === 404) {
} catch (error: unknown) {
if ((error as APIError).status_code === 404) {
dispatch({
type: ActionTypes.RECEIVED_GITHUB_USER,
userID,
Expand All @@ -256,21 +258,21 @@ export function getGitHubUser(userID) {
* Stores`showRHSPlugin` action returned by
* registerRightHandSidebarComponent in plugin initialization.
*/
export function setShowRHSAction(showRHSPluginAction) {
export function setShowRHSAction(showRHSPluginAction: ShowRhsPluginActionData) {
return {
type: ActionTypes.RECEIVED_SHOW_RHS_ACTION,
showRHSPluginAction,
};
}

export function updateRhsState(rhsState) {
export function updateRhsState(rhsState: string) {
return {
type: ActionTypes.UPDATE_RHS_STATE,
state: rhsState,
};
}

export function openCreateIssueModal(postId) {
export function openCreateIssueModal(postId: string) {
return {
type: ActionTypes.OPEN_CREATE_ISSUE_MODAL,
data: {
Expand All @@ -279,7 +281,7 @@ export function openCreateIssueModal(postId) {
};
}

export function openCreateIssueModalWithoutPost(title, channelId) {
export function openCreateIssueModalWithoutPost(title: string, channelId: string) {
return {
type: ActionTypes.OPEN_CREATE_ISSUE_MODAL_WITHOUT_POST,
data: {
Expand All @@ -295,16 +297,16 @@ export function closeCreateIssueModal() {
};
}

export function createIssue(payload) {
return async (dispatch) => {
export function createIssue(payload: CreateIssuePayload) {
return async (dispatch: DispatchFunc) => {
let data;
try {
data = await Client.createIssue(payload);
} catch (error) {
return {error};
}

const connected = await dispatch(checkAndHandleNotConnected(data));
const connected = await checkAndHandleNotConnected(data);
if (!connected) {
return {error: data};
}
Expand All @@ -313,7 +315,7 @@ export function createIssue(payload) {
};
}

export function openAttachCommentToIssueModal(postId) {
export function openAttachCommentToIssueModal(postId: string) {
return {
type: ActionTypes.OPEN_ATTACH_COMMENT_TO_ISSUE_MODAL,
data: {
Expand All @@ -328,16 +330,16 @@ export function closeAttachCommentToIssueModal() {
};
}

export function attachCommentToIssue(payload) {
return async (dispatch) => {
export function attachCommentToIssue(payload: AttachCommentToIssuePayload) {
return async (dispatch: DispatchFunc) => {
let data;
try {
data = await Client.attachCommentToIssue(payload);
} catch (error) {
return {error};
}

const connected = await dispatch(checkAndHandleNotConnected(data));
const connected = await checkAndHandleNotConnected(data);
if (!connected) {
return {error: data};
}
Expand Down
58 changes: 1 addition & 57 deletions webapp/src/components/sidebar_right/github_items.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,62 +33,6 @@ const notificationReasons = {
team_mention: 'You were on a team that was mentioned.',
};

interface Label {
id: number;
name: string;
color: CSS.Properties;
}

interface User {
login: string;
}

interface Review {
state: string;
user: User;
}

interface Item {
url: string;
number: number;

id: number;
title: string;
created_at: string;
updated_at: string;
html_url: string;
repository_url?: string;
user: User;
owner?: User;
milestone?: {
title: string;
}
repository?: {
full_name: string;
}
labels?: Label[];

// PRs
status?: string;
mergeable?: boolean;
requestedReviewers?: string[];
reviews?: Review[];

// Assignments
pullRequest?: unknown;

// Notifications
subject?: {
title: string;
}
reason?: keyof typeof notificationReasons;
}

interface GithubItemsProps {
items: Item[];
theme: Theme;
}

function GithubItems(props: GithubItemsProps) {
const style = getStyle(props.theme);

Expand Down Expand Up @@ -306,7 +250,7 @@ function GithubItems(props: GithubItemsProps) {
{item.reason ? (<>
{(item.created_at || userName || milestone) && (<br/>)}
{item.updated_at && (formatTimeSince(item.updated_at) + ' ago')}{<br/>}
{notificationReasons[item.reason]}
{notificationReasons[item.reason as keyof typeof notificationReasons]}
</>) : null }
</div>
{reviews}
Expand Down
Loading

0 comments on commit a7ba497

Please sign in to comment.