This repository has no description
1import { logoutUser, verifySessionOnClient } from '@/lib/auth/dal';
2import { createSembleClient } from '@/services/client.apiClient';
3import {
4 CardSortField,
5 CollectionSortField,
6 SortOrder,
7 UrlType,
8} from '@semble/types';
9import { cache } from 'react';
10
11interface PageParams {
12 page?: number;
13 limit?: number;
14 cardSortBy?: CardSortField;
15 collectionSortBy?: CollectionSortField;
16}
17
18interface SearchParams {
19 sortBy?: string;
20 sortOrder?: SortOrder;
21 searchText?: string;
22 urlType?: UrlType;
23 handleOrDid?: string;
24}
25
26export const getCollectionsForUrl = cache(
27 async (url: string, params?: PageParams) => {
28 const client = createSembleClient();
29 const response = await client.getCollectionsForUrl({
30 url,
31 page: params?.page,
32 limit: params?.limit,
33 sortBy: params?.collectionSortBy,
34 });
35
36 return response;
37 },
38);
39
40export const getCollections = cache(
41 async (handleOrDid: string, params?: PageParams & SearchParams) => {
42 const client = createSembleClient();
43 const response = await client.getCollections({
44 identifier: handleOrDid,
45 limit: params?.limit,
46 page: params?.page,
47 sortBy: params?.collectionSortBy,
48 searchText: params?.searchText,
49 });
50
51 // Temp fix: filter out collections without uri
52 return {
53 ...response,
54 collections: response.collections.filter(
55 (collection) => collection.uri !== undefined,
56 ),
57 };
58 },
59);
60
61export const getMyCollections = cache(
62 async (params?: PageParams & SearchParams) => {
63 const session = await verifySessionOnClient({ redirectOnFail: true });
64 if (!session) throw new Error('No session found');
65 const client = createSembleClient();
66 const response = await client.getMyCollections({
67 page: params?.page,
68 limit: params?.limit,
69 sortBy: params?.collectionSortBy,
70 sortOrder: params?.sortOrder,
71 searchText: params?.searchText,
72 });
73
74 // Temp fix: filter out collections without uri
75 return {
76 ...response,
77 collections: response.collections.filter(
78 (collection) => collection.uri !== undefined,
79 ),
80 };
81 },
82);
83
84export const getMyGemCollections = cache(
85 async (params?: PageParams & SearchParams) => {
86 const client = createSembleClient();
87 const response = await client.getMyCollections({
88 page: params?.page,
89 limit: params?.limit,
90 sortBy: params?.collectionSortBy,
91 sortOrder: params?.sortOrder,
92 searchText: params?.searchText,
93 });
94
95 // Temp fix: filter out collections without uri
96 return {
97 ...response,
98 collections: response.collections.filter(
99 (collection) => collection.uri !== undefined,
100 ),
101 };
102 },
103);
104
105export const createCollection = cache(
106 async (newCollection: { name: string; description: string }) => {
107 const session = await verifySessionOnClient({ redirectOnFail: true });
108 if (!session) throw new Error('No session found');
109 const client = createSembleClient();
110
111 try {
112 const response = await client.createCollection(newCollection);
113
114 return response;
115 } catch (error) {
116 await logoutUser();
117 }
118 },
119);
120
121export const deleteCollection = cache(async (id: string) => {
122 const session = await verifySessionOnClient({ redirectOnFail: true });
123 if (!session) throw new Error('No session found');
124 const client = createSembleClient();
125
126 try {
127 const response = await client.deleteCollection({ collectionId: id });
128
129 return response;
130 } catch (error) {
131 await logoutUser();
132 }
133});
134
135export const updateCollection = cache(
136 async (collection: {
137 collectionId: string;
138 rkey: string;
139 name: string;
140 description?: string;
141 }) => {
142 const session = await verifySessionOnClient({ redirectOnFail: true });
143 if (!session) throw new Error('No session found');
144 const client = createSembleClient();
145
146 try {
147 const response = await client.updateCollection(collection);
148
149 return response;
150 } catch (error) {
151 await logoutUser();
152 }
153 },
154);
155
156export const getCollectionPageByAtUri = cache(
157 async ({
158 recordKey,
159 handle,
160 params,
161 }: {
162 recordKey: string;
163 handle: string;
164 params?: PageParams & SearchParams;
165 }) => {
166 const client = createSembleClient();
167 const response = await client.getCollectionPageByAtUri({
168 recordKey,
169 handle,
170 page: params?.page,
171 limit: params?.limit,
172 sortBy: params?.cardSortBy,
173 sortOrder: params?.sortOrder,
174 urlType: params?.urlType,
175 });
176
177 return response;
178 },
179);
180
181export const searchCollections = cache(
182 async (params?: PageParams & SearchParams) => {
183 const client = createSembleClient();
184 const response = await client.searchCollections({
185 page: params?.page,
186 limit: params?.limit,
187 sortBy: params?.collectionSortBy,
188 sortOrder: params?.sortOrder,
189 searchText: params?.searchText,
190 identifier: params?.handleOrDid,
191 });
192
193 // Temp fix: filter out collections without uri
194 return {
195 ...response,
196 collections: response.collections.filter(
197 (collection) => collection.uri !== undefined,
198 ),
199 };
200 },
201);