mirror of
https://github.com/filecoin-project/slate.git
synced 2024-11-26 13:45:30 +03:00
23 lines
593 B
JavaScript
23 lines
593 B
JavaScript
import * as Data from "~/node_common/data";
|
|
import * as Strings from "~/common/strings";
|
|
import * as Numbers from "~/common/numbers";
|
|
|
|
export const getRandomSlateElementURL = async ({ id, fallback = "" }) => {
|
|
if (Strings.isEmpty(id)) {
|
|
return fallback;
|
|
}
|
|
|
|
const query = await Data.getSlateById({ id, includeFiles: true, sanitize: true });
|
|
|
|
if (!query || query.error) {
|
|
return fallback;
|
|
}
|
|
|
|
if (!query.objects.length) {
|
|
return fallback;
|
|
}
|
|
|
|
const index = Numbers.getRandomInt(0, query.objects.length - 1);
|
|
return Strings.getURLfromCID(query.objects[index].cid);
|
|
};
|