@@ -4,11 +4,11 @@ import type { Video } from "@cap/web-domain";
44import { useQuery } from "@tanstack/react-query" ;
55import { Effect , Exit } from "effect" ;
66import { useRouter } from "next/navigation" ;
7- import { useRef , useState } from "react" ;
7+ import { useMemo , useRef , useState } from "react" ;
88import { toast } from "sonner" ;
99import { useDashboardContext } from "@/app/(org)/dashboard/Contexts" ;
1010import { useEffectMutation } from "@/lib/EffectRuntime" ;
11- import { Rpc } from "@/lib/Rpcs" ;
11+ import { Rpc , withRpc } from "@/lib/Rpcs" ;
1212import type { VideoData } from "../../../caps/Caps" ;
1313import { CapCard } from "../../../caps/components/CapCard/CapCard" ;
1414import { SelectedCapsBar } from "../../../caps/components/SelectedCapsBar" ;
@@ -28,12 +28,12 @@ export default function FolderVideosSection({
2828} : FolderVideosSectionProps ) {
2929 const router = useRouter ( ) ;
3030 const { isUploading, uploadingCapId } = useUploadingContext ( ) ;
31- const { activeOrganization , user } = useDashboardContext ( ) ;
31+ const { user } = useDashboardContext ( ) ;
3232
3333 const [ selectedCaps , setSelectedCaps ] = useState < Video . VideoId [ ] > ( [ ] ) ;
3434 const previousCountRef = useRef < number > ( 0 ) ;
3535
36- const deleteCaps = useEffectMutation ( {
36+ const { mutate : deleteCaps , isPending : isDeletingCaps } = useEffectMutation ( {
3737 mutationFn : Effect . fn ( function * ( ids : Video . VideoId [ ] ) {
3838 if ( ids . length === 0 ) return ;
3939
@@ -63,9 +63,7 @@ export default function FolderVideosSection({
6363 } ) . pipe ( Effect . fork ) ;
6464
6565 toast . promise ( Effect . runPromise ( fiber . await . pipe ( Effect . flatten ) ) , {
66- loading : `Deleting ${ selectedCaps . length } cap${
67- selectedCaps . length === 1 ? "" : "s"
68- } ...`,
66+ loading : `Deleting ${ ids . length } cap${ ids . length === 1 ? "" : "s" } ...` ,
6967 success : ( data ) => {
7068 if ( data . error ) {
7169 return `Successfully deleted ${ data . success } cap${
@@ -90,6 +88,17 @@ export default function FolderVideosSection({
9088 } ,
9189 } ) ;
9290
91+ const { mutate : deleteCap , isPending : isDeletingCap } = useEffectMutation ( {
92+ mutationFn : ( id : Video . VideoId ) => withRpc ( ( r ) => r . VideoDelete ( id ) ) ,
93+ onSuccess : ( ) => {
94+ toast . success ( "Cap deleted successfully" ) ;
95+ router . refresh ( ) ;
96+ } ,
97+ onError : ( ) => {
98+ toast . error ( "Failed to delete cap" ) ;
99+ } ,
100+ } ) ;
101+
93102 const handleCapSelection = ( capId : Video . VideoId ) => {
94103 setSelectedCaps ( ( prev ) => {
95104 const newSelection = prev . includes ( capId )
@@ -146,6 +155,14 @@ export default function FolderVideosSection({
146155 refetchOnMount : true ,
147156 } ) ;
148157
158+ const visibleVideos = useMemo (
159+ ( ) =>
160+ isUploading && uploadingCapId
161+ ? initialVideos . filter ( ( video ) => video . id !== uploadingCapId )
162+ : initialVideos ,
163+ [ initialVideos , isUploading , uploadingCapId ] ,
164+ ) ;
165+
149166 const analytics = analyticsData || { } ;
150167
151168 return (
@@ -154,7 +171,7 @@ export default function FolderVideosSection({
154171 < h1 className = "text-2xl font-medium text-gray-12" > Videos</ h1 >
155172 </ div >
156173 < div className = "grid grid-cols-1 gap-4 sm:gap-6 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 2xl:grid-cols-5" >
157- { initialVideos . length === 0 && ! isUploading ? (
174+ { visibleVideos . length === 0 && ! isUploading ? (
158175 < p className = "col-span-full text-gray-9" >
159176 No videos in this folder yet. Drag and drop into the folder or
160177 upload.
@@ -164,30 +181,34 @@ export default function FolderVideosSection({
164181 { isUploading && (
165182 < UploadPlaceholderCard key = { "upload-placeholder" } />
166183 ) }
167- { initialVideos
168- . filter ( ( cap ) => ! isUploading || cap . id !== uploadingCapId )
169- . map ( ( video ) => (
170- < CapCard
171- key = { video . id }
172- cap = { video }
173- analytics = { analytics [ video . id ] || 0 }
174- userId = { user ?. id }
175- isLoadingAnalytics = { isLoadingAnalytics }
176- isSelected = { selectedCaps . includes ( video . id ) }
177- anyCapSelected = { selectedCaps . length > 0 }
178- isDeleting = { deleteCaps . isPending }
179- onSelectToggle = { ( ) => handleCapSelection ( video . id ) }
180- onDelete = { ( ) => deleteCaps . mutateAsync ( selectedCaps ) }
181- />
182- ) ) }
184+ { visibleVideos . map ( ( video ) => (
185+ < CapCard
186+ key = { video . id }
187+ cap = { video }
188+ analytics = { analytics [ video . id ] || 0 }
189+ userId = { user ?. id }
190+ isLoadingAnalytics = { isLoadingAnalytics }
191+ isSelected = { selectedCaps . includes ( video . id ) }
192+ anyCapSelected = { selectedCaps . length > 0 }
193+ isDeleting = { isDeletingCaps || isDeletingCap }
194+ onSelectToggle = { ( ) => handleCapSelection ( video . id ) }
195+ onDelete = { ( ) => {
196+ if ( selectedCaps . length > 0 ) {
197+ deleteCaps ( selectedCaps ) ;
198+ } else {
199+ deleteCap ( video . id ) ;
200+ }
201+ } }
202+ />
203+ ) ) }
183204 </ >
184205 ) }
185206 </ div >
186207 < SelectedCapsBar
187208 selectedCaps = { selectedCaps }
188209 setSelectedCaps = { setSelectedCaps }
189- deleteSelectedCaps = { ( ) => deleteCaps . mutateAsync ( selectedCaps ) }
190- isDeleting = { deleteCaps . isPending }
210+ deleteSelectedCaps = { ( ) => deleteCaps ( selectedCaps ) }
211+ isDeleting = { isDeletingCaps || isDeletingCap }
191212 />
192213 </ >
193214 ) ;
0 commit comments