Browse Source

fix relay selection component

imwald
Silberengel 5 months ago
parent
commit
0eed88ef1d
  1. 116
      src/components/PostEditor/PostRelaySelector.tsx
  2. 4
      src/components/PostEditor/index.tsx

116
src/components/PostEditor/PostRelaySelector.tsx

@ -3,12 +3,15 @@ import { useCurrentRelays } from '@/providers/CurrentRelaysProvider'
import { useFavoriteRelays } from '@/providers/FavoriteRelaysProvider' import { useFavoriteRelays } from '@/providers/FavoriteRelaysProvider'
import { useScreenSize } from '@/providers/ScreenSizeProvider' import { useScreenSize } from '@/providers/ScreenSizeProvider'
import { useNostr } from '@/providers/NostrProvider' import { useNostr } from '@/providers/NostrProvider'
import { Check } from 'lucide-react' import { Check, ChevronDown, Server } from 'lucide-react'
import { NostrEvent } from 'nostr-tools' import { NostrEvent } from 'nostr-tools'
import { Dispatch, SetStateAction, useCallback, useEffect, useState, useMemo } from 'react' import { Dispatch, SetStateAction, useCallback, useEffect, useState, useMemo } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import RelayIcon from '../RelayIcon' import RelayIcon from '../RelayIcon'
import relaySelectionService from '@/services/relay-selection.service' import relaySelectionService from '@/services/relay-selection.service'
import { Button } from '@/components/ui/button'
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
import { Sheet, SheetContent, SheetTrigger } from '@/components/ui/sheet'
export default function PostRelaySelector({ export default function PostRelaySelector({
parentEvent: _parentEvent, parentEvent: _parentEvent,
@ -212,9 +215,9 @@ export default function PostRelaySelector({
}, []) }, [])
const content = ( const content = (
<div className="space-y-2"> <>
{selectableRelays.length > 0 && ( {selectableRelays.length > 0 && (
<div className="flex gap-2"> <div className="flex gap-2 mb-2">
<button <button
onClick={handleSelectAll} onClick={handleSelectAll}
className="text-xs text-muted-foreground hover:text-foreground" className="text-xs text-muted-foreground hover:text-foreground"
@ -230,18 +233,18 @@ export default function PostRelaySelector({
</div> </div>
)} )}
<div className="max-h-48 overflow-y-auto space-y-1"> {isLoading ? (
{isLoading ? ( <div className="text-sm text-muted-foreground p-2">{t('Loading relays...')}</div>
<div className="text-sm text-muted-foreground p-2">{t('Loading relays...')}</div> ) : selectableRelays.length === 0 ? (
) : selectableRelays.length === 0 ? ( <div className="text-sm text-muted-foreground p-2">{t('No relays available')}</div>
<div className="text-sm text-muted-foreground p-2">{t('No relays available')}</div> ) : (
) : ( <div className="space-y-1">
selectableRelays.map((url) => { {selectableRelays.map((url) => {
const isChecked = selectedRelayUrls.includes(url) const isChecked = selectedRelayUrls.includes(url)
return ( return (
<div <div
key={url} key={url}
className="flex items-center gap-2 p-2 hover:bg-accent rounded cursor-pointer" className="flex items-center gap-2 p-2 hover:bg-accent rounded cursor-pointer touch-manipulation"
onClick={() => handleRelayCheckedChange(!isChecked, url)} onClick={() => handleRelayCheckedChange(!isChecked, url)}
> >
<div className="flex items-center justify-center w-4 h-4 border border-border rounded"> <div className="flex items-center justify-center w-4 h-4 border border-border rounded">
@ -251,38 +254,83 @@ export default function PostRelaySelector({
<span className="text-sm flex-1 truncate">{simplifyUrl(url)}</span> <span className="text-sm flex-1 truncate">{simplifyUrl(url)}</span>
</div> </div>
) )
}) })}
)} </div>
</div> )}
</div> </>
) )
// Create compact trigger button text
const triggerText = useMemo(() => {
if (isLoading) return t('Loading...')
if (selectedRelayUrls.length === 0) return t('Select relays')
if (selectedRelayUrls.length === 1) return simplifyUrl(selectedRelayUrls[0])
return t('{{count}} relays', { count: selectedRelayUrls.length })
}, [selectedRelayUrls, isLoading, t])
if (isSmallScreen) { if (isSmallScreen) {
return ( return (
<div className="space-y-2"> <div className="flex items-center gap-2">
<div className="flex items-center justify-between"> <span className="text-sm font-medium">{t('Post to')}</span>
<span className="text-sm font-medium">{t('Post to')}</span> <Sheet>
<span className="text-sm text-muted-foreground">{description}</span> <SheetTrigger asChild>
</div> <Button
variant="outline"
{/* Drawer implementation would go here */} size="sm"
<div className="border border-border rounded p-2"> className="h-8 px-3 text-xs justify-between min-w-0 flex-1"
{content} >
</div> <div className="flex items-center gap-2 min-w-0">
<Server className="w-3 h-3 shrink-0" />
<span className="truncate">{triggerText}</span>
</div>
<ChevronDown className="w-3 h-3 shrink-0" />
</Button>
</SheetTrigger>
<SheetContent side="bottom" className="h-[60vh] p-0">
<div className="flex flex-col h-full">
<div className="p-4 border-b flex items-center justify-between shrink-0 pr-12">
<div className="flex flex-col min-w-0 flex-1">
<span className="text-lg font-medium">{t('Select relays')}</span>
<span className="text-sm text-muted-foreground truncate">{description}</span>
</div>
</div>
<div className="flex-1 overflow-y-auto p-4">
{content}
</div>
</div>
</SheetContent>
</Sheet>
</div> </div>
) )
} }
return ( return (
<div className="space-y-2"> <div className="flex items-center gap-2">
<div className="flex items-center justify-between"> <span className="text-sm font-medium">{t('Post to')}</span>
<span className="text-sm font-medium">{t('Post to')}</span> <Popover>
<span className="text-sm text-muted-foreground">{description}</span> <PopoverTrigger asChild>
</div> <Button
variant="outline"
<div className="border border-border rounded p-2"> size="sm"
{content} className="h-8 px-3 text-xs justify-between min-w-0 flex-1"
</div> >
<div className="flex items-center gap-2 min-w-0">
<Server className="w-3 h-3 shrink-0" />
<span className="truncate">{triggerText}</span>
</div>
<ChevronDown className="w-3 h-3 shrink-0" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-[90vw] max-w-md p-0 max-h-[40vh] flex flex-col" align="start" side="bottom" sideOffset={8}>
<div className="p-3 border-b flex items-center justify-between shrink-0">
<span className="text-sm font-medium">{t('Select relays')}</span>
<span className="text-xs text-muted-foreground truncate ml-2">{description}</span>
</div>
<div className="p-3 overflow-y-auto overscroll-contain touch-pan-y max-h-[30vh] -webkit-overflow-scrolling-touch">
{content}
</div>
</PopoverContent>
</Popover>
</div> </div>
) )
} }

4
src/components/PostEditor/index.tsx

@ -58,7 +58,7 @@ export default function PostEditor({
} }
}} }}
> >
<ScrollArea className="px-4 h-full max-h-screen"> <ScrollArea className="px-4 h-full max-h-screen" scrollBarClassName="opacity-100">
<div className="space-y-4 px-2 py-6"> <div className="space-y-4 px-2 py-6">
<SheetHeader> <SheetHeader>
<SheetDescription className="hidden" /> <SheetDescription className="hidden" />
@ -83,7 +83,7 @@ export default function PostEditor({
} }
}} }}
> >
<ScrollArea className="px-4 h-full max-h-screen"> <ScrollArea className="px-4 h-full max-h-screen" scrollBarClassName="opacity-100">
<div className="space-y-4 px-2 py-6"> <div className="space-y-4 px-2 py-6">
<DialogHeader className="sr-only"> <DialogHeader className="sr-only">
<DialogTitle>Post Editor</DialogTitle> <DialogTitle>Post Editor</DialogTitle>

Loading…
Cancel
Save