Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

2.9 KB
Raw Blame
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { Navigate, useSearchParams } from "react-router-dom";
import { Icon } from "@editor/ui/Icon";
import { Button, Skeleton } from "@editor/ui";
import { useSectionFlags } from "@processor/hooks/useAsync";
import { useSources } from "@processor/queries/sources";
import { type SourceView } from "@processor/api/sources";
import { VIEW_PATHS, toPortalPath } from "@processor/contexts/ViewContext";
import { SourcesTable } from "@processor/components/sources/SourcesTable";
import { SourceModal } from "@processor/components/sources/SourceModal";
import "@processor/views/Sources.css";

export function Sources() {
  const { t } = useTranslation();
  const [searchParams, setSearchParams] = useSearchParams();

  const state = useSources();
  const { data } = state;
  const { isLoading } = useSectionFlags(state);

  // Create/edit live in a modal on this list; `?new=1` (old /sources/new deep
  // links redirect here with it) opens the create flow on arrival.
  const [modal, setModal] = useState<{
    open: boolean;
    sourceId: string | null;
  }>({ open: false, sourceId: null });

  useEffect(() => {
    if (searchParams.get("new") !== "1") return;
    setModal({ open: true, sourceId: null });
    const next = new URLSearchParams(searchParams);
    next.delete("new");
    setSearchParams(next, { replace: true });
  }, [searchParams, setSearchParams]);

  const sources = data?.sources ?? [];

  const openCreate = () => setModal({ open: true, sourceId: null });
  const openSource = (source: SourceView) =>
    setModal({ open: true, sourceId: source.id });

  // The Connections tab moved to its own Integrations view.
  if (searchParams.get("tab") === "connections") {
    return <Navigate to={toPortalPath(VIEW_PATHS.integrations)} replace />;
  }

  return (
    <div className="portal-sources">
      <header className="portal-sources__head">
        <div>
          <h1 className="portal-sources__title">{t("portal.sources.title")}</h1>
          <p className="portal-sources__sub">{t("portal.sources.subtitle")}</p>
        </div>
        <div className="portal-sources__actions">
          <Button
            fat
            onClick={openCreate}
            leftSection={<Icon name="plus" size={"1.125rem"} />}
          >
            {t("portal.sources.actions.connectSource")}
          </Button>
        </div>
      </header>

      {isLoading && (
        <div className="portal-sources__table-skeleton" aria-hidden>
          {Array.from({ length: 5 }).map((_, i) => (
            <Skeleton key={i} height="3rem" />
          ))}
        </div>
      )}

      {!isLoading && <SourcesTable sources={sources} onRowClick={openSource} />}

      <SourceModal
        open={modal.open}
        sourceId={modal.sourceId}
        onClose={() => setModal({ open: false, sourceId: null })}
      />
    </div>
  );
}