Skip to content

Commit

Permalink
Merge pull request #3 from drewlyton/sanity-upgrade
Browse files Browse the repository at this point in the history
Sanity upgrade
  • Loading branch information
drewlyton authored Jan 12, 2024
2 parents baf2c73 + b8466cf commit c302bf0
Show file tree
Hide file tree
Showing 26 changed files with 15,569 additions and 8,897 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
legacy-peer-deps=true
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16.17.0
18
6 changes: 3 additions & 3 deletions app/components/StoriesSection.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { cloneElement, PropsWithChildren, ReactElement } from "react";

Check warning on line 1 in app/components/StoriesSection.tsx

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

Imports "PropsWithChildren" and "ReactElement" are only used as types
import type Story from "../data/Story";
import { Post } from "~/data/types";

Check warning on line 2 in app/components/StoriesSection.tsx

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

All imports in the declaration are only used as types. Use `import type`
import StoryCard from "./StoryCard";

interface Props extends PropsWithChildren {
label: string;
id: string;
animation: ReactElement;
stories: Story[];
stories: Post[];
}

const StoriesSection: React.FC<Props> = ({
Expand All @@ -33,7 +33,7 @@ const StoriesSection: React.FC<Props> = ({
</div>
<div className="flex flex-wrap items-start">
{stories.map((story) => (
<StoryCard story={story} key={story.id} />
<StoryCard story={story} key={story._id} />
))}
</div>
</section>
Expand Down
9 changes: 5 additions & 4 deletions app/components/StoryCard.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { Link } from "@remix-run/react";
import classNames from "classnames";
import React from "react";
import { imageBuilder } from "~/helpers/imageBuilder";
import { Post } from "~/data/types";

Check warning on line 5 in app/components/StoryCard.tsx

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

All imports in the declaration are only used as types. Use `import type`
import routes from "../helpers/routes";
import { truncateString } from "../helpers/truncateString";
import type Story from "../data/Story";

const StoryCard: React.FC<Props> = ({ story }) => {
return (
<div className="post-item relative px-2 py-4 flex-grow">
<Link to={routes.story(story.slug)}>
<div
className={classNames("mb-4 rounded-2xl overflow-hidden relative", {
featured: story.highlighted
featured: story.tags.find((t) => t.title === "highlighted")
})}
>
<img
className="w-full"
src={story.featuredImage.url}
src={imageBuilder.image(story.mainImage).url()}
alt={`${story.title}`}
loading="lazy"
/>
Expand All @@ -41,7 +42,7 @@ const StoryCard: React.FC<Props> = ({ story }) => {
};

type Props = {
story: Story;
story: Post;
};

export default StoryCard;
2 changes: 0 additions & 2 deletions app/components/Subscribe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export const Subscribe: React.FC<{
? "error"
: "idle";

console.log(data);

return (
<div className="bg-black bg-opacity-5 dark:bg-opacity-20 px-10 py-8 rounded-2xl mb-8">
{noPreamble ? (
Expand Down
25 changes: 0 additions & 25 deletions app/data/GetHighlighted.ts

This file was deleted.

26 changes: 0 additions & 26 deletions app/data/GetStories.ts

This file was deleted.

21 changes: 0 additions & 21 deletions app/data/GetStoriesByTag.ts

This file was deleted.

31 changes: 0 additions & 31 deletions app/data/GetStory.ts

This file was deleted.

5 changes: 0 additions & 5 deletions app/data/RichText.ts

This file was deleted.

24 changes: 0 additions & 24 deletions app/data/Story.ts

This file was deleted.

15 changes: 0 additions & 15 deletions app/data/sanity.ts

This file was deleted.

24 changes: 24 additions & 0 deletions app/data/sanityClient.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createClient } from "@sanity/client";

export const sanity = createClient({
projectId: "xpnntlnd",
dataset: "production",
useCdn: process.env.NODE_ENV === "production", // set to `false` to bypass the edge cache
apiVersion: "2023-05-03", // use current date (YYYY-MM-DD) to target the latest API version
token: process.env.SANITY_TOKEN // Only if you want to update content with the client
});

export function getPostsByTag(tags: string[]) {
const tagsFilters = tags.map((t) => `"${t}" in tags[]->title`).join(" || ");
return `*[_type == "post" ${
tagsFilters.length ? `&& ${tagsFilters}` : ""
}]{_id, title, description, mainImage, tags[]->{title}, "slug": slug.current}`;
}

export function getAllPosts() {
return `*[_type == "post"]{_id, title, description, mainImage, tags[]->{title}, "slug": slug.current}`;
}

export function getPostBySlug(slug: string) {
return `*[_type == "post" && slug.current match "${slug}" ][0]{_id, title, description, mainImage, tags[]->{title}, "slug": slug.current, body, author->{...}, publishedAt}`;
}
29 changes: 29 additions & 0 deletions app/data/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export type Author = {
name: string;
bio: string;
image: {
asset: {
_ref: string;
};
};
};

export type Image = {
asset: {
_ref: string;
};
};

export type Tag = { title: string; description: string };

export type Post = {
_id: string;
title: string;
description: string;
body: string;
slug: string;
mainImage: Image;
author: Author;
publishedAt: string;
tags: Tag[];
};
9 changes: 5 additions & 4 deletions app/emails/NewPostNewsletter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import {
Section,
Text
} from "@react-email/components";
import { getMDXComponent } from "mdx-bundler/client";
import { getMDXComponent } from "~/helpers/mdx.server";
import type { PropsWithChildren } from "react";
import type { Newsletter } from "~/data/Newsletter";
import type Story from "~/data/Story";
import { Post } from "~/data/types";
import { imageBuilder } from "~/helpers/imageBuilder";

type NewsletterEmailProps = Pick<Newsletter, "body" | "author" | "preview">;

Expand Down Expand Up @@ -62,7 +63,7 @@ const MDXBlockQuote: React.FC<PropsWithChildren> = ({ children }) => {
);
};

const MDXStory: React.FC<{ story: Story }> = ({ story }) => {
const MDXStory: React.FC<{ story: Post }> = ({ story }) => {
return (
<Button
href={`https://www.drewis.cool/story/${story.slug}?ref=newsletter`}
Expand All @@ -71,7 +72,7 @@ const MDXStory: React.FC<{ story: Story }> = ({ story }) => {
<Section>
<div className="border border-solid rounded-md border-gray-300 my-6">
<Img
src={story.featuredImage.url}
src={imageBuilder.image(story.mainImage).url()}
className="rounded-t-md"
width={"100%"}
/>
Expand Down
6 changes: 3 additions & 3 deletions app/helpers/filterStories.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type Story from "~/data/Story";
import { Post } from "~/data/types";

export function filterStories(
storiesArray: Array<Story>,
storiesArray: Array<Post>,
searchString: string
): Array<Story> {
): Array<Post> {
if (!storiesArray) return [];
return storiesArray.filter((story): Boolean => {
return (
Expand Down
11 changes: 11 additions & 0 deletions app/helpers/imageBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import imageUrlBuilder from "@sanity/image-url";
import { createClient } from "@sanity/client";

const sanity = createClient({
projectId: "xpnntlnd",
dataset: "production",
useCdn: process.env.NODE_ENV === "production", // set to `false` to bypass the edge cache
apiVersion: "2023-05-03" // use current date (YYYY-MM-DD) to target the latest API version
});

export const imageBuilder = imageUrlBuilder(sanity);
12 changes: 6 additions & 6 deletions app/routes/[feed.atom.xml].tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { LoaderFunction } from "@remix-run/node";
import { client } from "~/data/client";
import GetStories from "~/data/GetStories";
import type Story from "~/data/Story";
import { getAllPosts, sanity } from "~/data/sanityClient.server";
import { Post } from "~/data/types";
import { feed } from "~/helpers/feed";
import { getHost } from "~/helpers/getHost.server";
import { imageBuilder } from "~/helpers/imageBuilder";

export const loader: LoaderFunction = async ({ request }) => {
const { stories }: { stories: Story[] } = await client.request(GetStories);
const stories = (await sanity.fetch(getAllPosts())) as Post[];

const origin = new URL(request.url).origin;
const storyURL = (slug: string) =>
Expand All @@ -15,7 +15,7 @@ export const loader: LoaderFunction = async ({ request }) => {
const rss = feed(origin);

stories.forEach(
({ author, title, description, slug, publishedAt, featuredImage }) => {
({ author, title, description, slug, publishedAt, mainImage }) => {
rss.addItem({
title,
id: storyURL(slug),
Expand All @@ -29,7 +29,7 @@ export const loader: LoaderFunction = async ({ request }) => {
}
],
image: {
url: featuredImage.url,
url: imageBuilder.image(mainImage).url(),
type: "image/jpg",
length: 0
}
Expand Down
Loading

0 comments on commit c302bf0

Please sign in to comment.