Repository: toeverything/AFFiNE
File: packages/frontend/core/src/desktop/pages/open-app/index.tsx (TypeScript)

Code before:
import { useNavigateHelper } from '@affine/core/components/hooks/use-navigate-helper';
import { GraphQLService } from '@affine/core/modules/cloud';
import { OpenInAppPage } from '@affine/core/modules/open-in-app/views/open-in-app-page';
import {
  appSchemaUrl,
  appSchemes,
  channelToScheme,
} from '@affine/core/utils/channel';
import { extractUrlSearchParams } from '@affine/core/utils/url';
import type { GetCurrentUserQuery } from '@affine/graphql';
import { getCurrentUserQuery } from '@affine/graphql';
import { useService } from '@toeverything/infra';
import { useCallback, useEffect, useState } from 'react';
import {
  type LoaderFunction,
  redirect,
  useLoaderData,
  useParams,
} from 'react-router-dom';
import { z } from 'zod';

import { AppContainer } from '../../components/app-container';

const LoaderDataSchema = z.object({
  action: z.enum(['url', 'signin-redirect']),
  url: appSchemaUrl,
  params: z.record(z.string()),
});

type LoaderData = z.infer<typeof LoaderDataSchema>;

export const loader: LoaderFunction = async args => {
  const action = args.params.action || '';

  try {
    const { url, ...params } = extractUrlSearchParams(args.request.url);
    return LoaderDataSchema.parse({ action, url, params });
  } catch (e) {
    console.error(e);
    return redirect('/404');
  }
};

const OpenUrl = () => {
  const { params, url } = useLoaderData() as LoaderData;
  const navigateHelper = useNavigateHelper();

  const onOpenHere = useCallback(
    (e: React.MouseEvent) => {
      e.preventDefault();

Review comment (suggestion):
action doesn't seem to need judgment, filtering is already done inside the `Component`, illegal actions cannot be rendered

Code after:
import { useNavigateHelper } from '@affine/core/components/hooks/use-navigate-helper';
import { GraphQLService } from '@affine/core/modules/cloud';
import { OpenInAppPage } from '@affine/core/modules/open-in-app/views/open-in-app-page';
import {
  appSchemaUrl,
  appSchemes,
  channelToScheme,
} from '@affine/core/utils/channel';
import type { GetCurrentUserQuery } from '@affine/graphql';
import { getCurrentUserQuery } from '@affine/graphql';
import { useService } from '@toeverything/infra';
import { useCallback, useEffect, useState } from 'react';
import { useParams, useSearchParams } from 'react-router-dom';

import { AppContainer } from '../../components/app-container';

const OpenUrl = () => {
  const [params] = useSearchParams();
  const urlToOpen = params.get('url');
  const navigateHelper = useNavigateHelper();

  const onOpenHere = useCallback(
    (e: React.MouseEvent) => {
      e.preventDefault();
      navigateHelper.jumpToIndex();
    },
    [navigateHelper]
  );

  const parsed = appSchemaUrl.safeParse(urlToOpen);
  if (!parsed.success) {
    console.error(parsed.error);
    return null;
  }

  const urlObj = new URL(parsed.data);
  params.forEach((v, k) => {
    if (k === 'url') {
      return;
    }
    urlObj.searchParams.set(k, v);
  });

  return (
    <OpenInAppPage urlToOpen={urlObj.toString()} openHereClicked={onOpenHere} />
  );
};

/**
 * @deprecated