refactor: use tabs instead of spaces

This commit is contained in:
Sonny
2024-10-07 01:33:59 +02:00
parent f425decf2c
commit eea9732100
197 changed files with 5206 additions and 5209 deletions

View File

@@ -3,7 +3,7 @@
root = true root = true
[*] [*]
indent_style = space indent_style = tab
indent_size = 2 indent_size = 2
end_of_line = lf end_of_line = lf
charset = utf-8 charset = utf-8
@@ -17,8 +17,5 @@ insert_final_newline = unset
indent_style = unset indent_style = unset
insert_final_newline = unset insert_final_newline = unset
[MakeFile]
indent_style = space
[*.md] [*.md]
trim_trailing_whitespace = false trim_trailing_whitespace = false

View File

@@ -1,7 +1,7 @@
import { defineConfig } from '@adonisjs/core/app'; import { defineConfig } from '@adonisjs/core/app';
export default defineConfig({ export default defineConfig({
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Commands | Commands
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@@ -10,13 +10,13 @@ export default defineConfig({
| will be scanned automatically from the "./commands" directory. | will be scanned automatically from the "./commands" directory.
| |
*/ */
commands: [ commands: [
() => import('@adonisjs/core/commands'), () => import('@adonisjs/core/commands'),
() => import('@adonisjs/lucid/commands'), () => import('@adonisjs/lucid/commands'),
() => import('@izzyjs/route/commands'), () => import('@izzyjs/route/commands'),
], ],
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Service providers | Service providers
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@@ -25,29 +25,29 @@ export default defineConfig({
| application | application
| |
*/ */
providers: [ providers: [
() => import('@adonisjs/core/providers/app_provider'), () => import('@adonisjs/core/providers/app_provider'),
() => import('@adonisjs/core/providers/hash_provider'), () => import('@adonisjs/core/providers/hash_provider'),
{ {
file: () => import('@adonisjs/core/providers/repl_provider'), file: () => import('@adonisjs/core/providers/repl_provider'),
environment: ['repl', 'test'], environment: ['repl', 'test'],
}, },
() => import('@adonisjs/core/providers/vinejs_provider'), () => import('@adonisjs/core/providers/vinejs_provider'),
() => import('@adonisjs/core/providers/edge_provider'), () => import('@adonisjs/core/providers/edge_provider'),
() => import('@adonisjs/session/session_provider'), () => import('@adonisjs/session/session_provider'),
() => import('@adonisjs/vite/vite_provider'), () => import('@adonisjs/vite/vite_provider'),
() => import('@adonisjs/shield/shield_provider'), () => import('@adonisjs/shield/shield_provider'),
() => import('@adonisjs/static/static_provider'), () => import('@adonisjs/static/static_provider'),
() => import('@adonisjs/cors/cors_provider'), () => import('@adonisjs/cors/cors_provider'),
() => import('@adonisjs/lucid/database_provider'), () => import('@adonisjs/lucid/database_provider'),
() => import('@adonisjs/auth/auth_provider'), () => import('@adonisjs/auth/auth_provider'),
() => import('@adonisjs/inertia/inertia_provider'), () => import('@adonisjs/inertia/inertia_provider'),
() => import('@adonisjs/ally/ally_provider'), () => import('@adonisjs/ally/ally_provider'),
() => import('@izzyjs/route/izzy_provider'), () => import('@izzyjs/route/izzy_provider'),
() => import('#providers/route_provider'), () => import('#providers/route_provider'),
], ],
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Preloads | Preloads
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@@ -55,9 +55,9 @@ export default defineConfig({
| List of modules to import before starting the application. | List of modules to import before starting the application.
| |
*/ */
preloads: [() => import('#start/routes'), () => import('#start/kernel')], preloads: [() => import('#start/routes'), () => import('#start/kernel')],
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Tests | Tests
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@@ -66,23 +66,23 @@ export default defineConfig({
| and add additional suites. | and add additional suites.
| |
*/ */
tests: { tests: {
suites: [ suites: [
{ {
files: ['tests/unit/**/*.spec(.ts|.js)'], files: ['tests/unit/**/*.spec(.ts|.js)'],
name: 'unit', name: 'unit',
timeout: 2000, timeout: 2000,
}, },
{ {
files: ['tests/functional/**/*.spec(.ts|.js)'], files: ['tests/functional/**/*.spec(.ts|.js)'],
name: 'functional', name: 'functional',
timeout: 30000, timeout: 30000,
}, },
], ],
forceExit: false, forceExit: false,
}, },
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Metafiles | Metafiles
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@@ -91,20 +91,20 @@ export default defineConfig({
| the production build. | the production build.
| |
*/ */
metaFiles: [ metaFiles: [
{ {
pattern: 'resources/views/**/*.edge', pattern: 'resources/views/**/*.edge',
reloadServer: false, reloadServer: false,
}, },
{ {
pattern: 'public/**', pattern: 'public/**',
reloadServer: false, reloadServer: false,
}, },
], ],
assetsBundler: false, assetsBundler: false,
unstable_assembler: { unstable_assembler: {
onBuildStarting: [() => import('@adonisjs/vite/build_hook')], onBuildStarting: [() => import('@adonisjs/vite/build_hook')],
onDevServerStarted: [() => import('@izzyjs/route/dev_hook')], onDevServerStarted: [() => import('@izzyjs/route/dev_hook')],
}, },
}); });

View File

@@ -8,12 +8,12 @@ const ARROW_UP = 'ArrowUp';
const ARROW_DOWN = 'ArrowDown'; const ARROW_DOWN = 'ArrowDown';
const KEYS = { const KEYS = {
ARROW_DOWN, ARROW_DOWN,
ARROW_UP, ARROW_UP,
ESCAPE_KEY, ESCAPE_KEY,
OPEN_CREATE_COLLECTION_KEY, OPEN_CREATE_COLLECTION_KEY,
OPEN_CREATE_LINK_KEY, OPEN_CREATE_LINK_KEY,
OPEN_SEARCH_KEY, OPEN_SEARCH_KEY,
}; };
export default KEYS; export default KEYS;

View File

@@ -1,8 +1,8 @@
const PATHS = { const PATHS = {
AUTHOR: 'https://www.sonny.dev/', AUTHOR: 'https://www.sonny.dev/',
REPO_GITHUB: 'https://github.com/Sonny93/my-links', REPO_GITHUB: 'https://github.com/Sonny93/my-links',
EXTENSION: EXTENSION:
'https://chromewebstore.google.com/detail/mylinks/agkmlplihacolkakgeccnbhphnepphma', 'https://chromewebstore.google.com/detail/mylinks/agkmlplihacolkakgeccnbhphnepphma',
} as const; } as const;
export default PATHS; export default PATHS;

View File

@@ -6,41 +6,41 @@ import { inject } from '@adonisjs/core';
import type { HttpContext } from '@adonisjs/core/http'; import type { HttpContext } from '@adonisjs/core/http';
class UserWithRelationCountDto { class UserWithRelationCountDto {
constructor(private user: User) {} constructor(private user: User) {}
toJson = () => ({ toJson = () => ({
id: this.user.id, id: this.user.id,
email: this.user.email, email: this.user.email,
fullname: this.user.name, fullname: this.user.name,
avatarUrl: this.user.avatarUrl, avatarUrl: this.user.avatarUrl,
isAdmin: this.user.isAdmin, isAdmin: this.user.isAdmin,
createdAt: this.user.createdAt, createdAt: this.user.createdAt,
updatedAt: this.user.updatedAt, updatedAt: this.user.updatedAt,
count: { count: {
link: Number(this.user.$extras.totalLinks), link: Number(this.user.$extras.totalLinks),
collection: Number(this.user.$extras.totalCollections), collection: Number(this.user.$extras.totalCollections),
}, },
}); });
} }
@inject() @inject()
export default class AdminController { export default class AdminController {
constructor( constructor(
protected usersController: UsersController, protected usersController: UsersController,
protected linksController: LinksController, protected linksController: LinksController,
protected collectionsController: CollectionsController protected collectionsController: CollectionsController
) {} ) {}
async index({ inertia }: HttpContext) { async index({ inertia }: HttpContext) {
const users = await this.usersController.getAllUsersWithTotalRelations(); const users = await this.usersController.getAllUsersWithTotalRelations();
const linksCount = await this.linksController.getTotalLinksCount(); const linksCount = await this.linksController.getTotalLinksCount();
const collectionsCount = const collectionsCount =
await this.collectionsController.getTotalCollectionsCount(); await this.collectionsController.getTotalCollectionsCount();
return inertia.render('admin/dashboard', { return inertia.render('admin/dashboard', {
users: users.map((user) => new UserWithRelationCountDto(user).toJson()), users: users.map((user) => new UserWithRelationCountDto(user).toJson()),
totalLinks: linksCount, totalLinks: linksCount,
totalCollections: collectionsCount, totalCollections: collectionsCount,
}); });
} }
} }

View File

@@ -3,11 +3,11 @@ import { updateUserThemeValidator } from '#validators/user';
import type { HttpContext } from '@adonisjs/core/http'; import type { HttpContext } from '@adonisjs/core/http';
export default class AppsController { export default class AppsController {
async updateUserTheme({ request, session, response }: HttpContext) { async updateUserTheme({ request, session, response }: HttpContext) {
const { preferDarkTheme } = await request.validateUsing( const { preferDarkTheme } = await request.validateUsing(
updateUserThemeValidator updateUserThemeValidator
); );
session.put(PREFER_DARK_THEME, preferDarkTheme); session.put(PREFER_DARK_THEME, preferDarkTheme);
return response.ok({ message: 'ok' }); return response.ok({ message: 'ok' });
} }
} }

View File

@@ -1,143 +1,143 @@
import Collection from '#models/collection'; import Collection from '#models/collection';
import User from '#models/user'; import User from '#models/user';
import { import {
createCollectionValidator, createCollectionValidator,
deleteCollectionValidator, deleteCollectionValidator,
updateCollectionValidator, updateCollectionValidator,
} from '#validators/collection'; } from '#validators/collection';
import type { HttpContext } from '@adonisjs/core/http'; import type { HttpContext } from '@adonisjs/core/http';
import db from '@adonisjs/lucid/services/db'; import db from '@adonisjs/lucid/services/db';
export default class CollectionsController { export default class CollectionsController {
// Dashboard // Dashboard
async index({ auth, inertia, request, response }: HttpContext) { async index({ auth, inertia, request, response }: HttpContext) {
const collections = await this.getCollectionsByAuthorId(auth.user!.id); const collections = await this.getCollectionsByAuthorId(auth.user!.id);
if (collections.length === 0) { if (collections.length === 0) {
return response.redirectToNamedRoute('collection.create-form'); return response.redirectToNamedRoute('collection.create-form');
} }
const activeCollectionId = Number(request.qs()?.collectionId ?? ''); const activeCollectionId = Number(request.qs()?.collectionId ?? '');
const activeCollection = collections.find( const activeCollection = collections.find(
(c) => c.id === activeCollectionId (c) => c.id === activeCollectionId
); );
if (!activeCollection && !!activeCollectionId) { if (!activeCollection && !!activeCollectionId) {
return response.redirectToNamedRoute('dashboard'); return response.redirectToNamedRoute('dashboard');
} }
// TODO: Create DTOs // TODO: Create DTOs
return inertia.render('dashboard', { return inertia.render('dashboard', {
collections: collections.map((collection) => collection.serialize()), collections: collections.map((collection) => collection.serialize()),
activeCollection: activeCollection:
activeCollection?.serialize() || collections[0].serialize(), activeCollection?.serialize() || collections[0].serialize(),
}); });
} }
// Create collection form // Create collection form
async showCreatePage({ inertia, auth }: HttpContext) { async showCreatePage({ inertia, auth }: HttpContext) {
const collections = await this.getCollectionsByAuthorId(auth.user!.id); const collections = await this.getCollectionsByAuthorId(auth.user!.id);
return inertia.render('collections/create', { return inertia.render('collections/create', {
disableHomeLink: collections.length === 0, disableHomeLink: collections.length === 0,
}); });
} }
// Method called when creating a collection // Method called when creating a collection
async store({ request, response, auth }: HttpContext) { async store({ request, response, auth }: HttpContext) {
const payload = await request.validateUsing(createCollectionValidator); const payload = await request.validateUsing(createCollectionValidator);
const collection = await Collection.create({ const collection = await Collection.create({
...payload, ...payload,
authorId: auth.user?.id!, authorId: auth.user?.id!,
}); });
return this.redirectToCollectionId(response, collection.id); return this.redirectToCollectionId(response, collection.id);
} }
async showEditPage({ auth, request, inertia, response }: HttpContext) { async showEditPage({ auth, request, inertia, response }: HttpContext) {
const collectionId = request.qs()?.collectionId; const collectionId = request.qs()?.collectionId;
if (!collectionId) { if (!collectionId) {
return response.redirectToNamedRoute('dashboard'); return response.redirectToNamedRoute('dashboard');
} }
const collection = await this.getCollectionById( const collection = await this.getCollectionById(
collectionId, collectionId,
auth.user!.id auth.user!.id
); );
return inertia.render('collections/edit', { return inertia.render('collections/edit', {
collection, collection,
}); });
} }
async update({ request, auth, response }: HttpContext) { async update({ request, auth, response }: HttpContext) {
const { params, ...payload } = await request.validateUsing( const { params, ...payload } = await request.validateUsing(
updateCollectionValidator updateCollectionValidator
); );
// Cant use validator (vinejs) custom rule 'cause its too generic, // Cant use validator (vinejs) custom rule 'cause its too generic,
// because we have to find a collection by identifier and // because we have to find a collection by identifier and
// check whether the current user is the author. // check whether the current user is the author.
// https://vinejs.dev/docs/extend/custom_rules // https://vinejs.dev/docs/extend/custom_rules
await this.getCollectionById(params.id, auth.user!.id); await this.getCollectionById(params.id, auth.user!.id);
await Collection.updateOrCreate( await Collection.updateOrCreate(
{ {
id: params.id, id: params.id,
}, },
payload payload
); );
return this.redirectToCollectionId(response, params.id); return this.redirectToCollectionId(response, params.id);
} }
async showDeletePage({ auth, request, inertia, response }: HttpContext) { async showDeletePage({ auth, request, inertia, response }: HttpContext) {
const collectionId = request.qs()?.collectionId; const collectionId = request.qs()?.collectionId;
if (!collectionId) { if (!collectionId) {
return response.redirectToNamedRoute('dashboard'); return response.redirectToNamedRoute('dashboard');
} }
const collection = await this.getCollectionById( const collection = await this.getCollectionById(
collectionId, collectionId,
auth.user!.id auth.user!.id
); );
return inertia.render('collections/delete', { return inertia.render('collections/delete', {
collection, collection,
}); });
} }
async delete({ request, auth, response }: HttpContext) { async delete({ request, auth, response }: HttpContext) {
const { params } = await request.validateUsing(deleteCollectionValidator); const { params } = await request.validateUsing(deleteCollectionValidator);
const collection = await this.getCollectionById(params.id, auth.user!.id); const collection = await this.getCollectionById(params.id, auth.user!.id);
await collection.delete(); await collection.delete();
return response.redirectToNamedRoute('dashboard'); return response.redirectToNamedRoute('dashboard');
} }
async getTotalCollectionsCount() { async getTotalCollectionsCount() {
const totalCount = await db.from('collections').count('* as total'); const totalCount = await db.from('collections').count('* as total');
return Number(totalCount[0].total); return Number(totalCount[0].total);
} }
/** /**
* Get collection by id. * Get collection by id.
* *
* /!\ Only return private collection (create by the current user) * /!\ Only return private collection (create by the current user)
*/ */
async getCollectionById(id: Collection['id'], userId: User['id']) { async getCollectionById(id: Collection['id'], userId: User['id']) {
return await Collection.query() return await Collection.query()
.where('id', id) .where('id', id)
.andWhere('author_id', userId) .andWhere('author_id', userId)
.firstOrFail(); .firstOrFail();
} }
async getCollectionsByAuthorId(authorId: User['id']) { async getCollectionsByAuthorId(authorId: User['id']) {
return await Collection.query() return await Collection.query()
.where('author_id', authorId) .where('author_id', authorId)
.orderBy('created_at') .orderBy('created_at')
.preload('links'); .preload('links');
} }
redirectToCollectionId( redirectToCollectionId(
response: HttpContext['response'], response: HttpContext['response'],
collectionId: Collection['id'] collectionId: Collection['id']
) { ) {
return response.redirectToNamedRoute('dashboard', { return response.redirectToNamedRoute('dashboard', {
qs: { collectionId }, qs: { collectionId },
}); });
} }
} }

View File

@@ -5,156 +5,156 @@ import logger from '@adonisjs/core/services/logger';
import { parse } from 'node-html-parser'; import { parse } from 'node-html-parser';
interface Favicon { interface Favicon {
buffer: Buffer; buffer: Buffer;
url: string; url: string;
type: string; type: string;
size: number; size: number;
} }
export default class FaviconsController { export default class FaviconsController {
private userAgent = private userAgent =
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0'; 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0';
private relList = [ private relList = [
'icon', 'icon',
'shortcut icon', 'shortcut icon',
'apple-touch-icon', 'apple-touch-icon',
'apple-touch-icon-precomposed', 'apple-touch-icon-precomposed',
'apple-touch-startup-image', 'apple-touch-startup-image',
'mask-icon', 'mask-icon',
'fluid-icon', 'fluid-icon',
]; ];
async index(ctx: HttpContext) { async index(ctx: HttpContext) {
const url = ctx.request.qs()?.url; const url = ctx.request.qs()?.url;
if (!url) { if (!url) {
throw new Error('Missing URL'); throw new Error('Missing URL');
} }
const cacheNs = cache.namespace('favicon'); const cacheNs = cache.namespace('favicon');
const favicon = await cacheNs.getOrSet({ const favicon = await cacheNs.getOrSet({
key: url, key: url,
ttl: '1h', ttl: '1h',
factory: () => this.tryGetFavicon(url), factory: () => this.tryGetFavicon(url),
}); });
return this.sendImage(ctx, favicon); return this.sendImage(ctx, favicon);
} }
private async tryGetFavicon(url: string): Promise<Favicon> { private async tryGetFavicon(url: string): Promise<Favicon> {
const faviconUrl = this.buildFaviconUrl(url, '/favicon.ico'); const faviconUrl = this.buildFaviconUrl(url, '/favicon.ico');
try { try {
return await this.fetchFavicon(faviconUrl); return await this.fetchFavicon(faviconUrl);
} catch { } catch {
logger.debug(`Unable to retrieve favicon from ${faviconUrl}`); logger.debug(`Unable to retrieve favicon from ${faviconUrl}`);
} }
const documentText = await this.fetchDocumentText(url); const documentText = await this.fetchDocumentText(url);
const faviconPath = this.extractFaviconPath(documentText); const faviconPath = this.extractFaviconPath(documentText);
if (!faviconPath) { if (!faviconPath) {
throw new FaviconNotFoundException(`No favicon path found in ${url}`); throw new FaviconNotFoundException(`No favicon path found in ${url}`);
} }
if (faviconPath.startsWith('http')) { if (faviconPath.startsWith('http')) {
try { try {
return await this.fetchFavicon(faviconPath); return await this.fetchFavicon(faviconPath);
} catch { } catch {
logger.debug(`Unable to retrieve favicon from ${faviconPath}`); logger.debug(`Unable to retrieve favicon from ${faviconPath}`);
} }
} }
return this.fetchFaviconFromPath(url, faviconPath); return this.fetchFaviconFromPath(url, faviconPath);
} }
private async fetchFavicon(url: string): Promise<Favicon> { private async fetchFavicon(url: string): Promise<Favicon> {
const response = await this.fetchWithUserAgent(url); const response = await this.fetchWithUserAgent(url);
if (!response.ok) { if (!response.ok) {
throw new FaviconNotFoundException(`Request to favicon ${url} failed`); throw new FaviconNotFoundException(`Request to favicon ${url} failed`);
} }
const blob = await response.blob(); const blob = await response.blob();
if (!this.isImage(blob.type) || blob.size === 0) { if (!this.isImage(blob.type) || blob.size === 0) {
throw new FaviconNotFoundException(`Invalid image at ${url}`); throw new FaviconNotFoundException(`Invalid image at ${url}`);
} }
return { return {
buffer: Buffer.from(await blob.arrayBuffer()), buffer: Buffer.from(await blob.arrayBuffer()),
url: response.url, url: response.url,
type: blob.type, type: blob.type,
size: blob.size, size: blob.size,
}; };
} }
private async fetchDocumentText(url: string): Promise<string> { private async fetchDocumentText(url: string): Promise<string> {
const response = await this.fetchWithUserAgent(url); const response = await this.fetchWithUserAgent(url);
if (!response.ok) { if (!response.ok) {
throw new FaviconNotFoundException(`Request to ${url} failed`); throw new FaviconNotFoundException(`Request to ${url} failed`);
} }
return await response.text(); return await response.text();
} }
private extractFaviconPath(html: string): string | undefined { private extractFaviconPath(html: string): string | undefined {
const document = parse(html); const document = parse(html);
const link = document const link = document
.getElementsByTagName('link') .getElementsByTagName('link')
.find((element) => this.relList.includes(element.getAttribute('rel')!)); .find((element) => this.relList.includes(element.getAttribute('rel')!));
return link?.getAttribute('href'); return link?.getAttribute('href');
} }
private async fetchFaviconFromPath( private async fetchFaviconFromPath(
baseUrl: string, baseUrl: string,
path: string path: string
): Promise<Favicon> { ): Promise<Favicon> {
if (this.isBase64Image(path)) { if (this.isBase64Image(path)) {
const buffer = this.convertBase64ToBuffer(path); const buffer = this.convertBase64ToBuffer(path);
return { return {
buffer, buffer,
type: 'image/x-icon', type: 'image/x-icon',
size: buffer.length, size: buffer.length,
url: path, url: path,
}; };
} }
const faviconUrl = this.buildFaviconUrl(baseUrl, path); const faviconUrl = this.buildFaviconUrl(baseUrl, path);
return this.fetchFavicon(faviconUrl); return this.fetchFavicon(faviconUrl);
} }
private buildFaviconUrl(base: string, path: string): string { private buildFaviconUrl(base: string, path: string): string {
const { origin } = new URL(base); const { origin } = new URL(base);
if (path.startsWith('/')) { if (path.startsWith('/')) {
return origin + path; return origin + path;
} }
const basePath = this.urlWithoutSearchParams(base); const basePath = this.urlWithoutSearchParams(base);
const baseUrl = basePath.endsWith('/') ? basePath.slice(0, -1) : basePath; const baseUrl = basePath.endsWith('/') ? basePath.slice(0, -1) : basePath;
return `${baseUrl}/${path}`; return `${baseUrl}/${path}`;
} }
private urlWithoutSearchParams(url: string): string { private urlWithoutSearchParams(url: string): string {
const { protocol, host, pathname } = new URL(url); const { protocol, host, pathname } = new URL(url);
return `${protocol}//${host}${pathname}`; return `${protocol}//${host}${pathname}`;
} }
private isImage(type: string): boolean { private isImage(type: string): boolean {
return type.startsWith('image/'); return type.startsWith('image/');
} }
private isBase64Image(data: string): boolean { private isBase64Image(data: string): boolean {
return data.startsWith('data:image/'); return data.startsWith('data:image/');
} }
private convertBase64ToBuffer(base64: string): Buffer { private convertBase64ToBuffer(base64: string): Buffer {
return Buffer.from(base64.split(',')[1], 'base64'); return Buffer.from(base64.split(',')[1], 'base64');
} }
private async fetchWithUserAgent(url: string): Promise<Response> { private async fetchWithUserAgent(url: string): Promise<Response> {
const headers = new Headers({ 'User-Agent': this.userAgent }); const headers = new Headers({ 'User-Agent': this.userAgent });
return fetch(url, { headers }); return fetch(url, { headers });
} }
private sendImage(ctx: HttpContext, { buffer, type, size }: Favicon) { private sendImage(ctx: HttpContext, { buffer, type, size }: Favicon) {
ctx.response.header('Content-Type', type); ctx.response.header('Content-Type', type);
ctx.response.header('Content-Length', size.toString()); ctx.response.header('Content-Length', size.toString());
ctx.response.send(buffer, true); ctx.response.send(buffer, true);
} }
} }

View File

@@ -1,10 +1,10 @@
import CollectionsController from '#controllers/collections_controller'; import CollectionsController from '#controllers/collections_controller';
import Link from '#models/link'; import Link from '#models/link';
import { import {
createLinkValidator, createLinkValidator,
deleteLinkValidator, deleteLinkValidator,
updateLinkFavoriteStatusValidator, updateLinkFavoriteStatusValidator,
updateLinkValidator, updateLinkValidator,
} from '#validators/link'; } from '#validators/link';
import { inject } from '@adonisjs/core'; import { inject } from '@adonisjs/core';
import type { HttpContext } from '@adonisjs/core/http'; import type { HttpContext } from '@adonisjs/core/http';
@@ -12,120 +12,120 @@ import db from '@adonisjs/lucid/services/db';
@inject() @inject()
export default class LinksController { export default class LinksController {
constructor(protected collectionsController: CollectionsController) {} constructor(protected collectionsController: CollectionsController) {}
async showCreatePage({ auth, inertia }: HttpContext) { async showCreatePage({ auth, inertia }: HttpContext) {
const collections = const collections =
await this.collectionsController.getCollectionsByAuthorId(auth.user!.id); await this.collectionsController.getCollectionsByAuthorId(auth.user!.id);
return inertia.render('links/create', { collections }); return inertia.render('links/create', { collections });
} }
async store({ auth, request, response }: HttpContext) { async store({ auth, request, response }: HttpContext) {
const { collectionId, ...payload } = const { collectionId, ...payload } =
await request.validateUsing(createLinkValidator); await request.validateUsing(createLinkValidator);
await this.collectionsController.getCollectionById( await this.collectionsController.getCollectionById(
collectionId, collectionId,
auth.user!.id auth.user!.id
); );
await Link.create({ await Link.create({
...payload, ...payload,
collectionId, collectionId,
authorId: auth.user?.id!, authorId: auth.user?.id!,
}); });
return this.collectionsController.redirectToCollectionId( return this.collectionsController.redirectToCollectionId(
response, response,
collectionId collectionId
); );
} }
async showEditPage({ auth, inertia, request, response }: HttpContext) { async showEditPage({ auth, inertia, request, response }: HttpContext) {
const linkId = request.qs()?.linkId; const linkId = request.qs()?.linkId;
if (!linkId) { if (!linkId) {
return response.redirectToNamedRoute('dashboard'); return response.redirectToNamedRoute('dashboard');
} }
const userId = auth.user!.id; const userId = auth.user!.id;
const collections = const collections =
await this.collectionsController.getCollectionsByAuthorId(userId); await this.collectionsController.getCollectionsByAuthorId(userId);
const link = await this.getLinkById(linkId, userId); const link = await this.getLinkById(linkId, userId);
return inertia.render('links/edit', { collections, link }); return inertia.render('links/edit', { collections, link });
} }
async update({ request, auth, response }: HttpContext) { async update({ request, auth, response }: HttpContext) {
const { params, ...payload } = const { params, ...payload } =
await request.validateUsing(updateLinkValidator); await request.validateUsing(updateLinkValidator);
// Throw if invalid link id provided // Throw if invalid link id provided
await this.getLinkById(params.id, auth.user!.id); await this.getLinkById(params.id, auth.user!.id);
await Link.updateOrCreate( await Link.updateOrCreate(
{ {
id: params.id, id: params.id,
}, },
payload payload
); );
return response.redirectToNamedRoute('dashboard', { return response.redirectToNamedRoute('dashboard', {
qs: { collectionId: payload.collectionId }, qs: { collectionId: payload.collectionId },
}); });
} }
async toggleFavorite({ request, auth, response }: HttpContext) { async toggleFavorite({ request, auth, response }: HttpContext) {
const { params, favorite } = await request.validateUsing( const { params, favorite } = await request.validateUsing(
updateLinkFavoriteStatusValidator updateLinkFavoriteStatusValidator
); );
// Throw if invalid link id provided // Throw if invalid link id provided
await this.getLinkById(params.id, auth.user!.id); await this.getLinkById(params.id, auth.user!.id);
await Link.updateOrCreate( await Link.updateOrCreate(
{ {
id: params.id, id: params.id,
}, },
{ favorite } { favorite }
); );
return response.json({ status: 'ok' }); return response.json({ status: 'ok' });
} }
async showDeletePage({ auth, inertia, request, response }: HttpContext) { async showDeletePage({ auth, inertia, request, response }: HttpContext) {
const linkId = request.qs()?.linkId; const linkId = request.qs()?.linkId;
if (!linkId) { if (!linkId) {
return response.redirectToNamedRoute('dashboard'); return response.redirectToNamedRoute('dashboard');
} }
const link = await this.getLinkById(linkId, auth.user!.id); const link = await this.getLinkById(linkId, auth.user!.id);
await link.load('collection'); await link.load('collection');
return inertia.render('links/delete', { link }); return inertia.render('links/delete', { link });
} }
async delete({ request, auth, response }: HttpContext) { async delete({ request, auth, response }: HttpContext) {
const { params } = await request.validateUsing(deleteLinkValidator); const { params } = await request.validateUsing(deleteLinkValidator);
const link = await this.getLinkById(params.id, auth.user!.id); const link = await this.getLinkById(params.id, auth.user!.id);
await link.delete(); await link.delete();
return response.redirectToNamedRoute('dashboard', { return response.redirectToNamedRoute('dashboard', {
qs: { collectionId: link.id }, qs: { collectionId: link.id },
}); });
} }
async getTotalLinksCount() { async getTotalLinksCount() {
const totalCount = await db.from('links').count('* as total'); const totalCount = await db.from('links').count('* as total');
return Number(totalCount[0].total); return Number(totalCount[0].total);
} }
/** /**
* Get link by id. * Get link by id.
* *
* /!\ Only return private link (create by the current user) * /!\ Only return private link (create by the current user)
*/ */
private async getLinkById(id: Link['id'], userId: Link['id']) { private async getLinkById(id: Link['id'], userId: Link['id']) {
return await Link.query() return await Link.query()
.where('id', id) .where('id', id)
.andWhere('author_id', userId) .andWhere('author_id', userId)
.firstOrFail(); .firstOrFail();
} }
} }

View File

@@ -2,17 +2,17 @@ import type { HttpContext } from '@adonisjs/core/http';
import db from '@adonisjs/lucid/services/db'; import db from '@adonisjs/lucid/services/db';
export default class SearchesController { export default class SearchesController {
async search({ request, auth }: HttpContext) { async search({ request, auth }: HttpContext) {
const term = request.qs()?.term; const term = request.qs()?.term;
if (!term) { if (!term) {
console.warn('qs term null'); console.warn('qs term null');
return { error: 'missing "term" query param' }; return { error: 'missing "term" query param' };
} }
const { rows } = await db.rawQuery('SELECT * FROM search_text(?, ?)', [ const { rows } = await db.rawQuery('SELECT * FROM search_text(?, ?)', [
term, term,
auth.user!.id, auth.user!.id,
]); ]);
return { results: rows }; return { results: rows };
} }
} }

View File

@@ -4,21 +4,21 @@ import { getSharedCollectionValidator } from '#validators/shared_collection';
import type { HttpContext } from '@adonisjs/core/http'; import type { HttpContext } from '@adonisjs/core/http';
export default class SharedCollectionsController { export default class SharedCollectionsController {
async index({ request, inertia }: HttpContext) { async index({ request, inertia }: HttpContext) {
const { params } = await request.validateUsing( const { params } = await request.validateUsing(
getSharedCollectionValidator getSharedCollectionValidator
); );
const collection = await this.getSharedCollectionById(params.id); const collection = await this.getSharedCollectionById(params.id);
return inertia.render('shared', { collection }); return inertia.render('shared', { collection });
} }
private async getSharedCollectionById(id: Collection['id']) { private async getSharedCollectionById(id: Collection['id']) {
return await Collection.query() return await Collection.query()
.where('id', id) .where('id', id)
.andWhere('visibility', Visibility.PUBLIC) .andWhere('visibility', Visibility.PUBLIC)
.preload('links') .preload('links')
.preload('author') .preload('author')
.firstOrFail(); .firstOrFail();
} }
} }

View File

@@ -5,74 +5,74 @@ import db from '@adonisjs/lucid/services/db';
import { RouteName } from '@izzyjs/route/types'; import { RouteName } from '@izzyjs/route/types';
export default class UsersController { export default class UsersController {
private redirectTo: RouteName = 'auth.login'; private redirectTo: RouteName = 'auth.login';
login({ inertia }: HttpContext) { login({ inertia }: HttpContext) {
return inertia.render('login'); return inertia.render('login');
} }
google = ({ ally }: HttpContext) => ally.use('google').redirect(); google = ({ ally }: HttpContext) => ally.use('google').redirect();
async callbackAuth({ ally, auth, response, session }: HttpContext) { async callbackAuth({ ally, auth, response, session }: HttpContext) {
const google = ally.use('google'); const google = ally.use('google');
if (google.accessDenied()) { if (google.accessDenied()) {
// TODO: translate error messages + show them in UI // TODO: translate error messages + show them in UI
session.flash('flash', 'Access was denied'); session.flash('flash', 'Access was denied');
return response.redirectToNamedRoute(this.redirectTo); return response.redirectToNamedRoute(this.redirectTo);
} }
if (google.stateMisMatch()) { if (google.stateMisMatch()) {
session.flash('flash', 'Request expired. Retry again'); session.flash('flash', 'Request expired. Retry again');
return response.redirectToNamedRoute(this.redirectTo); return response.redirectToNamedRoute(this.redirectTo);
} }
if (google.hasError()) { if (google.hasError()) {
session.flash('flash', google.getError() || 'Something went wrong'); session.flash('flash', google.getError() || 'Something went wrong');
return response.redirectToNamedRoute(this.redirectTo); return response.redirectToNamedRoute(this.redirectTo);
} }
const userCount = await db.from('users').count('* as total'); const userCount = await db.from('users').count('* as total');
const { const {
email, email,
id: providerId, id: providerId,
name, name,
nickName, nickName,
avatarUrl, avatarUrl,
token, token,
} = await google.user(); } = await google.user();
const user = await User.updateOrCreate( const user = await User.updateOrCreate(
{ {
email, email,
}, },
{ {
email, email,
providerId, providerId,
name, name,
nickName, nickName,
avatarUrl, avatarUrl,
token, token,
providerType: 'google', providerType: 'google',
isAdmin: userCount[0].total === '0' ? true : undefined, isAdmin: userCount[0].total === '0' ? true : undefined,
} }
); );
await auth.use('web').login(user); await auth.use('web').login(user);
session.flash('flash', 'Successfully authenticated'); session.flash('flash', 'Successfully authenticated');
logger.info(`[${user.email}] auth success`); logger.info(`[${user.email}] auth success`);
response.redirectToNamedRoute('dashboard'); response.redirectToNamedRoute('dashboard');
} }
async logout({ auth, response, session }: HttpContext) { async logout({ auth, response, session }: HttpContext) {
await auth.use('web').logout(); await auth.use('web').logout();
session.flash('flash', 'Successfully disconnected'); session.flash('flash', 'Successfully disconnected');
logger.info(`[${auth.user?.email}] disconnected successfully`); logger.info(`[${auth.user?.email}] disconnected successfully`);
response.redirectToNamedRoute(this.redirectTo); response.redirectToNamedRoute(this.redirectTo);
} }
async getAllUsersWithTotalRelations() { async getAllUsersWithTotalRelations() {
return User.query() return User.query()
.withCount('collections', (q) => q.as('totalCollections')) .withCount('collections', (q) => q.as('totalCollections'))
.withCount('links', (q) => q.as('totalLinks')); .withCount('links', (q) => q.as('totalLinks'));
} }
} }

View File

@@ -1,4 +1,4 @@
export enum Visibility { export enum Visibility {
PUBLIC = 'PUBLIC', PUBLIC = 'PUBLIC',
PRIVATE = 'PRIVATE', PRIVATE = 'PRIVATE',
} }

View File

@@ -5,16 +5,16 @@ import { createReadStream } from 'node:fs';
import { resolve } from 'node:path'; import { resolve } from 'node:path';
export default class FaviconNotFoundException extends Exception { export default class FaviconNotFoundException extends Exception {
static status = 404; static status = 404;
static code = 'E_FAVICON_NOT_FOUND'; static code = 'E_FAVICON_NOT_FOUND';
async handle(error: this, ctx: HttpContext) { async handle(error: this, ctx: HttpContext) {
const readStream = createReadStream( const readStream = createReadStream(
resolve(process.cwd(), './public/empty-image.png') resolve(process.cwd(), './public/empty-image.png')
); );
ctx.response.header('Content-Type', 'image/png'); ctx.response.header('Content-Type', 'image/png');
ctx.response.stream(readStream); ctx.response.stream(readStream);
logger.debug(error.message); logger.debug(error.message);
} }
} }

View File

@@ -1,54 +1,54 @@
import { ExceptionHandler, HttpContext } from '@adonisjs/core/http'; import { ExceptionHandler, HttpContext } from '@adonisjs/core/http';
import app from '@adonisjs/core/services/app'; import app from '@adonisjs/core/services/app';
import type { import type {
StatusPageRange, StatusPageRange,
StatusPageRenderer, StatusPageRenderer,
} from '@adonisjs/core/types/http'; } from '@adonisjs/core/types/http';
import { errors } from '@adonisjs/lucid'; import { errors } from '@adonisjs/lucid';
export default class HttpExceptionHandler extends ExceptionHandler { export default class HttpExceptionHandler extends ExceptionHandler {
/** /**
* In debug mode, the exception handler will display verbose errors * In debug mode, the exception handler will display verbose errors
* with pretty printed stack traces. * with pretty printed stack traces.
*/ */
protected debug = !app.inProduction; protected debug = !app.inProduction;
/** /**
* Status pages are used to display a custom HTML pages for certain error * Status pages are used to display a custom HTML pages for certain error
* codes. You might want to enable them in production only, but feel * codes. You might want to enable them in production only, but feel
* free to enable them in development as well. * free to enable them in development as well.
*/ */
protected renderStatusPages = app.inProduction; protected renderStatusPages = app.inProduction;
/** /**
* Status pages is a collection of error code range and a callback * Status pages is a collection of error code range and a callback
* to return the HTML contents to send as a response. * to return the HTML contents to send as a response.
*/ */
protected statusPages: Record<StatusPageRange, StatusPageRenderer> = { protected statusPages: Record<StatusPageRange, StatusPageRenderer> = {
'404': (error, { inertia }) => '404': (error, { inertia }) =>
inertia.render('errors/not_found', { error }), inertia.render('errors/not_found', { error }),
'500..599': (error, { inertia }) => '500..599': (error, { inertia }) =>
inertia.render('errors/server_error', { error }), inertia.render('errors/server_error', { error }),
}; };
/** /**
* The method is used for handling errors and returning * The method is used for handling errors and returning
* response to the client * response to the client
*/ */
async handle(error: unknown, ctx: HttpContext) { async handle(error: unknown, ctx: HttpContext) {
if (error instanceof errors.E_ROW_NOT_FOUND) { if (error instanceof errors.E_ROW_NOT_FOUND) {
return ctx.response.redirectToNamedRoute('dashboard'); return ctx.response.redirectToNamedRoute('dashboard');
} }
return super.handle(error, ctx); return super.handle(error, ctx);
} }
/** /**
* The method is used to report error to the logging service or * The method is used to report error to the logging service or
* the a third party error monitoring service. * the a third party error monitoring service.
* *
* @note You should not attempt to send a response from this method. * @note You should not attempt to send a response from this method.
*/ */
async report(error: unknown, ctx: HttpContext) { async report(error: unknown, ctx: HttpContext) {
return super.report(error, ctx); return super.report(error, ctx);
} }
} }

View File

@@ -2,9 +2,9 @@ import { BentoCache, bentostore } from 'bentocache';
import { memoryDriver } from 'bentocache/drivers/memory'; import { memoryDriver } from 'bentocache/drivers/memory';
export const cache = new BentoCache({ export const cache = new BentoCache({
default: 'cache', default: 'cache',
stores: { stores: {
cache: bentostore().useL1Layer(memoryDriver({ maxSize: 10_000 })), cache: bentostore().useL1Layer(memoryDriver({ maxSize: 10_000 })),
}, },
}); });

View File

@@ -2,10 +2,10 @@ import type { HttpContext } from '@adonisjs/core/http';
import type { NextFn } from '@adonisjs/core/types/http'; import type { NextFn } from '@adonisjs/core/types/http';
export default class AdminMiddleware { export default class AdminMiddleware {
async handle(ctx: HttpContext, next: NextFn) { async handle(ctx: HttpContext, next: NextFn) {
if (!ctx.auth.user?.isAdmin) { if (!ctx.auth.user?.isAdmin) {
return ctx.response.redirectToNamedRoute('dashboard'); return ctx.response.redirectToNamedRoute('dashboard');
} }
return next(); return next();
} }
} }

View File

@@ -8,21 +8,21 @@ import { route } from '@izzyjs/route/client';
* access to unauthenticated users. * access to unauthenticated users.
*/ */
export default class AuthMiddleware { export default class AuthMiddleware {
/** /**
* The URL to redirect to, when authentication fails * The URL to redirect to, when authentication fails
*/ */
redirectTo = route('auth.login').url; redirectTo = route('auth.login').url;
async handle( async handle(
ctx: HttpContext, ctx: HttpContext,
next: NextFn, next: NextFn,
options: { options: {
guards?: (keyof Authenticators)[]; guards?: (keyof Authenticators)[];
} = {} } = {}
) { ) {
await ctx.auth.authenticateUsing(options.guards, { await ctx.auth.authenticateUsing(options.guards, {
loginRoute: this.redirectTo, loginRoute: this.redirectTo,
}); });
return next(); return next();
} }
} }

View File

@@ -10,10 +10,10 @@ import { NextFn } from '@adonisjs/core/types/http';
* - And bind "Logger" class to the "ctx.logger" object * - And bind "Logger" class to the "ctx.logger" object
*/ */
export default class ContainerBindingsMiddleware { export default class ContainerBindingsMiddleware {
handle(ctx: HttpContext, next: NextFn) { handle(ctx: HttpContext, next: NextFn) {
ctx.containerResolver.bindValue(HttpContext, ctx); ctx.containerResolver.bindValue(HttpContext, ctx);
ctx.containerResolver.bindValue(Logger, ctx.logger); ctx.containerResolver.bindValue(Logger, ctx.logger);
return next(); return next();
} }
} }

View File

@@ -10,22 +10,22 @@ import type { Authenticators } from '@adonisjs/auth/types';
* is already logged-in * is already logged-in
*/ */
export default class GuestMiddleware { export default class GuestMiddleware {
/** /**
* The URL to redirect to when user is logged-in * The URL to redirect to when user is logged-in
*/ */
redirectTo = '/'; redirectTo = '/';
async handle( async handle(
ctx: HttpContext, ctx: HttpContext,
next: NextFn, next: NextFn,
options: { guards?: (keyof Authenticators)[] } = {} options: { guards?: (keyof Authenticators)[] } = {}
) { ) {
for (let guard of options.guards || [ctx.auth.defaultGuard]) { for (let guard of options.guards || [ctx.auth.defaultGuard]) {
if (await ctx.auth.use(guard).check()) { if (await ctx.auth.use(guard).check()) {
return ctx.response.redirect(this.redirectTo, true); return ctx.response.redirect(this.redirectTo, true);
} }
} }
return next(); return next();
} }
} }

View File

@@ -2,16 +2,16 @@ import { HttpContext } from '@adonisjs/core/http';
import logger from '@adonisjs/core/services/logger'; import logger from '@adonisjs/core/services/logger';
export default class LogRequest { export default class LogRequest {
async handle({ request }: HttpContext, next: () => Promise<void>) { async handle({ request }: HttpContext, next: () => Promise<void>) {
if ( if (
!request.url().startsWith('/node_modules') && !request.url().startsWith('/node_modules') &&
!request.url().startsWith('/inertia') && !request.url().startsWith('/inertia') &&
!request.url().startsWith('/@vite') && !request.url().startsWith('/@vite') &&
!request.url().startsWith('/@react-refresh') && !request.url().startsWith('/@react-refresh') &&
!request.url().includes('.ts') !request.url().includes('.ts')
) { ) {
logger.debug(`[${request.method()}]: ${request.url()}`); logger.debug(`[${request.method()}]: ${request.url()}`);
} }
await next(); await next();
} }
} }

View File

@@ -1,25 +1,25 @@
import { import {
BaseModel, BaseModel,
CamelCaseNamingStrategy, CamelCaseNamingStrategy,
column, column,
} from '@adonisjs/lucid/orm'; } from '@adonisjs/lucid/orm';
import { DateTime } from 'luxon'; import { DateTime } from 'luxon';
export default class AppBaseModel extends BaseModel { export default class AppBaseModel extends BaseModel {
static namingStrategy = new CamelCaseNamingStrategy(); static namingStrategy = new CamelCaseNamingStrategy();
serializeExtras = true; serializeExtras = true;
@column({ isPrimary: true }) @column({ isPrimary: true })
declare id: number; declare id: number;
@column.dateTime({ @column.dateTime({
autoCreate: true, autoCreate: true,
}) })
declare createdAt: DateTime; declare createdAt: DateTime;
@column.dateTime({ @column.dateTime({
autoCreate: true, autoCreate: true,
autoUpdate: true, autoUpdate: true,
}) })
declare updatedAt: DateTime; declare updatedAt: DateTime;
} }

View File

@@ -6,24 +6,24 @@ import type { BelongsTo, HasMany } from '@adonisjs/lucid/types/relations';
import { Visibility } from '#enums/visibility'; import { Visibility } from '#enums/visibility';
export default class Collection extends AppBaseModel { export default class Collection extends AppBaseModel {
@column() @column()
declare name: string; declare name: string;
@column() @column()
declare description: string | null; declare description: string | null;
@column() @column()
declare visibility: Visibility; declare visibility: Visibility;
@column() @column()
declare nextId: number; declare nextId: number;
@column() @column()
declare authorId: number; declare authorId: number;
@belongsTo(() => User, { foreignKey: 'authorId' }) @belongsTo(() => User, { foreignKey: 'authorId' })
declare author: BelongsTo<typeof User>; declare author: BelongsTo<typeof User>;
@hasMany(() => Link) @hasMany(() => Link)
declare links: HasMany<typeof Link>; declare links: HasMany<typeof Link>;
} }

View File

@@ -5,27 +5,27 @@ import { belongsTo, column } from '@adonisjs/lucid/orm';
import type { BelongsTo } from '@adonisjs/lucid/types/relations'; import type { BelongsTo } from '@adonisjs/lucid/types/relations';
export default class Link extends AppBaseModel { export default class Link extends AppBaseModel {
@column() @column()
declare name: string; declare name: string;
@column() @column()
declare description: string | null; declare description: string | null;
@column() @column()
declare url: string; declare url: string;
@column() @column()
declare favorite: boolean; declare favorite: boolean;
@column() @column()
declare collectionId: number; declare collectionId: number;
@belongsTo(() => Collection, { foreignKey: 'collectionId' }) @belongsTo(() => Collection, { foreignKey: 'collectionId' })
declare collection: BelongsTo<typeof Collection>; declare collection: BelongsTo<typeof Collection>;
@column() @column()
declare authorId: number; declare authorId: number;
@belongsTo(() => User, { foreignKey: 'authorId' }) @belongsTo(() => User, { foreignKey: 'authorId' })
declare author: BelongsTo<typeof User>; declare author: BelongsTo<typeof User>;
} }

View File

@@ -6,42 +6,42 @@ import type { HasMany } from '@adonisjs/lucid/types/relations';
import AppBaseModel from './app_base_model.js'; import AppBaseModel from './app_base_model.js';
export default class User extends AppBaseModel { export default class User extends AppBaseModel {
@column() @column()
declare email: string; declare email: string;
@column() @column()
declare name: string; declare name: string;
@column() @column()
declare nickName: string; // public username declare nickName: string; // public username
@column() @column()
declare avatarUrl: string; declare avatarUrl: string;
@column() @column()
declare isAdmin: boolean; declare isAdmin: boolean;
@column({ serializeAs: null }) @column({ serializeAs: null })
declare token?: GoogleToken; declare token?: GoogleToken;
@column({ serializeAs: null }) @column({ serializeAs: null })
declare providerId: number; declare providerId: number;
@column({ serializeAs: null }) @column({ serializeAs: null })
declare providerType: 'google'; declare providerType: 'google';
@hasMany(() => Collection, { @hasMany(() => Collection, {
foreignKey: 'authorId', foreignKey: 'authorId',
}) })
declare collections: HasMany<typeof Collection>; declare collections: HasMany<typeof Collection>;
@hasMany(() => Link, { @hasMany(() => Link, {
foreignKey: 'authorId', foreignKey: 'authorId',
}) })
declare links: HasMany<typeof Link>; declare links: HasMany<typeof Link>;
@computed() @computed()
get fullname() { get fullname() {
return this.nickName || this.name; return this.nickName || this.name;
} }
} }

View File

@@ -2,36 +2,36 @@ import { Visibility } from '#enums/visibility';
import vine, { SimpleMessagesProvider } from '@vinejs/vine'; import vine, { SimpleMessagesProvider } from '@vinejs/vine';
const params = vine.object({ const params = vine.object({
id: vine.number(), id: vine.number(),
}); });
export const createCollectionValidator = vine.compile( export const createCollectionValidator = vine.compile(
vine.object({ vine.object({
name: vine.string().trim().minLength(1).maxLength(254), name: vine.string().trim().minLength(1).maxLength(254),
description: vine.string().trim().maxLength(254).nullable(), description: vine.string().trim().maxLength(254).nullable(),
visibility: vine.enum(Visibility), visibility: vine.enum(Visibility),
nextId: vine.number().optional(), nextId: vine.number().optional(),
}) })
); );
export const updateCollectionValidator = vine.compile( export const updateCollectionValidator = vine.compile(
vine.object({ vine.object({
name: vine.string().trim().minLength(1).maxLength(254), name: vine.string().trim().minLength(1).maxLength(254),
description: vine.string().trim().maxLength(254).nullable(), description: vine.string().trim().maxLength(254).nullable(),
visibility: vine.enum(Visibility), visibility: vine.enum(Visibility),
nextId: vine.number().optional(), nextId: vine.number().optional(),
params, params,
}) })
); );
export const deleteCollectionValidator = vine.compile( export const deleteCollectionValidator = vine.compile(
vine.object({ vine.object({
params, params,
}) })
); );
createCollectionValidator.messagesProvider = new SimpleMessagesProvider({ createCollectionValidator.messagesProvider = new SimpleMessagesProvider({
name: 'Collection name is required', name: 'Collection name is required',
'visibility.required': 'Collection visibiliy is required', 'visibility.required': 'Collection visibiliy is required',
}); });

View File

@@ -1,43 +1,43 @@
import vine from '@vinejs/vine'; import vine from '@vinejs/vine';
const params = vine.object({ const params = vine.object({
id: vine.number(), id: vine.number(),
}); });
export const createLinkValidator = vine.compile( export const createLinkValidator = vine.compile(
vine.object({ vine.object({
name: vine.string().trim().minLength(1).maxLength(254), name: vine.string().trim().minLength(1).maxLength(254),
description: vine.string().trim().maxLength(300).optional(), description: vine.string().trim().maxLength(300).optional(),
url: vine.string().trim(), url: vine.string().trim(),
favorite: vine.boolean(), favorite: vine.boolean(),
collectionId: vine.number(), collectionId: vine.number(),
}) })
); );
export const updateLinkValidator = vine.compile( export const updateLinkValidator = vine.compile(
vine.object({ vine.object({
name: vine.string().trim().minLength(1).maxLength(254), name: vine.string().trim().minLength(1).maxLength(254),
description: vine.string().trim().maxLength(300).optional(), description: vine.string().trim().maxLength(300).optional(),
url: vine.string().trim(), url: vine.string().trim(),
favorite: vine.boolean(), favorite: vine.boolean(),
collectionId: vine.number(), collectionId: vine.number(),
params, params,
}) })
); );
export const deleteLinkValidator = vine.compile( export const deleteLinkValidator = vine.compile(
vine.object({ vine.object({
params, params,
}) })
); );
export const updateLinkFavoriteStatusValidator = vine.compile( export const updateLinkFavoriteStatusValidator = vine.compile(
vine.object({ vine.object({
favorite: vine.boolean(), favorite: vine.boolean(),
params: vine.object({ params: vine.object({
id: vine.number(), id: vine.number(),
}), }),
}) })
); );

View File

@@ -1,11 +1,11 @@
import vine from '@vinejs/vine'; import vine from '@vinejs/vine';
const params = vine.object({ const params = vine.object({
id: vine.number(), id: vine.number(),
}); });
export const getSharedCollectionValidator = vine.compile( export const getSharedCollectionValidator = vine.compile(
vine.object({ vine.object({
params, params,
}) })
); );

View File

@@ -1,7 +1,7 @@
import vine from '@vinejs/vine'; import vine from '@vinejs/vine';
export const updateUserThemeValidator = vine.compile( export const updateUserThemeValidator = vine.compile(
vine.object({ vine.object({
preferDarkTheme: vine.boolean(), preferDarkTheme: vine.boolean(),
}) })
); );

View File

@@ -25,23 +25,23 @@ const APP_ROOT = new URL('../', import.meta.url);
* application. * application.
*/ */
const IMPORTER = (filePath: string) => { const IMPORTER = (filePath: string) => {
if (filePath.startsWith('./') || filePath.startsWith('../')) { if (filePath.startsWith('./') || filePath.startsWith('../')) {
return import(new URL(filePath, APP_ROOT).href); return import(new URL(filePath, APP_ROOT).href);
} }
return import(filePath); return import(filePath);
}; };
new Ignitor(APP_ROOT, { importer: IMPORTER }) new Ignitor(APP_ROOT, { importer: IMPORTER })
.tap((app) => { .tap((app) => {
app.booting(async () => { app.booting(async () => {
await import('#start/env'); await import('#start/env');
}); });
app.listen('SIGTERM', () => app.terminate()); app.listen('SIGTERM', () => app.terminate());
app.listenIf(app.managedByPm2, 'SIGINT', () => app.terminate()); app.listenIf(app.managedByPm2, 'SIGINT', () => app.terminate());
}) })
.ace() .ace()
.handle(process.argv.splice(2)) .handle(process.argv.splice(2))
.catch((error) => { .catch((error) => {
process.exitCode = 1; process.exitCode = 1;
prettyPrintError(error); prettyPrintError(error);
}); });

View File

@@ -23,23 +23,23 @@ const APP_ROOT = new URL('../', import.meta.url);
* application. * application.
*/ */
const IMPORTER = (filePath: string) => { const IMPORTER = (filePath: string) => {
if (filePath.startsWith('./') || filePath.startsWith('../')) { if (filePath.startsWith('./') || filePath.startsWith('../')) {
return import(new URL(filePath, APP_ROOT).href); return import(new URL(filePath, APP_ROOT).href);
} }
return import(filePath); return import(filePath);
}; };
new Ignitor(APP_ROOT, { importer: IMPORTER }) new Ignitor(APP_ROOT, { importer: IMPORTER })
.tap((app) => { .tap((app) => {
app.booting(async () => { app.booting(async () => {
await import('#start/env'); await import('#start/env');
}); });
app.listen('SIGTERM', () => app.terminate()); app.listen('SIGTERM', () => app.terminate());
app.listenIf(app.managedByPm2, 'SIGINT', () => app.terminate()); app.listenIf(app.managedByPm2, 'SIGINT', () => app.terminate());
}) })
.httpServer() .httpServer()
.start() .start()
.catch((error) => { .catch((error) => {
process.exitCode = 1; process.exitCode = 1;
prettyPrintError(error); prettyPrintError(error);
}); });

View File

@@ -27,36 +27,36 @@ const APP_ROOT = new URL('../', import.meta.url);
* application. * application.
*/ */
const IMPORTER = (filePath: string) => { const IMPORTER = (filePath: string) => {
if (filePath.startsWith('./') || filePath.startsWith('../')) { if (filePath.startsWith('./') || filePath.startsWith('../')) {
return import(new URL(filePath, APP_ROOT).href); return import(new URL(filePath, APP_ROOT).href);
} }
return import(filePath); return import(filePath);
}; };
new Ignitor(APP_ROOT, { importer: IMPORTER }) new Ignitor(APP_ROOT, { importer: IMPORTER })
.tap((app) => { .tap((app) => {
app.booting(async () => { app.booting(async () => {
await import('#start/env'); await import('#start/env');
}); });
app.listen('SIGTERM', () => app.terminate()); app.listen('SIGTERM', () => app.terminate());
app.listenIf(app.managedByPm2, 'SIGINT', () => app.terminate()); app.listenIf(app.managedByPm2, 'SIGINT', () => app.terminate());
}) })
.testRunner() .testRunner()
.configure(async (app) => { .configure(async (app) => {
const { runnerHooks, ...config } = await import('../tests/bootstrap.js'); const { runnerHooks, ...config } = await import('../tests/bootstrap.js');
processCLIArgs(process.argv.splice(2)); processCLIArgs(process.argv.splice(2));
configure({ configure({
...app.rcFile.tests, ...app.rcFile.tests,
...config, ...config,
...{ ...{
setup: runnerHooks.setup, setup: runnerHooks.setup,
teardown: runnerHooks.teardown.concat([() => app.terminate()]), teardown: runnerHooks.teardown.concat([() => app.terminate()]),
}, },
}); });
}) })
.run(() => run()) .run(() => run())
.catch((error) => { .catch((error) => {
process.exitCode = 1; process.exitCode = 1;
prettyPrintError(error); prettyPrintError(error);
}); });

View File

@@ -2,18 +2,18 @@ import env from '#start/env';
import { defineConfig, services } from '@adonisjs/ally'; import { defineConfig, services } from '@adonisjs/ally';
const allyConfig = defineConfig({ const allyConfig = defineConfig({
google: services.google({ google: services.google({
clientId: env.get('GOOGLE_CLIENT_ID'), clientId: env.get('GOOGLE_CLIENT_ID'),
clientSecret: env.get('GOOGLE_CLIENT_SECRET'), clientSecret: env.get('GOOGLE_CLIENT_SECRET'),
callbackUrl: env.get('GOOGLE_CLIENT_CALLBACK_URL'), callbackUrl: env.get('GOOGLE_CLIENT_CALLBACK_URL'),
prompt: 'select_account', prompt: 'select_account',
display: 'page', display: 'page',
scopes: ['userinfo.email', 'userinfo.profile'], scopes: ['userinfo.email', 'userinfo.profile'],
}), }),
}); });
export default allyConfig; export default allyConfig;
declare module '@adonisjs/ally/types' { declare module '@adonisjs/ally/types' {
interface SocialProviders extends InferSocialProviders<typeof allyConfig> {} interface SocialProviders extends InferSocialProviders<typeof allyConfig> {}
} }

View File

@@ -16,25 +16,25 @@ export const appKey = new Secret(env.get('APP_KEY'));
* The configuration settings used by the HTTP server * The configuration settings used by the HTTP server
*/ */
export const http = defineConfig({ export const http = defineConfig({
generateRequestId: true, generateRequestId: true,
allowMethodSpoofing: false, allowMethodSpoofing: false,
/** /**
* Enabling async local storage will let you access HTTP context * Enabling async local storage will let you access HTTP context
* from anywhere inside your application. * from anywhere inside your application.
*/ */
useAsyncLocalStorage: false, useAsyncLocalStorage: false,
/** /**
* Manage cookies configuration. The settings for the session id cookie are * Manage cookies configuration. The settings for the session id cookie are
* defined inside the "config/session.ts" file. * defined inside the "config/session.ts" file.
*/ */
cookie: { cookie: {
domain: '', domain: '',
path: '/', path: '/',
maxAge: '2h', maxAge: '2h',
httpOnly: true, httpOnly: true,
secure: app.inProduction, secure: app.inProduction,
sameSite: 'lax', sameSite: 'lax',
}, },
}); });

View File

@@ -3,15 +3,15 @@ import { InferAuthEvents, Authenticators } from '@adonisjs/auth/types';
import { sessionGuard, sessionUserProvider } from '@adonisjs/auth/session'; import { sessionGuard, sessionUserProvider } from '@adonisjs/auth/session';
const authConfig = defineConfig({ const authConfig = defineConfig({
default: 'web', default: 'web',
guards: { guards: {
web: sessionGuard({ web: sessionGuard({
useRememberMeTokens: false, useRememberMeTokens: false,
provider: sessionUserProvider({ provider: sessionUserProvider({
model: () => import('#models/user'), model: () => import('#models/user'),
}), }),
}), }),
}, },
}); });
export default authConfig; export default authConfig;
@@ -21,8 +21,8 @@ export default authConfig;
* guards. * guards.
*/ */
declare module '@adonisjs/auth/types' { declare module '@adonisjs/auth/types' {
interface Authenticators extends InferAuthenticators<typeof authConfig> {} interface Authenticators extends InferAuthenticators<typeof authConfig> {}
} }
declare module '@adonisjs/core/types' { declare module '@adonisjs/core/types' {
interface EventsList extends InferAuthEvents<Authenticators> {} interface EventsList extends InferAuthEvents<Authenticators> {}
} }

View File

@@ -1,55 +1,55 @@
import { defineConfig } from '@adonisjs/core/bodyparser'; import { defineConfig } from '@adonisjs/core/bodyparser';
const bodyParserConfig = defineConfig({ const bodyParserConfig = defineConfig({
/** /**
* The bodyparser middleware will parse the request body * The bodyparser middleware will parse the request body
* for the following HTTP methods. * for the following HTTP methods.
*/ */
allowedMethods: ['POST', 'PUT', 'PATCH', 'DELETE'], allowedMethods: ['POST', 'PUT', 'PATCH', 'DELETE'],
/** /**
* Config for the "application/x-www-form-urlencoded" * Config for the "application/x-www-form-urlencoded"
* content-type parser * content-type parser
*/ */
form: { form: {
convertEmptyStringsToNull: true, convertEmptyStringsToNull: true,
types: ['application/x-www-form-urlencoded'], types: ['application/x-www-form-urlencoded'],
}, },
/** /**
* Config for the JSON parser * Config for the JSON parser
*/ */
json: { json: {
convertEmptyStringsToNull: true, convertEmptyStringsToNull: true,
types: [ types: [
'application/json', 'application/json',
'application/json-patch+json', 'application/json-patch+json',
'application/vnd.api+json', 'application/vnd.api+json',
'application/csp-report', 'application/csp-report',
], ],
}, },
/** /**
* Config for the "multipart/form-data" content-type parser. * Config for the "multipart/form-data" content-type parser.
* File uploads are handled by the multipart parser. * File uploads are handled by the multipart parser.
*/ */
multipart: { multipart: {
/** /**
* Enabling auto process allows bodyparser middleware to * Enabling auto process allows bodyparser middleware to
* move all uploaded files inside the tmp folder of your * move all uploaded files inside the tmp folder of your
* operating system * operating system
*/ */
autoProcess: true, autoProcess: true,
convertEmptyStringsToNull: true, convertEmptyStringsToNull: true,
processManually: [], processManually: [],
/** /**
* Maximum limit of data to parse including all files * Maximum limit of data to parse including all files
* and fields * and fields
*/ */
limit: '20mb', limit: '20mb',
types: ['multipart/form-data'], types: ['multipart/form-data'],
}, },
}); });
export default bodyParserConfig; export default bodyParserConfig;

View File

@@ -7,13 +7,13 @@ import { defineConfig } from '@adonisjs/cors';
* https://docs.adonisjs.com/guides/security/cors * https://docs.adonisjs.com/guides/security/cors
*/ */
const corsConfig = defineConfig({ const corsConfig = defineConfig({
enabled: true, enabled: true,
origin: [], origin: [],
methods: ['GET', 'HEAD', 'POST', 'PUT', 'DELETE'], methods: ['GET', 'HEAD', 'POST', 'PUT', 'DELETE'],
headers: true, headers: true,
exposeHeaders: [], exposeHeaders: [],
credentials: true, credentials: true,
maxAge: 90, maxAge: 90,
}); });
export default corsConfig; export default corsConfig;

View File

@@ -2,26 +2,26 @@ import env from '#start/env';
import { defineConfig } from '@adonisjs/lucid'; import { defineConfig } from '@adonisjs/lucid';
const dbConfig = defineConfig({ const dbConfig = defineConfig({
connection: 'postgres', connection: 'postgres',
connections: { connections: {
postgres: { postgres: {
client: 'pg', client: 'pg',
connection: { connection: {
host: env.get('DB_HOST'), host: env.get('DB_HOST'),
port: env.get('DB_PORT'), port: env.get('DB_PORT'),
user: env.get('DB_USER'), user: env.get('DB_USER'),
password: env.get('DB_PASSWORD'), password: env.get('DB_PASSWORD'),
database: env.get('DB_DATABASE'), database: env.get('DB_DATABASE'),
}, },
migrations: { migrations: {
naturalSort: true, naturalSort: true,
paths: ['database/migrations'], paths: ['database/migrations'],
}, },
seeders: { seeders: {
paths: ['./database/seeders/main'], paths: ['./database/seeders/main'],
}, },
}, },
}, },
}); });
export default dbConfig; export default dbConfig;

View File

@@ -1,16 +1,16 @@
import { defineConfig, drivers } from '@adonisjs/core/hash'; import { defineConfig, drivers } from '@adonisjs/core/hash';
const hashConfig = defineConfig({ const hashConfig = defineConfig({
default: 'scrypt', default: 'scrypt',
list: { list: {
scrypt: drivers.scrypt({ scrypt: drivers.scrypt({
cost: 16384, cost: 16384,
blockSize: 8, blockSize: 8,
parallelization: 1, parallelization: 1,
maxMemory: 33554432, maxMemory: 33554432,
}), }),
}, },
}); });
export default hashConfig; export default hashConfig;
@@ -20,5 +20,5 @@ export default hashConfig;
* in your application. * in your application.
*/ */
declare module '@adonisjs/core/types' { declare module '@adonisjs/core/types' {
export interface HashersList extends InferHashers<typeof hashConfig> {} export interface HashersList extends InferHashers<typeof hashConfig> {}
} }

View File

@@ -1,36 +1,36 @@
import { import {
DARK_THEME_DEFAULT_VALUE, DARK_THEME_DEFAULT_VALUE,
PREFER_DARK_THEME, PREFER_DARK_THEME,
} from '#constants/session'; } from '#constants/session';
import { defineConfig } from '@adonisjs/inertia'; import { defineConfig } from '@adonisjs/inertia';
export default defineConfig({ export default defineConfig({
/** /**
* Path to the Edge view that will be used as the root view for Inertia responses * Path to the Edge view that will be used as the root view for Inertia responses
*/ */
rootView: 'inertia_layout', rootView: 'inertia_layout',
/** /**
* Data that should be shared with all rendered pages * Data that should be shared with all rendered pages
*/ */
sharedData: { sharedData: {
errors: (ctx) => ctx.session?.flashMessages.get('errors'), errors: (ctx) => ctx.session?.flashMessages.get('errors'),
preferDarkTheme: (ctx) => preferDarkTheme: (ctx) =>
ctx.session?.get(PREFER_DARK_THEME, DARK_THEME_DEFAULT_VALUE), ctx.session?.get(PREFER_DARK_THEME, DARK_THEME_DEFAULT_VALUE),
auth: async (ctx) => { auth: async (ctx) => {
await ctx.auth?.check(); await ctx.auth?.check();
return { return {
user: ctx.auth?.user || null, user: ctx.auth?.user || null,
isAuthenticated: ctx.auth?.isAuthenticated || false, isAuthenticated: ctx.auth?.isAuthenticated || false,
}; };
}, },
}, },
/** /**
* Options for the server-side rendering * Options for the server-side rendering
*/ */
ssr: { ssr: {
enabled: true, enabled: true,
entrypoint: 'inertia/app/ssr.tsx', entrypoint: 'inertia/app/ssr.tsx',
}, },
}); });

View File

@@ -3,25 +3,25 @@ import app from '@adonisjs/core/services/app';
import { defineConfig, targets } from '@adonisjs/core/logger'; import { defineConfig, targets } from '@adonisjs/core/logger';
const loggerConfig = defineConfig({ const loggerConfig = defineConfig({
default: 'app', default: 'app',
/** /**
* The loggers object can be used to define multiple loggers. * The loggers object can be used to define multiple loggers.
* By default, we configure only one logger (named "app"). * By default, we configure only one logger (named "app").
*/ */
loggers: { loggers: {
app: { app: {
enabled: true, enabled: true,
name: env.get('APP_NAME'), name: env.get('APP_NAME'),
level: env.get('LOG_LEVEL'), level: env.get('LOG_LEVEL'),
transport: { transport: {
targets: targets() targets: targets()
.pushIf(!app.inProduction, targets.pretty()) .pushIf(!app.inProduction, targets.pretty())
.pushIf(app.inProduction, targets.file({ destination: 1 })) .pushIf(app.inProduction, targets.file({ destination: 1 }))
.toArray(), .toArray(),
}, },
}, },
}, },
}); });
export default loggerConfig; export default loggerConfig;
@@ -31,5 +31,5 @@ export default loggerConfig;
* in your application. * in your application.
*/ */
declare module '@adonisjs/core/types' { declare module '@adonisjs/core/types' {
export interface LoggersList extends InferLoggers<typeof loggerConfig> {} export interface LoggersList extends InferLoggers<typeof loggerConfig> {}
} }

View File

@@ -2,48 +2,48 @@ import env from '#start/env';
import { defineConfig, stores } from '@adonisjs/session'; import { defineConfig, stores } from '@adonisjs/session';
const sessionConfig = defineConfig({ const sessionConfig = defineConfig({
enabled: true, enabled: true,
cookieName: 'adonis-session', cookieName: 'adonis-session',
/** /**
* When set to true, the session id cookie will be deleted * When set to true, the session id cookie will be deleted
* once the user closes the browser. * once the user closes the browser.
*/ */
clearWithBrowser: false, clearWithBrowser: false,
/** /**
* Define how long to keep the session data alive without * Define how long to keep the session data alive without
* any activity. * any activity.
*/ */
age: '7d', age: '7d',
/** /**
* Configuration for session cookie and the * Configuration for session cookie and the
* cookie store * cookie store
*/ */
cookie: { cookie: {
path: '/', path: '/',
httpOnly: true, httpOnly: true,
secure: true, secure: true,
// TODO: set this to lax and found a solution to keep auth when using extension // TODO: set this to lax and found a solution to keep auth when using extension
sameSite: 'none', sameSite: 'none',
}, },
/** /**
* The store to use. Make sure to validate the environment * The store to use. Make sure to validate the environment
* variable in order to infer the store name without any * variable in order to infer the store name without any
* errors. * errors.
*/ */
store: env.get('SESSION_DRIVER'), store: env.get('SESSION_DRIVER'),
/** /**
* List of configured stores. Refer documentation to see * List of configured stores. Refer documentation to see
* list of available stores and their config. * list of available stores and their config.
*/ */
stores: { stores: {
cookie: stores.cookie(), cookie: stores.cookie(),
}, },
}); });
export default sessionConfig; export default sessionConfig;

View File

@@ -1,50 +1,50 @@
import { defineConfig } from '@adonisjs/shield'; import { defineConfig } from '@adonisjs/shield';
const shieldConfig = defineConfig({ const shieldConfig = defineConfig({
/** /**
* Configure CSP policies for your app. Refer documentation * Configure CSP policies for your app. Refer documentation
* to learn more * to learn more
*/ */
csp: { csp: {
enabled: false, enabled: false,
directives: {}, directives: {},
reportOnly: false, reportOnly: false,
}, },
/** /**
* Configure CSRF protection options. Refer documentation * Configure CSRF protection options. Refer documentation
* to learn more * to learn more
*/ */
csrf: { csrf: {
enabled: false, enabled: false,
exceptRoutes: [], exceptRoutes: [],
enableXsrfCookie: true, enableXsrfCookie: true,
methods: ['POST', 'PUT', 'PATCH', 'DELETE'], methods: ['POST', 'PUT', 'PATCH', 'DELETE'],
}, },
/** /**
* Control how your website should be embedded inside * Control how your website should be embedded inside
* iFrames * iFrames
*/ */
xFrame: { xFrame: {
enabled: false, enabled: false,
}, },
/** /**
* Force browser to always use HTTPS * Force browser to always use HTTPS
*/ */
hsts: { hsts: {
enabled: true, enabled: true,
maxAge: '180 days', maxAge: '180 days',
}, },
/** /**
* Disable browsers from sniffing the content type of a * Disable browsers from sniffing the content type of a
* response and always rely on the "content-type" header. * response and always rely on the "content-type" header.
*/ */
contentTypeSniffing: { contentTypeSniffing: {
enabled: true, enabled: true,
}, },
}); });
export default shieldConfig; export default shieldConfig;

View File

@@ -8,10 +8,10 @@ import { defineConfig } from '@adonisjs/static';
* https://docs.adonisjs.com/guides/static-assets * https://docs.adonisjs.com/guides/static-assets
*/ */
const staticServerConfig = defineConfig({ const staticServerConfig = defineConfig({
enabled: true, enabled: true,
etag: true, etag: true,
lastModified: true, lastModified: true,
dotFiles: 'ignore', dotFiles: 'ignore',
}); });
export default staticServerConfig; export default staticServerConfig;

View File

@@ -1,28 +1,28 @@
import { defineConfig } from '@adonisjs/vite'; import { defineConfig } from '@adonisjs/vite';
const viteBackendConfig = defineConfig({ const viteBackendConfig = defineConfig({
/** /**
* The output of vite will be written inside this * The output of vite will be written inside this
* directory. The path should be relative from * directory. The path should be relative from
* the application root. * the application root.
*/ */
buildDirectory: 'public/assets', buildDirectory: 'public/assets',
/** /**
* The path to the manifest file generated by the * The path to the manifest file generated by the
* "vite build" command. * "vite build" command.
*/ */
manifestFile: 'public/assets/.vite/manifest.json', manifestFile: 'public/assets/.vite/manifest.json',
/** /**
* Feel free to change the value of the "assetsUrl" to * Feel free to change the value of the "assetsUrl" to
* point to a CDN in production. * point to a CDN in production.
*/ */
assetsUrl: '/assets', assetsUrl: '/assets',
scriptAttributes: { scriptAttributes: {
defer: true, defer: true,
}, },
}); });
export default viteBackendConfig; export default viteBackendConfig;

View File

@@ -1,8 +1,8 @@
import { Knex } from 'knex'; import { Knex } from 'knex';
export function defaultTableFields(table: Knex.CreateTableBuilder) { export function defaultTableFields(table: Knex.CreateTableBuilder) {
table.increments('id').primary().first().unique().notNullable(); table.increments('id').primary().first().unique().notNullable();
table.timestamp('created_at').notNullable(); table.timestamp('created_at').notNullable();
table.timestamp('updated_at').nullable(); table.timestamp('updated_at').nullable();
} }

View File

@@ -2,25 +2,25 @@ import { defaultTableFields } from '#database/default_table_fields';
import { BaseSchema } from '@adonisjs/lucid/schema'; import { BaseSchema } from '@adonisjs/lucid/schema';
export default class CreateUsersTable extends BaseSchema { export default class CreateUsersTable extends BaseSchema {
static tableName = 'users'; static tableName = 'users';
async up() { async up() {
this.schema.createTableIfNotExists(CreateUsersTable.tableName, (table) => { this.schema.createTableIfNotExists(CreateUsersTable.tableName, (table) => {
table.string('email', 254).notNullable().unique(); table.string('email', 254).notNullable().unique();
table.string('name', 254).notNullable(); table.string('name', 254).notNullable();
table.string('nick_name', 254).nullable(); table.string('nick_name', 254).nullable();
table.text('avatar_url').notNullable(); table.text('avatar_url').notNullable();
table.boolean('is_admin').defaultTo(0).notNullable(); table.boolean('is_admin').defaultTo(0).notNullable();
table.json('token').nullable(); table.json('token').nullable();
table.string('provider_id').notNullable(); table.string('provider_id').notNullable();
table.enum('provider_type', ['google']).notNullable().defaultTo('google'); table.enum('provider_type', ['google']).notNullable().defaultTo('google');
defaultTableFields(table); defaultTableFields(table);
}); });
} }
async down() { async down() {
this.schema.dropTable(CreateUsersTable.tableName); this.schema.dropTable(CreateUsersTable.tableName);
} }
} }

View File

@@ -3,42 +3,42 @@ import { Visibility } from '#enums/visibility';
import { BaseSchema } from '@adonisjs/lucid/schema'; import { BaseSchema } from '@adonisjs/lucid/schema';
export default class CreateCollectionTable extends BaseSchema { export default class CreateCollectionTable extends BaseSchema {
static tableName = 'collections'; static tableName = 'collections';
private visibilityEnumName = 'collection_visibility'; private visibilityEnumName = 'collection_visibility';
async up() { async up() {
this.schema.raw(`DROP TYPE IF EXISTS ${this.visibilityEnumName}`); this.schema.raw(`DROP TYPE IF EXISTS ${this.visibilityEnumName}`);
this.schema.createTableIfNotExists( this.schema.createTableIfNotExists(
CreateCollectionTable.tableName, CreateCollectionTable.tableName,
(table) => { (table) => {
table.string('name', 254).notNullable(); table.string('name', 254).notNullable();
table.string('description', 254).nullable(); table.string('description', 254).nullable();
table table
.enum('visibility', Object.values(Visibility), { .enum('visibility', Object.values(Visibility), {
useNative: true, useNative: true,
enumName: this.visibilityEnumName, enumName: this.visibilityEnumName,
existingType: false, existingType: false,
}) })
.nullable() .nullable()
.defaultTo(Visibility.PRIVATE); .defaultTo(Visibility.PRIVATE);
table table
.integer('next_id') .integer('next_id')
.references('id') .references('id')
.inTable('collections') .inTable('collections')
.defaultTo(null); .defaultTo(null);
table table
.integer('author_id') .integer('author_id')
.references('id') .references('id')
.inTable('users') .inTable('users')
.onDelete('CASCADE'); .onDelete('CASCADE');
defaultTableFields(table); defaultTableFields(table);
} }
); );
} }
async down() { async down() {
this.schema.raw(`DROP TYPE IF EXISTS ${this.visibilityEnumName}`); this.schema.raw(`DROP TYPE IF EXISTS ${this.visibilityEnumName}`);
this.schema.dropTable(CreateCollectionTable.tableName); this.schema.dropTable(CreateCollectionTable.tableName);
} }
} }

View File

@@ -2,30 +2,30 @@ import { defaultTableFields } from '#database/default_table_fields';
import { BaseSchema } from '@adonisjs/lucid/schema'; import { BaseSchema } from '@adonisjs/lucid/schema';
export default class CreateLinksTable extends BaseSchema { export default class CreateLinksTable extends BaseSchema {
static tableName = 'links'; static tableName = 'links';
async up() { async up() {
this.schema.createTableIfNotExists(CreateLinksTable.tableName, (table) => { this.schema.createTableIfNotExists(CreateLinksTable.tableName, (table) => {
table.string('name', 254).notNullable(); table.string('name', 254).notNullable();
table.string('description', 254).nullable(); table.string('description', 254).nullable();
table.text('url').notNullable(); table.text('url').notNullable();
table.boolean('favorite').notNullable().defaultTo(0); table.boolean('favorite').notNullable().defaultTo(0);
table table
.integer('collection_id') .integer('collection_id')
.references('id') .references('id')
.inTable('collections') .inTable('collections')
.onDelete('CASCADE'); .onDelete('CASCADE');
table table
.integer('author_id') .integer('author_id')
.references('id') .references('id')
.inTable('users') .inTable('users')
.onDelete('CASCADE'); .onDelete('CASCADE');
defaultTableFields(table); defaultTableFields(table);
}); });
} }
async down() { async down() {
this.schema.dropTable(CreateLinksTable.tableName); this.schema.dropTable(CreateLinksTable.tableName);
} }
} }

View File

@@ -1,18 +1,18 @@
import { BaseSchema } from '@adonisjs/lucid/schema'; import { BaseSchema } from '@adonisjs/lucid/schema';
export default class extends BaseSchema { export default class extends BaseSchema {
async up() { async up() {
this.schema.raw(` this.schema.raw(`
CREATE EXTENSION IF NOT EXISTS unaccent; CREATE EXTENSION IF NOT EXISTS unaccent;
CREATE EXTENSION IF NOT EXISTS pg_trgm; CREATE EXTENSION IF NOT EXISTS pg_trgm;
`); `);
this.schema.raw(` this.schema.raw(`
CREATE INDEX ON links USING gin(to_tsvector('english', name)); CREATE INDEX ON links USING gin(to_tsvector('english', name));
CREATE INDEX ON collections USING gin(to_tsvector('english', name)); CREATE INDEX ON collections USING gin(to_tsvector('english', name));
CREATE INDEX ON links USING gin(to_tsvector('french', name)); CREATE INDEX ON links USING gin(to_tsvector('french', name));
CREATE INDEX ON collections USING gin(to_tsvector('french', name)); CREATE INDEX ON collections USING gin(to_tsvector('french', name));
`); `);
this.schema.raw(` this.schema.raw(`
CREATE OR REPLACE FUNCTION search_text(search_query TEXT, p_author_id INTEGER) CREATE OR REPLACE FUNCTION search_text(search_query TEXT, p_author_id INTEGER)
RETURNS TABLE ( RETURNS TABLE (
id INTEGER, id INTEGER,
@@ -45,9 +45,9 @@ export default class extends BaseSchema {
$$ $$
LANGUAGE plpgsql; LANGUAGE plpgsql;
`); `);
} }
async down() { async down() {
this.schema.raw('DROP FUNCTION IF EXISTS search_text'); this.schema.raw('DROP FUNCTION IF EXISTS search_text');
} }
} }

View File

@@ -5,36 +5,36 @@ import { BaseSeeder } from '@adonisjs/lucid/seeders';
import { faker } from '@faker-js/faker'; import { faker } from '@faker-js/faker';
export default class extends BaseSeeder { export default class extends BaseSeeder {
static environment = ['development', 'testing']; static environment = ['development', 'testing'];
async run() { async run() {
const users = await getUserIds(); const users = await getUserIds();
const collections = faker.helpers.multiple( const collections = faker.helpers.multiple(
() => createRandomCollection(users), () => createRandomCollection(users),
{ {
count: 50, count: 50,
} }
); );
await Collection.createMany(collections); await Collection.createMany(collections);
} }
} }
export async function getUserIds() { export async function getUserIds() {
const users = await User.all(); const users = await User.all();
return users.map(({ id }) => id); return users.map(({ id }) => id);
} }
let collectionId = 0; let collectionId = 0;
function createRandomCollection(userIds: User['id'][]) { function createRandomCollection(userIds: User['id'][]) {
const authorId = faker.helpers.arrayElements(userIds, 1).at(0); const authorId = faker.helpers.arrayElements(userIds, 1).at(0);
collectionId++; collectionId++;
return { return {
id: collectionId, id: collectionId,
name: faker.string.alphanumeric({ length: { min: 5, max: 25 } }), name: faker.string.alphanumeric({ length: { min: 5, max: 25 } }),
description: faker.string.alphanumeric({ length: { min: 0, max: 254 } }), description: faker.string.alphanumeric({ length: { min: 0, max: 254 } }),
visibility: Visibility.PRIVATE, visibility: Visibility.PRIVATE,
nextId: collectionId + 1, nextId: collectionId + 1,
authorId, authorId,
}; };
} }

View File

@@ -6,41 +6,41 @@ import { BaseSeeder } from '@adonisjs/lucid/seeders';
import { faker } from '@faker-js/faker'; import { faker } from '@faker-js/faker';
export default class extends BaseSeeder { export default class extends BaseSeeder {
static environment = ['development', 'testing']; static environment = ['development', 'testing'];
async run() { async run() {
const users = await getUserIds(); const users = await getUserIds();
const links = await Promise.all( const links = await Promise.all(
faker.helpers.multiple(async () => createRandomLink(users), { faker.helpers.multiple(async () => createRandomLink(users), {
count: 500, count: 500,
}) })
); );
await Link.createMany(links.filter((a) => typeof a !== 'undefined') as any); await Link.createMany(links.filter((a) => typeof a !== 'undefined') as any);
} }
} }
async function getCollectionIds(authorId: User['id']) { async function getCollectionIds(authorId: User['id']) {
const collection = await Collection.findManyBy('author_id', authorId); const collection = await Collection.findManyBy('author_id', authorId);
return collection.map(({ id }) => id); return collection.map(({ id }) => id);
} }
async function createRandomLink(userIds: User['id'][]) { async function createRandomLink(userIds: User['id'][]) {
const authorId = faker.helpers.arrayElements(userIds, 1).at(0)!; const authorId = faker.helpers.arrayElements(userIds, 1).at(0)!;
const collections = await getCollectionIds(authorId); const collections = await getCollectionIds(authorId);
const collectionId = faker.helpers.arrayElements(collections, 1).at(0); const collectionId = faker.helpers.arrayElements(collections, 1).at(0);
if (!collectionId) { if (!collectionId) {
return undefined; return undefined;
} }
return { return {
id: faker.string.uuid(), id: faker.string.uuid(),
name: faker.string.alphanumeric({ length: { min: 5, max: 25 } }), name: faker.string.alphanumeric({ length: { min: 5, max: 25 } }),
description: faker.string.alphanumeric({ length: { min: 0, max: 254 } }), description: faker.string.alphanumeric({ length: { min: 0, max: 254 } }),
url: faker.internet.url(), url: faker.internet.url(),
favorite: faker.number.int({ min: 0, max: 1 }), favorite: faker.number.int({ min: 0, max: 1 }),
authorId, authorId,
collectionId, collectionId,
}; };
} }

View File

@@ -3,31 +3,31 @@ import logger from '@adonisjs/core/services/logger';
import { BaseSeeder } from '@adonisjs/lucid/seeders'; import { BaseSeeder } from '@adonisjs/lucid/seeders';
export default class IndexSeeder extends BaseSeeder { export default class IndexSeeder extends BaseSeeder {
private async seed(Seeder: { default: typeof BaseSeeder }) { private async seed(Seeder: { default: typeof BaseSeeder }) {
/** /**
* Do not run when not in a environment specified in Seeder * Do not run when not in a environment specified in Seeder
*/ */
if ( if (
!Seeder.default.environment || !Seeder.default.environment ||
(!Seeder.default.environment.includes('development') && app.inDev) || (!Seeder.default.environment.includes('development') && app.inDev) ||
(!Seeder.default.environment.includes('testing') && app.inTest) || (!Seeder.default.environment.includes('testing') && app.inTest) ||
(!Seeder.default.environment.includes('production') && app.inProduction) (!Seeder.default.environment.includes('production') && app.inProduction)
) { ) {
return; return;
} }
await new Seeder.default(this.client).run(); await new Seeder.default(this.client).run();
} }
async run() { async run() {
logger.info('Start user seed'); logger.info('Start user seed');
await this.seed(await import('#database/seeders/user_seeder')); await this.seed(await import('#database/seeders/user_seeder'));
logger.info('User seed done'); logger.info('User seed done');
logger.info('Collection user seed'); logger.info('Collection user seed');
await this.seed(await import('#database/seeders/collection_seeder')); await this.seed(await import('#database/seeders/collection_seeder'));
logger.info('Collection seed done'); logger.info('Collection seed done');
logger.info('Link user seed'); logger.info('Link user seed');
await this.seed(await import('#database/seeders/link_seeder')); await this.seed(await import('#database/seeders/link_seeder'));
logger.info('Link seed done'); logger.info('Link seed done');
} }
} }

View File

@@ -4,26 +4,26 @@ import { BaseSeeder } from '@adonisjs/lucid/seeders';
import { faker } from '@faker-js/faker'; import { faker } from '@faker-js/faker';
export default class extends BaseSeeder { export default class extends BaseSeeder {
static environment = ['development', 'testing']; static environment = ['development', 'testing'];
async run() { async run() {
const users = faker.helpers.multiple(createRandomUser, { const users = faker.helpers.multiple(createRandomUser, {
count: 25, count: 25,
}); });
await User.createMany(users); await User.createMany(users);
} }
} }
export function createRandomUser() { export function createRandomUser() {
return { return {
id: faker.number.int(), id: faker.number.int(),
email: faker.internet.email(), email: faker.internet.email(),
name: faker.internet.userName(), name: faker.internet.userName(),
nickName: faker.internet.displayName(), nickName: faker.internet.displayName(),
avatarUrl: faker.image.avatar(), avatarUrl: faker.image.avatar(),
isAdmin: false, isAdmin: false,
providerId: faker.number.int(), providerId: faker.number.int(),
providerType: 'google' as const, providerType: 'google' as const,
token: {} as GoogleToken, token: {} as GoogleToken,
}; };
} }

View File

@@ -10,18 +10,18 @@ import '../i18n/index';
const appName = import.meta.env.VITE_APP_NAME || 'MyLinks'; const appName = import.meta.env.VITE_APP_NAME || 'MyLinks';
createInertiaApp({ createInertiaApp({
progress: { color: primaryColor }, progress: { color: primaryColor },
title: (title) => `${appName}${title && ` - ${title}`}`, title: (title) => `${appName}${title && ` - ${title}`}`,
resolve: (name) => { resolve: (name) => {
return resolvePageComponent( return resolvePageComponent(
`../pages/${name}.tsx`, `../pages/${name}.tsx`,
import.meta.glob('../pages/**/*.tsx') import.meta.glob('../pages/**/*.tsx')
); );
}, },
setup({ el, App, props }) { setup({ el, App, props }) {
hydrateRoot(el, <App {...props} />); hydrateRoot(el, <App {...props} />);
}, },
}); });

View File

@@ -2,13 +2,13 @@ import { createInertiaApp } from '@inertiajs/react';
import ReactDOMServer from 'react-dom/server'; import ReactDOMServer from 'react-dom/server';
export default function render(page: any) { export default function render(page: any) {
return createInertiaApp({ return createInertiaApp({
page, page,
render: ReactDOMServer.renderToString, render: ReactDOMServer.renderToString,
resolve: (name) => { resolve: (name) => {
const pages = import.meta.glob('../pages/**/*.tsx', { eager: true }); const pages = import.meta.glob('../pages/**/*.tsx', { eager: true });
return pages[`../pages/${name}.tsx`]; return pages[`../pages/${name}.tsx`];
}, },
setup: ({ App, props }) => <App {...props} />, setup: ({ App, props }) => <App {...props} />,
}); });
} }

View File

@@ -7,58 +7,58 @@ import useToggle from '~/hooks/use_modal';
import useShortcut from '~/hooks/use_shortcut'; import useShortcut from '~/hooks/use_shortcut';
const DropdownStyle = styled.div<{ opened: boolean; svgSize?: number }>( const DropdownStyle = styled.div<{ opened: boolean; svgSize?: number }>(
({ opened, theme, svgSize = 24 }) => ({ ({ opened, theme, svgSize = 24 }) => ({
cursor: 'pointer', cursor: 'pointer',
userSelect: 'none', userSelect: 'none',
position: 'relative', position: 'relative',
minWidth: 'fit-content', minWidth: 'fit-content',
width: 'fit-content', width: 'fit-content',
maxWidth: '250px', maxWidth: '250px',
backgroundColor: opened ? theme.colors.secondary : theme.colors.background, backgroundColor: opened ? theme.colors.secondary : theme.colors.background,
padding: '4px', padding: '4px',
borderRadius: theme.border.radius, borderRadius: theme.border.radius,
'&:hover': { '&:hover': {
backgroundColor: theme.colors.secondary, backgroundColor: theme.colors.secondary,
}, },
'& svg': { '& svg': {
height: `${svgSize}px`, height: `${svgSize}px`,
width: `${svgSize}px`, width: `${svgSize}px`,
}, },
}) })
); );
export default function Dropdown({ export default function Dropdown({
children, children,
label, label,
className, className,
svgSize, svgSize,
onClick, onClick,
}: HtmlHTMLAttributes<HTMLDivElement> & { }: HtmlHTMLAttributes<HTMLDivElement> & {
label: ReactNode | string; label: ReactNode | string;
className?: string; className?: string;
svgSize?: number; svgSize?: number;
}) { }) {
const dropdownRef = useRef<HTMLDivElement>(null); const dropdownRef = useRef<HTMLDivElement>(null);
const { isShowing, toggle, close } = useToggle(); const { isShowing, toggle, close } = useToggle();
useClickOutside(dropdownRef, close); useClickOutside(dropdownRef, close);
useShortcut('ESCAPE_KEY', close, { disableGlobalCheck: true }); useShortcut('ESCAPE_KEY', close, { disableGlobalCheck: true });
return ( return (
<DropdownStyle <DropdownStyle
opened={isShowing} opened={isShowing}
onClick={(event) => { onClick={(event) => {
onClick?.(event); onClick?.(event);
toggle(); toggle();
}} }}
ref={dropdownRef} ref={dropdownRef}
className={className} className={className}
svgSize={svgSize} svgSize={svgSize}
> >
<DropdownLabel>{label}</DropdownLabel> <DropdownLabel>{label}</DropdownLabel>
<DropdownContainer show={isShowing}>{children}</DropdownContainer> <DropdownContainer show={isShowing}>{children}</DropdownContainer>
</DropdownStyle> </DropdownStyle>
); );
} }

View File

@@ -2,20 +2,20 @@ import styled from '@emotion/styled';
import TransitionLayout from '~/components/layouts/_transition_layout'; import TransitionLayout from '~/components/layouts/_transition_layout';
const DropdownContainer = styled(TransitionLayout)<{ show: boolean }>( const DropdownContainer = styled(TransitionLayout)<{ show: boolean }>(
({ show, theme }) => ({ ({ show, theme }) => ({
zIndex: 99, zIndex: 99,
position: 'absolute', position: 'absolute',
top: 'calc(100% + 0.5em)', top: 'calc(100% + 0.5em)',
right: 0, right: 0,
minWidth: '175px', minWidth: '175px',
backgroundColor: show ? theme.colors.secondary : theme.colors.background, backgroundColor: show ? theme.colors.secondary : theme.colors.background,
border: `2px solid ${theme.colors.secondary}`, border: `2px solid ${theme.colors.secondary}`,
borderRadius: theme.border.radius, borderRadius: theme.border.radius,
boxShadow: theme.colors.boxShadow, boxShadow: theme.colors.boxShadow,
display: show ? 'flex' : 'none', display: show ? 'flex' : 'none',
flexDirection: 'column', flexDirection: 'column',
overflow: 'hidden', overflow: 'hidden',
}) })
); );
export default DropdownContainer; export default DropdownContainer;

View File

@@ -2,30 +2,30 @@ import styled from '@emotion/styled';
import { Link } from '@inertiajs/react'; import { Link } from '@inertiajs/react';
const DropdownItemBase = styled('div', { const DropdownItemBase = styled('div', {
shouldForwardProp: (propName) => propName !== 'danger', shouldForwardProp: (propName) => propName !== 'danger',
})<{ danger?: boolean }>(({ theme, danger }) => ({ })<{ danger?: boolean }>(({ theme, danger }) => ({
fontSize: '14px', fontSize: '14px',
whiteSpace: 'nowrap', whiteSpace: 'nowrap',
color: danger ? theme.colors.lightRed : theme.colors.primary, color: danger ? theme.colors.lightRed : theme.colors.primary,
padding: '8px 12px', padding: '8px 12px',
borderRadius: theme.border.radius, borderRadius: theme.border.radius,
'&:hover': { '&:hover': {
backgroundColor: theme.colors.background, backgroundColor: theme.colors.background,
}, },
})); }));
const DropdownItemButton = styled(DropdownItemBase)({ const DropdownItemButton = styled(DropdownItemBase)({
display: 'flex', display: 'flex',
gap: '0.75em', gap: '0.75em',
alignItems: 'center', alignItems: 'center',
}); });
const DropdownItemLink = styled(DropdownItemBase.withComponent(Link))({ const DropdownItemLink = styled(DropdownItemBase.withComponent(Link))({
width: '100%', width: '100%',
display: 'flex', display: 'flex',
gap: '0.75em', gap: '0.75em',
alignItems: 'center', alignItems: 'center',
}); });
export { DropdownItemButton, DropdownItemLink }; export { DropdownItemButton, DropdownItemLink };

View File

@@ -1,11 +1,11 @@
import styled from '@emotion/styled'; import styled from '@emotion/styled';
const DropdownLabel = styled.div(({ theme }) => ({ const DropdownLabel = styled.div(({ theme }) => ({
height: 'auto', height: 'auto',
width: 'auto', width: 'auto',
color: theme.colors.font, color: theme.colors.font,
display: 'flex', display: 'flex',
gap: '0.35em', gap: '0.35em',
})); }));
export default DropdownLabel; export default DropdownLabel;

View File

@@ -1,18 +1,18 @@
import { AnchorHTMLAttributes, CSSProperties, ReactNode } from 'react'; import { AnchorHTMLAttributes, CSSProperties, ReactNode } from 'react';
export default function ExternalLink({ export default function ExternalLink({
children, children,
title, title,
...props ...props
}: AnchorHTMLAttributes<HTMLAnchorElement> & { }: AnchorHTMLAttributes<HTMLAnchorElement> & {
children: ReactNode; children: ReactNode;
style?: CSSProperties; style?: CSSProperties;
title?: string; title?: string;
className?: string; className?: string;
}) { }) {
return ( return (
<a target="_blank" rel="noreferrer" title={title} {...props}> <a target="_blank" rel="noreferrer" title={title} {...props}>
{children} {children}
</a> </a>
); );
} }

View File

@@ -1,31 +1,31 @@
import styled from '@emotion/styled'; import styled from '@emotion/styled';
const Button = styled.button<{ danger?: boolean }>(({ theme, danger }) => { const Button = styled.button<{ danger?: boolean }>(({ theme, danger }) => {
const btnColor = !danger ? theme.colors.primary : theme.colors.lightRed; const btnColor = !danger ? theme.colors.primary : theme.colors.lightRed;
const btnDarkColor = !danger ? theme.colors.darkBlue : theme.colors.lightRed; const btnDarkColor = !danger ? theme.colors.darkBlue : theme.colors.lightRed;
return { return {
cursor: 'pointer', cursor: 'pointer',
width: '100%', width: '100%',
textTransform: 'uppercase', textTransform: 'uppercase',
fontSize: '14px', fontSize: '14px',
color: theme.colors.white, color: theme.colors.white,
background: btnColor, background: btnColor,
padding: '0.75em', padding: '0.75em',
border: `1px solid ${btnColor}`, border: `1px solid ${btnColor}`,
borderRadius: theme.border.radius, borderRadius: theme.border.radius,
transition: theme.transition.delay, transition: theme.transition.delay,
'&:disabled': { '&:disabled': {
cursor: 'not-allowed', cursor: 'not-allowed',
opacity: '0.5', opacity: '0.5',
}, },
'&:not(:disabled):hover': { '&:not(:disabled):hover': {
boxShadow: `${btnDarkColor} 0 0 3px 1px`, boxShadow: `${btnDarkColor} 0 0 3px 1px`,
background: btnDarkColor, background: btnDarkColor,
color: theme.colors.white, color: theme.colors.white,
}, },
}; };
}); });
export default Button; export default Button;

View File

@@ -1,10 +1,10 @@
import styled from '@emotion/styled'; import styled from '@emotion/styled';
const Form = styled.form({ const Form = styled.form({
width: '100%', width: '100%',
display: 'flex', display: 'flex',
gap: '1em', gap: '1em',
flexDirection: 'column', flexDirection: 'column',
}); });
export default Form; export default Form;

View File

@@ -1,25 +1,25 @@
import styled from '@emotion/styled'; import styled from '@emotion/styled';
const FormField = styled('div', { const FormField = styled('div', {
shouldForwardProp: (propName) => propName !== 'required', shouldForwardProp: (propName) => propName !== 'required',
})<{ required?: boolean }>(({ required, theme }) => ({ })<{ required?: boolean }>(({ required, theme }) => ({
display: 'flex', display: 'flex',
gap: '0.25em', gap: '0.25em',
flexDirection: 'column', flexDirection: 'column',
'& label': { '& label': {
position: 'relative', position: 'relative',
userSelect: 'none', userSelect: 'none',
width: 'fit-content', width: 'fit-content',
}, },
'& label::after': { '& label::after': {
position: 'absolute', position: 'absolute',
top: 0, top: 0,
right: '-0.75em', right: '-0.75em',
color: theme.colors.lightRed, color: theme.colors.lightRed,
content: (required ? '"*"' : '""') as any, content: (required ? '"*"' : '""') as any,
}, },
})); }));
export default FormField; export default FormField;

View File

@@ -2,8 +2,8 @@ import styled from '@emotion/styled';
// TODO: create a global style variable (fontSize) // TODO: create a global style variable (fontSize)
const FormFieldError = styled.p(({ theme }) => ({ const FormFieldError = styled.p(({ theme }) => ({
fontSize: '12px', fontSize: '12px',
color: theme.colors.lightRed, color: theme.colors.lightRed,
})); }));
export default FormFieldError; export default FormFieldError;

View File

@@ -1,27 +1,27 @@
import styled from '@emotion/styled'; import styled from '@emotion/styled';
const Input = styled.input(({ theme }) => ({ const Input = styled.input(({ theme }) => ({
width: '100%', width: '100%',
color: theme.colors.font, color: theme.colors.font,
backgroundColor: theme.colors.secondary, backgroundColor: theme.colors.secondary,
padding: '0.75em', padding: '0.75em',
border: `1px solid ${theme.colors.lightGrey}`, border: `1px solid ${theme.colors.lightGrey}`,
borderBottom: `2px solid ${theme.colors.lightGrey}`, borderBottom: `2px solid ${theme.colors.lightGrey}`,
borderRadius: theme.border.radius, borderRadius: theme.border.radius,
transition: theme.transition.delay, transition: theme.transition.delay,
'&:focus': { '&:focus': {
borderBottom: `2px solid ${theme.colors.primary}`, borderBottom: `2px solid ${theme.colors.primary}`,
}, },
'&:disabled': { '&:disabled': {
opacity: 0.85, opacity: 0.85,
}, },
'&::placeholder': { '&::placeholder': {
fontStyle: 'italic', fontStyle: 'italic',
color: theme.colors.grey, color: theme.colors.grey,
}, },
})); }));
export default Input; export default Input;

View File

@@ -4,52 +4,52 @@ import FormField from '~/components/common/form/_form_field';
import FormFieldError from '~/components/common/form/_form_field_error'; import FormFieldError from '~/components/common/form/_form_field_error';
interface InputProps interface InputProps
extends Omit<InputHTMLAttributes<HTMLInputElement>, 'onChange'> { extends Omit<InputHTMLAttributes<HTMLInputElement>, 'onChange'> {
label: string; label: string;
name: string; name: string;
checked: boolean; checked: boolean;
errors?: string[]; errors?: string[];
onChange?: (name: string, checked: boolean) => void; onChange?: (name: string, checked: boolean) => void;
} }
export default function Checkbox({ export default function Checkbox({
name, name,
label, label,
checked = false, checked = false,
errors = [], errors = [],
onChange, onChange,
required = false, required = false,
...props ...props
}: InputProps): JSX.Element { }: InputProps): JSX.Element {
const [checkboxChecked, setCheckboxChecked] = useState<boolean>(checked); const [checkboxChecked, setCheckboxChecked] = useState<boolean>(checked);
if (typeof window === 'undefined') return <Fragment />; if (typeof window === 'undefined') return <Fragment />;
function _onChange({ target }: ChangeEvent<HTMLInputElement>) { function _onChange({ target }: ChangeEvent<HTMLInputElement>) {
setCheckboxChecked(target.checked); setCheckboxChecked(target.checked);
if (onChange) { if (onChange) {
onChange(target.name, target.checked); onChange(target.name, target.checked);
} }
} }
return ( return (
<FormField <FormField
css={{ alignItems: 'center', gap: '1em', flexDirection: 'row' }} css={{ alignItems: 'center', gap: '1em', flexDirection: 'row' }}
required={required} required={required}
> >
<label htmlFor={name} title={label}> <label htmlFor={name} title={label}>
{label} {label}
</label> </label>
<Toggle <Toggle
{...props} {...props}
onChange={_onChange} onChange={_onChange}
checked={checkboxChecked} checked={checkboxChecked}
placeholder={props.placeholder ?? 'Type something...'} placeholder={props.placeholder ?? 'Type something...'}
name={name} name={name}
id={name} id={name}
/> />
{errors.length > 0 && {errors.length > 0 &&
errors.map((error) => <FormFieldError>{error}</FormFieldError>)} errors.map((error) => <FormFieldError>{error}</FormFieldError>)}
</FormField> </FormField>
); );
} }

View File

@@ -1,79 +1,79 @@
import { useTheme } from '@emotion/react'; import { useTheme } from '@emotion/react';
import { InputHTMLAttributes, ReactNode, useEffect, useState } from 'react'; import { InputHTMLAttributes, ReactNode, useEffect, useState } from 'react';
import Select, { import Select, {
FormatOptionLabelMeta, FormatOptionLabelMeta,
GroupBase, GroupBase,
OptionsOrGroups, OptionsOrGroups,
} from 'react-select'; } from 'react-select';
import FormField from '~/components/common/form/_form_field'; import FormField from '~/components/common/form/_form_field';
type Option = { label: string | number; value: string | number }; type Option = { label: string | number; value: string | number };
interface SelectorProps interface SelectorProps
extends Omit<InputHTMLAttributes<HTMLInputElement>, 'onChange'> { extends Omit<InputHTMLAttributes<HTMLInputElement>, 'onChange'> {
label: string; label: string;
name: string; name: string;
errors?: string[]; errors?: string[];
options: OptionsOrGroups<Option, GroupBase<Option>>; options: OptionsOrGroups<Option, GroupBase<Option>>;
value: number | string; value: number | string;
onChangeCallback?: (value: number | string) => void; onChangeCallback?: (value: number | string) => void;
formatOptionLabel?: ( formatOptionLabel?: (
data: Option, data: Option,
formatOptionLabelMeta: FormatOptionLabelMeta<Option> formatOptionLabelMeta: FormatOptionLabelMeta<Option>
) => ReactNode; ) => ReactNode;
} }
export default function Selector({ export default function Selector({
name, name,
label, label,
value, value,
errors = [], errors = [],
options, options,
onChangeCallback, onChangeCallback,
formatOptionLabel, formatOptionLabel,
required = false, required = false,
...props ...props
}: SelectorProps): JSX.Element { }: SelectorProps): JSX.Element {
const theme = useTheme(); const theme = useTheme();
const [selectorValue, setSelectorValue] = useState<Option>(); const [selectorValue, setSelectorValue] = useState<Option>();
useEffect(() => { useEffect(() => {
if (options.length === 0) return; if (options.length === 0) return;
const option = options.find((o: any) => o.value === value); const option = options.find((o: any) => o.value === value);
if (option) { if (option) {
setSelectorValue(option as Option); setSelectorValue(option as Option);
} }
}, [options, value]); }, [options, value]);
const handleChange = (selectedOption: Option) => { const handleChange = (selectedOption: Option) => {
setSelectorValue(selectedOption); setSelectorValue(selectedOption);
if (onChangeCallback) { if (onChangeCallback) {
onChangeCallback(selectedOption.value); onChangeCallback(selectedOption.value);
} }
}; };
return ( return (
<FormField required={required}> <FormField required={required}>
{label && ( {label && (
<label htmlFor={name} title={`${name} field`}> <label htmlFor={name} title={`${name} field`}>
{label} {label}
</label> </label>
)} )}
<Select <Select
value={selectorValue} value={selectorValue}
onChange={(newValue) => handleChange(newValue as Option)} onChange={(newValue) => handleChange(newValue as Option)}
options={options} options={options}
isDisabled={props.disabled} isDisabled={props.disabled}
menuPlacement="auto" menuPlacement="auto"
formatOptionLabel={ formatOptionLabel={
formatOptionLabel formatOptionLabel
? (val, formatOptionLabelMeta) => ? (val, formatOptionLabelMeta) =>
formatOptionLabel(val, formatOptionLabelMeta) formatOptionLabel(val, formatOptionLabelMeta)
: undefined : undefined
} }
css={{ color: theme.colors.black }} css={{ color: theme.colors.black }}
/> />
</FormField> </FormField>
); );
} }

View File

@@ -4,46 +4,46 @@ import FormFieldError from '~/components/common/form/_form_field_error';
import Input from '~/components/common/form/_input'; import Input from '~/components/common/form/_input';
interface InputProps interface InputProps
extends Omit<InputHTMLAttributes<HTMLInputElement>, 'onChange'> { extends Omit<InputHTMLAttributes<HTMLInputElement>, 'onChange'> {
label: string; label: string;
name: string; name: string;
value?: string; value?: string;
errors?: string[]; errors?: string[];
onChange?: (name: string, value: string) => void; onChange?: (name: string, value: string) => void;
} }
export default function TextBox({ export default function TextBox({
name, name,
label, label,
value = '', value = '',
errors = [], errors = [],
onChange, onChange,
required = false, required = false,
...props ...props
}: InputProps): JSX.Element { }: InputProps): JSX.Element {
const [inputValue, setInputValue] = useState<string>(value); const [inputValue, setInputValue] = useState<string>(value);
function _onChange({ target }: ChangeEvent<HTMLInputElement>) { function _onChange({ target }: ChangeEvent<HTMLInputElement>) {
setInputValue(target.value); setInputValue(target.value);
if (onChange) { if (onChange) {
onChange(target.name, target.value); onChange(target.name, target.value);
} }
} }
return ( return (
<FormField required={required}> <FormField required={required}>
<label htmlFor={name} title={label}> <label htmlFor={name} title={label}>
{label} {label}
</label> </label>
<Input <Input
{...props} {...props}
name={name} name={name}
onChange={_onChange} onChange={_onChange}
value={inputValue} value={inputValue}
placeholder={props.placeholder ?? 'Type something...'} placeholder={props.placeholder ?? 'Type something...'}
/> />
{errors.length > 0 && {errors.length > 0 &&
errors.map((error) => <FormFieldError>{error}</FormFieldError>)} errors.map((error) => <FormFieldError>{error}</FormFieldError>)}
</FormField> </FormField>
); );
} }

View File

@@ -1,22 +1,22 @@
import styled from '@emotion/styled'; import styled from '@emotion/styled';
const IconButton = styled.button(({ theme }) => ({ const IconButton = styled.button(({ theme }) => ({
cursor: 'pointer', cursor: 'pointer',
height: '2rem', height: '2rem',
width: '2rem', width: '2rem',
fontSize: '1rem', fontSize: '1rem',
color: theme.colors.font, color: theme.colors.font,
backgroundColor: theme.colors.grey, backgroundColor: theme.colors.grey,
borderRadius: '50%', borderRadius: '50%',
border: 0, border: 0,
display: 'flex', display: 'flex',
alignItems: 'center', alignItems: 'center',
justifyContent: 'center', justifyContent: 'center',
'&:disabled': { '&:disabled': {
cursor: 'not-allowed', cursor: 'not-allowed',
opacity: 0.15, opacity: 0.15,
}, },
})); }));
export default IconButton; export default IconButton;

View File

@@ -1,8 +1,8 @@
import styled from '@emotion/styled'; import styled from '@emotion/styled';
const Legend = styled.span(({ theme }) => ({ const Legend = styled.span(({ theme }) => ({
fontSize: '13px', fontSize: '13px',
color: theme.colors.grey, color: theme.colors.grey,
})); }));
export default Legend; export default Legend;

View File

@@ -1,11 +1,11 @@
import styled from '@emotion/styled'; import styled from '@emotion/styled';
const ModalBody = styled.div({ const ModalBody = styled.div({
width: '100%', width: '100%',
display: 'flex', display: 'flex',
flex: 1, flex: 1,
alignItems: 'center', alignItems: 'center',
flexDirection: 'column', flexDirection: 'column',
}); });
export default ModalBody; export default ModalBody;

View File

@@ -2,23 +2,23 @@ import styled from '@emotion/styled';
import TransitionLayout from '~/components/layouts/_transition_layout'; import TransitionLayout from '~/components/layouts/_transition_layout';
const ModalContainer = styled(TransitionLayout)(({ theme }) => ({ const ModalContainer = styled(TransitionLayout)(({ theme }) => ({
minWidth: '500px', minWidth: '500px',
background: theme.colors.background, background: theme.colors.background,
padding: '1em', padding: '1em',
borderRadius: theme.border.radius, borderRadius: theme.border.radius,
marginTop: '6em', marginTop: '6em',
display: 'flex', display: 'flex',
alignItems: 'center', alignItems: 'center',
justifyContent: 'center', justifyContent: 'center',
flexDirection: 'column', flexDirection: 'column',
boxShadow: theme.colors.boxShadow, boxShadow: theme.colors.boxShadow,
[`@media (max-width: ${theme.media.mobile})`]: { [`@media (max-width: ${theme.media.mobile})`]: {
maxHeight: 'calc(100% - 2em)', maxHeight: 'calc(100% - 2em)',
width: 'calc(100% - 2em)', width: 'calc(100% - 2em)',
minWidth: 'unset', minWidth: 'unset',
marginTop: '1em', marginTop: '1em',
}, },
})); }));
export default ModalContainer; export default ModalContainer;

View File

@@ -1,20 +1,20 @@
import styled from '@emotion/styled'; import styled from '@emotion/styled';
const ModalHeader = styled.h3({ const ModalHeader = styled.h3({
width: '100%', width: '100%',
marginBottom: '0.75em', marginBottom: '0.75em',
display: 'flex', display: 'flex',
alignItems: 'center', alignItems: 'center',
justifyContent: 'space-between', justifyContent: 'space-between',
}); });
const ModalCloseBtn = styled.button(({ theme }) => ({ const ModalCloseBtn = styled.button(({ theme }) => ({
cursor: 'pointer', cursor: 'pointer',
color: theme.colors.primary, color: theme.colors.primary,
backgroundColor: 'transparent', backgroundColor: 'transparent',
border: 0, border: 0,
padding: 0, padding: 0,
margin: 0, margin: 0,
})); }));
export { ModalHeader, ModalCloseBtn }; export { ModalHeader, ModalCloseBtn };

View File

@@ -2,18 +2,18 @@ import styled from '@emotion/styled';
import { rgba } from '~/lib/color'; import { rgba } from '~/lib/color';
const ModalWrapper = styled.div(({ theme }) => ({ const ModalWrapper = styled.div(({ theme }) => ({
zIndex: 9999, zIndex: 9999,
position: 'absolute', position: 'absolute',
top: 0, top: 0,
left: 0, left: 0,
height: '100%', height: '100%',
width: '100%', width: '100%',
background: rgba(theme.colors.black, 0.35), background: rgba(theme.colors.black, 0.35),
backdropFilter: 'blur(0.1em)', backdropFilter: 'blur(0.1em)',
display: 'flex', display: 'flex',
alignItems: 'center', alignItems: 'center',
flexDirection: 'column', flexDirection: 'column',
transition: theme.transition.delay, transition: theme.transition.delay,
})); }));
export default ModalWrapper; export default ModalWrapper;

View File

@@ -4,8 +4,8 @@ import { IoClose } from 'react-icons/io5';
import ModalBody from '~/components/common/modal/_modal_body'; import ModalBody from '~/components/common/modal/_modal_body';
import ModalContainer from '~/components/common/modal/_modal_container'; import ModalContainer from '~/components/common/modal/_modal_container';
import { import {
ModalCloseBtn, ModalCloseBtn,
ModalHeader, ModalHeader,
} from '~/components/common/modal/_modal_header'; } from '~/components/common/modal/_modal_header';
import ModalWrapper from '~/components/common/modal/_modal_wrapper'; import ModalWrapper from '~/components/common/modal/_modal_wrapper';
import TextEllipsis from '~/components/common/text_ellipsis'; import TextEllipsis from '~/components/common/text_ellipsis';
@@ -14,54 +14,54 @@ import useGlobalHotkeys from '~/hooks/use_global_hotkeys';
import useShortcut from '~/hooks/use_shortcut'; import useShortcut from '~/hooks/use_shortcut';
interface ModalProps { interface ModalProps {
title?: string; title?: string;
children: ReactNode; children: ReactNode;
opened: boolean; opened: boolean;
hideCloseBtn?: boolean; hideCloseBtn?: boolean;
className?: string; className?: string;
close: () => void; close: () => void;
} }
export default function Modal({ export default function Modal({
title, title,
children, children,
opened = true, opened = true,
hideCloseBtn = false, hideCloseBtn = false,
className, className,
close, close,
}: ModalProps) { }: ModalProps) {
const modalRef = useRef<HTMLDivElement>(null); const modalRef = useRef<HTMLDivElement>(null);
const { setGlobalHotkeysEnabled } = useGlobalHotkeys(); const { setGlobalHotkeysEnabled } = useGlobalHotkeys();
useClickOutside(modalRef, close); useClickOutside(modalRef, close);
useShortcut('ESCAPE_KEY', close, { disableGlobalCheck: true }); useShortcut('ESCAPE_KEY', close, { disableGlobalCheck: true });
useEffect(() => setGlobalHotkeysEnabled(!opened), [opened]); useEffect(() => setGlobalHotkeysEnabled(!opened), [opened]);
if (typeof window === 'undefined') { if (typeof window === 'undefined') {
return <Fragment />; return <Fragment />;
} }
return ( return (
opened && opened &&
createPortal( createPortal(
<ModalWrapper> <ModalWrapper>
<ModalContainer className={className} ref={modalRef}> <ModalContainer className={className} ref={modalRef}>
{(!hideCloseBtn || title) && ( {(!hideCloseBtn || title) && (
<ModalHeader> <ModalHeader>
{title && <TextEllipsis>{title}</TextEllipsis>} {title && <TextEllipsis>{title}</TextEllipsis>}
{!hideCloseBtn && ( {!hideCloseBtn && (
<ModalCloseBtn onClick={close}> <ModalCloseBtn onClick={close}>
<IoClose size={20} /> <IoClose size={20} />
</ModalCloseBtn> </ModalCloseBtn>
)} )}
</ModalHeader> </ModalHeader>
)} )}
<ModalBody>{children}</ModalBody> <ModalBody>{children}</ModalBody>
</ModalContainer> </ModalContainer>
</ModalWrapper>, </ModalWrapper>,
document.body document.body
) )
); );
} }

View File

@@ -6,12 +6,12 @@ import useShortcut from '~/hooks/use_shortcut';
import { appendCollectionId } from '~/lib/navigation'; import { appendCollectionId } from '~/lib/navigation';
export default function BackToDashboard({ children }: { children: ReactNode }) { export default function BackToDashboard({ children }: { children: ReactNode }) {
const collectionId = Number(useSearchParam('collectionId')); const collectionId = Number(useSearchParam('collectionId'));
useShortcut( useShortcut(
'ESCAPE_KEY', 'ESCAPE_KEY',
() => () =>
router.visit(appendCollectionId(route('dashboard').url, collectionId)), router.visit(appendCollectionId(route('dashboard').url, collectionId)),
{ disableGlobalCheck: true } { disableGlobalCheck: true }
); );
return <>{children}</>; return <>{children}</>;
} }

View File

@@ -2,14 +2,14 @@ import styled from '@emotion/styled';
import { rgba } from '~/lib/color'; import { rgba } from '~/lib/color';
const RoundedImage = styled.img(({ theme }) => { const RoundedImage = styled.img(({ theme }) => {
const transparentBlack = rgba(theme.colors.black, 0.1); const transparentBlack = rgba(theme.colors.black, 0.1);
return { return {
borderRadius: '50%', borderRadius: '50%',
'&:hover': { '&:hover': {
boxShadow: `0 1px 3px 0 ${transparentBlack}, 0 1px 2px -1px ${transparentBlack}`, boxShadow: `0 1px 3px 0 ${transparentBlack}, 0 1px 2px -1px ${transparentBlack}`,
}, },
}; };
}); });
export default RoundedImage; export default RoundedImage;

View File

@@ -1,218 +1,218 @@
import styled from '@emotion/styled'; import styled from '@emotion/styled';
import { import {
ColumnDef, ColumnDef,
flexRender, flexRender,
getCoreRowModel, getCoreRowModel,
getPaginationRowModel, getPaginationRowModel,
getSortedRowModel, getSortedRowModel,
PaginationState, PaginationState,
useReactTable, useReactTable,
} from '@tanstack/react-table'; } from '@tanstack/react-table';
import { useState } from 'react'; import { useState } from 'react';
import IconButton from '~/components/common/icon_button'; import IconButton from '~/components/common/icon_button';
import { import {
MdKeyboardArrowLeft, MdKeyboardArrowLeft,
MdKeyboardArrowRight, MdKeyboardArrowRight,
MdKeyboardDoubleArrowLeft, MdKeyboardDoubleArrowLeft,
MdKeyboardDoubleArrowRight, MdKeyboardDoubleArrowRight,
} from 'react-icons/md'; } from 'react-icons/md';
import Input from '~/components/common/form/_input'; import Input from '~/components/common/form/_input';
const TablePageFooter = styled.div({ const TablePageFooter = styled.div({
display: 'flex', display: 'flex',
gap: '1em', gap: '1em',
alignItems: 'center', alignItems: 'center',
}); });
const Box = styled(TablePageFooter)({ const Box = styled(TablePageFooter)({
gap: '0.35em', gap: '0.35em',
}); });
const Resizer = styled.div<{ isResizing: boolean }>( const Resizer = styled.div<{ isResizing: boolean }>(
({ theme, isResizing }) => ({ ({ theme, isResizing }) => ({
cursor: 'col-resize', cursor: 'col-resize',
userSelect: 'none', userSelect: 'none',
touchAction: 'none', touchAction: 'none',
position: 'absolute', position: 'absolute',
right: 0, right: 0,
top: 0, top: 0,
height: '100%', height: '100%',
width: '5px', width: '5px',
opacity: !isResizing ? 0 : 1, opacity: !isResizing ? 0 : 1,
background: !isResizing ? theme.colors.white : theme.colors.primary, background: !isResizing ? theme.colors.white : theme.colors.primary,
'&:hover': { '&:hover': {
opacity: 0.5, opacity: 0.5,
}, },
}) })
); );
type TableProps<T> = { type TableProps<T> = {
columns: ColumnDef<T>[]; columns: ColumnDef<T>[];
data: T[]; data: T[];
}; };
export default function Table<T>({ columns, data }: TableProps<T>) { export default function Table<T>({ columns, data }: TableProps<T>) {
const [pagination, setPagination] = useState<PaginationState>({ const [pagination, setPagination] = useState<PaginationState>({
pageIndex: 0, pageIndex: 0,
pageSize: 10, pageSize: 10,
}); });
const table = useReactTable({ const table = useReactTable({
data, data,
columns, columns,
enableColumnResizing: true, enableColumnResizing: true,
columnResizeMode: 'onChange', columnResizeMode: 'onChange',
state: { state: {
pagination, pagination,
}, },
onPaginationChange: setPagination, onPaginationChange: setPagination,
getCoreRowModel: getCoreRowModel(), getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(), getPaginationRowModel: getPaginationRowModel(),
getSortedRowModel: getSortedRowModel(), getSortedRowModel: getSortedRowModel(),
debugTable: true, debugTable: true,
}); });
return ( return (
<div css={{ fontSize: '0.9rem', paddingBlock: '1em' }}> <div css={{ fontSize: '0.9rem', paddingBlock: '1em' }}>
<div <div
css={{ css={{
maxWidth: '100%', maxWidth: '100%',
marginBottom: '1em', marginBottom: '1em',
display: 'block', display: 'block',
overflowX: 'auto', overflowX: 'auto',
overflowY: 'hidden', overflowY: 'hidden',
}} }}
> >
<table> <table>
<thead> <thead>
{table.getHeaderGroups().map((headerGroup) => ( {table.getHeaderGroups().map((headerGroup) => (
<tr key={headerGroup.id}> <tr key={headerGroup.id}>
{headerGroup.headers.map((header) => ( {headerGroup.headers.map((header) => (
<th <th
key={header.id} key={header.id}
style={{ style={{
width: header.getSize(), width: header.getSize(),
}} }}
css={{ css={{
position: 'relative', position: 'relative',
userSelect: 'none', userSelect: 'none',
// resizer // resizer
'&:hover > div > div': { '&:hover > div > div': {
opacity: 0.5, opacity: 0.5,
}, },
}} }}
colSpan={header.colSpan} colSpan={header.colSpan}
> >
{header.isPlaceholder ? null : ( {header.isPlaceholder ? null : (
<div <div
css={{ css={{
cursor: header.column.getCanSort() cursor: header.column.getCanSort()
? 'pointer' ? 'pointer'
: 'default', : 'default',
}} }}
onClick={header.column.getToggleSortingHandler()} onClick={header.column.getToggleSortingHandler()}
title={ title={
header.column.getCanSort() header.column.getCanSort()
? header.column.getNextSortingOrder() === 'asc' ? header.column.getNextSortingOrder() === 'asc'
? 'Sort ascending' ? 'Sort ascending'
: header.column.getNextSortingOrder() === 'desc' : header.column.getNextSortingOrder() === 'desc'
? 'Sort descending' ? 'Sort descending'
: 'Clear sort' : 'Clear sort'
: undefined : undefined
} }
> >
{flexRender( {flexRender(
header.column.columnDef.header, header.column.columnDef.header,
header.getContext() header.getContext()
)} )}
{{ {{
asc: ' 🔼', asc: ' 🔼',
desc: ' 🔽', desc: ' 🔽',
}[header.column.getIsSorted() as string] ?? null} }[header.column.getIsSorted() as string] ?? null}
{header.column.getCanResize() && ( {header.column.getCanResize() && (
<Resizer <Resizer
onMouseDown={header.getResizeHandler()} onMouseDown={header.getResizeHandler()}
onTouchStart={header.getResizeHandler()} onTouchStart={header.getResizeHandler()}
isResizing={header.column.getIsResizing()} isResizing={header.column.getIsResizing()}
/> />
)} )}
</div> </div>
)} )}
</th> </th>
))} ))}
</tr> </tr>
))} ))}
</thead> </thead>
<tbody> <tbody>
{table {table
.getRowModel() .getRowModel()
.rows.slice(0, 10) .rows.slice(0, 10)
.map((row) => ( .map((row) => (
<tr key={row.id}> <tr key={row.id}>
{row.getVisibleCells().map((cell) => ( {row.getVisibleCells().map((cell) => (
<td key={cell.id} style={{ width: cell.column.getSize() }}> <td key={cell.id} style={{ width: cell.column.getSize() }}>
{flexRender( {flexRender(
cell.column.columnDef.cell, cell.column.columnDef.cell,
cell.getContext() cell.getContext()
)} )}
</td> </td>
))} ))}
</tr> </tr>
))} ))}
</tbody> </tbody>
</table> </table>
</div> </div>
{table.getPageCount() > 1 && ( {table.getPageCount() > 1 && (
<TablePageFooter> <TablePageFooter>
<Box> <Box>
<IconButton <IconButton
onClick={() => table.setPageIndex(0)} onClick={() => table.setPageIndex(0)}
disabled={!table.getCanPreviousPage()} disabled={!table.getCanPreviousPage()}
> >
<MdKeyboardDoubleArrowLeft /> <MdKeyboardDoubleArrowLeft />
</IconButton> </IconButton>
<IconButton <IconButton
onClick={() => table.previousPage()} onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()} disabled={!table.getCanPreviousPage()}
> >
<MdKeyboardArrowLeft /> <MdKeyboardArrowLeft />
</IconButton> </IconButton>
<IconButton <IconButton
onClick={() => table.nextPage()} onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()} disabled={!table.getCanNextPage()}
> >
<MdKeyboardArrowRight /> <MdKeyboardArrowRight />
</IconButton> </IconButton>
<IconButton <IconButton
onClick={() => table.setPageIndex(table.getPageCount() - 1)} onClick={() => table.setPageIndex(table.getPageCount() - 1)}
disabled={!table.getCanNextPage()} disabled={!table.getCanNextPage()}
> >
<MdKeyboardDoubleArrowRight /> <MdKeyboardDoubleArrowRight />
</IconButton> </IconButton>
</Box> </Box>
<Box> <Box>
<span>Page</span> <span>Page</span>
<strong> <strong>
{table.getState().pagination.pageIndex + 1} of{' '} {table.getState().pagination.pageIndex + 1} of{' '}
{table.getPageCount()} {table.getPageCount()}
</strong> </strong>
</Box> </Box>
<Box> <Box>
Go to page Go to page
<Input <Input
type="number" type="number"
min="1" min="1"
max={table.getPageCount()} max={table.getPageCount()}
defaultValue={table.getState().pagination.pageIndex + 1} defaultValue={table.getState().pagination.pageIndex + 1}
onChange={(e) => { onChange={(e) => {
const page = e.target.value ? Number(e.target.value) - 1 : 0; const page = e.target.value ? Number(e.target.value) - 1 : 0;
table.setPageIndex(page); table.setPageIndex(page);
}} }}
/> />
</Box> </Box>
</TablePageFooter> </TablePageFooter>
)} )}
</div> </div>
); );
} }

View File

@@ -2,23 +2,23 @@ import styled from '@emotion/styled';
import { rgba } from '~/lib/color'; import { rgba } from '~/lib/color';
const TabItem = styled.li<{ active?: boolean; danger?: boolean }>( const TabItem = styled.li<{ active?: boolean; danger?: boolean }>(
({ theme, active, danger }) => { ({ theme, active, danger }) => {
const activeColor = !danger ? theme.colors.primary : theme.colors.lightRed; const activeColor = !danger ? theme.colors.primary : theme.colors.lightRed;
return { return {
userSelect: 'none', userSelect: 'none',
cursor: 'pointer', cursor: 'pointer',
backgroundColor: active backgroundColor: active
? rgba(activeColor, 0.15) ? rgba(activeColor, 0.15)
: theme.colors.secondary, : theme.colors.secondary,
padding: '10px 20px', padding: '10px 20px',
border: `1px solid ${active ? rgba(activeColor, 0.1) : theme.colors.secondary}`, border: `1px solid ${active ? rgba(activeColor, 0.1) : theme.colors.secondary}`,
borderBottom: `1px solid ${active ? rgba(activeColor, 0.25) : theme.colors.secondary}`, borderBottom: `1px solid ${active ? rgba(activeColor, 0.25) : theme.colors.secondary}`,
display: 'flex', display: 'flex',
gap: '0.35em', gap: '0.35em',
alignItems: 'center', alignItems: 'center',
transition: '.075s', transition: '.075s',
}; };
} }
); );
export default TabItem; export default TabItem;

View File

@@ -1,10 +1,10 @@
import styled from '@emotion/styled'; import styled from '@emotion/styled';
const TabList = styled.ul({ const TabList = styled.ul({
padding: 0, padding: 0,
margin: 0, margin: 0,
display: 'flex', display: 'flex',
listStyle: 'none', listStyle: 'none',
}); });
export default TabList; export default TabList;

View File

@@ -2,11 +2,11 @@ import styled from '@emotion/styled';
import { rgba } from '~/lib/color'; import { rgba } from '~/lib/color';
const TabPanel = styled.div(({ theme }) => ({ const TabPanel = styled.div(({ theme }) => ({
zIndex: 1, zIndex: 1,
position: 'relative', position: 'relative',
border: `1px solid ${rgba(theme.colors.primary, 0.25)}`, border: `1px solid ${rgba(theme.colors.primary, 0.25)}`,
padding: '20px', padding: '20px',
marginTop: '-1px', marginTop: '-1px',
})); }));
export default TabPanel; export default TabPanel;

View File

@@ -6,37 +6,37 @@ import TabPanel from '~/components/common/tabs/tab_panel';
import TransitionLayout from '~/components/layouts/_transition_layout'; import TransitionLayout from '~/components/layouts/_transition_layout';
export interface Tab { export interface Tab {
title: string; title: string;
content: ReactNode; content: ReactNode;
icon?: IconType; icon?: IconType;
danger?: boolean; danger?: boolean;
} }
export default function Tabs({ tabs }: { tabs: Tab[] }) { export default function Tabs({ tabs }: { tabs: Tab[] }) {
const [activeTabIndex, setActiveTabIndex] = useState<number>(0); const [activeTabIndex, setActiveTabIndex] = useState<number>(0);
const handleTabClick = (index: number) => { const handleTabClick = (index: number) => {
setActiveTabIndex(index); setActiveTabIndex(index);
}; };
return ( return (
<div css={{ width: '100%' }}> <div css={{ width: '100%' }}>
<TabList> <TabList>
{tabs.map(({ title, icon: Icon, danger }, index) => ( {tabs.map(({ title, icon: Icon, danger }, index) => (
<TabItem <TabItem
key={index} key={index}
active={index === activeTabIndex} active={index === activeTabIndex}
onClick={() => handleTabClick(index)} onClick={() => handleTabClick(index)}
danger={danger ?? false} danger={danger ?? false}
> >
{!!Icon && <Icon size={20} />} {!!Icon && <Icon size={20} />}
{title} {title}
</TabItem> </TabItem>
))} ))}
</TabList> </TabList>
<TabPanel key={tabs[activeTabIndex].title}> <TabPanel key={tabs[activeTabIndex].title}>
<TransitionLayout>{tabs[activeTabIndex].content}</TransitionLayout> <TransitionLayout>{tabs[activeTabIndex].content}</TransitionLayout>
</TabPanel> </TabPanel>
</div> </div>
); );
} }

View File

@@ -1,20 +1,20 @@
import styled from '@emotion/styled'; import styled from '@emotion/styled';
const TextEllipsis = styled.p<{ lines?: number }>(({ lines = 1 }) => { const TextEllipsis = styled.p<{ lines?: number }>(({ lines = 1 }) => {
if (lines > 1) { if (lines > 1) {
return { return {
overflow: 'hidden', overflow: 'hidden',
display: '-webkit-box', display: '-webkit-box',
WebkitLineClamp: lines, WebkitLineClamp: lines,
WebkitBoxOrient: 'vertical', WebkitBoxOrient: 'vertical',
}; };
} }
return { return {
textOverflow: 'ellipsis', textOverflow: 'ellipsis',
whiteSpace: 'nowrap', whiteSpace: 'nowrap',
overflow: 'hidden', overflow: 'hidden',
}; };
}); });
export default TextEllipsis; export default TextEllipsis;

View File

@@ -1,12 +1,12 @@
import styled from '@emotion/styled'; import styled from '@emotion/styled';
const UnstyledList = styled.ul({ const UnstyledList = styled.ul({
'&, & li': { '&, & li': {
listStyleType: 'none', listStyleType: 'none',
margin: 0, margin: 0,
padding: 0, padding: 0,
border: 0, border: 0,
}, },
}); });
export default UnstyledList; export default UnstyledList;

View File

@@ -7,33 +7,33 @@ import Footer from '~/components/footer/footer';
import useActiveCollection from '~/hooks/use_active_collection'; import useActiveCollection from '~/hooks/use_active_collection';
export interface CollectionHeaderProps { export interface CollectionHeaderProps {
showButtons: boolean; showButtons: boolean;
showControls?: boolean; showControls?: boolean;
openNavigationItem?: ReactNode; openNavigationItem?: ReactNode;
openCollectionItem?: ReactNode; openCollectionItem?: ReactNode;
} }
const CollectionContainerStyle = styled.div({ const CollectionContainerStyle = styled.div({
height: '100%', height: '100%',
minWidth: 0, minWidth: 0,
padding: '0.5em 0.5em 0', padding: '0.5em 0.5em 0',
display: 'flex', display: 'flex',
flex: 1, flex: 1,
flexDirection: 'column', flexDirection: 'column',
}); });
export default function CollectionContainer(props: CollectionHeaderProps) { export default function CollectionContainer(props: CollectionHeaderProps) {
const { activeCollection } = useActiveCollection(); const { activeCollection } = useActiveCollection();
if (activeCollection === null) { if (activeCollection === null) {
return <NoCollection />; return <NoCollection />;
} }
return ( return (
<CollectionContainerStyle> <CollectionContainerStyle>
<CollectionHeader {...props} /> <CollectionHeader {...props} />
<LinkList links={activeCollection.links} /> <LinkList links={activeCollection.links} />
<Footer css={{ paddingBottom: 0 }} /> <Footer css={{ paddingBottom: 0 }} />
</CollectionContainerStyle> </CollectionContainerStyle>
); );
} }

View File

@@ -10,33 +10,33 @@ import { appendCollectionId } from '~/lib/navigation';
import { Collection } from '~/types/app'; import { Collection } from '~/types/app';
export default function CollectionControls({ export default function CollectionControls({
collectionId, collectionId,
}: { }: {
collectionId: Collection['id']; collectionId: Collection['id'];
}) { }) {
const { t } = useTranslation('common'); const { t } = useTranslation('common');
return ( return (
<Dropdown label={<BsThreeDotsVertical />} svgSize={18}> <Dropdown label={<BsThreeDotsVertical />} svgSize={18}>
<DropdownItemLink href={route('link.create-form').url}> <DropdownItemLink href={route('link.create-form').url}>
<IoIosAddCircleOutline /> {t('link.create')} <IoIosAddCircleOutline /> {t('link.create')}
</DropdownItemLink> </DropdownItemLink>
<DropdownItemLink <DropdownItemLink
href={appendCollectionId( href={appendCollectionId(
route('collection.edit-form').url, route('collection.edit-form').url,
collectionId collectionId
)} )}
> >
<GoPencil /> {t('collection.edit')} <GoPencil /> {t('collection.edit')}
</DropdownItemLink> </DropdownItemLink>
<DropdownItemLink <DropdownItemLink
href={appendCollectionId( href={appendCollectionId(
route('collection.delete-form').url, route('collection.delete-form').url,
collectionId collectionId
)} )}
danger danger
> >
<IoTrashOutline /> {t('collection.delete')} <IoTrashOutline /> {t('collection.delete')}
</DropdownItemLink> </DropdownItemLink>
</Dropdown> </Dropdown>
); );
} }

View File

@@ -3,16 +3,16 @@ import TextEllipsis from '~/components/common/text_ellipsis';
import useActiveCollection from '~/hooks/use_active_collection'; import useActiveCollection from '~/hooks/use_active_collection';
const CollectionDescriptionStyle = styled.div({ const CollectionDescriptionStyle = styled.div({
width: '100%', width: '100%',
fontSize: '0.85rem', fontSize: '0.85rem',
marginBottom: '0.5rem', marginBottom: '0.5rem',
}); });
export default function CollectionDescription() { export default function CollectionDescription() {
const { activeCollection } = useActiveCollection(); const { activeCollection } = useActiveCollection();
return ( return (
<CollectionDescriptionStyle> <CollectionDescriptionStyle>
<TextEllipsis lines={3}>{activeCollection!.description}</TextEllipsis> <TextEllipsis lines={3}>{activeCollection!.description}</TextEllipsis>
</CollectionDescriptionStyle> </CollectionDescriptionStyle>
); );
} }

View File

@@ -12,76 +12,76 @@ const paddingLeft = '1.25em';
const paddingRight = '1.65em'; const paddingRight = '1.65em';
const CollectionHeaderWrapper = styled.div(({ theme }) => ({ const CollectionHeaderWrapper = styled.div(({ theme }) => ({
minWidth: 0, minWidth: 0,
width: '100%', width: '100%',
paddingInline: `${paddingLeft} ${paddingRight}`, paddingInline: `${paddingLeft} ${paddingRight}`,
marginBottom: '0.5em', marginBottom: '0.5em',
[`@media (max-width: ${theme.media.tablet})`]: { [`@media (max-width: ${theme.media.tablet})`]: {
paddingInline: 0, paddingInline: 0,
marginBottom: '1rem', marginBottom: '1rem',
}, },
})); }));
const CollectionHeaderStyle = styled.div({ const CollectionHeaderStyle = styled.div({
display: 'flex', display: 'flex',
alignItems: 'center', alignItems: 'center',
justifyContent: 'space-between', justifyContent: 'space-between',
'& > svg': { '& > svg': {
display: 'flex', display: 'flex',
}, },
}); });
const CollectionName = styled.h2(({ theme }) => ({ const CollectionName = styled.h2(({ theme }) => ({
width: `calc(100% - (${paddingLeft} + ${paddingRight}))`, width: `calc(100% - (${paddingLeft} + ${paddingRight}))`,
color: theme.colors.primary, color: theme.colors.primary,
display: 'flex', display: 'flex',
gap: '0.35em', gap: '0.35em',
alignItems: 'center', alignItems: 'center',
[`@media (max-width: ${theme.media.tablet})`]: { [`@media (max-width: ${theme.media.tablet})`]: {
width: `calc(100% - (${paddingLeft} + ${paddingRight} + 1.75em))`, width: `calc(100% - (${paddingLeft} + ${paddingRight} + 1.75em))`,
}, },
})); }));
const LinksCount = styled.div(({ theme }) => ({ const LinksCount = styled.div(({ theme }) => ({
minWidth: 'fit-content', minWidth: 'fit-content',
fontWeight: 300, fontWeight: 300,
fontSize: '0.8em', fontSize: '0.8em',
color: theme.colors.grey, color: theme.colors.grey,
})); }));
export default function CollectionHeader({ export default function CollectionHeader({
showButtons, showButtons,
showControls = true, showControls = true,
openNavigationItem, openNavigationItem,
openCollectionItem, openCollectionItem,
}: CollectionHeaderProps) { }: CollectionHeaderProps) {
const { t } = useTranslation('common'); const { t } = useTranslation('common');
const { activeCollection } = useActiveCollection(); const { activeCollection } = useActiveCollection();
if (!activeCollection) return <Fragment />; if (!activeCollection) return <Fragment />;
const { name, links, visibility } = activeCollection; const { name, links, visibility } = activeCollection;
return ( return (
<CollectionHeaderWrapper> <CollectionHeaderWrapper>
<CollectionHeaderStyle> <CollectionHeaderStyle>
{showButtons && openNavigationItem && openNavigationItem} {showButtons && openNavigationItem && openNavigationItem}
<CollectionName title={name}> <CollectionName title={name}>
<TextEllipsis>{name}</TextEllipsis> <TextEllipsis>{name}</TextEllipsis>
{links.length > 0 && <LinksCount> {links.length}</LinksCount>} {links.length > 0 && <LinksCount> {links.length}</LinksCount>}
<VisibilityBadge <VisibilityBadge
label={t('collection.visibility')} label={t('collection.visibility')}
visibility={visibility} visibility={visibility}
/> />
</CollectionName> </CollectionName>
{showControls && ( {showControls && (
<CollectionControls collectionId={activeCollection.id} /> <CollectionControls collectionId={activeCollection.id} />
)} )}
{showButtons && openCollectionItem && openCollectionItem} {showButtons && openCollectionItem && openCollectionItem}
</CollectionHeaderStyle> </CollectionHeaderStyle>
{activeCollection.description && <CollectionDescription />} {activeCollection.description && <CollectionDescription />}
</CollectionHeaderWrapper> </CollectionHeaderWrapper>
); );
} }

View File

@@ -10,48 +10,48 @@ import { appendCollectionId } from '~/lib/navigation';
import { CollectionWithLinks } from '~/types/app'; import { CollectionWithLinks } from '~/types/app';
const CollectionItemStyle = styled(Item, { const CollectionItemStyle = styled(Item, {
shouldForwardProp: (propName) => propName !== 'isActive', shouldForwardProp: (propName) => propName !== 'isActive',
})<{ isActive: boolean }>(({ theme, isActive }) => ({ })<{ isActive: boolean }>(({ theme, isActive }) => ({
cursor: 'pointer', cursor: 'pointer',
color: isActive ? theme.colors.primary : theme.colors.font, color: isActive ? theme.colors.primary : theme.colors.font,
backgroundColor: theme.colors.secondary, backgroundColor: theme.colors.secondary,
})); }));
const CollectionItemLink = CollectionItemStyle.withComponent(Link); const CollectionItemLink = CollectionItemStyle.withComponent(Link);
const LinksCount = styled.div(({ theme }) => ({ const LinksCount = styled.div(({ theme }) => ({
minWidth: 'fit-content', minWidth: 'fit-content',
fontWeight: 300, fontWeight: 300,
fontSize: '0.9rem', fontSize: '0.9rem',
color: theme.colors.grey, color: theme.colors.grey,
})); }));
export default function CollectionItem({ export default function CollectionItem({
collection, collection,
}: { }: {
collection: CollectionWithLinks; collection: CollectionWithLinks;
}) { }) {
const itemRef = useRef<HTMLDivElement>(null); const itemRef = useRef<HTMLDivElement>(null);
const { activeCollection } = useActiveCollection(); const { activeCollection } = useActiveCollection();
const isActiveCollection = collection.id === activeCollection?.id; const isActiveCollection = collection.id === activeCollection?.id;
const FolderIcon = isActiveCollection ? AiFillFolderOpen : AiOutlineFolder; const FolderIcon = isActiveCollection ? AiFillFolderOpen : AiOutlineFolder;
useEffect(() => { useEffect(() => {
if (collection.id === activeCollection?.id) { if (collection.id === activeCollection?.id) {
itemRef.current?.scrollIntoView({ behavior: 'smooth', block: 'nearest' }); itemRef.current?.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
} }
}, [collection.id, activeCollection?.id]); }, [collection.id, activeCollection?.id]);
return ( return (
<CollectionItemLink <CollectionItemLink
href={appendCollectionId(route('dashboard').url, collection.id)} href={appendCollectionId(route('dashboard').url, collection.id)}
isActive={isActiveCollection} isActive={isActiveCollection}
ref={itemRef} ref={itemRef}
> >
<FolderIcon css={{ minWidth: '24px' }} size={24} /> <FolderIcon css={{ minWidth: '24px' }} size={24} />
<TextEllipsis>{collection.name}</TextEllipsis> <TextEllipsis>{collection.name}</TextEllipsis>
{collection.links.length > 0 && ( {collection.links.length > 0 && (
<LinksCount> {collection.links.length}</LinksCount> <LinksCount> {collection.links.length}</LinksCount>
)} )}
</CollectionItemLink> </CollectionItemLink>
); );
} }

View File

@@ -7,76 +7,76 @@ import useCollections from '~/hooks/use_collections';
import useShortcut from '~/hooks/use_shortcut'; import useShortcut from '~/hooks/use_shortcut';
const SideMenu = styled.nav(({ theme }) => ({ const SideMenu = styled.nav(({ theme }) => ({
height: '100%', height: '100%',
width: '300px', width: '300px',
backgroundColor: theme.colors.background, backgroundColor: theme.colors.background,
paddingLeft: '10px', paddingLeft: '10px',
marginLeft: '5px', marginLeft: '5px',
borderLeft: `1px solid ${theme.colors.lightGrey}`, borderLeft: `1px solid ${theme.colors.lightGrey}`,
display: 'flex', display: 'flex',
gap: '.35em', gap: '.35em',
flexDirection: 'column', flexDirection: 'column',
})); }));
const CollectionLabel = styled.p(({ theme }) => ({ const CollectionLabel = styled.p(({ theme }) => ({
color: theme.colors.grey, color: theme.colors.grey,
marginBlock: '0.35em', marginBlock: '0.35em',
paddingInline: '15px', paddingInline: '15px',
})); }));
const CollectionListStyle = styled.div({ const CollectionListStyle = styled.div({
padding: '1px', padding: '1px',
paddingRight: '5px', paddingRight: '5px',
display: 'flex', display: 'flex',
flex: 1, flex: 1,
gap: '.35em', gap: '.35em',
flexDirection: 'column', flexDirection: 'column',
overflow: 'auto', overflow: 'auto',
}); });
export default function CollectionList() { export default function CollectionList() {
const { t } = useTranslation('common'); const { t } = useTranslation('common');
const { collections } = useCollections(); const { collections } = useCollections();
const { activeCollection, setActiveCollection } = useActiveCollection(); const { activeCollection, setActiveCollection } = useActiveCollection();
const goToPreviousCollection = () => { const goToPreviousCollection = () => {
const currentCategoryIndex = collections.findIndex( const currentCategoryIndex = collections.findIndex(
({ id }) => id === activeCollection?.id ({ id }) => id === activeCollection?.id
); );
if (currentCategoryIndex === -1 || currentCategoryIndex === 0) return; if (currentCategoryIndex === -1 || currentCategoryIndex === 0) return;
setActiveCollection(collections[currentCategoryIndex - 1]); setActiveCollection(collections[currentCategoryIndex - 1]);
}; };
const goToNextCollection = () => { const goToNextCollection = () => {
const currentCategoryIndex = collections.findIndex( const currentCategoryIndex = collections.findIndex(
({ id }) => id === activeCollection?.id ({ id }) => id === activeCollection?.id
); );
if ( if (
currentCategoryIndex === -1 || currentCategoryIndex === -1 ||
currentCategoryIndex === collections.length - 1 currentCategoryIndex === collections.length - 1
) )
return; return;
setActiveCollection(collections[currentCategoryIndex + 1]); setActiveCollection(collections[currentCategoryIndex + 1]);
}; };
useShortcut('ARROW_UP', goToPreviousCollection); useShortcut('ARROW_UP', goToPreviousCollection);
useShortcut('ARROW_DOWN', goToNextCollection); useShortcut('ARROW_DOWN', goToNextCollection);
return ( return (
<SideMenu> <SideMenu>
<CollectionListContainer> <CollectionListContainer>
<CollectionLabel> <CollectionLabel>
{t('collection.collections', { count: collections.length })} {' '} {t('collection.collections', { count: collections.length })} {' '}
{collections.length} {collections.length}
</CollectionLabel> </CollectionLabel>
<CollectionListStyle> <CollectionListStyle>
{collections.map((collection) => ( {collections.map((collection) => (
<CollectionItem collection={collection} key={collection.id} /> <CollectionItem collection={collection} key={collection.id} />
))} ))}
</CollectionListStyle> </CollectionListStyle>
</CollectionListContainer> </CollectionListContainer>
</SideMenu> </SideMenu>
); );
} }

View File

@@ -1,11 +1,11 @@
import styled from '@emotion/styled'; import styled from '@emotion/styled';
const CollectionListContainer = styled.div({ const CollectionListContainer = styled.div({
height: '100%', height: '100%',
minHeight: 0, minHeight: 0,
paddingInline: '10px', paddingInline: '10px',
display: 'flex', display: 'flex',
flexDirection: 'column', flexDirection: 'column',
}); });
export default CollectionListContainer; export default CollectionListContainer;

View File

@@ -10,86 +10,86 @@ import { appendCollectionId } from '~/lib/navigation';
import { CollectionWithLinks, LinkWithCollection } from '~/types/app'; import { CollectionWithLinks, LinkWithCollection } from '~/types/app';
export default function DashboardProviders( export default function DashboardProviders(
props: Readonly<{ props: Readonly<{
children: ReactNode; children: ReactNode;
collections: CollectionWithLinks[]; collections: CollectionWithLinks[];
activeCollection: CollectionWithLinks; activeCollection: CollectionWithLinks;
}> }>
) { ) {
const [globalHotkeysEnabled, setGlobalHotkeysEnabled] = const [globalHotkeysEnabled, setGlobalHotkeysEnabled] =
useState<boolean>(true); useState<boolean>(true);
const [collections, setCollections] = useState<CollectionWithLinks[]>( const [collections, setCollections] = useState<CollectionWithLinks[]>(
props.collections props.collections
); );
const [activeCollection, setActiveCollection] = const [activeCollection, setActiveCollection] =
useState<CollectionWithLinks | null>( useState<CollectionWithLinks | null>(
props.activeCollection || collections?.[0] props.activeCollection || collections?.[0]
); );
const handleChangeCollection = (collection: CollectionWithLinks) => { const handleChangeCollection = (collection: CollectionWithLinks) => {
setActiveCollection(collection); setActiveCollection(collection);
router.visit(appendCollectionId(route('dashboard').url, collection.id)); router.visit(appendCollectionId(route('dashboard').url, collection.id));
}; };
// TODO: compute this in controller // TODO: compute this in controller
const favorites = useMemo<LinkWithCollection[]>( const favorites = useMemo<LinkWithCollection[]>(
() => () =>
collections.reduce((acc, collection) => { collections.reduce((acc, collection) => {
collection.links.forEach((link) => { collection.links.forEach((link) => {
if (link.favorite) { if (link.favorite) {
const newLink: LinkWithCollection = { ...link, collection }; const newLink: LinkWithCollection = { ...link, collection };
acc.push(newLink); acc.push(newLink);
} }
}); });
return acc; return acc;
}, [] as LinkWithCollection[]), }, [] as LinkWithCollection[]),
[collections] [collections]
); );
const collectionsContextValue = useMemo( const collectionsContextValue = useMemo(
() => ({ collections, setCollections }), () => ({ collections, setCollections }),
[collections] [collections]
); );
const activeCollectionContextValue = useMemo( const activeCollectionContextValue = useMemo(
() => ({ activeCollection, setActiveCollection: handleChangeCollection }), () => ({ activeCollection, setActiveCollection: handleChangeCollection }),
[activeCollection, handleChangeCollection] [activeCollection, handleChangeCollection]
); );
const favoritesContextValue = useMemo(() => ({ favorites }), [favorites]); const favoritesContextValue = useMemo(() => ({ favorites }), [favorites]);
const globalHotkeysContextValue = useMemo( const globalHotkeysContextValue = useMemo(
() => ({ () => ({
globalHotkeysEnabled, globalHotkeysEnabled,
setGlobalHotkeysEnabled, setGlobalHotkeysEnabled,
}), }),
[globalHotkeysEnabled] [globalHotkeysEnabled]
); );
useShortcut( useShortcut(
'OPEN_CREATE_LINK_KEY', 'OPEN_CREATE_LINK_KEY',
() => () =>
router.visit( router.visit(
appendCollectionId(route('link.create-form').url, activeCollection?.id) appendCollectionId(route('link.create-form').url, activeCollection?.id)
), ),
{ {
enabled: globalHotkeysEnabled, enabled: globalHotkeysEnabled,
} }
); );
useShortcut( useShortcut(
'OPEN_CREATE_COLLECTION_KEY', 'OPEN_CREATE_COLLECTION_KEY',
() => router.visit(route('collection.create-form').url), () => router.visit(route('collection.create-form').url),
{ {
enabled: globalHotkeysEnabled, enabled: globalHotkeysEnabled,
} }
); );
return ( return (
<CollectionsContext.Provider value={collectionsContextValue}> <CollectionsContext.Provider value={collectionsContextValue}>
<ActiveCollectionContext.Provider value={activeCollectionContextValue}> <ActiveCollectionContext.Provider value={activeCollectionContextValue}>
<FavoritesContext.Provider value={favoritesContextValue}> <FavoritesContext.Provider value={favoritesContextValue}>
<GlobalHotkeysContext.Provider value={globalHotkeysContextValue}> <GlobalHotkeysContext.Provider value={globalHotkeysContextValue}>
{props.children} {props.children}
</GlobalHotkeysContext.Provider> </GlobalHotkeysContext.Provider>
</FavoritesContext.Provider> </FavoritesContext.Provider>
</ActiveCollectionContext.Provider> </ActiveCollectionContext.Provider>
</CollectionsContext.Provider> </CollectionsContext.Provider>
); );
} }

View File

@@ -11,27 +11,27 @@ import { appendLinkId } from '~/lib/navigation';
import { Link } from '~/types/app'; import { Link } from '~/types/app';
export default function LinkControls({ link }: { link: Link }) { export default function LinkControls({ link }: { link: Link }) {
const theme = useTheme(); const theme = useTheme();
const { t } = useTranslation('common'); const { t } = useTranslation('common');
return ( return (
<Dropdown <Dropdown
label={<BsThreeDotsVertical css={{ color: theme.colors.grey }} />} label={<BsThreeDotsVertical css={{ color: theme.colors.grey }} />}
css={{ backgroundColor: theme.colors.secondary }} css={{ backgroundColor: theme.colors.secondary }}
svgSize={18} svgSize={18}
> >
<FavoriteDropdownItem link={link} /> <FavoriteDropdownItem link={link} />
<DropdownItemLink <DropdownItemLink
href={appendLinkId(route('link.edit-form').url, link.id)} href={appendLinkId(route('link.edit-form').url, link.id)}
> >
<GoPencil /> {t('link.edit')} <GoPencil /> {t('link.edit')}
</DropdownItemLink> </DropdownItemLink>
<DropdownItemLink <DropdownItemLink
href={appendLinkId(route('link.delete-form').url, link.id)} href={appendLinkId(route('link.delete-form').url, link.id)}
danger danger
> >
<IoTrashOutline /> {t('link.delete')} <IoTrashOutline /> {t('link.delete')}
</DropdownItemLink> </DropdownItemLink>
</Dropdown> </Dropdown>
); );
} }

View File

@@ -7,76 +7,76 @@ import { rotate } from '~/styles/keyframes';
const IMG_LOAD_TIMEOUT = 7_500; const IMG_LOAD_TIMEOUT = 7_500;
interface LinkFaviconProps { interface LinkFaviconProps {
url: string; url: string;
size?: number; size?: number;
} }
const Favicon = styled.div({ const Favicon = styled.div({
position: 'relative', position: 'relative',
display: 'flex', display: 'flex',
alignItems: 'center', alignItems: 'center',
justifyContent: 'center', justifyContent: 'center',
}); });
const FaviconLoader = styled.div(({ theme }) => ({ const FaviconLoader = styled.div(({ theme }) => ({
position: 'absolute', position: 'absolute',
top: 0, top: 0,
left: 0, left: 0,
color: theme.colors.font, color: theme.colors.font,
backgroundColor: theme.colors.secondary, backgroundColor: theme.colors.secondary,
'& > *': { '& > *': {
animation: `${rotate} 1s both reverse infinite linear`, animation: `${rotate} 1s both reverse infinite linear`,
}, },
})); }));
// The Favicon API should always return an image, so it's not really useful to keep the loader nor placeholder icon, // The Favicon API should always return an image, so it's not really useful to keep the loader nor placeholder icon,
// but for slow connections and other random stuff, I'll keep this // but for slow connections and other random stuff, I'll keep this
export default function LinkFavicon({ url, size = 32 }: LinkFaviconProps) { export default function LinkFavicon({ url, size = 32 }: LinkFaviconProps) {
const imgRef = useRef<HTMLImageElement>(null); const imgRef = useRef<HTMLImageElement>(null);
const [isFailed, setFailed] = useState<boolean>(false); const [isFailed, setFailed] = useState<boolean>(false);
const [isLoading, setLoading] = useState<boolean>(true); const [isLoading, setLoading] = useState<boolean>(true);
const setFallbackFavicon = () => setFailed(true); const setFallbackFavicon = () => setFailed(true);
const handleStopLoading = () => setLoading(false); const handleStopLoading = () => setLoading(false);
const handleErrorLoading = () => { const handleErrorLoading = () => {
setFallbackFavicon(); setFallbackFavicon();
handleStopLoading(); handleStopLoading();
}; };
useEffect(() => { useEffect(() => {
// Ugly hack, onLoad cb not triggered on first load when SSR // Ugly hack, onLoad cb not triggered on first load when SSR
if (imgRef.current?.complete) { if (imgRef.current?.complete) {
handleStopLoading(); handleStopLoading();
return; return;
} }
const id = setTimeout(() => handleErrorLoading(), IMG_LOAD_TIMEOUT); const id = setTimeout(() => handleErrorLoading(), IMG_LOAD_TIMEOUT);
return () => clearTimeout(id); return () => clearTimeout(id);
}, [isLoading]); }, [isLoading]);
return ( return (
<Favicon> <Favicon>
{!isFailed ? ( {!isFailed ? (
<img <img
src={`/favicon?url=${url}`} src={`/favicon?url=${url}`}
onError={handleErrorLoading} onError={handleErrorLoading}
onLoad={handleStopLoading} onLoad={handleStopLoading}
height={size} height={size}
width={size} width={size}
alt="icon" alt="icon"
ref={imgRef} ref={imgRef}
decoding="async" decoding="async"
/> />
) : ( ) : (
<TfiWorld size={size} /> <TfiWorld size={size} />
)} )}
{isLoading && ( {isLoading && (
<FaviconLoader style={{ height: `${size}px`, width: `${size}px` }}> <FaviconLoader style={{ height: `${size}px`, width: `${size}px` }}>
<TbLoader3 size={size} /> <TbLoader3 size={size} />
</FaviconLoader> </FaviconLoader>
)} )}
</Favicon> </Favicon>
); );
} }

View File

@@ -6,121 +6,121 @@ import LinkFavicon from '~/components/dashboard/link/link_favicon';
import { Link } from '~/types/app'; import { Link } from '~/types/app';
const LinkWrapper = styled.li(({ theme }) => ({ const LinkWrapper = styled.li(({ theme }) => ({
userSelect: 'none', userSelect: 'none',
cursor: 'pointer', cursor: 'pointer',
height: 'fit-content', height: 'fit-content',
width: '100%', width: '100%',
color: theme.colors.primary, color: theme.colors.primary,
backgroundColor: theme.colors.secondary, backgroundColor: theme.colors.secondary,
padding: '0.75em 1em', padding: '0.75em 1em',
borderRadius: theme.border.radius, borderRadius: theme.border.radius,
'&:hover': { '&:hover': {
outlineWidth: '1px', outlineWidth: '1px',
outlineStyle: 'solid', outlineStyle: 'solid',
}, },
})); }));
const LinkHeader = styled.div(({ theme }) => ({ const LinkHeader = styled.div(({ theme }) => ({
display: 'flex', display: 'flex',
gap: '1em', gap: '1em',
alignItems: 'center', alignItems: 'center',
'& > a': { '& > a': {
height: '100%', height: '100%',
maxWidth: 'calc(100% - 75px)', // TODO: fix this, it is ugly af :( maxWidth: 'calc(100% - 75px)', // TODO: fix this, it is ugly af :(
textDecoration: 'none', textDecoration: 'none',
display: 'flex', display: 'flex',
flex: 1, flex: 1,
flexDirection: 'column', flexDirection: 'column',
transition: theme.transition.delay, transition: theme.transition.delay,
'&, &:hover': { '&, &:hover': {
border: 0, border: 0,
}, },
}, },
})); }));
const LinkName = styled.div({ const LinkName = styled.div({
width: '100%', width: '100%',
textOverflow: 'ellipsis', textOverflow: 'ellipsis',
whiteSpace: 'nowrap', whiteSpace: 'nowrap',
overflow: 'hidden', overflow: 'hidden',
}); });
const LinkDescription = styled.div(({ theme }) => ({ const LinkDescription = styled.div(({ theme }) => ({
marginTop: '0.5em', marginTop: '0.5em',
color: theme.colors.font, color: theme.colors.font,
fontSize: '0.8em', fontSize: '0.8em',
wordWrap: 'break-word', wordWrap: 'break-word',
})); }));
const LinkUrl = styled.span(({ theme }) => ({ const LinkUrl = styled.span(({ theme }) => ({
width: '100%', width: '100%',
textOverflow: 'ellipsis', textOverflow: 'ellipsis',
whiteSpace: 'nowrap', whiteSpace: 'nowrap',
overflow: 'hidden', overflow: 'hidden',
color: theme.colors.grey, color: theme.colors.grey,
fontSize: '0.8em', fontSize: '0.8em',
})); }));
const StarIcon = styled(AiFillStar)(({ theme }) => ({ const StarIcon = styled(AiFillStar)(({ theme }) => ({
color: theme.colors.yellow, color: theme.colors.yellow,
})); }));
const LinkUrlPathname = styled.span({ const LinkUrlPathname = styled.span({
opacity: 0, opacity: 0,
}); });
export default function LinkItem({ export default function LinkItem({
link, link,
showUserControls = false, showUserControls = false,
}: { }: {
link: Link; link: Link;
showUserControls: boolean; showUserControls: boolean;
}) { }) {
const { id, name, url, description, favorite } = link; const { id, name, url, description, favorite } = link;
return ( return (
<LinkWrapper key={id} title={name}> <LinkWrapper key={id} title={name}>
<LinkHeader> <LinkHeader>
<LinkFavicon url={url} /> <LinkFavicon url={url} />
<ExternalLink href={url} className="reset"> <ExternalLink href={url} className="reset">
<LinkName> <LinkName>
{name} {showUserControls && favorite && <StarIcon />} {name} {showUserControls && favorite && <StarIcon />}
</LinkName> </LinkName>
<LinkItemURL url={url} /> <LinkItemURL url={url} />
</ExternalLink> </ExternalLink>
{showUserControls && <LinkControls link={link} />} {showUserControls && <LinkControls link={link} />}
</LinkHeader> </LinkHeader>
{description && <LinkDescription>{description}</LinkDescription>} {description && <LinkDescription>{description}</LinkDescription>}
</LinkWrapper> </LinkWrapper>
); );
} }
function LinkItemURL({ url }: { url: Link['url'] }) { function LinkItemURL({ url }: { url: Link['url'] }) {
try { try {
const { origin, pathname, search } = new URL(url); const { origin, pathname, search } = new URL(url);
let text = ''; let text = '';
if (pathname !== '/') { if (pathname !== '/') {
text += pathname; text += pathname;
} }
if (search !== '') { if (search !== '') {
if (text === '') { if (text === '') {
text += '/'; text += '/';
} }
text += search; text += search;
} }
return ( return (
<LinkUrl> <LinkUrl>
{origin} {origin}
<LinkUrlPathname>{text}</LinkUrlPathname> <LinkUrlPathname>{text}</LinkUrlPathname>
</LinkUrl> </LinkUrl>
); );
} catch (error) { } catch (error) {
console.error('error', error); console.error('error', error);
return <LinkUrl>{url}</LinkUrl>; return <LinkUrl>{url}</LinkUrl>;
} }
} }

View File

@@ -5,34 +5,34 @@ import { sortByCreationDate } from '~/lib/array';
import { Link } from '~/types/app'; import { Link } from '~/types/app';
const LinkListStyle = styled.ul({ const LinkListStyle = styled.ul({
height: '100%', height: '100%',
width: '100%', width: '100%',
minWidth: 0, minWidth: 0,
display: 'flex', display: 'flex',
flex: 1, flex: 1,
gap: '0.5em', gap: '0.5em',
padding: '3px', padding: '3px',
flexDirection: 'column', flexDirection: 'column',
overflowX: 'hidden', overflowX: 'hidden',
overflowY: 'scroll', overflowY: 'scroll',
}); });
export default function LinkList({ export default function LinkList({
links, links,
showControls = true, showControls = true,
}: { }: {
links: Link[]; links: Link[];
showControls?: boolean; showControls?: boolean;
}) { }) {
if (links.length === 0) { if (links.length === 0) {
return <NoLink />; return <NoLink />;
} }
return ( return (
<LinkListStyle> <LinkListStyle>
{sortByCreationDate(links).map((link) => ( {sortByCreationDate(links).map((link) => (
<LinkItem link={link} key={link.id} showUserControls={showControls} /> <LinkItem link={link} key={link.id} showUserControls={showControls} />
))} ))}
</LinkListStyle> </LinkListStyle>
); );
} }

View File

@@ -7,60 +7,60 @@ import { appendCollectionId } from '~/lib/navigation';
import { fadeIn } from '~/styles/keyframes'; import { fadeIn } from '~/styles/keyframes';
const NoCollectionStyle = styled.div({ const NoCollectionStyle = styled.div({
minWidth: 0, minWidth: 0,
display: 'flex', display: 'flex',
flex: 1, flex: 1,
alignItems: 'center', alignItems: 'center',
justifyContent: 'center', justifyContent: 'center',
flexDirection: 'column', flexDirection: 'column',
animation: `${fadeIn} 0.3s both`, animation: `${fadeIn} 0.3s both`,
}); });
const Text = styled.p({ const Text = styled.p({
minWidth: 0, minWidth: 0,
width: '100%', width: '100%',
textAlign: 'center', textAlign: 'center',
textOverflow: 'ellipsis', textOverflow: 'ellipsis',
whiteSpace: 'nowrap', whiteSpace: 'nowrap',
overflow: 'hidden', overflow: 'hidden',
}); });
export function NoCollection() { export function NoCollection() {
const { t } = useTranslation('home'); const { t } = useTranslation('home');
return ( return (
<NoCollectionStyle> <NoCollectionStyle>
<Text>{t('select-collection')}</Text> <Text>{t('select-collection')}</Text>
<Link href={route('collection.create-form').url}> <Link href={route('collection.create-form').url}>
{t('or-create-one')} {t('or-create-one')}
</Link> </Link>
</NoCollectionStyle> </NoCollectionStyle>
); );
} }
export function NoLink() { export function NoLink() {
const { t } = useTranslation('common'); const { t } = useTranslation('common');
const { activeCollection } = useActiveCollection(); const { activeCollection } = useActiveCollection();
return ( return (
<NoCollectionStyle> <NoCollectionStyle>
<Text <Text
dangerouslySetInnerHTML={{ dangerouslySetInnerHTML={{
__html: t( __html: t(
'home:no-link', 'home:no-link',
{ name: activeCollection?.name ?? '' } as any, { name: activeCollection?.name ?? '' } as any,
{ {
interpolation: { escapeValue: false }, interpolation: { escapeValue: false },
} }
), ),
}} }}
/> />
<Link <Link
href={appendCollectionId( href={appendCollectionId(
route('link.create-form').url, route('link.create-form').url,
activeCollection?.id activeCollection?.id
)} )}
> >
{t('link.create')} {t('link.create')}
</Link> </Link>
</NoCollectionStyle> </NoCollectionStyle>
); );
} }

View File

@@ -3,27 +3,27 @@ import { useTranslation } from 'react-i18next';
import { FcGoogle } from 'react-icons/fc'; import { FcGoogle } from 'react-icons/fc';
const NoSearchResultStyle = styled.i({ const NoSearchResultStyle = styled.i({
display: 'flex', display: 'flex',
alignItems: 'center', alignItems: 'center',
justifyContent: 'center', justifyContent: 'center',
gap: '0.25em', gap: '0.25em',
'& > span': { '& > span': {
display: 'flex', display: 'flex',
alignItems: 'center', alignItems: 'center',
}, },
}); });
export default function NoSearchResult() { export default function NoSearchResult() {
const { t } = useTranslation('common'); const { t } = useTranslation('common');
return ( return (
<NoSearchResultStyle> <NoSearchResultStyle>
{t('search-with')} {t('search-with')}
{''} {''}
<span> <span>
<FcGoogle size={20} /> <FcGoogle size={20} />
oogle oogle
</span> </span>
</NoSearchResultStyle> </NoSearchResultStyle>
); );
} }

View File

@@ -15,142 +15,142 @@ import { makeRequest } from '~/lib/request';
import { SearchResult } from '~/types/search'; import { SearchResult } from '~/types/search';
const SearchInput = styled.input(({ theme }) => ({ const SearchInput = styled.input(({ theme }) => ({
width: '100%', width: '100%',
fontSize: '20px', fontSize: '20px',
color: theme.colors.font, color: theme.colors.font,
backgroundColor: 'transparent', backgroundColor: 'transparent',
paddingLeft: 0, paddingLeft: 0,
border: '1px solid transparent', border: '1px solid transparent',
})); }));
interface SearchModalProps { interface SearchModalProps {
openItem: any; openItem: any;
} }
function SearchModal({ openItem: OpenItem }: SearchModalProps) { function SearchModal({ openItem: OpenItem }: SearchModalProps) {
const { t } = useTranslation(); const { t } = useTranslation();
const { collections } = useCollections(); const { collections } = useCollections();
const { setActiveCollection } = useActiveCollection(); const { setActiveCollection } = useActiveCollection();
const [searchTerm, setSearchTerm] = useState<string>(''); const [searchTerm, setSearchTerm] = useState<string>('');
const [results, setResults] = useState<SearchResult[]>([]); const [results, setResults] = useState<SearchResult[]>([]);
const [selectedItem, setSelectedItem] = useState<SearchResult | null>(null); const [selectedItem, setSelectedItem] = useState<SearchResult | null>(null);
const searchModal = useToggle(!!searchTerm); const searchModal = useToggle(!!searchTerm);
const handleCloseModal = useCallback(() => { const handleCloseModal = useCallback(() => {
searchModal.close(); searchModal.close();
setSearchTerm(''); setSearchTerm('');
}, [searchModal]); }, [searchModal]);
const handleSearchInputChange = (value: string) => setSearchTerm(value); const handleSearchInputChange = (value: string) => setSearchTerm(value);
const handleSubmit = (event: FormEvent<HTMLFormElement>) => { const handleSubmit = (event: FormEvent<HTMLFormElement>) => {
event.preventDefault(); event.preventDefault();
handleCloseModal(); handleCloseModal();
if (results.length === 0) { if (results.length === 0) {
return window.open(GOOGLE_SEARCH_URL + encodeURI(searchTerm.trim())); return window.open(GOOGLE_SEARCH_URL + encodeURI(searchTerm.trim()));
} }
if (!selectedItem) return; if (!selectedItem) return;
if (selectedItem.type === 'collection') { if (selectedItem.type === 'collection') {
const collection = collections.find((c) => c.id === selectedItem.id); const collection = collections.find((c) => c.id === selectedItem.id);
if (collection) { if (collection) {
setActiveCollection(collection); setActiveCollection(collection);
} }
return; return;
} }
window.open(selectedItem.url); window.open(selectedItem.url);
}; };
useShortcut('OPEN_SEARCH_KEY', searchModal.open, { useShortcut('OPEN_SEARCH_KEY', searchModal.open, {
enabled: !searchModal.isShowing, enabled: !searchModal.isShowing,
}); });
useShortcut('ESCAPE_KEY', handleCloseModal, { useShortcut('ESCAPE_KEY', handleCloseModal, {
enabled: searchModal.isShowing, enabled: searchModal.isShowing,
disableGlobalCheck: true, disableGlobalCheck: true,
}); });
useEffect(() => { useEffect(() => {
if (searchTerm.trim() === '') { if (searchTerm.trim() === '') {
return setResults([]); return setResults([]);
} }
const controller = new AbortController(); const controller = new AbortController();
const { url, method } = route('search', { qs: { term: searchTerm } }); const { url, method } = route('search', { qs: { term: searchTerm } });
makeRequest({ makeRequest({
method, method,
url, url,
controller, controller,
}).then(({ results: _results }) => { }).then(({ results: _results }) => {
setResults(_results); setResults(_results);
setSelectedItem(_results?.[0]); setSelectedItem(_results?.[0]);
}); });
return () => controller.abort(); return () => controller.abort();
}, [searchTerm]); }, [searchTerm]);
return ( return (
<> <>
<OpenItem onClick={searchModal.open}> <OpenItem onClick={searchModal.open}>
<IoIosSearch /> {t('common:search')} <IoIosSearch /> {t('common:search')}
</OpenItem> </OpenItem>
<Modal <Modal
close={handleCloseModal} close={handleCloseModal}
opened={searchModal.isShowing} opened={searchModal.isShowing}
hideCloseBtn hideCloseBtn
css={{ width: '650px' }} css={{ width: '650px' }}
> >
<form <form
onSubmit={handleSubmit} onSubmit={handleSubmit}
css={{ css={{
width: '100%', width: '100%',
display: 'flex', display: 'flex',
gap: '0.5em', gap: '0.5em',
flexDirection: 'column', flexDirection: 'column',
}} }}
> >
<div <div
css={{ css={{
display: 'flex', display: 'flex',
gap: '0.35em', gap: '0.35em',
alignItems: 'center', alignItems: 'center',
justifyContent: 'center', justifyContent: 'center',
}} }}
> >
<label htmlFor="search" css={{ display: 'flex' }}> <label htmlFor="search" css={{ display: 'flex' }}>
<IoIosSearch size={24} /> <IoIosSearch size={24} />
</label> </label>
<SearchInput <SearchInput
name="search" name="search"
id="search" id="search"
onChange={({ target }) => handleSearchInputChange(target.value)} onChange={({ target }) => handleSearchInputChange(target.value)}
value={searchTerm} value={searchTerm}
placeholder={t('common:search')} placeholder={t('common:search')}
autoFocus autoFocus
/> />
</div> </div>
{results.length > 0 && selectedItem && ( {results.length > 0 && selectedItem && (
<SearchResultList <SearchResultList
results={results} results={results}
selectedItem={selectedItem} selectedItem={selectedItem}
setSelectedItem={setSelectedItem} setSelectedItem={setSelectedItem}
/> />
)} )}
{results.length === 0 && !!searchTerm.trim() && <NoSearchResult />} {results.length === 0 && !!searchTerm.trim() && <NoSearchResult />}
<button <button
type="submit" type="submit"
disabled={searchTerm.length === 0} disabled={searchTerm.length === 0}
style={{ display: 'none' }} style={{ display: 'none' }}
> >
{t('common:confirm')} {t('common:confirm')}
</button> </button>
</form> </form>
</Modal> </Modal>
</> </>
); );
} }
export default SearchModal; export default SearchModal;

Some files were not shown because too many files have changed in this diff Show More