import { ActivityIndicator, Icon } from '@ant-design/react-native'; import { clsx } from 'clsx'; import { Link } from 'expo-router'; import React, { useEffect, useState } from 'react'; import { Pressable, Text, View } from 'react-native'; import { Colors } from '@/constants/theme'; import { isPromise } from '@/utils/tsutils'; import type {IconNames} from '@ant-design/react-native/lib/icon'; import type { Href} from 'expo-router'; import type { ViewStyle } from 'react-native'; interface UIButtonProps { children?: string | React.ReactNode; title?: string | React.ReactNode; onPress?: () => PromiseLike | unknown; disabled?: boolean; className?: string; textClassName?: string; href?: Href; type?: 'primary' | 'second' | 'link'; icon?: IconNames | React.ReactNode; style?: ViewStyle; loading?: boolean; } type ButtonType = UIButtonProps['type']; function getIconColor(type: ButtonType, disabled: boolean) { if (disabled) return Colors['on-surface']['variant']; if (type === 'primary') return '#fff'; if (type === 'second') return Colors.secondary.DEFAULT; return Colors.primary.DEFAULT; } function ButtonLabel({ type, disabled, textClassName, children, loading, }: { type: ButtonType; disabled: boolean; textClassName?: string; children: React.ReactNode; loading: boolean; }) { const labelClass = clsx(textClassName, 'text-sm font-semibold', { 'text-on-primary': !disabled && type === 'primary', 'text-primary': !disabled && type !== 'primary' && type !== 'second', 'text-secondary': !disabled && type === 'second', 'text-on-surface-variant/65 font-normal': disabled, }); if (!loading) return {children}; return ( {children} ); } export default function UIButton({ onPress, href, type, icon, className, style, disabled, children, title, textClassName, loading, }: UIButtonProps) { const [busy, setBusy] = useState(loading); const isDisabled = Boolean(disabled || busy); useEffect(() => { setBusy(loading); }, [loading]); const label = children ?? title; const handlePress = () => { if (isDisabled) return; const res = onPress?.(); if (isPromise(res)) { setBusy(true); (res as Promise).finally(() => setBusy(false)); } }; const inner = ( {typeof icon === 'string' ? ( ) : ( icon )} {typeof label === 'string' ? ( {label} ) : ( label )} ); if (typeof href !== 'undefined') { return ( {inner} ); } return inner; }