mirror of
https://github.com/TheRealOwenRees/chess-scribe-website.git
synced 2026-07-23 01:46:57 +00:00
Compare commits
39 Commits
8d19564a4e
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
1245bf74c2
|
|||
|
908dd9b57b
|
|||
|
18f86125da
|
|||
|
f4ff9d8c68
|
|||
|
7994f79e02
|
|||
|
d11d360cbf
|
|||
|
573d063d55
|
|||
|
2dd006f14e
|
|||
|
072e24b227
|
|||
|
0795daba4b
|
|||
|
f139449d9a
|
|||
|
f9d02efe49
|
|||
| 2c657c7598 | |||
|
fbc704dc30
|
|||
|
c082a356fd
|
|||
|
6120c48377
|
|||
|
99f8aa8ac2
|
|||
|
2e05f86d5f
|
|||
|
4577755c93
|
|||
|
a110c7490c
|
|||
|
0b10637df4
|
|||
|
41cbd5db16
|
|||
|
af6a8426ad
|
|||
|
0dde848cfe
|
|||
|
ee9968e99b
|
|||
|
7384a3f825
|
|||
|
887a43636f
|
|||
|
734c631356
|
|||
|
802d46191e
|
|||
|
4edcb49f58
|
|||
|
030d44226d
|
|||
|
f12176e713
|
|||
|
3da81680a1
|
|||
|
0b32e77d7e
|
|||
|
f475732715
|
|||
|
f4b9820b5b
|
|||
|
98a5bb742b
|
|||
|
6ef08f4b91
|
|||
|
5bf4ec95c8
|
+6
-1
@@ -1,4 +1,9 @@
|
|||||||
|
VITE_API_BASE_URL=
|
||||||
|
VITE_MATOMO_URL=
|
||||||
|
VITE_MATOMO_SITE_ID=
|
||||||
|
VITE_MAINTENANCE_MODE= # nothing = false, anything else = true
|
||||||
|
VITE_MAINTENANCE_MODE_MESSAGE="ChessScribe is currently undergoing maintenance. Some features may be unavailable."
|
||||||
|
|
||||||
DISCORD_CONTACT_WEBHOOK=
|
DISCORD_CONTACT_WEBHOOK=
|
||||||
DISCORD_ERROR_LOG_WEBHOOK=
|
DISCORD_ERROR_LOG_WEBHOOK=
|
||||||
LICHESS_CLIENT_ID=
|
LICHESS_CLIENT_ID=
|
||||||
VITE_API_BASE_URL=
|
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
name: Production Deploy
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [main]
|
||||||
|
|
||||||
|
env:
|
||||||
|
REGISTRY: ghcr.io
|
||||||
|
GH_USER: therealowenrees
|
||||||
|
IMAGE_NAME: therealowenrees/chess-scribe:latest
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
publish:
|
||||||
|
name: publish image
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v6
|
||||||
|
|
||||||
|
- name: Login to Github Container Registry
|
||||||
|
run: echo "${{ secrets.GHCR_SECRET }}" | docker login ghcr.io -u ${{ env.GH_USER }} --password-stdin
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
|
- name: Build image and publish with Cache
|
||||||
|
uses: docker/build-push-action@v5
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
push: true
|
||||||
|
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
||||||
|
cache-from: type=gha
|
||||||
|
cache-to: type=gha,mode=max
|
||||||
|
build-args: |
|
||||||
|
VITE_API_BASE_URL=https://api.chess-scribe.org/api/v1
|
||||||
|
VITE_MATOMO_URL=https://analytics.owenrees.eu
|
||||||
|
VITE_MATOMO_SITE_ID="3"
|
||||||
|
VITE_MAINTENANCE_MODE=
|
||||||
|
VITE_MAINTENANCE_MODE_MESSAGE="ChessScribe is currently undergoing maintenance. Some features may be unavailable."
|
||||||
|
|
||||||
|
deploy:
|
||||||
|
needs: publish
|
||||||
|
name: deploy image
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Connect and pull image from GHCR
|
||||||
|
uses: appleboy/ssh-action@v1.2.5
|
||||||
|
with:
|
||||||
|
host: ${{ secrets.HOST }}
|
||||||
|
username: ${{ secrets.USERNAME }}
|
||||||
|
key: ${{ secrets.SSH_KEY }}
|
||||||
|
port: ${{ secrets.PORT }}
|
||||||
|
script: |
|
||||||
|
cd /home/${{ secrets.USERNAME }}
|
||||||
|
echo "${{ secrets.GHCR_SECRET }}" | docker login ghcr.io -u ${{ env.GH_USER }} --password-stdin
|
||||||
|
docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
||||||
|
docker compose up -d
|
||||||
@@ -11,3 +11,4 @@ dist-ssr
|
|||||||
.vinxi
|
.vinxi
|
||||||
__unconfig*
|
__unconfig*
|
||||||
todos.json
|
todos.json
|
||||||
|
data
|
||||||
|
|||||||
+38
@@ -0,0 +1,38 @@
|
|||||||
|
# --- Stage 1: Build Stage ---
|
||||||
|
FROM node:24-alpine AS builder
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# 1. Declare the build-time arguments Vite needs
|
||||||
|
ARG VITE_API_BASE_URL
|
||||||
|
ARG VITE_MATOMO_URL
|
||||||
|
ARG VITE_MATOMO_SITE_ID
|
||||||
|
ARG VITE_MAINTENANCE_MODE
|
||||||
|
ARG VITE_MAINTENANCE_MODE_MESSAGE
|
||||||
|
|
||||||
|
# 2. Assign them to environment variables so Vite detects them
|
||||||
|
ENV VITE_API_BASE_URL=$VITE_API_BASE_URL
|
||||||
|
ENV VITE_MATOMO_URL=$VITE_MATOMO_URL
|
||||||
|
ENV VITE_MATOMO_SITE_ID=$VITE_MATOMO_SITE_ID
|
||||||
|
ENV VITE_MAINTENANCE_MODE=$VITE_MAINTENANCE_MODE
|
||||||
|
ENV VITE_MAINTENANCE_MODE_MESSAGE=$VITE_MAINTENANCE_MODE_MESSAGE
|
||||||
|
|
||||||
|
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
|
||||||
|
RUN corepack enable pnpm && pnpm i --frozen-lockfile
|
||||||
|
COPY . .
|
||||||
|
RUN pnpm run build
|
||||||
|
|
||||||
|
# --- Stage 2: Production Stage ---
|
||||||
|
FROM node:24-alpine AS runner
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
ENV NODE_ENV=production
|
||||||
|
ENV PORT=3000
|
||||||
|
ENV HOST=0.0.0.0
|
||||||
|
|
||||||
|
# Nitro compiles everything needed into the .output directory
|
||||||
|
COPY --from=builder /app/.output ./.output
|
||||||
|
|
||||||
|
EXPOSE 3000
|
||||||
|
|
||||||
|
# Run the compiled Nitro server
|
||||||
|
CMD ["node", ".output/server/index.mjs"]
|
||||||
@@ -1,12 +1,13 @@
|
|||||||
- og / json-sd
|
- og / json-sd
|
||||||
- logger
|
- logger
|
||||||
- bring sections headers on all pages other than home page further up (reduce top margin / padding)
|
|
||||||
- set game current position on game load to ply 0 and whatever the FEN is (starting position probably, but could be custom position)
|
- set game current position on game load to ply 0 and whatever the FEN is (starting position probably, but could be custom position)
|
||||||
- privacy policy - have no access to anything you generate, only keep a count of success/failed pdf generations with timestamp and error message
|
- privacy policy - have no access to anything you generate, only keep a count of success/failed pdf generations with timestamp and error message
|
||||||
- a11y and lighthouse fixes
|
- check wcag compliance - footer colour contrast
|
||||||
- check wcag compliance
|
- 429 lichess error handling - retry after 10 seconds
|
||||||
- Callback page - improve loading display in suspense
|
|
||||||
- 429 lichess error handling
|
|
||||||
- add throw errors or return errors in server code
|
|
||||||
- try/catch where needed in client code
|
- try/catch where needed in client code
|
||||||
- generate state and check it against the state returned from lichess https://lichess.org/api#tag/oauth/GET/oauth
|
- generate state and check it against the state returned from lichess https://lichess.org/api#tag/oauth/GET/oauth
|
||||||
|
- checkbox and toggle disabled states
|
||||||
|
- react query on for study and chapter load, to allow caching
|
||||||
|
- https://specification.website/checklist/
|
||||||
|
|
||||||
|
- react query to cache the study and chapters
|
||||||
+6
-2
@@ -13,7 +13,12 @@
|
|||||||
"**/index.html",
|
"**/index.html",
|
||||||
"**/vite.config.ts",
|
"**/vite.config.ts",
|
||||||
"!**/src/routeTree.gen.ts",
|
"!**/src/routeTree.gen.ts",
|
||||||
"!**/src/styles.css"
|
"!**/src/styles.css",
|
||||||
|
"!**/.tanstack/*",
|
||||||
|
"!**/.output/*",
|
||||||
|
"!**/.nitro/*",
|
||||||
|
"!**/node_modules/*",
|
||||||
|
"!**/styles/vendor/*"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"formatter": {
|
"formatter": {
|
||||||
@@ -33,4 +38,3 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
services:
|
||||||
|
chess-scribe-frontend:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
ports:
|
||||||
|
- "3000:3000"
|
||||||
|
environment:
|
||||||
|
- NODE_ENV=production
|
||||||
|
- PORT=3000
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
|
restart: unless-stopped
|
||||||
+3
-6
@@ -9,8 +9,11 @@
|
|||||||
"dev": "vite dev --port 3000 --host",
|
"dev": "vite dev --port 3000 --host",
|
||||||
"build": "vite build",
|
"build": "vite build",
|
||||||
"preview": "vite preview",
|
"preview": "vite preview",
|
||||||
|
"start": "node .output/server/index.mjs",
|
||||||
"test": "vitest run",
|
"test": "vitest run",
|
||||||
|
"lint:fix": "biome lint --write",
|
||||||
"format": "biome format",
|
"format": "biome format",
|
||||||
|
"format:fix": "biome format --write",
|
||||||
"lint": "biome lint",
|
"lint": "biome lint",
|
||||||
"check": "biome check"
|
"check": "biome check"
|
||||||
},
|
},
|
||||||
@@ -48,11 +51,5 @@
|
|||||||
"typescript": "^6.0.2",
|
"typescript": "^6.0.2",
|
||||||
"vite": "^8.0.0",
|
"vite": "^8.0.0",
|
||||||
"vitest": "^4.1.5"
|
"vitest": "^4.1.5"
|
||||||
},
|
|
||||||
"pnpm": {
|
|
||||||
"onlyBuiltDependencies": [
|
|
||||||
"esbuild",
|
|
||||||
"lightningcss"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Generated
+13
-5
@@ -315,6 +315,9 @@ packages:
|
|||||||
'@emnapi/runtime@1.10.0':
|
'@emnapi/runtime@1.10.0':
|
||||||
resolution: {integrity: sha512-ewvYlk86xUoGI0zQRNq/mC+16R1QeDlKQy21Ki3oSYXNgLb45GV1P6A0M+/s6nyCuNDqe5VpaY84BzXGwVbwFA==}
|
resolution: {integrity: sha512-ewvYlk86xUoGI0zQRNq/mC+16R1QeDlKQy21Ki3oSYXNgLb45GV1P6A0M+/s6nyCuNDqe5VpaY84BzXGwVbwFA==}
|
||||||
|
|
||||||
|
'@emnapi/runtime@1.11.1':
|
||||||
|
resolution: {integrity: sha512-vgj7R3y3Wgx24IQaGPA/R6YFXLHVMOZ0uVEyIQPaWs+rd1AzfEMXlAC22FYwO1XkKR6NPsq7mUandH8oIRdZFw==}
|
||||||
|
|
||||||
'@emnapi/wasi-threads@1.2.1':
|
'@emnapi/wasi-threads@1.2.1':
|
||||||
resolution: {integrity: sha512-uTII7OYF+/Mes/MrcIOYp5yOtSMLBWSIoLPpcgwipoiKbli6k322tcoFsxoIIxPDqW01SQGAgko4EzZi2BNv2w==}
|
resolution: {integrity: sha512-uTII7OYF+/Mes/MrcIOYp5yOtSMLBWSIoLPpcgwipoiKbli6k322tcoFsxoIIxPDqW01SQGAgko4EzZi2BNv2w==}
|
||||||
|
|
||||||
@@ -1903,8 +1906,8 @@ packages:
|
|||||||
resolution: {integrity: sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==}
|
resolution: {integrity: sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==}
|
||||||
hasBin: true
|
hasBin: true
|
||||||
|
|
||||||
semver@7.8.1:
|
semver@7.8.5:
|
||||||
resolution: {integrity: sha512-rkVq3IXh+4FDGch+KwzX3aV9W3kO54GyEgpvBzSyctDA6Xtd7RJQV1xmXbeQp5v7+VzLOfVqiutSE6GICgPFvg==}
|
resolution: {integrity: sha512-Y7/KDsb8LjooZpwaqGyulO6DQlksgCncchHGk+sZIY4SBvUocMBEFH5Ur1fI4dV+Jvl0w6cjvucaIi40puRioA==}
|
||||||
engines: {node: '>=10'}
|
engines: {node: '>=10'}
|
||||||
hasBin: true
|
hasBin: true
|
||||||
|
|
||||||
@@ -2489,6 +2492,11 @@ snapshots:
|
|||||||
tslib: 2.8.1
|
tslib: 2.8.1
|
||||||
optional: true
|
optional: true
|
||||||
|
|
||||||
|
'@emnapi/runtime@1.11.1':
|
||||||
|
dependencies:
|
||||||
|
tslib: 2.8.1
|
||||||
|
optional: true
|
||||||
|
|
||||||
'@emnapi/wasi-threads@1.2.1':
|
'@emnapi/wasi-threads@1.2.1':
|
||||||
dependencies:
|
dependencies:
|
||||||
tslib: 2.8.1
|
tslib: 2.8.1
|
||||||
@@ -2583,7 +2591,7 @@ snapshots:
|
|||||||
|
|
||||||
'@img/sharp-wasm32@0.34.5':
|
'@img/sharp-wasm32@0.34.5':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@emnapi/runtime': 1.10.0
|
'@emnapi/runtime': 1.11.1
|
||||||
optional: true
|
optional: true
|
||||||
|
|
||||||
'@img/sharp-win32-arm64@0.34.5':
|
'@img/sharp-win32-arm64@0.34.5':
|
||||||
@@ -3912,7 +3920,7 @@ snapshots:
|
|||||||
|
|
||||||
semver@6.3.1: {}
|
semver@6.3.1: {}
|
||||||
|
|
||||||
semver@7.8.1:
|
semver@7.8.5:
|
||||||
optional: true
|
optional: true
|
||||||
|
|
||||||
seroval-plugins@1.5.4(seroval@1.5.4):
|
seroval-plugins@1.5.4(seroval@1.5.4):
|
||||||
@@ -3925,7 +3933,7 @@ snapshots:
|
|||||||
dependencies:
|
dependencies:
|
||||||
'@img/colour': 1.1.0
|
'@img/colour': 1.1.0
|
||||||
detect-libc: 2.1.2
|
detect-libc: 2.1.2
|
||||||
semver: 7.8.1
|
semver: 7.8.5
|
||||||
optionalDependencies:
|
optionalDependencies:
|
||||||
'@img/sharp-darwin-arm64': 0.34.5
|
'@img/sharp-darwin-arm64': 0.34.5
|
||||||
'@img/sharp-darwin-x64': 0.34.5
|
'@img/sharp-darwin-x64': 0.34.5
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
# pnpm v11 configuration file
|
||||||
|
packages:
|
||||||
|
- "."
|
||||||
|
|
||||||
|
# This replaces 'onlyBuiltDependencies'
|
||||||
|
allowBuilds:
|
||||||
|
"sharp": true
|
||||||
|
"esbuild": true
|
||||||
|
"lightningcss": true
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 24 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 67 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 22 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 859 B |
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
@@ -0,0 +1 @@
|
|||||||
|
{"name":"","short_name":"","icons":[{"src":"/android-chrome-192x192.png","sizes":"192x192","type":"image/png"},{"src":"/android-chrome-512x512.png","sizes":"512x512","type":"image/png"}],"theme_color":"#ffffff","background_color":"#ffffff","display":"standalone"}
|
||||||
@@ -9,10 +9,7 @@ interface IProps {
|
|||||||
const AccordionItem = ({ title, id, children }: IProps) => (
|
const AccordionItem = ({ title, id, children }: IProps) => (
|
||||||
<div className="hero__accordion-item">
|
<div className="hero__accordion-item">
|
||||||
<details className="hero__accordion-details" name="linked">
|
<details className="hero__accordion-details" name="linked">
|
||||||
<summary
|
<summary className="hero__accordion-item__summary" id={`${id}-summary`}>
|
||||||
className="hero__accordion-item__summary"
|
|
||||||
aria-describedby={title}
|
|
||||||
>
|
|
||||||
<span className="hero__accordion-item__title-container">
|
<span className="hero__accordion-item__title-container">
|
||||||
<span className="hero__accordion-item__title">{title}</span>
|
<span className="hero__accordion-item__title">{title}</span>
|
||||||
</span>
|
</span>
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ const CoffeeWidget = ({ height, width }: IProps) => {
|
|||||||
target="_blank"
|
target="_blank"
|
||||||
className="ml-auto flex items-center gap-2 text-sm"
|
className="ml-auto flex items-center gap-2 text-sm"
|
||||||
rel="noopener"
|
rel="noopener"
|
||||||
|
aria-label="Buy Me A Coffee"
|
||||||
>
|
>
|
||||||
<Image
|
<Image
|
||||||
src={buyMeACoffeeLogo}
|
src={buyMeACoffeeLogo}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ interface IProps {
|
|||||||
|
|
||||||
const Feature = ({ title, children }: IProps) => (
|
const Feature = ({ title, children }: IProps) => (
|
||||||
<div className="mb-auto">
|
<div className="mb-auto">
|
||||||
<h6 className="text-(--neutral-content) text-2xl font-bold">{title}</h6>
|
<h5 className="text-(--neutral-content) text-2xl font-bold">{title}</h5>
|
||||||
<p className="text-(--neutral-content) font-medium" data-testid="text">
|
<p className="text-(--neutral-content) font-medium" data-testid="text">
|
||||||
{children}
|
{children}
|
||||||
</p>
|
</p>
|
||||||
|
|||||||
@@ -10,26 +10,26 @@ import footerLogo from "@/assets/images/footerLogo.svg?url";
|
|||||||
export default function Footer() {
|
export default function Footer() {
|
||||||
return (
|
return (
|
||||||
<footer className="text-white w-full grid-cols-2 items-center bg-(--accent) px-10 py-5">
|
<footer className="text-white w-full grid-cols-2 items-center bg-(--accent) px-10 py-5">
|
||||||
<div className="flex w-full items-center">
|
<div className="flex w-full items-center gap-2">
|
||||||
<Link to="/">
|
<Link to="/" aria-label="Home">
|
||||||
<Image
|
<Image
|
||||||
src={footerLogo}
|
src={footerLogo}
|
||||||
alt="ChessScribe Logo"
|
alt="ChessScribe Logo"
|
||||||
width={10}
|
width={10}
|
||||||
height={10}
|
height={10}
|
||||||
className="hidden h-10 w-10 sm:block"
|
className="hidden h-10 w-10 sm:block"
|
||||||
|
aria-label="ChessScribe Logo"
|
||||||
/>
|
/>
|
||||||
</Link>
|
</Link>
|
||||||
<div>
|
<div>
|
||||||
<p data-testid="copyright">
|
<p>© 2023 - {new Date().getFullYear()}</p>
|
||||||
Copyright © 2023 - {new Date().getFullYear()}
|
|
||||||
</p>
|
|
||||||
</div>
|
</div>
|
||||||
<div className="ml-auto flex gap-2">
|
<div className="ml-auto flex gap-2">
|
||||||
<a
|
<a
|
||||||
href={GITHUB_URL}
|
href={GITHUB_URL}
|
||||||
target="_blank"
|
target="_blank"
|
||||||
rel="noopener noreferrer"
|
rel="noopener noreferrer"
|
||||||
|
aria-label="GitHub"
|
||||||
className="text-2xl hover:text-(--neutral-content) transition-colors duration-300 ease-in-out cursor-pointer"
|
className="text-2xl hover:text-(--neutral-content) transition-colors duration-300 ease-in-out cursor-pointer"
|
||||||
>
|
>
|
||||||
<LuGithub />
|
<LuGithub />
|
||||||
@@ -38,6 +38,7 @@ export default function Footer() {
|
|||||||
<Link
|
<Link
|
||||||
to="/contact"
|
to="/contact"
|
||||||
rel="noopener noreferrer"
|
rel="noopener noreferrer"
|
||||||
|
aria-label="Contact"
|
||||||
className="text-2xl hover:text-(--neutral-content) transition-colors duration-300 ease-in-out"
|
className="text-2xl hover:text-(--neutral-content) transition-colors duration-300 ease-in-out"
|
||||||
>
|
>
|
||||||
<LuMail />
|
<LuMail />
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
import { useNavigate, useRouter } from "@tanstack/react-router";
|
||||||
|
|
||||||
|
interface GenericErrorViewProps {
|
||||||
|
error?: Error | unknown;
|
||||||
|
resetErrorBoundary?: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const GenericErrorView = ({
|
||||||
|
error,
|
||||||
|
resetErrorBoundary,
|
||||||
|
}: GenericErrorViewProps) => {
|
||||||
|
const router = useRouter();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
const errorMessage =
|
||||||
|
error instanceof Error
|
||||||
|
? error.message
|
||||||
|
: "An unexpected system error occurred.";
|
||||||
|
|
||||||
|
const handleTryAgain = async () => {
|
||||||
|
if (resetErrorBoundary) {
|
||||||
|
resetErrorBoundary();
|
||||||
|
}
|
||||||
|
|
||||||
|
await router.invalidate();
|
||||||
|
const currentSearch = router.state.location.search;
|
||||||
|
await navigate({ to: ".", search: currentSearch });
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex min-h-[calc(100vh-194px)] flex-col items-center justify-center p-6">
|
||||||
|
<div className="relative flex max-w-md flex-col items-center text-center">
|
||||||
|
<h1 className="text-2xl font-bold tracking-tight mb-2 text-(--accent)">
|
||||||
|
Something went wrong
|
||||||
|
</h1>
|
||||||
|
<p className="text-sm text-(--neutral-content) mb-6 max-w-sm">
|
||||||
|
The application encountered an issue it couldn't recover from.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<div className="w-full mb-8 rounded-md bg-slate-300/60 border border-slate-800/80 p-4 text-left font-mono text-xs text-red-400 max-h-32 overflow-y-auto">
|
||||||
|
<span className="text-(--base-content) block mb-1">Error Logs:</span>
|
||||||
|
{errorMessage}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex w-full gap-3 justify-center">
|
||||||
|
{resetErrorBoundary && (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={handleTryAgain}
|
||||||
|
className="btn btn-secondary"
|
||||||
|
>
|
||||||
|
Try Again
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => navigate({ to: "/" })}
|
||||||
|
className="btn btn-primary"
|
||||||
|
>
|
||||||
|
Go Home
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default GenericErrorView;
|
||||||
@@ -1,11 +1,26 @@
|
|||||||
import { Link } from "@tanstack/react-router";
|
import { Link } from "@tanstack/react-router";
|
||||||
import { Image } from "@unpic/react";
|
import { Image } from "@unpic/react";
|
||||||
|
import { z } from "zod";
|
||||||
|
import MaintenanceModeBanner from "#/components/MaintenanceModeBanner.tsx";
|
||||||
|
import { env } from "#/env.ts";
|
||||||
import logo from "@/assets/images/logo.svg?url";
|
import logo from "@/assets/images/logo.svg?url";
|
||||||
|
|
||||||
export default function Header() {
|
export default function Header() {
|
||||||
|
const maintenanceModeMessage = env.VITE_MAINTENANCE_MODE
|
||||||
|
? z
|
||||||
|
.string()
|
||||||
|
.transform((value) => (value.trim() === "" ? null : value))
|
||||||
|
.parse(env.VITE_MAINTENANCE_MODE_MESSAGE)
|
||||||
|
: null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<header>
|
<header>
|
||||||
|
{maintenanceModeMessage ? (
|
||||||
|
<div className="absolute w-full">
|
||||||
|
<MaintenanceModeBanner message={maintenanceModeMessage} />
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
|
||||||
<nav className="w-full max-w-screen-2xl place-self-center p-8">
|
<nav className="w-full max-w-screen-2xl place-self-center p-8">
|
||||||
<div className="container mx-auto flex items-center justify-between">
|
<div className="container mx-auto flex items-center justify-between">
|
||||||
<div className="flex items-center">
|
<div className="flex items-center">
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ const LichessButton = () => {
|
|||||||
const { user } = useLichessUser();
|
const { user } = useLichessUser();
|
||||||
|
|
||||||
const onClickHandler = () => {
|
const onClickHandler = () => {
|
||||||
return user?.isLoggedIn ? lichessLogout() : lichessLogin();
|
return user ? lichessLogout() : lichessLogin();
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -26,9 +26,7 @@ const LichessButton = () => {
|
|||||||
</svg>
|
</svg>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p>
|
<p>{user ? `Logout ${user.username}` : "Log into Lichess.org"}</p>
|
||||||
{user?.isLoggedIn ? `Logout ${user.username}` : "Log into Lichess.org"}
|
|
||||||
</p>
|
|
||||||
</button>
|
</button>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
import type { FormEvent } from "react";
|
||||||
|
import { toast } from "react-toastify";
|
||||||
|
import { useLichess } from "#/hooks/useLichess.ts";
|
||||||
|
|
||||||
|
interface IProps {
|
||||||
|
setSelectedStudyId: (id: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const LichessStudyLinkInput = ({ setSelectedStudyId }: IProps) => {
|
||||||
|
const { parseLichessStudyLink } = useLichess();
|
||||||
|
|
||||||
|
const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
const formData = new FormData(e.currentTarget);
|
||||||
|
const studyLink = formData.get("lichess-study-link");
|
||||||
|
|
||||||
|
if (!studyLink) return;
|
||||||
|
|
||||||
|
if (typeof studyLink !== "string") {
|
||||||
|
toast.error("Invalid study link");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = parseLichessStudyLink(studyLink);
|
||||||
|
|
||||||
|
if (!result.ok) {
|
||||||
|
toast.error(result.error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(result.data);
|
||||||
|
|
||||||
|
setSelectedStudyId(result.data);
|
||||||
|
toast.success("Study loaded — pick a chapter");
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<form className="flex items-end gap-2" onSubmit={handleSubmit}>
|
||||||
|
<div>
|
||||||
|
<label htmlFor="lichess-study-link" className="text-sm capitalize">
|
||||||
|
Lichess Study Link
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
className="border rounded-md p-2 w-full"
|
||||||
|
type="text"
|
||||||
|
id="lichess-study-link"
|
||||||
|
name="lichess-study-link"
|
||||||
|
placeholder="https://lichess.org/study/..."
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
className="border py-2 px-3 rounded-md cursor-pointer bg-(--accent) text-white hover:bg-transparent hover:text-(--accent) transition-colors duration-300 ease-in-out"
|
||||||
|
>
|
||||||
|
Submit
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default LichessStudyLinkInput;
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
interface IProps {
|
||||||
|
title?: string;
|
||||||
|
description?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const LoadingView = ({ title, description }: IProps) => {
|
||||||
|
return (
|
||||||
|
<div className="flex min-h-[calc(100vh-194px)] flex-col items-center justify-center p-6 bg-base-100">
|
||||||
|
<div className="relative flex max-w-sm flex-col items-center text-center">
|
||||||
|
{/* Animated Custom Loader */}
|
||||||
|
<div className="relative mb-6 flex h-16 w-16 items-center justify-center">
|
||||||
|
{/* Outer Pulsing Ring */}
|
||||||
|
<div className="absolute inset-0 animate-ping rounded-full bg-(--accent) opacity-20 [animation-duration:1.5s]"></div>
|
||||||
|
|
||||||
|
{/* Inner Spinning Ring */}
|
||||||
|
<div className="h-12 w-12 animate-spin rounded-full border-4 border-(--accent) border-t-transparent"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Loading Content */}
|
||||||
|
<h2 className="text-xl font-bold tracking-tight mb-2 text-(--base-content)">
|
||||||
|
{title || "Loading Data"}
|
||||||
|
</h2>
|
||||||
|
<p className="text-sm text-(--neutral-content) max-w-xs">
|
||||||
|
{description || "Please wait a moment while we load the data."}
|
||||||
|
</p>
|
||||||
|
|
||||||
|
{/* Decorative background glow matching your layout */}
|
||||||
|
<div className="absolute -inset-10 -z-10 bg-(--accent)/5 blur-2xl rounded-full pointer-events-none" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default LoadingView;
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
interface IProps {
|
||||||
|
message?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const DEFAULT_MESSAGE = "Maintenance Mode";
|
||||||
|
|
||||||
|
const MaintenanceModeBanner = ({ message }: IProps) => (
|
||||||
|
<div className="bg-orange-400 text-white text-center py-1 w-full text-sm">
|
||||||
|
<p>{message || DEFAULT_MESSAGE}</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
|
||||||
|
export default MaintenanceModeBanner;
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
import { trackAppRouter } from "@socialgouv/matomo-next";
|
import { trackAppRouter } from "@socialgouv/matomo-next";
|
||||||
import { useLocation, useSearch } from "@tanstack/react-router";
|
import { useLocation, useSearch } from "@tanstack/react-router";
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
|
import { env } from "#/env.ts";
|
||||||
|
|
||||||
const MATOMO_URL = import.meta.env.NEXT_PUBLIC_MATOMO_URL;
|
const MATOMO_URL = env.VITE_MATOMO_URL;
|
||||||
const MATOMO_SITE_ID = import.meta.env.NEXT_PUBLIC_MATOMO_SITE_ID;
|
const MATOMO_SITE_ID = env.VITE_MATOMO_SITE_ID;
|
||||||
|
|
||||||
export function MatomoAnalytics() {
|
export function MatomoAnalytics() {
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import { Link } from "@tanstack/react-router";
|
||||||
|
|
||||||
|
const NotFoundView = () => {
|
||||||
|
return (
|
||||||
|
<div className="flex min-h-[calc(100vh-194px)] flex-col items-center justify-center p-6">
|
||||||
|
<div className="relative flex max-w-md flex-col items-center text-center">
|
||||||
|
<h1 className="text-2xl font-bold tracking-tight mb-4 text-(--accent)">
|
||||||
|
The page you are looking for does not exist.
|
||||||
|
</h1>
|
||||||
|
|
||||||
|
<div className="flex w-full gap-3 justify-center">
|
||||||
|
<Link to="/" className="btn btn-primary">
|
||||||
|
Go Home
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default NotFoundView;
|
||||||
@@ -14,7 +14,7 @@ const Section = ({ title, smallTitle, description, children }: IProps) => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<section className="mx-auto mt-8 grid max-w-5xl place-items-center gap-4 p-8 text-center md:grid-cols-3 md:text-left text-(--accent)">
|
<section className="mx-auto mt-8 grid max-w-5xl place-items-center gap-4 p-8 text-center md:grid-cols-3 md:text-left text-(--accent)">
|
||||||
<h3 className="col-span-full mb-4 text-center text-lg font-bold tracking-wider text-primary">
|
<h3 className="col-span-full mb-4 text-center text-2xl font-bold tracking-wider text-primary">
|
||||||
{smallTitle}
|
{smallTitle}
|
||||||
</h3>
|
</h3>
|
||||||
<h4 className="col-span-full mb-4 text-center text-4xl font-extrabold text-(--neutral-content) md:text-5xl">
|
<h4 className="col-span-full mb-4 text-center text-4xl font-extrabold text-(--neutral-content) md:text-5xl">
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
import { useRef, useState } from "react";
|
import { useRef, useState } from "react";
|
||||||
|
import { toast } from "react-toastify";
|
||||||
import { useClickOutside } from "#/hooks/useClickOutside.ts";
|
import { useClickOutside } from "#/hooks/useClickOutside.ts";
|
||||||
import { type IUserStudyChapter, useLichess } from "#/hooks/useLichess.ts";
|
import { useLichess } from "#/hooks/useLichess.ts";
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
setSelectedStudyId: (studyId: string) => void;
|
setSelectedStudyId: (studyId: string) => void;
|
||||||
setStudyChapters: (chapters: IUserStudyChapter[]) => void;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const SelectLichessStudy = ({ setSelectedStudyId }: IProps) => {
|
const SelectLichessStudy = ({ setSelectedStudyId }: IProps) => {
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
const [search, setSearch] = useState("");
|
const [search, setSearch] = useState("");
|
||||||
const { getLichessUserStudies, userStudies } = useLichess();
|
const { getLichessUserStudies, userStudies, setUserStudies } = useLichess();
|
||||||
const dropdownRef = useRef<HTMLDivElement>(null);
|
const dropdownRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
const filteredStudies = userStudies.filter((study) =>
|
const filteredStudies = userStudies.filter((study) =>
|
||||||
@@ -20,15 +20,20 @@ const SelectLichessStudy = ({ setSelectedStudyId }: IProps) => {
|
|||||||
const onClickHandler = async () => {
|
const onClickHandler = async () => {
|
||||||
setIsOpen(!isOpen);
|
setIsOpen(!isOpen);
|
||||||
|
|
||||||
if (!userStudies.length) {
|
if (userStudies.length) return;
|
||||||
await getLichessUserStudies();
|
|
||||||
|
const result = await getLichessUserStudies();
|
||||||
|
if (result.ok) {
|
||||||
|
setUserStudies(result.data);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleStudyClick = async (studyId: string) => {
|
const handleStudyClick = async (studyId: string) => {
|
||||||
|
const study = userStudies.find((s) => s.id === studyId);
|
||||||
setSelectedStudyId(studyId);
|
setSelectedStudyId(studyId);
|
||||||
setIsOpen(false);
|
setIsOpen(false);
|
||||||
setSearch("");
|
setSearch("");
|
||||||
|
toast.success(`Loaded study "${study?.name ?? studyId}"`);
|
||||||
};
|
};
|
||||||
|
|
||||||
useClickOutside({ isOpen, setIsOpen, setSearch, ref: dropdownRef });
|
useClickOutside({ isOpen, setIsOpen, setSearch, ref: dropdownRef });
|
||||||
|
|||||||
@@ -1,6 +1,14 @@
|
|||||||
import { type Dispatch, useRef, useState } from "react";
|
import {
|
||||||
import { useClickOutside } from "#/hooks/useClickOutside.ts";
|
type Dispatch,
|
||||||
import { type IUserStudyChapter, useLichess } from "#/hooks/useLichess.ts";
|
useActionState,
|
||||||
|
useEffect,
|
||||||
|
// useRef,
|
||||||
|
useState,
|
||||||
|
} from "react";
|
||||||
|
import { toast } from "react-toastify";
|
||||||
|
// import { useClickOutside } from "#/hooks/useClickOutside.ts";
|
||||||
|
import { useLichess } from "#/hooks/useLichess.ts";
|
||||||
|
import type { IUserStudyChapter } from "#/interfaces.ts";
|
||||||
import type { GameAction } from "#/reducers/gameReducer.ts";
|
import type { GameAction } from "#/reducers/gameReducer.ts";
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
@@ -8,26 +16,68 @@ interface IProps {
|
|||||||
gameDispatch: Dispatch<GameAction>;
|
gameDispatch: Dispatch<GameAction>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ChapterState = {
|
||||||
|
status: "idle" | "success" | "error";
|
||||||
|
chapters: IUserStudyChapter[];
|
||||||
|
error: string | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
const initialChapterState: ChapterState = {
|
||||||
|
status: "idle",
|
||||||
|
chapters: [],
|
||||||
|
error: null,
|
||||||
|
};
|
||||||
|
|
||||||
const SelectStudyChapter = ({ selectedStudyId, gameDispatch }: IProps) => {
|
const SelectStudyChapter = ({ selectedStudyId, gameDispatch }: IProps) => {
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
const [studyChapters, setStudyChapters] = useState<IUserStudyChapter[]>([]);
|
|
||||||
const [search, setSearch] = useState("");
|
const [search, setSearch] = useState("");
|
||||||
const dropdownRef = useRef<HTMLDivElement>(null);
|
// const dropdownRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
const { getLichessStudyChapters, loadLichessStudyChapter } = useLichess();
|
const { getLichessStudyChapters, loadLichessStudyChapter } = useLichess();
|
||||||
|
|
||||||
const filteredChapters = studyChapters.filter((chapter) =>
|
const [chapterState, fetchChaptersAction, isPending] = useActionState(
|
||||||
|
async (_prev: ChapterState, _formData: FormData): Promise<ChapterState> => {
|
||||||
|
if (!selectedStudyId) {
|
||||||
|
return {
|
||||||
|
status: "error",
|
||||||
|
chapters: [],
|
||||||
|
error: "No study selected.",
|
||||||
|
} satisfies ChapterState;
|
||||||
|
}
|
||||||
|
const result = await getLichessStudyChapters({
|
||||||
|
studyId: selectedStudyId,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(result);
|
||||||
|
|
||||||
|
if (result.ok) {
|
||||||
|
return {
|
||||||
|
status: "success",
|
||||||
|
chapters: result.data,
|
||||||
|
error: null,
|
||||||
|
} satisfies ChapterState;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: "error",
|
||||||
|
chapters: [],
|
||||||
|
error: result.error,
|
||||||
|
} satisfies ChapterState;
|
||||||
|
},
|
||||||
|
initialChapterState,
|
||||||
|
);
|
||||||
|
|
||||||
|
const filteredChapters = chapterState.chapters.filter((chapter) =>
|
||||||
chapter.name.toLowerCase().includes(search.toLowerCase()),
|
chapter.name.toLowerCase().includes(search.toLowerCase()),
|
||||||
);
|
);
|
||||||
|
|
||||||
useClickOutside({ isOpen, setIsOpen, setSearch, ref: dropdownRef });
|
// useClickOutside({ isOpen, setIsOpen, setSearch, ref: dropdownRef });
|
||||||
|
|
||||||
const onClickHandler = async () => {
|
useEffect(() => {
|
||||||
const studies = await getLichessStudyChapters({ studyId: selectedStudyId });
|
if (isPending || chapterState.status === "success") {
|
||||||
if (!studies) return;
|
setIsOpen(true);
|
||||||
setStudyChapters(studies);
|
}
|
||||||
setIsOpen(!isOpen);
|
}, [isPending, chapterState.status]);
|
||||||
};
|
|
||||||
|
|
||||||
const handleChapterClick = async (chapter: IUserStudyChapter) => {
|
const handleChapterClick = async (chapter: IUserStudyChapter) => {
|
||||||
const { headers, pgn } = await loadLichessStudyChapter({ chapter });
|
const { headers, pgn } = await loadLichessStudyChapter({ chapter });
|
||||||
@@ -36,16 +86,22 @@ const SelectStudyChapter = ({ selectedStudyId, gameDispatch }: IProps) => {
|
|||||||
|
|
||||||
setIsOpen(false);
|
setIsOpen(false);
|
||||||
setSearch("");
|
setSearch("");
|
||||||
|
toast.success(`Loaded chapter "${chapter.name}"`);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div ref={dropdownRef} className="relative">
|
<form
|
||||||
<button
|
key={selectedStudyId}
|
||||||
type="button"
|
action={fetchChaptersAction}
|
||||||
className="btn btn-secondary"
|
className="relative"
|
||||||
onClick={onClickHandler}
|
|
||||||
>
|
>
|
||||||
Import Study Chapter/Game
|
<input type="hidden" name="studyId" value={selectedStudyId} />
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
className="btn btn-secondary"
|
||||||
|
disabled={!selectedStudyId || isPending}
|
||||||
|
>
|
||||||
|
{isPending ? "Loading chapters…" : "Import Chapter"}
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
{isOpen ? (
|
{isOpen ? (
|
||||||
@@ -79,7 +135,11 @@ const SelectStudyChapter = ({ selectedStudyId, gameDispatch }: IProps) => {
|
|||||||
|
|
||||||
<hr className="border-(--neutral-content)/15 my-2" />
|
<hr className="border-(--neutral-content)/15 my-2" />
|
||||||
|
|
||||||
{filteredChapters.length > 0 ? (
|
{isPending ? (
|
||||||
|
<p className="text-(--neutral-content) text-sm text-center py-4">
|
||||||
|
Loading chapters…
|
||||||
|
</p>
|
||||||
|
) : filteredChapters.length > 0 ? (
|
||||||
<ul>
|
<ul>
|
||||||
{filteredChapters.map((chapter) => (
|
{filteredChapters.map((chapter) => (
|
||||||
<li key={`${chapter.chapterId}${chapter.name}`}>
|
<li key={`${chapter.chapterId}${chapter.name}`}>
|
||||||
@@ -100,7 +160,7 @@ const SelectStudyChapter = ({ selectedStudyId, gameDispatch }: IProps) => {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
</div>
|
</form>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -9,14 +9,10 @@ import {
|
|||||||
useEffect,
|
useEffect,
|
||||||
useState,
|
useState,
|
||||||
} from "react";
|
} from "react";
|
||||||
|
import type { ISession } from "#/interfaces.ts";
|
||||||
import { getSession } from "#/server/lichess.ts";
|
import { getSession } from "#/server/lichess.ts";
|
||||||
|
|
||||||
interface ILichessUser {
|
interface ILichessUser extends Omit<ISession, "token"> {}
|
||||||
username: string;
|
|
||||||
id: string;
|
|
||||||
isLoggedIn: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ILichessUserContextType {
|
interface ILichessUserContextType {
|
||||||
user: ILichessUser | null;
|
user: ILichessUser | null;
|
||||||
@@ -38,7 +34,6 @@ export const LichessUserProvider = ({ children }: { children: ReactNode }) => {
|
|||||||
setUser({
|
setUser({
|
||||||
username: session.username,
|
username: session.username,
|
||||||
id: session.id,
|
id: session.id,
|
||||||
isLoggedIn: true,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|||||||
+7
-1
@@ -1,7 +1,11 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
const clientSchema = z.object({
|
const clientSchema = z.object({
|
||||||
VITE_API_BASE_URL: z.string("Invalid API Base URL format"),
|
VITE_API_BASE_URL: z.url("Invalid API Base URL format"),
|
||||||
|
VITE_MATOMO_URL: z.url("Invalid URL format"),
|
||||||
|
VITE_MATOMO_SITE_ID: z.string("Invalid Site ID"),
|
||||||
|
VITE_MAINTENANCE_MODE: z.string("Invalid Maintenance Mode"),
|
||||||
|
VITE_MAINTENANCE_MODE_MESSAGE: z.string().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const serverSchema = z.object({
|
const serverSchema = z.object({
|
||||||
@@ -23,6 +27,7 @@ const getEnv = () => {
|
|||||||
const _env = fullSchema.safeParse(rawEnv);
|
const _env = fullSchema.safeParse(rawEnv);
|
||||||
if (!_env.success) {
|
if (!_env.success) {
|
||||||
console.error("❌ Invalid Server Environment Variables:");
|
console.error("❌ Invalid Server Environment Variables:");
|
||||||
|
console.error(JSON.stringify(_env.error.format(), null, 2));
|
||||||
throw new Error("Invalid environment variables");
|
throw new Error("Invalid environment variables");
|
||||||
}
|
}
|
||||||
return _env.data;
|
return _env.data;
|
||||||
@@ -30,6 +35,7 @@ const getEnv = () => {
|
|||||||
const _env = clientSchema.safeParse(rawEnv);
|
const _env = clientSchema.safeParse(rawEnv);
|
||||||
if (!_env.success) {
|
if (!_env.success) {
|
||||||
console.error("❌ Invalid Client Environment Variables:");
|
console.error("❌ Invalid Client Environment Variables:");
|
||||||
|
console.error(JSON.stringify(_env.error.format(), null, 2));
|
||||||
throw new Error("Invalid environment variables");
|
throw new Error("Invalid environment variables");
|
||||||
}
|
}
|
||||||
return _env.data as z.infer<typeof fullSchema>;
|
return _env.data as z.infer<typeof fullSchema>;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { useServerFn } from "@tanstack/react-start";
|
||||||
import {
|
import {
|
||||||
type ChangeEvent,
|
type ChangeEvent,
|
||||||
useCallback,
|
useCallback,
|
||||||
@@ -9,6 +10,7 @@ import { toast } from "react-toastify";
|
|||||||
import { env } from "#/env.ts";
|
import { env } from "#/env.ts";
|
||||||
import type { IHeader, IPosition } from "#/interfaces.ts";
|
import type { IHeader, IPosition } from "#/interfaces.ts";
|
||||||
import { gameReducer, initialGameState } from "#/reducers/gameReducer.ts";
|
import { gameReducer, initialGameState } from "#/reducers/gameReducer.ts";
|
||||||
|
import { recordPdfSuccess } from "#/server/pdfMetrics.ts";
|
||||||
import { downloadPDF } from "#/utils/pdfUtils.ts";
|
import { downloadPDF } from "#/utils/pdfUtils.ts";
|
||||||
import { buildPgnString, getHeaders } from "#/utils/pgnUtils.ts";
|
import { buildPgnString, getHeaders } from "#/utils/pgnUtils.ts";
|
||||||
import { downloadString } from "#/utils/stringUtils.ts";
|
import { downloadString } from "#/utils/stringUtils.ts";
|
||||||
@@ -16,6 +18,8 @@ import { downloadString } from "#/utils/stringUtils.ts";
|
|||||||
export const useChessGame = () => {
|
export const useChessGame = () => {
|
||||||
const [gameState, gameDispatch] = useReducer(gameReducer, initialGameState);
|
const [gameState, gameDispatch] = useReducer(gameReducer, initialGameState);
|
||||||
|
|
||||||
|
const recordPdfSuccessFn = useServerFn(recordPdfSuccess);
|
||||||
|
|
||||||
const [currentPosition, setCurrentPosition] = useState<IPosition>({
|
const [currentPosition, setCurrentPosition] = useState<IPosition>({
|
||||||
ply: 0,
|
ply: 0,
|
||||||
fen: "",
|
fen: "",
|
||||||
@@ -66,7 +70,7 @@ export const useChessGame = () => {
|
|||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
downloadPDF(await response.blob());
|
downloadPDF(await response.blob());
|
||||||
|
|
||||||
// TODO add metrics logger - add success to analytics
|
await recordPdfSuccessFn();
|
||||||
|
|
||||||
toast.success("PDF successfully generated!", {
|
toast.success("PDF successfully generated!", {
|
||||||
toastId: "pdf-success",
|
toastId: "pdf-success",
|
||||||
|
|||||||
+83
-39
@@ -1,7 +1,11 @@
|
|||||||
import { useNavigate } from "@tanstack/react-router";
|
|
||||||
import { useServerFn } from "@tanstack/react-start";
|
import { useServerFn } from "@tanstack/react-start";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useLichessUser } from "#/context/LichessUserContext.tsx";
|
import { useLichessUser } from "#/context/LichessUserContext.tsx";
|
||||||
|
import type {
|
||||||
|
ITokenData,
|
||||||
|
IUserStudy,
|
||||||
|
IUserStudyChapter,
|
||||||
|
} from "#/interfaces.ts";
|
||||||
import {
|
import {
|
||||||
getSession,
|
getSession,
|
||||||
getStudyChapters,
|
getStudyChapters,
|
||||||
@@ -14,32 +18,37 @@ import {
|
|||||||
} from "#/server/lichess.ts";
|
} from "#/server/lichess.ts";
|
||||||
import { getHeaders } from "#/utils/pgnUtils.ts";
|
import { getHeaders } from "#/utils/pgnUtils.ts";
|
||||||
|
|
||||||
interface ITokenData {
|
type Status = "loading" | "success" | "error";
|
||||||
access_token: string;
|
|
||||||
expires_in: number;
|
|
||||||
token_type: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type IUserStudy = {
|
export type LichessResult<T> =
|
||||||
id: string;
|
| { ok: true; data: T }
|
||||||
name: string;
|
| { ok: false; error: string };
|
||||||
createdAt: number;
|
|
||||||
updatedAt: number;
|
const LICHESS_STUDY_LINK_RE =
|
||||||
|
/^https?:\/\/lichess\.org\/study\/([A-Za-z0-9]{8})(?:\.pgn)?(?:$|\/|\?|#)/i;
|
||||||
|
|
||||||
|
const lichessFetch = async <T>(
|
||||||
|
fn: () => Promise<T | undefined>,
|
||||||
|
fallbackError: string,
|
||||||
|
): Promise<LichessResult<T>> => {
|
||||||
|
try {
|
||||||
|
const data = await fn();
|
||||||
|
if (data === undefined || data === null) {
|
||||||
|
return { ok: false, error: fallbackError };
|
||||||
|
}
|
||||||
|
return { ok: true, data };
|
||||||
|
} catch (err) {
|
||||||
|
return {
|
||||||
|
ok: false,
|
||||||
|
error: err instanceof Error ? err.message : fallbackError,
|
||||||
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface IUserStudyChapter {
|
|
||||||
chapterId: string;
|
|
||||||
name: string;
|
|
||||||
pgn: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const useLichess = () => {
|
export const useLichess = () => {
|
||||||
const { user, setUser } = useLichessUser();
|
const { user, setUser } = useLichessUser();
|
||||||
const [userStudies, setUserStudies] = useState<IUserStudy[]>([]);
|
const [userStudies, setUserStudies] = useState<IUserStudy[]>([]);
|
||||||
const [selectedStudyId, setSelectedStudyId] = useState<string | null>(null);
|
const [selectedStudyId, setSelectedStudyId] = useState<string | null>(null);
|
||||||
const [studyChapters, setStudyChapters] = useState<
|
|
||||||
IUserStudyChapter[] | null
|
|
||||||
>(null);
|
|
||||||
|
|
||||||
const loginFn = useServerFn(login);
|
const loginFn = useServerFn(login);
|
||||||
const logoutFn = useServerFn(logout);
|
const logoutFn = useServerFn(logout);
|
||||||
@@ -64,7 +73,7 @@ export const useLichess = () => {
|
|||||||
token: string;
|
token: string;
|
||||||
}) => {
|
}) => {
|
||||||
await setSessionFn({ data: { username, id, token } });
|
await setSessionFn({ data: { username, id, token } });
|
||||||
setUser({ username, id, isLoggedIn: true });
|
setUser({ username, id });
|
||||||
};
|
};
|
||||||
|
|
||||||
const getLichessUser = async ({ tokenData }: { tokenData: ITokenData }) => {
|
const getLichessUser = async ({ tokenData }: { tokenData: ITokenData }) => {
|
||||||
@@ -85,40 +94,59 @@ export const useLichess = () => {
|
|||||||
return getSessionFn();
|
return getSessionFn();
|
||||||
};
|
};
|
||||||
|
|
||||||
const useLichessCallback = async ({ code }: { code: string }) => {
|
const useLichessCallback = ({ code }: { code: string }) => {
|
||||||
const navigate = useNavigate();
|
const [status, setStatus] = useState<Status>("loading");
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!code) return;
|
if (!code) return;
|
||||||
|
|
||||||
|
const isMounted = true;
|
||||||
|
|
||||||
|
try {
|
||||||
(async () => {
|
(async () => {
|
||||||
const tokenData = await lichessAccessToken({ code });
|
const tokenData = await lichessAccessToken({ code });
|
||||||
const userData = await getLichessUser({ tokenData });
|
const userData = await getLichessUser({ tokenData });
|
||||||
|
|
||||||
|
if (!isMounted) return;
|
||||||
|
|
||||||
await setLichessUser({
|
await setLichessUser({
|
||||||
username: userData.username,
|
username: userData.username,
|
||||||
id: userData.id,
|
id: userData.id,
|
||||||
token: tokenData.access_token,
|
token: tokenData.access_token,
|
||||||
});
|
});
|
||||||
await navigate({ to: "/chessboard" });
|
|
||||||
|
setStatus("success");
|
||||||
})();
|
})();
|
||||||
}, [navigate, code]);
|
} catch (err: unknown) {
|
||||||
|
if (isMounted && err instanceof Error) {
|
||||||
|
console.error("Lichess Authentication Error:", err);
|
||||||
|
setError(err.message || "Failed to authenticate with Lichess");
|
||||||
|
setStatus("error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [code]);
|
||||||
|
|
||||||
|
return { status, error };
|
||||||
};
|
};
|
||||||
|
|
||||||
const getLichessUserStudies = async () => {
|
const getLichessUserStudies = async (): Promise<
|
||||||
if (!user) throw new Error("User not found");
|
LichessResult<IUserStudy[]>
|
||||||
const studies: IUserStudy[] | undefined = await getUserStudiesFn();
|
> => {
|
||||||
if (!studies) return;
|
if (!user) return { ok: false, error: "Not signed in to Lichess." };
|
||||||
setUserStudies(studies);
|
return lichessFetch(() => getUserStudiesFn(), "Failed to load studies.");
|
||||||
};
|
};
|
||||||
|
|
||||||
const getLichessStudyChapters = async ({ studyId }: { studyId: string }) => {
|
const getLichessStudyChapters = async ({
|
||||||
if (!user) throw new Error("User not found");
|
studyId,
|
||||||
const chapters: IUserStudyChapter[] | undefined = await getStudyChaptersFn({
|
}: {
|
||||||
data: { studyId },
|
studyId: string;
|
||||||
});
|
}): Promise<LichessResult<IUserStudyChapter[]>> => {
|
||||||
if (!chapters) return;
|
if (!user) return { ok: false, error: "Not signed in to Lichess." };
|
||||||
return chapters;
|
return lichessFetch(
|
||||||
|
() => getStudyChaptersFn({ data: { studyId } }),
|
||||||
|
"Failed to load chapters.",
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
// return headers and pgn from the Lichess chapter
|
// return headers and pgn from the Lichess chapter
|
||||||
@@ -141,6 +169,23 @@ export const useLichess = () => {
|
|||||||
return { headers, pgn };
|
return { headers, pgn };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// parse and process lichess study link
|
||||||
|
const parseLichessStudyLink = (studyLink: string): LichessResult<string> => {
|
||||||
|
const trimmed = studyLink.trim();
|
||||||
|
|
||||||
|
if (!trimmed) {
|
||||||
|
return { ok: false, error: "Please paste a Lichess study link." };
|
||||||
|
}
|
||||||
|
|
||||||
|
const match = trimmed.match(LICHESS_STUDY_LINK_RE);
|
||||||
|
|
||||||
|
if (!match) {
|
||||||
|
return { ok: false, error: "Invalid Lichess study link." };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { ok: true, data: match[1] };
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
lichessLogin,
|
lichessLogin,
|
||||||
lichessLogout,
|
lichessLogout,
|
||||||
@@ -155,8 +200,7 @@ export const useLichess = () => {
|
|||||||
setSelectedStudyId,
|
setSelectedStudyId,
|
||||||
selectedStudyId,
|
selectedStudyId,
|
||||||
getLichessStudyChapters,
|
getLichessStudyChapters,
|
||||||
studyChapters,
|
|
||||||
setStudyChapters,
|
|
||||||
loadLichessStudyChapter,
|
loadLichessStudyChapter,
|
||||||
|
parseLichessStudyLink,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,3 +1,22 @@
|
|||||||
|
import type { infer } from "zod";
|
||||||
|
import type { SessionSchema, TokenResponseSchema } from "#/schemas.ts";
|
||||||
|
|
||||||
|
export interface ISession extends infer<typeof SessionSchema> {}
|
||||||
|
export interface ITokenData extends infer<typeof TokenResponseSchema> {}
|
||||||
|
|
||||||
|
export interface IUserStudy {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
createdAt: number;
|
||||||
|
updatedAt: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IUserStudyChapter {
|
||||||
|
chapterId: string;
|
||||||
|
name: string;
|
||||||
|
pgn: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface IPosition {
|
export interface IPosition {
|
||||||
ply: number;
|
ply: number;
|
||||||
fen: string;
|
fen: string;
|
||||||
|
|||||||
+12
-3
@@ -1,14 +1,23 @@
|
|||||||
import { getRouteApi } from "@tanstack/react-router";
|
import { getRouteApi, useNavigate } from "@tanstack/react-router";
|
||||||
|
import GenericErrorView from "#/components/GenericErrorView.tsx";
|
||||||
|
import LoadingView from "#/components/LoadingView.tsx";
|
||||||
import { useLichess } from "#/hooks/useLichess.ts";
|
import { useLichess } from "#/hooks/useLichess.ts";
|
||||||
|
|
||||||
const routeApi = getRouteApi("/callback");
|
const routeApi = getRouteApi("/callback");
|
||||||
|
|
||||||
const Callback = () => {
|
const Callback = () => {
|
||||||
|
const navigate = useNavigate();
|
||||||
const { useLichessCallback } = useLichess();
|
const { useLichessCallback } = useLichess();
|
||||||
const { code } = routeApi.useSearch();
|
const { code } = routeApi.useSearch();
|
||||||
useLichessCallback({ code }).then();
|
const { status, error } = useLichessCallback({ code });
|
||||||
|
|
||||||
return <div>Loading...</div>;
|
if (status === "success") navigate({ to: "/chessboard" });
|
||||||
|
|
||||||
|
if (status === "error") return <GenericErrorView error={error} />;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<LoadingView title="Logging in..." description="Please wait a moment..." />
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Callback;
|
export default Callback;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { lazy, Suspense } from "react";
|
|||||||
import CustomHeaders from "#/components/CustomHeaders.tsx";
|
import CustomHeaders from "#/components/CustomHeaders.tsx";
|
||||||
import HeaderFields from "#/components/HeaderFields.tsx";
|
import HeaderFields from "#/components/HeaderFields.tsx";
|
||||||
import LichessButton from "#/components/LichessButton.tsx";
|
import LichessButton from "#/components/LichessButton.tsx";
|
||||||
|
import LichessStudyLinkInput from "#/components/LichessStudyLinkInput.tsx";
|
||||||
import Section from "#/components/Section.tsx";
|
import Section from "#/components/Section.tsx";
|
||||||
import SelectLichessStudy from "#/components/SelectLichessStudy.tsx";
|
import SelectLichessStudy from "#/components/SelectLichessStudy.tsx";
|
||||||
import SelectStudyChapter from "#/components/SelectStudyChapter.tsx";
|
import SelectStudyChapter from "#/components/SelectStudyChapter.tsx";
|
||||||
@@ -38,8 +39,7 @@ const Chessboard = () => {
|
|||||||
gameDispatch,
|
gameDispatch,
|
||||||
} = useChessGame();
|
} = useChessGame();
|
||||||
|
|
||||||
const { selectedStudyId, setSelectedStudyId, setStudyChapters } =
|
const { selectedStudyId, setSelectedStudyId } = useLichess();
|
||||||
useLichess();
|
|
||||||
|
|
||||||
const { user } = useLichessUser();
|
const { user } = useLichessUser();
|
||||||
|
|
||||||
@@ -55,18 +55,21 @@ const Chessboard = () => {
|
|||||||
onChange={handleLoadPgn}
|
onChange={handleLoadPgn}
|
||||||
/>
|
/>
|
||||||
{user ? (
|
{user ? (
|
||||||
<SelectLichessStudy
|
<SelectLichessStudy setSelectedStudyId={setSelectedStudyId} />
|
||||||
setSelectedStudyId={setSelectedStudyId}
|
|
||||||
setStudyChapters={setStudyChapters}
|
|
||||||
/>
|
|
||||||
) : null}
|
) : null}
|
||||||
<div />
|
<div />
|
||||||
{selectedStudyId && user ? (
|
{user ? (
|
||||||
<SelectStudyChapter
|
<SelectStudyChapter
|
||||||
selectedStudyId={selectedStudyId || ""}
|
selectedStudyId={selectedStudyId || ""}
|
||||||
gameDispatch={gameDispatch}
|
gameDispatch={gameDispatch}
|
||||||
/>
|
/>
|
||||||
) : null}
|
) : null}
|
||||||
|
{user ? (
|
||||||
|
<div className="flex flex-col">
|
||||||
|
<p className="text-center">or</p>
|
||||||
|
<LichessStudyLinkInput setSelectedStudyId={setSelectedStudyId} />
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
</Section>
|
</Section>
|
||||||
|
|
||||||
<div className="grid lg:grid-cols-2 gap-4">
|
<div className="grid lg:grid-cols-2 gap-4">
|
||||||
|
|||||||
+7
-7
@@ -1,19 +1,19 @@
|
|||||||
import { createRouter as createTanStackRouter } from '@tanstack/react-router'
|
import { createRouter as createTanStackRouter } from "@tanstack/react-router";
|
||||||
import { routeTree } from './routeTree.gen'
|
import { routeTree } from "./routeTree.gen";
|
||||||
|
|
||||||
export function getRouter() {
|
export function getRouter() {
|
||||||
const router = createTanStackRouter({
|
const router = createTanStackRouter({
|
||||||
routeTree,
|
routeTree,
|
||||||
scrollRestoration: true,
|
scrollRestoration: true,
|
||||||
defaultPreload: 'intent',
|
defaultPreload: "intent",
|
||||||
defaultPreloadStaleTime: 0,
|
defaultPreloadStaleTime: 0,
|
||||||
})
|
});
|
||||||
|
|
||||||
return router
|
return router;
|
||||||
}
|
}
|
||||||
|
|
||||||
declare module '@tanstack/react-router' {
|
declare module "@tanstack/react-router" {
|
||||||
interface Register {
|
interface Register {
|
||||||
router: ReturnType<typeof getRouter>
|
router: ReturnType<typeof getRouter>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,10 @@ import { createRootRoute, HeadContent, Scripts } from "@tanstack/react-router";
|
|||||||
import { TanStackRouterDevtoolsPanel } from "@tanstack/react-router-devtools";
|
import { TanStackRouterDevtoolsPanel } from "@tanstack/react-router-devtools";
|
||||||
import { type ReactNode, Suspense } from "react";
|
import { type ReactNode, Suspense } from "react";
|
||||||
import { ToastContainer } from "react-toastify";
|
import { ToastContainer } from "react-toastify";
|
||||||
|
import GenericErrorView from "#/components/GenericErrorView.tsx";
|
||||||
|
import LoadingView from "#/components/LoadingView.tsx";
|
||||||
import { MatomoAnalytics } from "#/components/MatomoAnalytics.tsx";
|
import { MatomoAnalytics } from "#/components/MatomoAnalytics.tsx";
|
||||||
|
import NotFoundView from "#/components/NotFoundView.tsx";
|
||||||
import { LichessUserProvider } from "#/context/LichessUserContext.tsx";
|
import { LichessUserProvider } from "#/context/LichessUserContext.tsx";
|
||||||
import Footer from "../components/Footer";
|
import Footer from "../components/Footer";
|
||||||
import Header from "../components/Header";
|
import Header from "../components/Header";
|
||||||
@@ -39,10 +42,27 @@ export const Route = createRootRoute({
|
|||||||
{
|
{
|
||||||
rel: "icon",
|
rel: "icon",
|
||||||
href: "/favicon.svg",
|
href: "/favicon.svg",
|
||||||
|
type: "image/svg+xml",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
rel: "icon",
|
||||||
|
href: "/favicon.ico",
|
||||||
|
sizes: "32x32",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
rel: "apple-touch-icon",
|
||||||
|
href: "/apple-touch-icon.png",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
rel: "manifest",
|
||||||
|
href: "/site.webmanifest",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
shellComponent: RootDocument,
|
shellComponent: RootDocument,
|
||||||
|
errorComponent: ({ error }) => <GenericErrorView error={error} />,
|
||||||
|
pendingComponent: () => <LoadingView />,
|
||||||
|
notFoundComponent: () => <NotFoundView />,
|
||||||
});
|
});
|
||||||
|
|
||||||
function RootDocument({ children }: { children: ReactNode }) {
|
function RootDocument({ children }: { children: ReactNode }) {
|
||||||
|
|||||||
+14
-3
@@ -1,16 +1,27 @@
|
|||||||
import { createFileRoute } from "@tanstack/react-router";
|
import { createFileRoute, notFound } from "@tanstack/react-router";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
import GenericErrorView from "#/components/GenericErrorView.tsx";
|
||||||
import Callback from "#/pages/Callback.tsx";
|
import Callback from "#/pages/Callback.tsx";
|
||||||
|
|
||||||
const callbackSchema = z.object({
|
const callbackSchema = z.object({
|
||||||
code: z.string(),
|
code: z.string(),
|
||||||
});
|
});
|
||||||
|
|
||||||
// type CallbackProps = z.infer<typeof callbackSchema>;
|
|
||||||
|
|
||||||
export const Route = createFileRoute("/callback")({
|
export const Route = createFileRoute("/callback")({
|
||||||
|
head: () => ({
|
||||||
|
meta: [{ name: "robots", content: "noindex, nofollow" }],
|
||||||
|
}),
|
||||||
|
|
||||||
validateSearch: (search) => callbackSchema.parse(search),
|
validateSearch: (search) => callbackSchema.parse(search),
|
||||||
|
|
||||||
|
beforeLoad: ({ search }) => {
|
||||||
|
if (!search.code) {
|
||||||
|
throw notFound();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
component: RouteComponent,
|
component: RouteComponent,
|
||||||
|
errorComponent: ({ error }) => <GenericErrorView error={error} />,
|
||||||
});
|
});
|
||||||
|
|
||||||
function RouteComponent() {
|
function RouteComponent() {
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
export const SessionSchema = z.object({
|
||||||
|
username: z.string(),
|
||||||
|
id: z.string(),
|
||||||
|
token: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const TokenResponseSchema = z.object({
|
||||||
|
access_token: z.string(),
|
||||||
|
expires_in: z.number(),
|
||||||
|
scope: z.string().optional(),
|
||||||
|
token_type: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const LichessAccountSchema = z.object({
|
||||||
|
id: z.string(),
|
||||||
|
username: z.string(),
|
||||||
|
});
|
||||||
+24
-25
@@ -6,8 +6,14 @@ import {
|
|||||||
getCookie,
|
getCookie,
|
||||||
setCookie,
|
setCookie,
|
||||||
} from "@tanstack/react-start/server";
|
} from "@tanstack/react-start/server";
|
||||||
|
import { z } from "zod";
|
||||||
import { env } from "#/env.ts";
|
import { env } from "#/env.ts";
|
||||||
import { base64UrlEncode, sha256 } from "#/lib/encodeDecode.ts";
|
import { base64UrlEncode, sha256 } from "#/lib/encodeDecode.ts";
|
||||||
|
import {
|
||||||
|
LichessAccountSchema,
|
||||||
|
SessionSchema,
|
||||||
|
TokenResponseSchema,
|
||||||
|
} from "#/schemas.ts";
|
||||||
|
|
||||||
const createVerifier = () => base64UrlEncode(randomBytes(32));
|
const createVerifier = () => base64UrlEncode(randomBytes(32));
|
||||||
const createChallenge = (verifier: string) => base64UrlEncode(sha256(verifier));
|
const createChallenge = (verifier: string) => base64UrlEncode(sha256(verifier));
|
||||||
@@ -22,11 +28,8 @@ export const getSession = createServerFn({ method: "GET" }).handler(
|
|||||||
const session = getCookie("lichess-session");
|
const session = getCookie("lichess-session");
|
||||||
if (!session) return null;
|
if (!session) return null;
|
||||||
try {
|
try {
|
||||||
return JSON.parse(session) as {
|
const parsed = SessionSchema.safeParse(JSON.parse(session));
|
||||||
username: string;
|
return parsed.success ? parsed.data : null;
|
||||||
id: string;
|
|
||||||
token: string;
|
|
||||||
};
|
|
||||||
} catch {
|
} catch {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -34,25 +37,15 @@ export const getSession = createServerFn({ method: "GET" }).handler(
|
|||||||
);
|
);
|
||||||
|
|
||||||
export const setSession = createServerFn({ method: "POST" })
|
export const setSession = createServerFn({ method: "POST" })
|
||||||
.inputValidator(
|
.inputValidator(SessionSchema)
|
||||||
(data: { username: string; id: string; token: string }) => data,
|
|
||||||
)
|
|
||||||
.handler(async ({ data }) => {
|
.handler(async ({ data }) => {
|
||||||
setCookie(
|
setCookie("lichess-session", JSON.stringify(data), {
|
||||||
"lichess-session",
|
|
||||||
JSON.stringify({
|
|
||||||
username: data.username,
|
|
||||||
id: data.id,
|
|
||||||
token: data.token,
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
path: "/",
|
path: "/",
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
secure: process.env.NODE_ENV === "production",
|
secure: process.env.NODE_ENV === "production",
|
||||||
sameSite: "lax",
|
sameSite: "lax",
|
||||||
maxAge: 60 * 60 * 24 * 7,
|
maxAge: 60 * 60 * 24 * 7,
|
||||||
},
|
});
|
||||||
);
|
|
||||||
|
|
||||||
setCookie("lichess-oauth-verifier", "", {
|
setCookie("lichess-oauth-verifier", "", {
|
||||||
path: "/",
|
path: "/",
|
||||||
@@ -66,12 +59,13 @@ export const setSession = createServerFn({ method: "POST" })
|
|||||||
});
|
});
|
||||||
|
|
||||||
export const getToken = createServerFn({ method: "POST" })
|
export const getToken = createServerFn({ method: "POST" })
|
||||||
.inputValidator((data: { code: string }) => data)
|
.inputValidator(z.object({ code: z.string() }))
|
||||||
.handler(async ({ data }) => {
|
.handler(async ({ data }) => {
|
||||||
const verifier = getCookie("lichess-oauth-verifier");
|
const verifier = getCookie("lichess-oauth-verifier");
|
||||||
|
|
||||||
if (!verifier)
|
if (!verifier) {
|
||||||
throw new Error("OAuth session expired. Please login again.");
|
throw new Error("OAuth session expired. Please login again.");
|
||||||
|
}
|
||||||
|
|
||||||
const response = await fetch("https://lichess.org/api/token", {
|
const response = await fetch("https://lichess.org/api/token", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@@ -87,14 +81,16 @@ export const getToken = createServerFn({ method: "POST" })
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok)
|
if (!response.ok) {
|
||||||
throw new Error(`Token exchange failed: ${await response.text()}`);
|
throw new Error(`Token exchange failed: ${await response.text()}`);
|
||||||
|
}
|
||||||
|
|
||||||
return response.json();
|
const json = await response.json();
|
||||||
|
return TokenResponseSchema.parse(json);
|
||||||
});
|
});
|
||||||
|
|
||||||
export const getUser = createServerFn({ method: "GET" })
|
export const getUser = createServerFn({ method: "POST" })
|
||||||
.inputValidator((data: { access_token: string }) => data)
|
.inputValidator(z.object({ access_token: z.string() }))
|
||||||
.handler(async ({ data }) => {
|
.handler(async ({ data }) => {
|
||||||
const response = await fetch("https://lichess.org/api/account", {
|
const response = await fetch("https://lichess.org/api/account", {
|
||||||
headers: {
|
headers: {
|
||||||
@@ -102,7 +98,10 @@ export const getUser = createServerFn({ method: "GET" })
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return response.json();
|
if (!response.ok) throw new Error("Failed to fetch Lichess account");
|
||||||
|
|
||||||
|
const json = await response.json();
|
||||||
|
return LichessAccountSchema.parse(json);
|
||||||
});
|
});
|
||||||
|
|
||||||
const deleteAccessToken = createServerFn().handler(async () => {
|
const deleteAccessToken = createServerFn().handler(async () => {
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
import { promises as fs } from "node:fs";
|
||||||
|
import { join } from "node:path";
|
||||||
|
import { createServerFn } from "@tanstack/react-start";
|
||||||
|
|
||||||
|
const METRICS_DIR = join(process.cwd(), "data");
|
||||||
|
const LOG_FILE = join(METRICS_DIR, "pdf-events.log");
|
||||||
|
const COUNT_FILE = join(METRICS_DIR, "pdf-count.txt");
|
||||||
|
|
||||||
|
const readCount = async (): Promise<number> => {
|
||||||
|
try {
|
||||||
|
const n = Number.parseInt(
|
||||||
|
(await fs.readFile(COUNT_FILE, "utf-8")).trim(),
|
||||||
|
10,
|
||||||
|
);
|
||||||
|
return Number.isFinite(n) ? n : 0;
|
||||||
|
} catch {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const writeCountAtomic = async (count: number) => {
|
||||||
|
const tmp = `${COUNT_FILE}.tmp`;
|
||||||
|
await fs.writeFile(tmp, String(count), "utf-8");
|
||||||
|
await fs.rename(tmp, COUNT_FILE);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const recordPdfSuccess = createServerFn({ method: "POST" }).handler(
|
||||||
|
async () => {
|
||||||
|
try {
|
||||||
|
await fs.mkdir(METRICS_DIR, { recursive: true });
|
||||||
|
const next = (await readCount()) + 1;
|
||||||
|
await Promise.all([
|
||||||
|
fs.appendFile(LOG_FILE, `${new Date().toISOString()}\n`, "utf-8"),
|
||||||
|
writeCountAtomic(next),
|
||||||
|
]);
|
||||||
|
return { count: next };
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Failed to record PDF success:", err);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
+13
-13
@@ -17,6 +17,19 @@
|
|||||||
border-bottom: 1px solid var(--neutral-content);
|
border-bottom: 1px solid var(--neutral-content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.hero__accordion-item__content {
|
||||||
|
padding: 0 10px;
|
||||||
|
display: grid;
|
||||||
|
grid-template-rows: 0fr;
|
||||||
|
transition:
|
||||||
|
grid-template-rows 0.3s ease,
|
||||||
|
padding 0.3s ease;
|
||||||
|
|
||||||
|
@media (prefers-reduced-motion) {
|
||||||
|
transition: none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.hero__accordion-details[open] + .hero__accordion-item__content {
|
.hero__accordion-details[open] + .hero__accordion-item__content {
|
||||||
grid-template-rows: 1fr;
|
grid-template-rows: 1fr;
|
||||||
padding: 10px;
|
padding: 10px;
|
||||||
@@ -53,19 +66,6 @@
|
|||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
}
|
}
|
||||||
|
|
||||||
.hero__accordion-item__content {
|
|
||||||
padding: 0 10px;
|
|
||||||
display: grid;
|
|
||||||
grid-template-rows: 0fr;
|
|
||||||
transition:
|
|
||||||
grid-template-rows 0.3s ease,
|
|
||||||
padding 0.3s ease;
|
|
||||||
|
|
||||||
@media (prefers-reduced-motion) {
|
|
||||||
transition: none;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
.hero__accordion-item__content__inner {
|
.hero__accordion-item__content__inner {
|
||||||
color: var(--neutral-content);
|
color: var(--neutral-content);
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
|
|||||||
+688
-1
File diff suppressed because one or more lines are too long
@@ -19,6 +19,17 @@ const config = defineConfig({
|
|||||||
enabled: true,
|
enabled: true,
|
||||||
crawlLinks: true,
|
crawlLinks: true,
|
||||||
},
|
},
|
||||||
|
pages: [
|
||||||
|
{
|
||||||
|
path: "/callback",
|
||||||
|
prerender: {
|
||||||
|
enabled: false,
|
||||||
|
},
|
||||||
|
sitemap: {
|
||||||
|
exclude: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
sitemap: {
|
sitemap: {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
host: HOST_URL,
|
host: HOST_URL,
|
||||||
|
|||||||
Reference in New Issue
Block a user