Skip to content

Commit

Permalink
🚀 add lint
Browse files Browse the repository at this point in the history
  • Loading branch information
lod61 committed Nov 28, 2024
1 parent 63e21e1 commit 2979cb3
Show file tree
Hide file tree
Showing 21 changed files with 274 additions and 215 deletions.
14 changes: 9 additions & 5 deletions .github/workflows/typecheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ jobs:
typecheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: "18"
- run: npm ci
- run: npm run typecheck
- run: npm run lint
cache: 'yarn'
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Type check
run: yarn typecheck
- name: Lint
run: yarn lint
6 changes: 2 additions & 4 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn lint-staged
#!/bin/sh
exec yarn lint-staged
7 changes: 7 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
semi: true,
trailingComma: "all",
singleQuote: false,
printWidth: 100,
tabWidth: 2,
};
45 changes: 22 additions & 23 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default [
window: "readonly",
document: "readonly",
console: "readonly",
process: true,
localStorage: "readonly",
fetch: "readonly",
HTMLElement: "readonly",
Expand All @@ -43,40 +44,38 @@ export default [
react: react,
},
rules: {
"no-console": "warn",
"no-console": "off",
"no-debugger": "warn",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["warn", {
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_"
}],
"@typescript-eslint/no-unused-vars": [
"warn",
{
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
},
],
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn",
"react/react-in-jsx-scope": "off",
"react/prop-types": "off",
"indent": ["error", 2, { "SwitchCase": 1 }],
"linebreak-style": ["error", "unix"],
"quotes": ["error", "double"],
"semi": ["error", "always"],
"comma-dangle": ["error", "always-multiline"],
"arrow-parens": ["error", "always"],
"object-curly-spacing": ["error", "always"],
"array-bracket-spacing": ["error", "never"],
"max-len": ["warn", {
"code": 100,
"ignoreStrings": true,
"ignoreTemplateLiterals": true,
"ignoreComments": true
}],
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/no-non-null-assertion": "warn",
"@typescript-eslint/no-empty-interface": "warn",
"no-constant-condition": ["error", { checkLoops: false }],
indent: ["error", 2, { SwitchCase: 1 }],
quotes: ["error", "double"],
semi: ["error", "always"],
"comma-dangle": ["error", "always-multiline"],
"no-undef": "off",
},
settings: {
react: {
version: "detect",
},
"import/resolver": {
typescript: {
alwaysTryTypes: true,
},
},
},
},
];
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@
"eslint-plugin-react-refresh": "^0.4.5",
"husky": "^9.1.7",
"lint-staged": "^15.2.10",
"prettier": "^3.4.1",
"typescript": "^5.2.2",
"vite": "^5.0.8"
},
"lint-staged": {
"*.{ts,tsx}": [
"eslint --fix",
"git add"
"eslint --fix"
]
}
}
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function App() {
divider: "rgba(0,0,0,0.1)",
},
typography: {
fontFamily: 'Söhne,ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,Ubuntu,Cantarell,Noto Sans,sans-serif,"Noto Color Emoji"',
fontFamily: "Söhne,ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,Ubuntu,Cantarell,Noto Sans,sans-serif,\"Noto Color Emoji\"",
},
});

Expand Down
31 changes: 16 additions & 15 deletions src/components/Chat/ChatContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useApiKey } from "@/hooks/useApiKey";
import { streamChat } from "@/services/api";
import { Message } from "@/types/chat";
import { logger } from "@/utils/logger";
import { Settings as SettingsIcon } from "@mui/icons-material";
import { Box, IconButton } from "@mui/material";
import { useCallback, useEffect, useRef, useState } from "react";
Expand All @@ -23,15 +24,15 @@ export default function ChatContainer() {
const isInInput =
target.tagName === "INPUT" || target.tagName === "TEXTAREA";

console.log("Key pressed:", e.key);
console.log("Target element:", target.tagName);
console.log("Is in input:", isInInput);
console.log("Input ref exists:", !!inputRef.current);
logger.log("Key pressed:", e.key);
logger.log("Target element:", target.tagName);
logger.log("Is in input:", isInInput);
logger.log("Input ref exists:", !!inputRef.current);

if (e.key === "/" && !isInInput) {
e.preventDefault();
if (inputRef.current) {
console.log("Focusing input");
logger.log("Focusing input");
inputRef.current.focus();
const input = inputRef.current as HTMLInputElement;
if (input.value === "/") {
Expand Down Expand Up @@ -72,8 +73,8 @@ export default function ChatContainer() {
index === arr.length - 1 &&
msg.role === "assistant" &&
!msg.content
)
)
),
),
);
}

Expand All @@ -85,7 +86,7 @@ export default function ChatContainer() {
setMessages((prevMessages) => {
const currentMessages = prevMessages.filter(
(msg) =>
msg.role === "user" || (msg.role === "assistant" && msg.content)
msg.role === "user" || (msg.role === "assistant" && msg.content),
);

const newMessages = [...currentMessages, userMessage, aiMessage];
Expand All @@ -100,13 +101,13 @@ export default function ChatContainer() {
apiKey,
currentMessages.concat(userMessage),
updateMessage,
abortControllerRef.current.signal
abortControllerRef.current.signal,
);
} catch (err) {
console.error("Chat error:", err);
logger.error("Chat error:", err);
if (err instanceof Error) {
if (err.name === "AbortError") {
console.log("Request cancelled");
logger.log("Request cancelled");
} else if (err.message.includes("API key")) {
clearApiKey();
setShowApiKeyInput(true);
Expand Down Expand Up @@ -141,13 +142,13 @@ export default function ChatContainer() {
const originalMessage = newMessages[index];

if (!originalMessage) {
console.error('Message not found at index:', index);
logger.error("Message not found at index:", index);
return;
}

newMessages[index] = {
role: originalMessage.role,
content: newContent
content: newContent,
};

newMessages.splice(index + 1);
Expand All @@ -162,12 +163,12 @@ export default function ChatContainer() {
apiKey!,
newMessages,
updateMessage,
abortControllerRef.current.signal
abortControllerRef.current.signal,
);
} catch (err) {
if (err instanceof Error) {
if (err.name === "AbortError") {
console.log("Request cancelled");
logger.log("Request cancelled");
} else if (err.message.includes("API key")) {
clearApiKey();
setShowApiKeyInput(true);
Expand Down
4 changes: 2 additions & 2 deletions src/components/Chat/ChatHistory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ function ChatHistory({ messages, onEdit }: ChatHistoryProps) {
}
};

window.addEventListener('resize', handleResize);
return () => window.removeEventListener('resize', handleResize);
window.addEventListener("resize", handleResize);
return () => window.removeEventListener("resize", handleResize);
}, []);

return (
Expand Down
4 changes: 2 additions & 2 deletions src/components/Chat/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const ChatInput = forwardRef<HTMLInputElement, ChatInputProps>(
};

const handleKeyDown = (
e: React.KeyboardEvent<HTMLInputElement | HTMLTextAreaElement>
e: React.KeyboardEvent<HTMLInputElement | HTMLTextAreaElement>,
) => {
if (e.key === "Enter" && !e.shiftKey && !disabled) {
e.preventDefault();
Expand Down Expand Up @@ -122,7 +122,7 @@ const ChatInput = forwardRef<HTMLInputElement, ChatInputProps>(
</Box>
</Box>
);
}
},
);

ChatInput.displayName = "ChatInput";
Expand Down
2 changes: 1 addition & 1 deletion src/components/Chat/ChatMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ const ChatMessage = memo(({ message, onEdit }: ChatMessageProps) => {
counterIncrement: "item",
display: "flex",
"&::before": {
content: 'counter(item) "."',
content: "counter(item) \".\"",
fontWeight: 600,
minWidth: "1.5em",
},
Expand Down
22 changes: 11 additions & 11 deletions src/components/Settings/ApiKeyInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react'
import React, { useState } from "react";
import {
Dialog,
DialogTitle,
Expand All @@ -9,7 +9,7 @@ import {
Alert,
CircularProgress,
Link,
} from '@mui/material'
} from "@mui/material";

interface ApiKeyInputProps {
open: boolean
Expand All @@ -24,16 +24,16 @@ export default function ApiKeyInput({
onClose,
onSubmit,
error,
isValidating = false
isValidating = false,
}: ApiKeyInputProps) {
const [apiKey, setApiKey] = useState('')
const [apiKey, setApiKey] = useState("");

const handleSubmit = (e: React.FormEvent) => {
e.preventDefault()
e.preventDefault();
if (apiKey.trim()) {
onSubmit(apiKey.trim())
onSubmit(apiKey.trim());
}
}
};

return (
<Dialog
Expand All @@ -59,11 +59,11 @@ export default function ApiKeyInput({
disabled={isValidating}
helperText={
<span>
Visit{' '}
Visit{" "}
<Link href="https://openrouter.ai/keys" target="_blank" rel="noopener">
openrouter.ai
</Link>
{' '}to get your API key
{" "}to get your API key
</span>
}
/>
Expand All @@ -76,10 +76,10 @@ export default function ApiKeyInput({
startIcon={isValidating ? <CircularProgress size={20} /> : null}
fullWidth
>
{isValidating ? 'Validating...' : 'Submit'}
{isValidating ? "Validating..." : "Submit"}
</Button>
</DialogActions>
</form>
</Dialog>
)
);
}
Loading

0 comments on commit 2979cb3

Please sign in to comment.