mirror of
https://github.com/hzrd149/nostrudel.git
synced 2025-09-26 19:47:25 +02:00
add banner size when editing community
This commit is contained in:
@@ -1,32 +1,14 @@
|
|||||||
import { useEffect, useMemo, useState } from "react";
|
import { useEffect, useMemo, useState } from "react";
|
||||||
import { Photo, PhotoAlbum, PhotoAlbumProps } from "react-photo-album";
|
import { Photo, PhotoAlbum, PhotoAlbumProps } from "react-photo-album";
|
||||||
|
import { ImageSize, getImageSize } from "../helpers/image";
|
||||||
type Size = { width: number; height: number };
|
|
||||||
const imageSizeCache = new Map<string, Size>();
|
|
||||||
function getImageSize(src: string): Promise<{ width: number; height: number }> {
|
|
||||||
const cached = imageSizeCache.get(src);
|
|
||||||
if (cached) return Promise.resolve(cached);
|
|
||||||
|
|
||||||
return new Promise((res, rej) => {
|
|
||||||
const image = new Image();
|
|
||||||
image.src = src;
|
|
||||||
|
|
||||||
image.onload = () => {
|
|
||||||
const size = { width: image.width, height: image.height };
|
|
||||||
imageSizeCache.set(src, size);
|
|
||||||
res(size);
|
|
||||||
};
|
|
||||||
image.onerror = (err) => rej(err);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export type PhotoWithoutSize = Omit<Photo, "width" | "height"> & { width?: number; height?: number };
|
export type PhotoWithoutSize = Omit<Photo, "width" | "height"> & { width?: number; height?: number };
|
||||||
|
|
||||||
export default function PhotoGallery<T extends PhotoWithoutSize>({
|
export default function PhotoGallery<T extends PhotoWithoutSize>({
|
||||||
photos,
|
photos,
|
||||||
...props
|
...props
|
||||||
}: Omit<PhotoAlbumProps<T & Size>, "photos"> & { photos: PhotoWithoutSize[] }) {
|
}: Omit<PhotoAlbumProps<T & ImageSize>, "photos"> & { photos: PhotoWithoutSize[] }) {
|
||||||
const [loadedSizes, setLoadedSizes] = useState<Record<string, Size>>({});
|
const [loadedSizes, setLoadedSizes] = useState<Record<string, ImageSize>>({});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
for (const photo of photos) {
|
for (const photo of photos) {
|
||||||
@@ -40,17 +22,17 @@ export default function PhotoGallery<T extends PhotoWithoutSize>({
|
|||||||
}, [photos]);
|
}, [photos]);
|
||||||
|
|
||||||
const loadedPhotos = useMemo(() => {
|
const loadedPhotos = useMemo(() => {
|
||||||
const loaded: (T & Size)[] = [];
|
const loaded: (T & ImageSize)[] = [];
|
||||||
|
|
||||||
for (const photo of photos) {
|
for (const photo of photos) {
|
||||||
if (photo.width && photo.height) {
|
if (photo.width && photo.height) {
|
||||||
loaded.push(photo as T & Size);
|
loaded.push(photo as T & ImageSize);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const loadedImage = loadedSizes[photo.src];
|
const loadedImage = loadedSizes[photo.src];
|
||||||
if (loadedImage) {
|
if (loadedImage) {
|
||||||
loaded.push({ ...photo, width: loadedImage.width, height: loadedImage.height } as T & Size);
|
loaded.push({ ...photo, width: loadedImage.width, height: loadedImage.height } as T & ImageSize);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -58,5 +40,5 @@ export default function PhotoGallery<T extends PhotoWithoutSize>({
|
|||||||
return loaded;
|
return loaded;
|
||||||
}, [loadedSizes, photos]);
|
}, [loadedSizes, photos]);
|
||||||
|
|
||||||
return <PhotoAlbum<T & Size> photos={loadedPhotos} {...props} />;
|
return <PhotoAlbum<T & ImageSize> photos={loadedPhotos} {...props} />;
|
||||||
}
|
}
|
||||||
|
19
src/helpers/image.ts
Normal file
19
src/helpers/image.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
export type ImageSize = { width: number; height: number };
|
||||||
|
const imageSizeCache = new Map<string, ImageSize>();
|
||||||
|
|
||||||
|
export function getImageSize(src: string): Promise<{ width: number; height: number }> {
|
||||||
|
const cached = imageSizeCache.get(src);
|
||||||
|
if (cached) return Promise.resolve(cached);
|
||||||
|
|
||||||
|
return new Promise((res, rej) => {
|
||||||
|
const image = new Image();
|
||||||
|
image.src = src;
|
||||||
|
|
||||||
|
image.onload = () => {
|
||||||
|
const size = { width: image.width, height: image.height };
|
||||||
|
imageSizeCache.set(src, size);
|
||||||
|
res(size);
|
||||||
|
};
|
||||||
|
image.onerror = () => rej(new Error("Failed to get image size"));
|
||||||
|
});
|
||||||
|
}
|
@@ -16,6 +16,7 @@ import NostrPublishAction from "../../classes/nostr-publish-action";
|
|||||||
import { unique } from "../../helpers/array";
|
import { unique } from "../../helpers/array";
|
||||||
import clientRelaysService from "../../services/client-relays";
|
import clientRelaysService from "../../services/client-relays";
|
||||||
import replaceableEventLoaderService from "../../services/replaceable-event-requester";
|
import replaceableEventLoaderService from "../../services/replaceable-event-requester";
|
||||||
|
import { getImageSize } from "../../helpers/image";
|
||||||
|
|
||||||
function CommunitiesHomePage() {
|
function CommunitiesHomePage() {
|
||||||
const toast = useToast();
|
const toast = useToast();
|
||||||
@@ -42,7 +43,14 @@ function CommunitiesHomePage() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (values.description) draft.tags.push(["description", values.description]);
|
if (values.description) draft.tags.push(["description", values.description]);
|
||||||
if (values.banner) draft.tags.push(["image", values.banner]);
|
if (values.banner) {
|
||||||
|
try {
|
||||||
|
const size = await getImageSize(values.banner);
|
||||||
|
draft.tags.push(["image", values.banner, `${size.width}x${size.height}`]);
|
||||||
|
} catch (e) {
|
||||||
|
draft.tags.push(["image", values.banner]);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (values.ranking) draft.tags.push(["rank_mode", values.ranking]);
|
if (values.ranking) draft.tags.push(["rank_mode", values.ranking]);
|
||||||
|
|
||||||
const signed = await requestSignature(draft);
|
const signed = await requestSignature(draft);
|
||||||
|
@@ -135,7 +135,9 @@ export default function CommunityHomePage({ community }: { community: NostrEvent
|
|||||||
</Flex>
|
</Flex>
|
||||||
</VerticalPageLayout>
|
</VerticalPageLayout>
|
||||||
</AdditionalRelayProvider>
|
</AdditionalRelayProvider>
|
||||||
{editModal && <CommunityEditModal isOpen={editModal.isOpen} onClose={editModal.onClose} community={community} />}
|
{editModal.isOpen && (
|
||||||
|
<CommunityEditModal isOpen={editModal.isOpen} onClose={editModal.onClose} community={community} />
|
||||||
|
)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@@ -19,6 +19,7 @@ import NostrPublishAction from "../../../classes/nostr-publish-action";
|
|||||||
import clientRelaysService from "../../../services/client-relays";
|
import clientRelaysService from "../../../services/client-relays";
|
||||||
import { unique } from "../../../helpers/array";
|
import { unique } from "../../../helpers/array";
|
||||||
import replaceableEventLoaderService from "../../../services/replaceable-event-requester";
|
import replaceableEventLoaderService from "../../../services/replaceable-event-requester";
|
||||||
|
import { getImageSize } from "../../../helpers/image";
|
||||||
|
|
||||||
export default function CommunityEditModal({
|
export default function CommunityEditModal({
|
||||||
isOpen,
|
isOpen,
|
||||||
@@ -59,7 +60,14 @@ export default function CommunityEditModal({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (values.description) draft.tags.push(["description", values.description]);
|
if (values.description) draft.tags.push(["description", values.description]);
|
||||||
if (values.banner) draft.tags.push(["image", values.banner]);
|
if (values.banner) {
|
||||||
|
try {
|
||||||
|
const size = await getImageSize(values.banner);
|
||||||
|
draft.tags.push(["image", values.banner, `${size.width}x${size.height}`]);
|
||||||
|
} catch (e) {
|
||||||
|
draft.tags.push(["image", values.banner]);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (values.ranking) draft.tags.push(["rank_mode", values.ranking]);
|
if (values.ranking) draft.tags.push(["rank_mode", values.ranking]);
|
||||||
|
|
||||||
const signed = await requestSignature(draft);
|
const signed = await requestSignature(draft);
|
||||||
@@ -73,6 +81,8 @@ export default function CommunityEditModal({
|
|||||||
|
|
||||||
onClose();
|
onClose();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
|
||||||
if (e instanceof Error) toast({ description: e.message, status: "error" });
|
if (e instanceof Error) toast({ description: e.message, status: "error" });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user