From 0b5f25a2630588046e415a4388a9d9fea67d073f Mon Sep 17 00:00:00 2001 From: lonelyhentxi Date: Fri, 6 Jun 2025 01:58:19 +0800 Subject: [PATCH] fix: fix credential 3rd error --- Cargo.lock | 36 ++++++++------ .../m20250501_021523_credential_3rd.rs | 6 ++- apps/recorder/src/models/credential_3rd.rs | 1 + apps/webui/package.json | 2 + .../recorder/services/subscription.service.ts | 20 ++++---- apps/webui/src/infra/errors/arktype.ts | 20 ++++++++ apps/webui/src/infra/routes/nav.ts | 13 +++++ .../routes/_app/credential3rd/create.tsx | 48 +++++++++++++++---- .../routes/_app/credential3rd/detail.$id.tsx | 26 +++++++--- .../routes/_app/credential3rd/edit.$id.tsx | 2 +- .../subscriptions/-credential3rd-select.tsx | 22 ++++++++- .../routes/_app/subscriptions/create.tsx | 16 +++++-- pnpm-lock.yaml | 18 +++++++ 13 files changed, 178 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9a0b9d4..e00faad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -886,9 +886,9 @@ dependencies = [ [[package]] name = "bytemuck" -version = "1.23.0" +version = "1.23.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9134a6ef01ce4b366b50689c94f82c14bc72bc5d0386829828a2e2752ef7958c" +checksum = "5c76a5792e44e4abe34d3abf15636779261d45a7450612059293d1d2cfc63422" [[package]] name = "byteorder" @@ -2347,11 +2347,11 @@ dependencies = [ [[package]] name = "getopts" -version = "0.2.21" +version = "0.2.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5" +checksum = "cba6ae63eb948698e300f645f87c70f76630d505f23b8907cf1e193ee85048c1" dependencies = [ - "unicode-width", + "unicode-width 0.2.0", ] [[package]] @@ -2523,9 +2523,9 @@ checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" [[package]] name = "hashbrown" -version = "0.15.3" +version = "0.15.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84b26c544d002229e640969970a2e74021aadf6e2f96372b9c58eff97de08eb3" +checksum = "5971ac85611da7067dbfcabef3c70ebb5606018acd9e2a3903a0da507521e0d5" dependencies = [ "allocator-api2", "equivalent", @@ -2538,7 +2538,7 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7382cf6263419f2d8df38c55d7da83da5c18aef87fc7a7fc1fb1e344edfe14c1" dependencies = [ - "hashbrown 0.15.3", + "hashbrown 0.15.4", ] [[package]] @@ -3027,7 +3027,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cea70ddb795996207ad57735b50c5982d8844f38ba9ee5f1aedcfb708a2aa11e" dependencies = [ "equivalent", - "hashbrown 0.15.3", + "hashbrown 0.15.4", "serde", ] @@ -3091,7 +3091,7 @@ dependencies = [ "newline-converter", "once_cell", "unicode-segmentation", - "unicode-width", + "unicode-width 0.1.14", ] [[package]] @@ -3751,7 +3751,7 @@ version = "0.12.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "234cf4f4a04dc1f57e24b96cc0cd600cf2af460d4161ac5ecdd0af8e1f3b2a38" dependencies = [ - "hashbrown 0.15.3", + "hashbrown 0.15.4", ] [[package]] @@ -3871,7 +3871,7 @@ dependencies = [ "miette-derive", "once_cell", "thiserror 1.0.69", - "unicode-width", + "unicode-width 0.1.14", ] [[package]] @@ -6513,7 +6513,7 @@ dependencies = [ "futures-intrusive", "futures-io", "futures-util", - "hashbrown 0.15.3", + "hashbrown 0.15.4", "hashlink", "indexmap 2.9.0", "log", @@ -7623,6 +7623,12 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" +[[package]] +name = "unicode-width" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fc81956842c57dac11422a97c3b8195a1ff727f06e85c84ed2e8aa277c9a0fd" + [[package]] name = "universal-hash" version = "0.5.1" @@ -8492,9 +8498,9 @@ dependencies = [ [[package]] name = "zune-jpeg" -version = "0.4.16" +version = "0.4.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e4a518c0ea2576f4da876349d7f67a7be489297cd77c2cf9e04c2e05fcd3974" +checksum = "0f6fe2e33d02a98ee64423802e16df3de99c43e5cf5ff983767e1128b394c8ac" dependencies = [ "zune-core", ] diff --git a/apps/recorder/src/migrations/m20250501_021523_credential_3rd.rs b/apps/recorder/src/migrations/m20250501_021523_credential_3rd.rs index b718621..51dab7f 100644 --- a/apps/recorder/src/migrations/m20250501_021523_credential_3rd.rs +++ b/apps/recorder/src/migrations/m20250501_021523_credential_3rd.rs @@ -28,7 +28,11 @@ impl MigrationTrait for Migration { table_auto_z(Credential3rd::Table) .col(pk_auto(Credential3rd::Id)) .col(integer(Credential3rd::SubscriberId)) - .col(string(Credential3rd::CredentialType)) + .col(enumeration( + Credential3rd::CredentialType, + Credential3rdTypeEnum, + Credential3rdType::iden_values(), + )) .col(string_null(Credential3rd::Cookies)) .col(string_null(Credential3rd::Username)) .col(string_null(Credential3rd::Password)) diff --git a/apps/recorder/src/models/credential_3rd.rs b/apps/recorder/src/models/credential_3rd.rs index 14bdc84..af91951 100644 --- a/apps/recorder/src/models/credential_3rd.rs +++ b/apps/recorder/src/models/credential_3rd.rs @@ -18,6 +18,7 @@ use crate::{ db_type = "Enum", enum_name = "credential_3rd_type" )] +#[serde(rename_all = "snake_case")] pub enum Credential3rdType { #[sea_orm(string_value = "mikan")] Mikan, diff --git a/apps/webui/package.json b/apps/webui/package.json index 1e0c64d..5189c11 100644 --- a/apps/webui/package.json +++ b/apps/webui/package.json @@ -65,6 +65,7 @@ "input-otp": "^1.4.2", "jotai": "^2.12.3", "jotai-signal": "^0.9.0", + "lodash-es": "^4.17.21", "lucide-react": "^0.512.0", "oidc-client-rx": "0.1.0-alpha.9", "react": "^19.1.0", @@ -91,6 +92,7 @@ "@tanstack/react-router": "^1.112.0", "@tanstack/router-devtools": "^1.112.6", "@tanstack/router-plugin": "^1.112.13", + "@types/lodash-es": "^4.17.12", "@types/react": "^19.1.2", "@types/react-dom": "^19.1.2", "chalk": "^5.4.1", diff --git a/apps/webui/src/domains/recorder/services/subscription.service.ts b/apps/webui/src/domains/recorder/services/subscription.service.ts index f457908..f7e7e35 100644 --- a/apps/webui/src/domains/recorder/services/subscription.service.ts +++ b/apps/webui/src/domains/recorder/services/subscription.service.ts @@ -3,6 +3,7 @@ import { type SubscriptionsInsertInput, } from '@/infra/graphql/gql/graphql'; import { Injectable, inject } from '@outposts/injection-js'; +import { omit } from 'lodash-es'; import { buildMikanSubscriptionSeasonSourceUrl } from '../schema/mikan'; import type { SubscriptionInsertForm } from '../schema/subscriptions'; import { MikanService } from './mikan.service'; @@ -14,18 +15,15 @@ export class SubscriptionService { transformInsertFormToInput( form: SubscriptionInsertForm ): SubscriptionsInsertInput { - let sourceUrl: string; if (form.category === SubscriptionCategoryEnum.MikanSeason) { - sourceUrl = buildMikanSubscriptionSeasonSourceUrl( - this.mikan.mikanBaseUrl, - form - ).toString(); - } else { - sourceUrl = form.sourceUrl; + return { + ...omit(form, ['seasonStr', 'year']), + sourceUrl: buildMikanSubscriptionSeasonSourceUrl( + this.mikan.mikanBaseUrl, + form + ).toString(), + }; } - return { - ...form, - sourceUrl, - }; + return form; } } diff --git a/apps/webui/src/infra/errors/arktype.ts b/apps/webui/src/infra/errors/arktype.ts index 4b15f8b..9ccbc4b 100644 --- a/apps/webui/src/infra/errors/arktype.ts +++ b/apps/webui/src/infra/errors/arktype.ts @@ -13,3 +13,23 @@ export function arkValidatorToTypeNarrower< return true; }; } + +export function validateOr( + validated: T | ArkErrors, + defaultValue: D +): T | D { + if (validated instanceof ArkErrors) { + return defaultValue; + } + return validated; +} + +export function validateOrElse( + validated: T | ArkErrors, + elseFn: (errors: ArkErrors) => D +): T | D { + if (validated instanceof ArkErrors) { + return elseFn(validated); + } + return validated; +} diff --git a/apps/webui/src/infra/routes/nav.ts b/apps/webui/src/infra/routes/nav.ts index 3180134..b2d07af 100644 --- a/apps/webui/src/infra/routes/nav.ts +++ b/apps/webui/src/infra/routes/nav.ts @@ -1,3 +1,4 @@ +import { type } from 'arktype'; import { BookOpen, Folders, @@ -112,3 +113,15 @@ export const AppNavMainData = [ ], }, ]; + +export const CreateCompleteAction = { + Back: 'back', + Detail: 'detail', +} as const; + +export type CreateCompleteAction = + (typeof CreateCompleteAction)[keyof typeof CreateCompleteAction]; + +export const CreateCompleteActionSchema = type.enumerated( + ...Object.values(CreateCompleteAction) +); diff --git a/apps/webui/src/presentation/routes/_app/credential3rd/create.tsx b/apps/webui/src/presentation/routes/_app/credential3rd/create.tsx index 9c112ad..68cc473 100644 --- a/apps/webui/src/presentation/routes/_app/credential3rd/create.tsx +++ b/apps/webui/src/presentation/routes/_app/credential3rd/create.tsx @@ -29,33 +29,65 @@ import { type InsertCredential3rdMutationVariables, } from '@/infra/graphql/gql/graphql'; import { PlatformService } from '@/infra/platform/platform.service'; +import { + CreateCompleteAction, + CreateCompleteActionSchema, +} from '@/infra/routes/nav'; import type { RouteStateDataOption } from '@/infra/routes/traits'; import { useMutation } from '@apollo/client'; -import { createFileRoute, useNavigate } from '@tanstack/react-router'; +import { + createFileRoute, + useCanGoBack, + useNavigate, + useRouter, +} from '@tanstack/react-router'; +import { type } from 'arktype'; import { Loader2, Save } from 'lucide-react'; import { toast } from 'sonner'; +const RouteSearchSchema = type({ + completeAction: CreateCompleteActionSchema.optional(), +}); + export const Route = createFileRoute('/_app/credential3rd/create')({ component: CredentialCreateRouteComponent, staticData: { breadcrumb: { label: 'Create' }, } satisfies RouteStateDataOption, + validateSearch: RouteSearchSchema, }); function CredentialCreateRouteComponent() { const navigate = useNavigate(); + const router = useRouter(); + const canGoBack = useCanGoBack(); + const search = Route.useSearch(); const platformService = useInject(PlatformService); + const handleBack = () => { + if (canGoBack) { + router.history.back(); + } else { + navigate({ + to: '/credential3rd/manage', + }); + } + }; + const [insertCredential3rd, { loading }] = useMutation< - InsertCredential3rdMutation['credential3rdCreateOne'], + InsertCredential3rdMutation, InsertCredential3rdMutationVariables >(INSERT_CREDENTIAL_3RD, { onCompleted(data) { toast.success('Credential created'); - navigate({ - to: '/credential3rd/detail/$id', - params: { id: `${data.id}` }, - }); + if (search.completeAction === CreateCompleteAction.Back) { + handleBack(); + } else { + navigate({ + to: '/credential3rd/detail/$id', + params: { id: `${data.credential3rdCreateOne.id}` }, + }); + } }, onError(error) { toast.error('Failed to create credential', { @@ -168,8 +200,6 @@ function CredentialCreateRouteComponent() { )} - - {/* 密码 */} {(field) => (
@@ -194,8 +224,6 @@ function CredentialCreateRouteComponent() {
)}
- - {/* User Agent */} {(field) => (
diff --git a/apps/webui/src/presentation/routes/_app/credential3rd/detail.$id.tsx b/apps/webui/src/presentation/routes/_app/credential3rd/detail.$id.tsx index 3571ced..e9749ac 100644 --- a/apps/webui/src/presentation/routes/_app/credential3rd/detail.$id.tsx +++ b/apps/webui/src/presentation/routes/_app/credential3rd/detail.$id.tsx @@ -16,7 +16,12 @@ import { GET_CREDENTIAL_3RD_DETAIL } from '@/domains/recorder/schema/credential3 import type { GetCredential3rdDetailQuery } from '@/infra/graphql/gql/graphql'; import type { RouteStateDataOption } from '@/infra/routes/traits'; import { useQuery } from '@apollo/client'; -import { createFileRoute, useNavigate } from '@tanstack/react-router'; +import { + createFileRoute, + useCanGoBack, + useNavigate, + useRouter, +} from '@tanstack/react-router'; import { format } from 'date-fns/format'; import { ArrowLeft, Edit, Eye, EyeOff } from 'lucide-react'; import { useState } from 'react'; @@ -31,8 +36,21 @@ export const Route = createFileRoute('/_app/credential3rd/detail/$id')({ function Credential3rdDetailRouteComponent() { const { id } = Route.useParams(); const navigate = useNavigate(); + const router = useRouter(); + const canGoBack = useCanGoBack(); + const [showPassword, setShowPassword] = useState(false); + const handleBack = () => { + if (canGoBack) { + router.history.back(); + } else { + navigate({ + to: '/credential3rd/manage', + }); + } + }; + const { loading, error, data } = useQuery( GET_CREDENTIAL_3RD_DETAIL, { @@ -43,12 +61,6 @@ function Credential3rdDetailRouteComponent() { } ); - const handleBack = () => { - navigate({ - to: '/credential3rd/manage', - }); - }; - const handleEnterEditMode = () => { navigate({ to: '/credential3rd/edit/$id', diff --git a/apps/webui/src/presentation/routes/_app/credential3rd/edit.$id.tsx b/apps/webui/src/presentation/routes/_app/credential3rd/edit.$id.tsx index 6f229b0..72a5515 100644 --- a/apps/webui/src/presentation/routes/_app/credential3rd/edit.$id.tsx +++ b/apps/webui/src/presentation/routes/_app/credential3rd/edit.$id.tsx @@ -67,7 +67,7 @@ function FormView({ }; const [updateCredential, { loading: updating }] = useMutation< - UpdateCredential3rdMutation['credential3rdUpdate'], + UpdateCredential3rdMutation, UpdateCredential3rdMutationVariables >(UPDATE_CREDENTIAL_3RD, { onCompleted, diff --git a/apps/webui/src/presentation/routes/_app/subscriptions/-credential3rd-select.tsx b/apps/webui/src/presentation/routes/_app/subscriptions/-credential3rd-select.tsx index c520d67..fdce79c 100644 --- a/apps/webui/src/presentation/routes/_app/subscriptions/-credential3rd-select.tsx +++ b/apps/webui/src/presentation/routes/_app/subscriptions/-credential3rd-select.tsx @@ -7,8 +7,10 @@ import { type GetCredential3rdQueryVariables, OrderByEnum, } from '@/infra/graphql/gql/graphql'; +import { CreateCompleteAction } from '@/infra/routes/nav'; import { useQuery } from '@apollo/client'; -import { AlertCircle, Loader2, RefreshCw } from 'lucide-react'; +import { useNavigate } from '@tanstack/react-router'; +import { AlertCircle, Loader2, Plus, RefreshCw } from 'lucide-react'; import type { ComponentProps } from 'react'; export interface Credential3rdSelectContentProps @@ -20,6 +22,8 @@ export function Credential3rdSelectContent({ credentialType, ...props }: Credential3rdSelectContentProps) { + const navigate = useNavigate(); + const { data, loading, error, refetch } = useQuery< GetCredential3rdQuery, GetCredential3rdQueryVariables @@ -76,10 +80,24 @@ export function Credential3rdSelectContent({ {!loading && !error && (credentials.length === 0 ? ( -
+
No credentials found +
) : ( credentials.map((credential) => ( diff --git a/apps/webui/src/presentation/routes/_app/subscriptions/create.tsx b/apps/webui/src/presentation/routes/_app/subscriptions/create.tsx index df558d0..3056412 100644 --- a/apps/webui/src/presentation/routes/_app/subscriptions/create.tsx +++ b/apps/webui/src/presentation/routes/_app/subscriptions/create.tsx @@ -52,14 +52,14 @@ function SubscriptionCreateRouteComponent() { const subscriptionService = useInject(SubscriptionService); const [insertSubscription, { loading }] = useMutation< - InsertSubscriptionMutation['subscriptionsCreateOne'], + InsertSubscriptionMutation, InsertSubscriptionMutationVariables >(INSERT_SUBSCRIPTION, { onCompleted(data) { toast.success('Subscription created'); navigate({ to: '/subscriptions/detail/$id', - params: { id: `${data.id}` }, + params: { id: `${data.subscriptionsCreateOne.id}` }, }); }, onError(error) { @@ -80,7 +80,8 @@ function SubscriptionCreateRouteComponent() { seasonStr: '', } as unknown as SubscriptionInsertForm, validators: { - onBlur: SubscriptionInsertFormSchema, + onChangeAsync: SubscriptionInsertFormSchema, + onChangeAsyncDebounceMs: 300, onSubmit: SubscriptionInsertFormSchema, }, onSubmit: async (form) => { @@ -233,7 +234,7 @@ function SubscriptionCreateRouteComponent() { Number.parseInt(e.target.value) ) } - placeholder="Please enter full year (e.g. 2025)" + placeholder={`Please enter full year (e.g. ${new Date().getFullYear()})`} autoComplete="off" /> {field.state.meta.errors && ( @@ -252,7 +253,12 @@ function SubscriptionCreateRouteComponent() { {(field) => (
- + field.handleChange(value as MikanSeasonEnum) + } + > diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ef46b9a..776ea88 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -224,6 +224,9 @@ importers: jotai-signal: specifier: ^0.9.0 version: 0.9.0(jotai@2.12.5(@types/react@19.1.6)(react@19.1.0))(react@19.1.0) + lodash-es: + specifier: ^4.17.21 + version: 4.17.21 lucide-react: specifier: ^0.512.0 version: 0.512.0(react@19.1.0) @@ -291,6 +294,9 @@ importers: '@tanstack/router-plugin': specifier: ^1.112.13 version: 1.120.15(@rsbuild/core@1.3.22)(@tanstack/react-router@1.120.15(react-dom@19.1.0(react@19.1.0))(react@19.1.0))(vite@5.4.11(@types/node@22.15.29)(lightningcss@1.30.1)(sass@1.77.4)(terser@5.41.0))(webpack@5.97.1) + '@types/lodash-es': + specifier: ^4.17.12 + version: 4.17.12 '@types/react': specifier: ^19.1.2 version: 19.1.6 @@ -3509,6 +3515,9 @@ packages: '@types/json-schema@7.0.15': resolution: {integrity: sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==} + '@types/lodash-es@4.17.12': + resolution: {integrity: sha512-0NgftHUcV4v34VhXm8QBSftKVXtbkBG3ViCjs6+eJ5a6y6Mi/jiFGPc1sC7QK+9BFhWrURE3EOggmWaSxL9OzQ==} + '@types/lodash.merge@4.6.9': resolution: {integrity: sha512-23sHDPmzd59kUgWyKGiOMO2Qb9YtqRO/x4IhkgNUiPQ1+5MUVqi6bCZeq9nBJ17msjIMbEIO5u+XW4Kz6aGUhQ==} @@ -5428,6 +5437,9 @@ packages: resolution: {integrity: sha512-t7hw9pI+WvuwNJXwk5zVHpyhIqzg2qTlklJOf0mVxGSbe3Fp2VieZcduNYjaLDoy6p9uGpQEGWG87WpMKlNq8g==} engines: {node: '>=8'} + lodash-es@4.17.21: + resolution: {integrity: sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==} + lodash.chunk@4.2.0: resolution: {integrity: sha512-ZzydJKfUHJwHa+hF5X66zLFCBrWn5GeF28OHEr4WVWtNDXlQ/IjWKPBiikqKo2ne0+v6JgCgJ0GzJp8k8bHC7w==} @@ -10673,6 +10685,10 @@ snapshots: '@types/json-schema@7.0.15': optional: true + '@types/lodash-es@4.17.12': + dependencies: + '@types/lodash': 4.17.13 + '@types/lodash.merge@4.6.9': dependencies: '@types/lodash': 4.17.13 @@ -12880,6 +12896,8 @@ snapshots: dependencies: p-locate: 4.1.0 + lodash-es@4.17.21: {} + lodash.chunk@4.2.0: {} lodash.get@4.4.2: {}