@@ -15,7 +15,7 @@ import { serverEnv } from "@cap/env";
1515import { Database , ImageUploads } from "@cap/web-backend" ;
1616import { type ImageUpload , Video } from "@cap/web-domain" ;
1717import { and , count , desc , eq , inArray , isNull , sql } from "drizzle-orm" ;
18- import { Array , Effect } from "effect" ;
18+ import { type Array , Effect } from "effect" ;
1919import type { Metadata } from "next" ;
2020import { redirect } from "next/navigation" ;
2121import { runPromise } from "@/lib/server" ;
@@ -32,41 +32,21 @@ const getSharedSpacesForVideos = Effect.fn(function* (
3232 if ( videoIds . length === 0 ) return { } ;
3333
3434 const db = yield * Database ;
35- const imageUploads = yield * ImageUploads ;
3635
3736 // Fetch space-level sharing
38- const spaceSharing = yield * db
39- . use ( ( db ) =>
40- db
41- . select ( {
42- videoId : spaceVideos . videoId ,
43- id : spaces . id ,
44- name : spaces . name ,
45- organizationId : spaces . organizationId ,
46- iconUrl : organizations . iconUrl ,
47- } )
48- . from ( spaceVideos )
49- . innerJoin ( spaces , eq ( spaceVideos . spaceId , spaces . id ) )
50- . innerJoin ( organizations , eq ( spaces . organizationId , organizations . id ) )
51- . where ( inArray ( spaceVideos . videoId , videoIds ) ) ,
52- )
53- . pipe (
54- Effect . map ( ( v ) =>
55- v . map (
56- Effect . fn ( function * ( v ) {
57- return {
58- ...v ,
59- iconUrl : v . iconUrl
60- ? yield * imageUploads . resolveImageUrl (
61- v . iconUrl as ImageUpload . ImageUrlOrKey ,
62- )
63- : null ,
64- } ;
65- } ) ,
66- ) ,
67- ) ,
68- Effect . flatMap ( Effect . all ) ,
69- ) ;
37+ const spaceSharing = yield * db . use ( ( db ) =>
38+ db
39+ . select ( {
40+ videoId : spaceVideos . videoId ,
41+ id : spaces . id ,
42+ name : spaces . name ,
43+ organizationId : spaces . organizationId ,
44+ } )
45+ . from ( spaceVideos )
46+ . innerJoin ( spaces , eq ( spaceVideos . spaceId , spaces . id ) )
47+ . innerJoin ( organizations , eq ( spaces . organizationId , organizations . id ) )
48+ . where ( inArray ( spaceVideos . videoId , videoIds ) ) ,
49+ ) ;
7050
7151 // Fetch organization-level sharing
7252 const orgSharing = yield * db . use ( ( db ) =>
@@ -93,7 +73,6 @@ const getSharedSpacesForVideos = Effect.fn(function* (
9373 id : string ;
9474 name : string ;
9575 organizationId : string ;
96- iconUrl : string ;
9776 isOrg : boolean ;
9877 } >
9978 > = { } ;
@@ -107,7 +86,6 @@ const getSharedSpacesForVideos = Effect.fn(function* (
10786 id : space . id ,
10887 name : space . name ,
10988 organizationId : space . organizationId ,
110- iconUrl : space . iconUrl || "" ,
11189 isOrg : false ,
11290 } ) ;
11391 } ) ;
@@ -121,7 +99,6 @@ const getSharedSpacesForVideos = Effect.fn(function* (
12199 id : org . id ,
122100 name : org . name ,
123101 organizationId : org . organizationId ,
124- iconUrl : org . iconUrl || "" ,
125102 isOrg : true ,
126103 } ) ;
127104 } ) ;
@@ -167,7 +144,13 @@ export default async function CapsPage(props: PageProps<"/dashboard/caps">) {
167144 public : videos . public ,
168145 totalComments : sql < number > `COUNT(DISTINCT CASE WHEN ${ comments . type } = 'text' THEN ${ comments . id } END)` ,
169146 totalReactions : sql < number > `COUNT(DISTINCT CASE WHEN ${ comments . type } = 'emoji' THEN ${ comments . id } END)` ,
170- sharedOrganizations : sql < { id : string ; name : string ; iconUrl : string } [ ] > `
147+ sharedOrganizations : sql <
148+ {
149+ id : string ;
150+ name : string ;
151+ iconUrl : ImageUpload . ImageUrlOrKey | null ;
152+ } [ ]
153+ > `
171154 COALESCE(
172155 JSON_ARRAYAGG(
173156 JSON_OBJECT(
@@ -247,31 +230,44 @@ export default async function CapsPage(props: PageProps<"/dashboard/caps">) {
247230 const sharedSpacesMap =
248231 await getSharedSpacesForVideos ( videoIds ) . pipe ( runPromise ) ;
249232
250- const processedVideoData = videoData . map ( ( video ) => {
251- const { effectiveDate, ...videoWithoutEffectiveDate } = video ;
233+ const processedVideoData = await Effect . all (
234+ videoData . map (
235+ Effect . fn ( function * ( video ) {
236+ const imageUploads = yield * ImageUploads ;
252237
253- return {
254- ...videoWithoutEffectiveDate ,
255- id : Video . VideoId . make ( video . id ) ,
256- foldersData,
257- settings : video . settings ,
258- sharedOrganizations : Array . isArray ( video . sharedOrganizations )
259- ? video . sharedOrganizations . filter (
260- ( organization ) => organization . id !== null ,
261- )
262- : [ ] ,
263- sharedSpaces : Array . isArray ( sharedSpacesMap [ video . id ] )
264- ? sharedSpacesMap [ video . id ]
265- : [ ] ,
266- ownerName : video . ownerName ?? "" ,
267- metadata : video . metadata as
268- | {
269- customCreatedAt ?: string ;
270- [ key : string ] : any ;
271- }
272- | undefined ,
273- } ;
274- } ) ;
238+ const { effectiveDate, ...videoWithoutEffectiveDate } = video ;
239+
240+ return {
241+ ...videoWithoutEffectiveDate ,
242+ id : Video . VideoId . make ( video . id ) ,
243+ foldersData,
244+ settings : video . settings ,
245+ sharedOrganizations : yield * Effect . all (
246+ ( video . sharedOrganizations ?? [ ] )
247+ . filter ( ( organization ) => organization . id !== null )
248+ . map (
249+ Effect . fn ( function * ( org ) {
250+ return {
251+ ...org ,
252+ iconUrl : org . iconUrl
253+ ? yield * imageUploads . resolveImageUrl ( org . iconUrl )
254+ : null ,
255+ } ;
256+ } ) ,
257+ ) ,
258+ ) ,
259+ sharedSpaces : sharedSpacesMap [ video . id ] ?? [ ] ,
260+ ownerName : video . ownerName ?? "" ,
261+ metadata : video . metadata as
262+ | {
263+ customCreatedAt ?: string ;
264+ [ key : string ] : any ;
265+ }
266+ | undefined ,
267+ } ;
268+ } ) ,
269+ ) ,
270+ ) . pipe ( runPromise ) ;
275271
276272 return (
277273 < Caps
0 commit comments