|
| 1 | +// This file is part of Edgehog. |
| 2 | +// |
| 3 | +// Copyright 2026 SECO Mind Srl |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | +// |
| 17 | +// SPDX-License-Identifier: Apache-2.0 |
| 18 | + |
| 19 | +import _ from "lodash"; |
| 20 | +import { useMemo } from "react"; |
| 21 | +import { FormattedMessage } from "react-intl"; |
| 22 | +import { graphql, useFragment } from "react-relay/hooks"; |
| 23 | + |
| 24 | +import type { |
| 25 | + RepositoriesTable_RepositoryEdgeFragment$data, |
| 26 | + RepositoriesTable_RepositoryEdgeFragment$key, |
| 27 | +} from "@/api/__generated__/RepositoriesTable_RepositoryEdgeFragment.graphql"; |
| 28 | + |
| 29 | +import InfiniteTable from "@/components/InfiniteTable"; |
| 30 | +import { createColumnHelper } from "@/components/Table"; |
| 31 | +import { Link, Route } from "@/Navigation"; |
| 32 | + |
| 33 | +// We use graphql fields below in columns configuration |
| 34 | +/* eslint-disable relay/unused-fields */ |
| 35 | +const REPOSITORIES_FRAGMENT = graphql` |
| 36 | + fragment RepositoriesTable_RepositoryEdgeFragment on RepositoryConnection { |
| 37 | + edges { |
| 38 | + node { |
| 39 | + id |
| 40 | + name |
| 41 | + handle |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | +`; |
| 46 | + |
| 47 | +type TableRecord = NonNullable< |
| 48 | + NonNullable<RepositoriesTable_RepositoryEdgeFragment$data>["edges"] |
| 49 | +>[number]["node"]; |
| 50 | + |
| 51 | +const columnHelper = createColumnHelper<TableRecord>(); |
| 52 | +const columns = [ |
| 53 | + columnHelper.accessor("name", { |
| 54 | + header: () => ( |
| 55 | + <FormattedMessage |
| 56 | + id="components.RepositoriesTable.nameTitle" |
| 57 | + defaultMessage="Repository Name" |
| 58 | + description="Title for the Name column of the repositories table" |
| 59 | + /> |
| 60 | + ), |
| 61 | + cell: ({ row, getValue }) => ( |
| 62 | + <Link |
| 63 | + route={Route.repositoryEdit} |
| 64 | + params={{ repositoryId: row.original.id }} |
| 65 | + > |
| 66 | + {getValue()} |
| 67 | + </Link> |
| 68 | + ), |
| 69 | + }), |
| 70 | + columnHelper.accessor("handle", { |
| 71 | + header: () => ( |
| 72 | + <FormattedMessage |
| 73 | + id="components.RepositoriesTable.handleTitle" |
| 74 | + defaultMessage="Handle" |
| 75 | + description="Title for the Handle column of the repositories table" |
| 76 | + /> |
| 77 | + ), |
| 78 | + }), |
| 79 | +]; |
| 80 | + |
| 81 | +type Props = { |
| 82 | + className?: string; |
| 83 | + repositoriesRef: RepositoriesTable_RepositoryEdgeFragment$key; |
| 84 | + loading?: boolean; |
| 85 | + onLoadMore?: () => void; |
| 86 | +}; |
| 87 | + |
| 88 | +const RepositoriesTable = ({ |
| 89 | + className, |
| 90 | + repositoriesRef, |
| 91 | + loading = false, |
| 92 | + onLoadMore, |
| 93 | +}: Props) => { |
| 94 | + const repositoriesFragment = useFragment( |
| 95 | + REPOSITORIES_FRAGMENT, |
| 96 | + repositoriesRef || null, |
| 97 | + ); |
| 98 | + |
| 99 | + const repositories = useMemo<TableRecord[]>(() => { |
| 100 | + return _.compact(repositoriesFragment?.edges?.map((e) => e?.node)) ?? []; |
| 101 | + }, [repositoriesFragment]); |
| 102 | + |
| 103 | + return ( |
| 104 | + <InfiniteTable |
| 105 | + className={className} |
| 106 | + columns={columns} |
| 107 | + data={repositories} |
| 108 | + loading={loading} |
| 109 | + onLoadMore={onLoadMore} |
| 110 | + hideSearch |
| 111 | + /> |
| 112 | + ); |
| 113 | +}; |
| 114 | + |
| 115 | +export default RepositoriesTable; |
0 commit comments