From cb7976bd3d5bbb031de671f991cf269e0039bfe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Sun, 12 May 2024 15:24:15 +0200 Subject: [PATCH] prefer arrow functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- scripts/do-release.ts | 4 +- scripts/lib/changelog.ts | 8 +-- src/actions/apps.ts | 33 +++++---- src/custom.ts | 4 +- src/is-mobile.ts | 12 ++-- src/ready.ts | 6 +- src/store.ts | 15 ++-- src/stream.ts | 147 ++++++++++++++++++++------------------- src/toast.tsx | 13 ++-- tailwind/colors.ts | 9 +-- vite.config.ts | 4 +- 11 files changed, 134 insertions(+), 121 deletions(-) diff --git a/scripts/do-release.ts b/scripts/do-release.ts index f7d2dc9be..a7674485f 100644 --- a/scripts/do-release.ts +++ b/scripts/do-release.ts @@ -13,7 +13,7 @@ const api = new Gitlab({ jobToken: CI_JOB_TOKEN, }); -async function main() { +const main = async () => { await api.Releases.create(CI_PROJECT_ID!, { name: CI_COMMIT_TAG, tag_name: CI_COMMIT_TAG, @@ -26,6 +26,6 @@ async function main() { }], }, }); -} +}; main(); \ No newline at end of file diff --git a/scripts/lib/changelog.ts b/scripts/lib/changelog.ts index d784ebd16..ab1912026 100644 --- a/scripts/lib/changelog.ts +++ b/scripts/lib/changelog.ts @@ -2,7 +2,7 @@ import fs from 'fs'; import { join } from 'path'; /** Parse the changelog into an object. */ -function parseChangelog(changelog: string): Record { +const parseChangelog = (changelog: string): Record => { const result: Record = {}; let currentVersion: string; @@ -16,15 +16,15 @@ function parseChangelog(changelog: string): Record { }); return result; -} +}; /** Get Markdown changes for a specific version. */ -function getChanges(version: string) { +const getChanges = (version: string) => { version = version.replace('v', ''); const content = fs.readFileSync(join(__dirname, '..', '..', 'CHANGELOG.md'), 'utf8'); const parsed = parseChangelog(content); return (parsed[version] || '').trim(); -} +}; export { parseChangelog, diff --git a/src/actions/apps.ts b/src/actions/apps.ts index 8ca8967be..7fd8146b1 100644 --- a/src/actions/apps.ts +++ b/src/actions/apps.ts @@ -10,16 +10,16 @@ import { getFetch } from '../api'; import type { AnyAction } from 'redux'; -export const APP_CREATE_REQUEST = 'APP_CREATE_REQUEST'; -export const APP_CREATE_SUCCESS = 'APP_CREATE_SUCCESS'; -export const APP_CREATE_FAIL = 'APP_CREATE_FAIL'; +const APP_CREATE_REQUEST = 'APP_CREATE_REQUEST'; +const APP_CREATE_SUCCESS = 'APP_CREATE_SUCCESS'; +const APP_CREATE_FAIL = 'APP_CREATE_FAIL'; -export const APP_VERIFY_CREDENTIALS_REQUEST = 'APP_VERIFY_CREDENTIALS_REQUEST'; -export const APP_VERIFY_CREDENTIALS_SUCCESS = 'APP_VERIFY_CREDENTIALS_SUCCESS'; -export const APP_VERIFY_CREDENTIALS_FAIL = 'APP_VERIFY_CREDENTIALS_FAIL'; +const APP_VERIFY_CREDENTIALS_REQUEST = 'APP_VERIFY_CREDENTIALS_REQUEST'; +const APP_VERIFY_CREDENTIALS_SUCCESS = 'APP_VERIFY_CREDENTIALS_SUCCESS'; +const APP_VERIFY_CREDENTIALS_FAIL = 'APP_VERIFY_CREDENTIALS_FAIL'; -export function createApp(params?: Record, baseURL?: string) { - return (dispatch: React.Dispatch) => { +const createApp = (params?: Record, baseURL?: string) => + (dispatch: React.Dispatch) => { dispatch({ type: APP_CREATE_REQUEST, params }); return getFetch(null, baseURL)('/api/v1/apps', { method: 'POST', body: JSON.stringify(params) }).then(({ json: app }) => { @@ -30,10 +30,9 @@ export function createApp(params?: Record, baseURL?: string) { throw error; }); }; -} -export function verifyAppCredentials(token: string) { - return (dispatch: React.Dispatch) => { +const verifyAppCredentials = (token: string) => + (dispatch: React.Dispatch) => { dispatch({ type: APP_VERIFY_CREDENTIALS_REQUEST, token }); return getFetch(token)('/api/v1/apps/verify_credentials').then(({ json: app }) => { dispatch({ type: APP_VERIFY_CREDENTIALS_SUCCESS, token, app }); @@ -43,4 +42,14 @@ export function verifyAppCredentials(token: string) { throw error; }); }; -} + +export { + APP_CREATE_REQUEST, + APP_CREATE_SUCCESS, + APP_CREATE_FAIL, + APP_VERIFY_CREDENTIALS_REQUEST, + APP_VERIFY_CREDENTIALS_SUCCESS, + APP_VERIFY_CREDENTIALS_FAIL, + createApp, + verifyAppCredentials, +}; diff --git a/src/custom.ts b/src/custom.ts index dcbcba547..375db2257 100644 --- a/src/custom.ts +++ b/src/custom.ts @@ -4,7 +4,7 @@ import * as BuildConfig from 'soapbox/build-config'; /** Require a custom JSON file if it exists */ -export const custom = (filename: string, fallback: any = {}): any => { +const custom = (filename: string, fallback: any = {}): any => { if (BuildConfig.NODE_ENV === 'test') return fallback; const modules = import.meta.glob('../custom/*.json', { eager: true }); @@ -12,3 +12,5 @@ export const custom = (filename: string, fallback: any = {}): any => { return modules[key] ? modules[key] : fallback; }; + +export { custom }; diff --git a/src/is-mobile.ts b/src/is-mobile.ts index cada4d479..e72513407 100644 --- a/src/is-mobile.ts +++ b/src/is-mobile.ts @@ -2,16 +2,14 @@ const LAYOUT_BREAKPOINT = 630; /** Check if the width is small enough to be considered "mobile". */ -export function isMobile(width: number) { - return width <= LAYOUT_BREAKPOINT; -} +const isMobile = (width: number) => width <= LAYOUT_BREAKPOINT; /** Whether the device is iOS (best guess). */ const iOS: boolean = /iPad|iPhone|iPod/.test(navigator.userAgent) && !(window as any).MSStream; -export const userTouching = window.matchMedia('(pointer: coarse)'); +const userTouching = window.matchMedia('(pointer: coarse)'); /** Whether the device is iOS (best guess). */ -export function isIOS(): boolean { - return iOS; -} +const isIOS = (): boolean => iOS; + +export { isMobile, userTouching, isIOS }; diff --git a/src/ready.ts b/src/ready.ts index 4ba42b1f4..7c9339a8d 100644 --- a/src/ready.ts +++ b/src/ready.ts @@ -1,7 +1,9 @@ -export default function ready(loaded: () => void): void { +const ready = (loaded: () => void): void => { if (['interactive', 'complete'].includes(document.readyState)) { loaded(); } else { document.addEventListener('DOMContentLoaded', loaded); } -} +}; + +export default ready; \ No newline at end of file diff --git a/src/store.ts b/src/store.ts index 4b1700e26..275c8d002 100644 --- a/src/store.ts +++ b/src/store.ts @@ -7,7 +7,7 @@ import appReducer from './reducers'; import type { AnyAction } from 'redux'; -export const store = configureStore({ +const store = configureStore({ reducer: appReducer, middleware: () => new Tuple( thunk, @@ -17,9 +17,16 @@ export const store = configureStore({ devTools: true, }); -export type Store = typeof store; +type Store = typeof store; // Infer the `RootState` and `AppDispatch` types from the store itself // https://redux.js.org/usage/usage-with-typescript -export type RootState = ReturnType; -export type AppDispatch = ThunkDispatch; +type RootState = ReturnType; +type AppDispatch = ThunkDispatch; + +export { + store, + type Store, + type RootState, + type AppDispatch, +}; diff --git a/src/stream.ts b/src/stream.ts index 599704d12..e74d374f9 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -14,86 +14,84 @@ interface ConnectStreamCallbacks { type PollingRefreshFn = (dispatch: AppDispatch, done?: () => void) => void -export function connectStream( +const connectStream = ( path: string, pollingRefresh: PollingRefreshFn | null = null, callbacks: (dispatch: AppDispatch, getState: () => RootState) => ConnectStreamCallbacks, -) { - return (dispatch: AppDispatch, getState: () => RootState) => { - const streamingAPIBaseURL = getState().instance.configuration.urls.streaming; - const accessToken = getAccessToken(getState()); - const { onConnect, onDisconnect, onReceive } = callbacks(dispatch, getState); +) => (dispatch: AppDispatch, getState: () => RootState) => { + const streamingAPIBaseURL = getState().instance.configuration.urls.streaming; + const accessToken = getAccessToken(getState()); + const { onConnect, onDisconnect, onReceive } = callbacks(dispatch, getState); - let polling: NodeJS.Timeout | null = null; - - const setupPolling = () => { - if (pollingRefresh) { - pollingRefresh(dispatch, () => { - polling = setTimeout(() => setupPolling(), 20000 + randomIntUpTo(20000)); - }); - } - }; - - const clearPolling = () => { - if (polling) { - clearTimeout(polling); - polling = null; - } - }; - - let subscription: WebSocket; - - // If the WebSocket fails to be created, don't crash the whole page, - // just proceed without a subscription. - try { - subscription = getStream(streamingAPIBaseURL!, accessToken!, path, { - connected() { - if (pollingRefresh) { - clearPolling(); - } - - onConnect(); - }, - - disconnected() { - if (pollingRefresh) { - polling = setTimeout(() => setupPolling(), randomIntUpTo(40000)); - } - - onDisconnect(); - }, - - received(data) { - onReceive(subscription, data); - }, - - reconnected() { - if (pollingRefresh) { - clearPolling(); - pollingRefresh(dispatch); - } - - onConnect(); - }, + let polling: NodeJS.Timeout | null = null; + const setupPolling = () => { + if (pollingRefresh) { + pollingRefresh(dispatch, () => { + polling = setTimeout(() => setupPolling(), 20000 + randomIntUpTo(20000)); }); - } catch (e) { - console.error(e); + } + }; + + const clearPolling = () => { + if (polling) { + clearTimeout(polling); + polling = null; + } + }; + + let subscription: WebSocket; + + // If the WebSocket fails to be created, don't crash the whole page, + // just proceed without a subscription. + try { + subscription = getStream(streamingAPIBaseURL!, accessToken!, path, { + connected() { + if (pollingRefresh) { + clearPolling(); + } + + onConnect(); + }, + + disconnected() { + if (pollingRefresh) { + polling = setTimeout(() => setupPolling(), randomIntUpTo(40000)); + } + + onDisconnect(); + }, + + received(data) { + onReceive(subscription, data); + }, + + reconnected() { + if (pollingRefresh) { + clearPolling(); + pollingRefresh(dispatch); + } + + onConnect(); + }, + + }); + } catch (e) { + console.error(e); + } + + const disconnect = () => { + if (subscription) { + subscription.close(); } - const disconnect = () => { - if (subscription) { - subscription.close(); - } - - clearPolling(); - }; - - return disconnect; + clearPolling(); }; -} -export default function getStream( + return disconnect; +}; + +const getStream = ( streamingAPIBaseURL: string, accessToken: string, stream: string, @@ -103,7 +101,7 @@ export default function getStream( disconnected: ((this: WebSocket, ev: Event) => any) | null; reconnected: ((this: WebSocket, ev: Event) => any); }, -) { +) => { const params = [ `stream=${stream}` ]; const ws = new WebSocketClient(`${streamingAPIBaseURL}/api/v1/streaming/?${params.join('&')}`, accessToken as any); @@ -123,4 +121,9 @@ export default function getStream( }; return ws; -} +}; + +export { + connectStream, + getStream as default, +}; diff --git a/src/toast.tsx b/src/toast.tsx index 6e75fe358..3c2c9092e 100644 --- a/src/toast.tsx +++ b/src/toast.tsx @@ -26,23 +26,20 @@ const createToast = (type: ToastType, message: ToastText, opts?: IToastOptions) }); }; -function info(message: ToastText, opts?: IToastOptions) { +const info = (message: ToastText, opts?: IToastOptions) => createToast('info', message, opts); -} -function success(message: ToastText, opts?: IToastOptions) { +const success = (message: ToastText, opts?: IToastOptions) => createToast('success', message, opts); -} -function error(message: ToastText, opts?: IToastOptions) { +const error = (message: ToastText, opts?: IToastOptions) => createToast('error', message, opts); -} const messages = defineMessages({ unexpectedMessage: { id: 'alert.unexpected.message', defaultMessage: 'Something went wrong.' }, }); -function showAlertForError(networkError: { response: Response & { json: any } }) { +const showAlertForError = (networkError: { response: Response & { json: any } }) => { if (networkError?.response) { const { json, status, statusText } = networkError.response; @@ -72,7 +69,7 @@ function showAlertForError(networkError: { response: Response & { json: any } }) console.error(networkError); return error(messages.unexpectedMessage); } -} +}; export default { info, diff --git a/tailwind/colors.ts b/tailwind/colors.ts index 089d29844..f1b80f4e7 100644 --- a/tailwind/colors.ts +++ b/tailwind/colors.ts @@ -1,9 +1,7 @@ import { type RecursiveKeyValuePair } from 'tailwindcss/types/config'; /** https://tailwindcss.com/docs/customizing-colors#using-css-variables */ -function withOpacityValue(variable: string): string { - return `rgb(var(${variable}) / )`; -} +const withOpacityValue = (variable: string): string => `rgb(var(${variable}) / )`; /** Parse a single color as a CSS variable. */ const toColorVariable = (colorName: string, tint: number | null = null): string => { @@ -41,7 +39,4 @@ const parseColorMatrix = (colorMatrix: ColorMatrix): RecursiveKeyValuePair => { }, {}); }; -export { - withOpacityValue, - parseColorMatrix, -}; +export { withOpacityValue, parseColorMatrix }; diff --git a/vite.config.ts b/vite.config.ts index 167d2ea0b..fd3ea15a7 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -119,10 +119,10 @@ export default defineConfig(({ command }) => ({ })); /** Return file as string, or return empty string if the file isn't found. */ -function readFileContents(path: string) { +const readFileContents = (path: string) => { try { return fs.readFileSync(path, 'utf8'); } catch { return ''; } -} +};