PlatformSelector.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import React from 'react';
  2. import { useQuery } from '@tanstack/react-query';
  3. import {
  4. Select,
  5. SelectContent,
  6. SelectItem,
  7. SelectTrigger,
  8. SelectValue,
  9. } from '@d8d/shared-ui-components/components/ui/select';
  10. import { platformClientManager } from '../api/platformClient';
  11. interface PlatformSelectorProps {
  12. value?: number;
  13. onChange?: (value: number) => void;
  14. placeholder?: string;
  15. disabled?: boolean;
  16. className?: string;
  17. 'data-testid'?: string;
  18. }
  19. export const PlatformSelector: React.FC<PlatformSelectorProps> = ({
  20. value,
  21. onChange,
  22. placeholder = "请选择平台",
  23. disabled = false,
  24. className,
  25. 'data-testid': testId,
  26. }) => {
  27. const {
  28. data: platforms,
  29. isLoading,
  30. isError,
  31. } = useQuery({
  32. queryKey: ['platforms'],
  33. queryFn: async () => {
  34. const client = platformClientManager.get()
  35. const res = await client.getAllPlatforms.$get({
  36. query: {
  37. skip: 0,
  38. take: 100
  39. }
  40. });
  41. if (res.status !== 200) throw new Error('获取平台列表失败');
  42. return await res.json();
  43. },
  44. });
  45. if (isError) {
  46. return (
  47. <div className="text-sm text-destructive">
  48. 加载平台列表失败
  49. </div>
  50. );
  51. }
  52. const platformList = platforms?.data || [];
  53. return (
  54. <Select
  55. value={value?.toString()}
  56. onValueChange={(val) => onChange?.(parseInt(val))}
  57. disabled={disabled || isLoading || platformList.length === 0}
  58. >
  59. <SelectTrigger className={className} data-testid={testId}>
  60. <SelectValue placeholder={isLoading ? '加载中...' : placeholder} />
  61. </SelectTrigger>
  62. <SelectContent>
  63. {platformList.map((platform) => (
  64. <SelectItem key={platform.id} value={platform.id.toString()}>
  65. {platform.platformName}
  66. </SelectItem>
  67. ))}
  68. </SelectContent>
  69. </Select>
  70. );
  71. };
  72. export default PlatformSelector;