Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Refactor functions for consistency

Hakan Shehu4895c32aca8c
diff --git a/desktop/src/components/accounts/email-login.tsx b/desktop/src/components/accounts/email-login.tsx
index 25ae4b50..e4754b8b 100644
--- a/desktop/src/components/accounts/email-login.tsx
+++ b/desktop/src/components/accounts/email-login.tsx
@@ -38,7 +38,7 @@ export const EmailLogin = ({ serverUrl, onLogin }: EmailLoginProps) => {
},
});
- async function handleSubmit(values: z.infer<typeof formSchema>) {
+ const handleSubmit = async (values: z.infer<typeof formSchema>) => {
setIsPending(true);
try {
const { data } = await axios.post<LoginOutput>(
diff --git a/desktop/src/components/accounts/email-register.tsx b/desktop/src/components/accounts/email-register.tsx
index 4d6801da..0a4d4c33 100644
--- a/desktop/src/components/accounts/email-register.tsx
+++ b/desktop/src/components/accounts/email-register.tsx
@@ -41,7 +41,7 @@ export const EmailRegister = ({ serverUrl, onRegister }: EmailRegisterProps) =>
},
});
- async function onSubmit(values: z.infer<typeof formSchema>) {
+ const onSubmit = async (values: z.infer<typeof formSchema>) => {
setIsPending(true);
try {
const { data } = await axios.post<LoginOutput>(
diff --git a/desktop/src/components/accounts/login.tsx b/desktop/src/components/accounts/login.tsx
index 600951f8..5fb9d1df 100644
--- a/desktop/src/components/accounts/login.tsx
+++ b/desktop/src/components/accounts/login.tsx
@@ -11,7 +11,7 @@ export const Login = observer(() => {
const store = useStore();
const [showRegister, setShowRegister] = React.useState(false);
- async function handleLogin(output: LoginOutput) {
+ const handleLogin = async (output: LoginOutput) => {
store.addAccount(output.account);
await window.globalDb.addAccount(output.account);
diff --git a/desktop/src/components/spaces/space-create-dialog.tsx b/desktop/src/components/spaces/space-create-dialog.tsx
index bdb9a493..0b47a252 100644
--- a/desktop/src/components/spaces/space-create-dialog.tsx
+++ b/desktop/src/components/spaces/space-create-dialog.tsx
@@ -44,12 +44,12 @@ export const SpaceCreateDialog = ({
},
});
- function handleCancel(){
+ const handleCancel = () => {
form.reset();
onOpenChange(false);
}
- async function handleSubmit(values: z.infer<typeof formSchema>) {
+ const handleSubmit = async (values: z.infer<typeof formSchema>) => {
setIsPending(true);
const node: Node = {
diff --git a/desktop/src/components/ui/avatar.tsx b/desktop/src/components/ui/avatar.tsx
index c624977a..5f6c8f03 100644
--- a/desktop/src/components/ui/avatar.tsx
+++ b/desktop/src/components/ui/avatar.tsx
@@ -1,7 +1,7 @@
import React from 'react';
import { cn, hashCode } from '@/lib/utils';
-function getAvatarSizeClasses(size?: string) {
+const getAvatarSizeClasses = (size?: string) => {
if (size === 'small') {
return 'w-5 h-5';
}
@@ -31,7 +31,7 @@ const colors = [
'rgb(156 163 175)',
];
-function getColorForId(id: string) {
+const getColorForId = (id: string) => {
const index = Math.abs(hashCode(id)) % colors.length;
return colors[index];
}
diff --git a/desktop/src/components/workspaces/workspace-create.tsx b/desktop/src/components/workspaces/workspace-create.tsx
index 3e216fe7..d136bc54 100644
--- a/desktop/src/components/workspaces/workspace-create.tsx
+++ b/desktop/src/components/workspaces/workspace-create.tsx
@@ -39,7 +39,7 @@ export const WorkspaceCreate = observer(() => {
},
});
- async function handleSubmit(values: z.infer<typeof formSchema>) {
+ const handleSubmit = async (values: z.infer<typeof formSchema>) => {
setIsPending(true);
try {
const { data } = await axios.post<Workspace>('v1/workspaces', values);
diff --git a/desktop/src/electron/event-loop.ts b/desktop/src/electron/event-loop.ts
index 50dbf696..47f626ee 100644
--- a/desktop/src/electron/event-loop.ts
+++ b/desktop/src/electron/event-loop.ts
@@ -1,12 +1,12 @@
import {deleteTransactions, getGroupedAccountTransactions} from "@/electron/global-db";
-export function initEventLoop() {
+export const initEventLoop = () => {
setInterval(async () => {
await sendTransactions();
}, 10000);
}
-async function sendTransactions() {
+const sendTransactions = async () => {
const groupedAccountTransactions = getGroupedAccountTransactions(10);
for (const accountTransactions of groupedAccountTransactions) {
diff --git a/desktop/src/electron/global-db.ts b/desktop/src/electron/global-db.ts
index f0ec166c..7097c6d3 100644
--- a/desktop/src/electron/global-db.ts
+++ b/desktop/src/electron/global-db.ts
@@ -106,12 +106,13 @@ const deleteTransactionsQuery = `
DELETE FROM transactions WHERE id IN (?)
`;
-export function addAccount(account: Account) {
+
+const addAccount = (account: Account) => {
const stmt = globalDb.prepare(insertAccountQuery);
stmt.run(account.id, account.name, account.email, account.avatar, account.token);
}
-export function getAccounts(): Account[] {
+const getAccounts = (): Account[] => {
const stmt = globalDb.prepare<[], AccountDbModel>(selectAccountsQuery);
const accounts = stmt.all();
@@ -124,7 +125,7 @@ export function getAccounts(): Account[] {
}));
}
-function getWorkspaces(): Workspace[] {
+const getWorkspaces = (): Workspace[] => {
const stmt = globalDb.prepare<[], WorkspaceDbModel>(selectWorkspacesQuery);
const workspaces = stmt.all();
@@ -140,18 +141,18 @@ function getWorkspaces(): Workspace[] {
}));
}
-function addWorkspace(workspace: Workspace) {
+const addWorkspace = (workspace: Workspace) => {
const stmt = globalDb.prepare(insertWorkspaceQuery);
stmt.run(workspace.id, workspace.name, workspace.description, workspace.avatar, workspace.versionId, workspace.accountId, workspace.role, workspace.userNodeId);
}
-function enqueueTransaction(transaction: Transaction) {
+const enqueueTransaction = (transaction: Transaction) => {
const stmt = globalDb.prepare(insertTransactionQuery);
const inputJson = JSON.stringify(transaction.input);
stmt.run(transaction.id, transaction.workspaceId, transaction.accountId, transaction.type, transaction.nodeId, inputJson, transaction.createdAt.toISOString());
}
-export function getGroupedAccountTransactions(count: number): AccountTransactions[] {
+export const getGroupedAccountTransactions = (count: number): AccountTransactions[] => {
const stmt = globalDb.prepare<[], TransactionDbModel>(`SELECT * FROM transactions ORDER BY id LIMIT ${count} `);
const transactions = stmt.all();
@@ -196,18 +197,18 @@ export function getGroupedAccountTransactions(count: number): AccountTransaction
}));
}
-export function deleteTransactions(ids: string[]) {
+export const deleteTransactions = (ids: string[]) => {
const stmt = globalDb.prepare(deleteTransactionsQuery);
stmt.run(ids.join(','));
}
-export function initGlobalDb() {
+export const initGlobalDb = () => {
globalDb.exec(createWorkspacesTableQuery);
globalDb.exec(createTransactionsTableQuery);
globalDb.exec(createAccountsTableQuery);
}
-export function defineGlobalDbHandlers() {
+export const defineGlobalDbHandlers = () => {
ipcMain.handle('get-accounts', () => getAccounts());
ipcMain.handle('add-account', (_, account: Account) => addAccount(account));
ipcMain.handle('get-workspaces', () => getWorkspaces());
diff --git a/desktop/src/lib/axios.ts b/desktop/src/lib/axios.ts
index 42b71ccf..48c5c522 100644
--- a/desktop/src/lib/axios.ts
+++ b/desktop/src/lib/axios.ts
@@ -1,11 +1,7 @@
-import Axios, { isAxiosError } from 'axios';
+import { isAxiosError } from 'axios';
import { ApiErrorOutput } from '@/types/errors';
-// export const axios = Axios.create({
-// baseURL: 'http://localhost:3000',
-// });
-
-export function parseApiError(error: unknown): ApiErrorOutput {
+export const parseApiError = (error: unknown): ApiErrorOutput => {
if (isAxiosError(error) && error.response) {
if (
error.response.data &&
diff --git a/desktop/src/lib/id.ts b/desktop/src/lib/id.ts
index c53a55f2..0b9e8c90 100644
--- a/desktop/src/lib/id.ts
+++ b/desktop/src/lib/id.ts
@@ -2,7 +2,7 @@ import { monotonicFactory } from 'ulid'
const ulid = monotonicFactory()
-export function generateId(type: IdType) {
+export const generateId = (type: IdType) => {
return ulid().toLowerCase() + type
}
diff --git a/desktop/src/lib/nodes.ts b/desktop/src/lib/nodes.ts
index 12fe3a70..5dd8d620 100644
--- a/desktop/src/lib/nodes.ts
+++ b/desktop/src/lib/nodes.ts
@@ -3,7 +3,7 @@ import {CreateNodeTransactionInput, Transaction} from "@/types/transactions";
import {generateId, IdType} from "@/lib/id";
import {store} from "@/store";
-export async function createNode(accountId: string, node: Node) {
+export const createNode = async (accountId: string, node: Node) => {
store.setNode(node);
const transactionInput: CreateNodeTransactionInput = {
diff --git a/desktop/src/lib/utils.ts b/desktop/src/lib/utils.ts
index 874a9d87..e5df9378 100644
--- a/desktop/src/lib/utils.ts
+++ b/desktop/src/lib/utils.ts
@@ -1,11 +1,11 @@
import { type ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"
-export function cn(...inputs: ClassValue[]) {
+export const cn = (...inputs: ClassValue[]) => {
return twMerge(clsx(inputs))
}
-export function hashCode(str: string) {
+export const hashCode = (str: string) => {
let hash = 0;
for (let i = 0; i < str.length; i++) {
const character = str.charCodeAt(i);
diff --git a/server/src/lib/id.ts b/server/src/lib/id.ts
index 18851c70..f066c1ca 100644
--- a/server/src/lib/id.ts
+++ b/server/src/lib/id.ts
@@ -2,14 +2,13 @@ import { monotonicFactory } from 'ulid'
const ulid = monotonicFactory()
-export function generateId(type: IdType) {
+export const generateId = (type: IdType) => {
return ulid().toLowerCase() + type
}
export enum IdType {
Account = 'ac',
Workspace = 'wc',
- WorkspaceAccount = 'wa',
User = 'us',
Version = 've'
}
\ No newline at end of file
diff --git a/server/src/middlewares/auth.ts b/server/src/middlewares/auth.ts
index 33ad0143..75c1fd8e 100644
--- a/server/src/middlewares/auth.ts
+++ b/server/src/middlewares/auth.ts
@@ -5,7 +5,7 @@ const JwtSecretKey = process.env.JWT_SECRET ?? '';
const JwtAudience = process.env.JWT_AUDIENCE ?? '';
const JwtIssuer = process.env.JWT_ISSUER ?? '';
-export function authMiddleware(req: NeuronRequest, res: NeuronResponse, next: NeuronNextFunction) {
+export const authMiddleware = (req: NeuronRequest, res: NeuronResponse, next: NeuronNextFunction) => {
const token = req.header('Authorization')?.split(' ')[1];
if (!token) {
diff --git a/server/src/routes/accounts.ts b/server/src/routes/accounts.ts
index b893aa3b..6d53730f 100644
--- a/server/src/routes/accounts.ts
+++ b/server/src/routes/accounts.ts
@@ -20,7 +20,7 @@ const JwtAudience = process.env.JWT_AUDIENCE ?? '';
const JwtIssuer = process.env.JWT_ISSUER ?? '';
const SaltRounds = 10;
-async function buildLoginOutput(id: string, name: string, email: string): Promise<LoginOutput> {
+const buildLoginOutput = async (id: string, name: string, email: string): Promise<LoginOutput> => {
const signOptions: jwt.SignOptions = {
issuer: JwtIssuer,
audience: JwtAudience,
@@ -84,7 +84,7 @@ async function buildLoginOutput(id: string, name: string, email: string): Promis
};
}
-async function registerWithEmail(req: Request, res: Response) {
+const registerWithEmail = async (req: Request, res: Response) => {
const input: EmailRegisterInput = req.body;
let existingAccount = await prisma
.accounts
@@ -119,7 +119,7 @@ async function registerWithEmail(req: Request, res: Response) {
return res.json(buildLoginOutput(account.id, account.name, account.email));
}
-async function loginWithEmail(req: Request, res: Response) {
+const loginWithEmail = async (req: Request, res: Response) => {
const input: EmailLoginInput = req.body;
let account = await prisma
.accounts
@@ -164,7 +164,7 @@ async function loginWithEmail(req: Request, res: Response) {
.json(output);
}
-async function loginWithGoogle(req: Request, res: Response) {
+const loginWithGoogle = async (req: Request, res: Response) => {
const input: GoogleLoginInput = req.body;
const url = `${GoogleUserInfoUrl}?access_token=${input.access_token}`;
const userInfoResponse = await axios.get(url);
diff --git a/server/src/routes/transactions.ts b/server/src/routes/transactions.ts
index ad40e8f2..5da7f1f0 100644
--- a/server/src/routes/transactions.ts
+++ b/server/src/routes/transactions.ts
@@ -7,7 +7,7 @@ import {
} from "@/types/transactions";
import {prisma} from "@/data/db";
-async function applyTransactions(req: Request, res: Response) {
+const applyTransactions = async (req: Request, res: Response) => {
const transactions: Transaction[] = req.body.transactions;
const appliedTransactionIds: string[] = [];
diff --git a/server/src/routes/workspaces.ts b/server/src/routes/workspaces.ts
index 1dd4db0a..810f2592 100644
--- a/server/src/routes/workspaces.ts
+++ b/server/src/routes/workspaces.ts
@@ -11,7 +11,7 @@ import {generateId, IdType} from "@/lib/id";
import {prisma} from "@/data/db";
import {Node} from "@/types/nodes";
-async function createWorkspace(req: NeuronRequest, res: NeuronResponse) {
+const createWorkspace = async (req: NeuronRequest, res: NeuronResponse) => {
const input: WorkspaceInput = req.body;
if (!req.accountId) {
@@ -123,7 +123,7 @@ async function createWorkspace(req: NeuronRequest, res: NeuronResponse) {
.json(output);
}
-async function updateWorkspace(req: NeuronRequest, res: NeuronResponse) {
+const updateWorkspace = async (req: NeuronRequest, res: NeuronResponse) => {
const id = req.params.id;
const input: WorkspaceInput = req.body;
@@ -209,7 +209,7 @@ async function updateWorkspace(req: NeuronRequest, res: NeuronResponse) {
.json(output);
}
-async function deleteWorkspace(req: NeuronRequest, res: NeuronResponse) {
+const deleteWorkspace = async (req: NeuronRequest, res: NeuronResponse) => {
const id = req.params.id;
if (!req.accountId) {
@@ -271,7 +271,7 @@ async function deleteWorkspace(req: NeuronRequest, res: NeuronResponse) {
});
}
-async function getWorkspace(req: NeuronRequest, res: NeuronResponse) {
+const getWorkspace = async (req: NeuronRequest, res: NeuronResponse) => {
const id = req.params.id;
if (!req.accountId) {
@@ -334,7 +334,7 @@ async function getWorkspace(req: NeuronRequest, res: NeuronResponse) {
.json(output);
}
-export async function getWorkspaces(req: NeuronRequest, res: NeuronResponse) {
+const getWorkspaces = async (req: NeuronRequest, res: NeuronResponse) => {
if (!req.accountId) {
return res
.status(401)