Skip to content

Commit

Permalink
Interop with v2 files
Browse files Browse the repository at this point in the history
  • Loading branch information
dabbott committed May 19, 2024
1 parent 298d09a commit 4e63104
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
4 changes: 2 additions & 2 deletions packages/noya-api/src/core/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export const noyaAssetSchema = z.object({
export const noyaFileDataSchema = z
.object({
name: z.string(),
type: z.literal('io.noya.ayon'),
schemaVersion: z.literal('0.1.0'),
type: z.string(),
schemaVersion: z.string(),
document: z.custom<SketchFile>(),
})
.passthrough();
Expand Down
49 changes: 34 additions & 15 deletions packages/site/src/components/Projects.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { formatDistance, parseISO } from 'date-fns';
import { useRouter } from 'next/router';
import { useNoyaClient, useNoyaFiles } from 'noya-api';
import { NoyaAPI, useNoyaClient, useNoyaFiles } from 'noya-api';
import {
Button,
createSectionedMenu,
Heading2,
ListView,
Small,
Spacer,
Stack,
createSectionedMenu,
} from 'noya-designsystem';
import Sketch from 'noya-file-format';
import { resize } from 'noya-geometry';
Expand Down Expand Up @@ -134,17 +134,7 @@ export function Projects() {
{sortedFiles
.filter((file) => file.id !== hideWhileNavigating)
.map((file) => {
const artboard = Layers.find<Sketch.Artboard>(
file.data.document.pages[0],
Layers.isArtboard,
);
const scaledThumbnailSize = artboard
? resize(artboard.frame, thumbnailSize, 'scaleAspectFit')
: thumbnailSize;
scaledThumbnailSize.width = Math.round(scaledThumbnailSize.width);
scaledThumbnailSize.height = Math.round(
scaledThumbnailSize.height,
);
const scaledThumbnailSize = getThumbnailSize(file);

return (
<ListView.Row
Expand Down Expand Up @@ -186,7 +176,11 @@ export function Projects() {
setHovered(hovered ? file.id : undefined);
}}
onPress={() => {
push(`/projects/${file.id}`);
if (file.data.type === 'io.noya.ayon') {
push(`/projects/${file.id}`);
} else {
window.location.href = `${NOYA_HOST}/v2/projects/${file.id}`;
}
}}
onMenuOpenChange={(open) => {
setSelected(open ? file.id : undefined);
Expand All @@ -200,7 +194,11 @@ export function Projects() {
flex="1"
>
<img
src={`${NOYA_HOST}/api/files/${file.id}/thumbnail.png?width=${scaledThumbnailSize.width}&height=${scaledThumbnailSize.height}&deviceScaleFactor=1`}
src={`${NOYA_HOST}/api/files/${file.id}/thumbnail.png?${
file.data.type === 'io.noya.ayon' ? '' : 'v2=1&'
}width=${scaledThumbnailSize.width}&height=${
scaledThumbnailSize.height
}&deviceScaleFactor=1`}
alt=""
style={{
...thumbnailSize,
Expand Down Expand Up @@ -251,3 +249,24 @@ export function Projects() {
</Stack.V>
);
}

function getThumbnailSize(file: NoyaAPI.File) {
if (file.data.type === 'io.noya.ayon') {
const artboard = Layers.find<Sketch.Artboard>(
file.data.document.pages[0],
Layers.isArtboard,
);
const scaledThumbnailSize = artboard
? resize(artboard.frame, thumbnailSize, 'scaleAspectFit')
: thumbnailSize;
scaledThumbnailSize.width = Math.round(scaledThumbnailSize.width);
scaledThumbnailSize.height = Math.round(scaledThumbnailSize.height);

return scaledThumbnailSize;
}

return {
width: 512,
height: 512,
};
}

0 comments on commit 4e63104

Please sign in to comment.