|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React, { useState } from "react"; |
| 4 | +import { useQuery, gql } from "@keystone-6/core/admin-ui/apollo"; |
| 5 | +import { EllipsisVertical, Plus } from "lucide-react"; |
| 6 | +import { Skeleton } from "@ui/skeleton"; |
| 7 | +import { Button } from "@keystone/themes/Tailwind/atlas/primitives/default/ui/button"; |
| 8 | + |
| 9 | +import { Badge } from "@ui/badge"; |
| 10 | +import { |
| 11 | + DropdownMenu, |
| 12 | + DropdownMenuContent, |
| 13 | + DropdownMenuItem, |
| 14 | + DropdownMenuPortal, |
| 15 | + DropdownMenuTrigger, |
| 16 | +} from "@keystone/themes/Tailwind/atlas/primitives/default/ui/dropdown-menu-depracated"; |
| 17 | +import { CreatePlatform } from "./CreatePlatform"; |
| 18 | + |
| 19 | +export const CHANNEL_PLATFORMS_QUERY = gql` |
| 20 | + query ( |
| 21 | + $where: ChannelPlatformWhereInput |
| 22 | + $take: Int! |
| 23 | + $skip: Int! |
| 24 | + $orderBy: [ChannelPlatformOrderByInput!] |
| 25 | + ) { |
| 26 | + items: channelPlatforms( |
| 27 | + where: $where |
| 28 | + take: $take |
| 29 | + skip: $skip |
| 30 | + orderBy: $orderBy |
| 31 | + ) { |
| 32 | + id |
| 33 | + name |
| 34 | + createPurchaseFunction |
| 35 | + searchProductsFunction |
| 36 | + getProductFunction |
| 37 | + getWebhooksFunction |
| 38 | + deleteWebhookFunction |
| 39 | + createWebhookFunction |
| 40 | + cancelPurchaseWebhookHandler |
| 41 | + createTrackingWebhookHandler |
| 42 | + oAuthFunction |
| 43 | + oAuthCallbackFunction |
| 44 | + appKey |
| 45 | + appSecret |
| 46 | + createdAt |
| 47 | + updatedAt |
| 48 | + __typename |
| 49 | + } |
| 50 | + count: channelPlatformsCount(where: $where) |
| 51 | + } |
| 52 | +`; |
| 53 | + |
| 54 | +const ChannelPlatformsContent = ({ data, openDrawer, showAll }) => { |
| 55 | + if (!data || !data.items) return null; |
| 56 | + |
| 57 | + const platformItems = [...data.items]; |
| 58 | + |
| 59 | + return platformItems |
| 60 | + .slice(0, showAll ? platformItems.length : 6) |
| 61 | + .map((platform, index) => ( |
| 62 | + <div key={index} className="flex items-center"> |
| 63 | + <Button |
| 64 | + variant="secondary" |
| 65 | + className="p-1" |
| 66 | + onClick={() => openDrawer(platform.id, "ChannelPlatform")} |
| 67 | + > |
| 68 | + <EllipsisVertical className="size-2.5" /> |
| 69 | + </Button> |
| 70 | + <label |
| 71 | + htmlFor={`filter-${platform.id}`} |
| 72 | + className="ml-3 text-sm text-muted-foreground" |
| 73 | + > |
| 74 | + {platform.name} |
| 75 | + </label> |
| 76 | + </div> |
| 77 | + )); |
| 78 | +}; |
| 79 | + |
| 80 | +const useChannelPlatformsQuery = () => { |
| 81 | + return useQuery(CHANNEL_PLATFORMS_QUERY, { |
| 82 | + variables: { |
| 83 | + where: { OR: [] }, |
| 84 | + take: 50, |
| 85 | + skip: 0, |
| 86 | + }, |
| 87 | + }); |
| 88 | +}; |
| 89 | + |
| 90 | +export const ChannelPlatforms = ({ openDrawer }) => { |
| 91 | + const { data, loading, error } = useChannelPlatformsQuery(); |
| 92 | + const [showAll, setShowAll] = useState(true); |
| 93 | + |
| 94 | + if (loading) { |
| 95 | + return ( |
| 96 | + <div className="grid gap-2 overflow-hidden"> |
| 97 | + {Array(6) |
| 98 | + .fill(0) |
| 99 | + .map((_, index) => ( |
| 100 | + <Skeleton key={index} className="h-8 w-full rounded-lg" /> |
| 101 | + ))} |
| 102 | + </div> |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + if (error) return <p>Error loading platforms: {error.message}</p>; |
| 107 | + |
| 108 | + return ( |
| 109 | + <ChannelPlatformsContent |
| 110 | + data={data} |
| 111 | + openDrawer={openDrawer} |
| 112 | + showAll={showAll} |
| 113 | + /> |
| 114 | + ); |
| 115 | +}; |
| 116 | + |
| 117 | +export const ChannelPlatformsMobile = ({ openDrawer }) => { |
| 118 | + const { data, loading, error, refetch } = useChannelPlatformsQuery(); |
| 119 | + |
| 120 | + if (loading) { |
| 121 | + return ( |
| 122 | + <div className="grid gap-2 overflow-hidden"> |
| 123 | + {Array(6) |
| 124 | + .fill(0) |
| 125 | + .map((_, index) => ( |
| 126 | + <Skeleton key={index} className="h-8 w-full rounded-lg" /> |
| 127 | + ))} |
| 128 | + </div> |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + if (error) return <p>Error loading platforms: {error.message}</p>; |
| 133 | + |
| 134 | + return ( |
| 135 | + <DropdownMenu> |
| 136 | + <DropdownMenuTrigger asChild> |
| 137 | + {data.items.length && ( |
| 138 | + <Button |
| 139 | + variant="secondary" |
| 140 | + className="rounded-r-none flex items-center gap-3" |
| 141 | + > |
| 142 | + Platforms |
| 143 | + <Badge |
| 144 | + color="teal" |
| 145 | + className="rounded-sm border text-xs/tight py-[1px] px-1.5" |
| 146 | + > |
| 147 | + {data.items.length} |
| 148 | + </Badge> |
| 149 | + </Button> |
| 150 | + )} |
| 151 | + </DropdownMenuTrigger> |
| 152 | + <CreatePlatform |
| 153 | + refetch={refetch} |
| 154 | + trigger={ |
| 155 | + data.items.length ? ( |
| 156 | + <Button |
| 157 | + variant="secondary" |
| 158 | + className="border-l-0 shadow-sm px-1.5 rounded-l-none flex items-center" |
| 159 | + > |
| 160 | + <Badge |
| 161 | + color="blue" |
| 162 | + className="rounded-sm border text-[.7rem] py-0 px-0" |
| 163 | + > |
| 164 | + <Plus className="size-4 p-0.5" /> |
| 165 | + </Badge> |
| 166 | + </Button> |
| 167 | + ) : ( |
| 168 | + <Button |
| 169 | + variant="secondary" |
| 170 | + className="h-full flex items-center gap-3" |
| 171 | + > |
| 172 | + Platform |
| 173 | + <Badge |
| 174 | + color="blue" |
| 175 | + className="rounded-sm border text-[.7rem] py-0 px-0" |
| 176 | + > |
| 177 | + <Plus className="size-4 p-0.5" /> |
| 178 | + </Badge> |
| 179 | + </Button> |
| 180 | + ) |
| 181 | + } |
| 182 | + /> |
| 183 | + <DropdownMenuPortal> |
| 184 | + <DropdownMenuContent className="w-40 origin-top-right shadow-2xl ring-1 ring-black ring-opacity-5 focus:outline-none"> |
| 185 | + {data.items.map((platform) => ( |
| 186 | + <DropdownMenuItem |
| 187 | + key={platform.id} |
| 188 | + onClick={() => openDrawer(platform.id, "ChannelPlatform")} |
| 189 | + className="block w-full text-left px-4 py-2 text-sm" |
| 190 | + > |
| 191 | + {platform.name} |
| 192 | + </DropdownMenuItem> |
| 193 | + ))} |
| 194 | + </DropdownMenuContent> |
| 195 | + </DropdownMenuPortal> |
| 196 | + </DropdownMenu> |
| 197 | + ); |
| 198 | +}; |
0 commit comments