Skip to content

Commit

Permalink
Add cake day 🍰 (aeharding#627)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Harding <[email protected]>
  • Loading branch information
sharunkumar and aeharding authored Oct 9, 2023
1 parent 9ee360a commit 062df5f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ jobs:
- name: 📦 Install dependencies
run: pnpm install --frozen-lockfile

# - name: 🧪 Test project
# run: pnpm test
- name: 🧪 Test project
run: pnpm test

- name: 📝 Lint
run: pnpm lint
Expand Down
7 changes: 0 additions & 7 deletions src/App.test.tsx

This file was deleted.

8 changes: 8 additions & 0 deletions src/features/labels/links/PersonLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import Handle from "../Handle";
import { StyledLink } from "./shared";
import { useAppSelector } from "../../../store";
import { OInstanceUrlDisplayMode } from "../../../services/db";
import { calculateIsCakeDay, fixLemmyDateString } from "../../../helpers/date";
import { useMemo } from "react";

const Prefix = styled.span`
font-weight: normal;
Expand Down Expand Up @@ -43,6 +45,11 @@ export default function PersonLink({
else if (distinguished) color = "var(--ion-color-success)";
else if (opId && person.id === opId) color = "var(--ion-color-primary-fixed)";

const isCakeDay = useMemo(
() => calculateIsCakeDay(new Date(fixLemmyDateString(person.published))),
[person.published],
);

return (
<StyledLink
to={buildGeneralBrowseLink(`/u/${getHandle(person)}`)}
Expand All @@ -67,6 +74,7 @@ export default function PersonLink({
item={person}
showInstanceWhenRemote={showInstanceWhenRemote || forceInstanceUrl}
/>
{isCakeDay && " 🍰"}
</StyledLink>
);
}
28 changes: 28 additions & 0 deletions src/helpers/date.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { addHours, addMinutes, subMinutes, subYears } from "date-fns";
import { calculateIsCakeDay } from "./date";

describe("cake cake", () => {
it("not cake day when created", () => {
expect(calculateIsCakeDay(new Date())).toBe(false);
});

it("not cake day soon after created", () => {
expect(calculateIsCakeDay(addHours(new Date(), 12))).toBe(false);
});

it("cake day soon one year after created", () => {
expect(calculateIsCakeDay(subYears(new Date(), 1))).toBe(true);
});

it("cake day in a couple minutes", () => {
expect(calculateIsCakeDay(addMinutes(subYears(new Date(), 1), 2))).toBe(
false,
);
});

it("cake day started a couple minutes ago", () => {
expect(calculateIsCakeDay(subMinutes(subYears(new Date(), 1), 2))).toBe(
true,
);
});
});
32 changes: 32 additions & 0 deletions src/helpers/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,35 @@ export function fixLemmyDateString(rawLemmyDateString: string): string {

return `${rawLemmyDateString}Z`;
}

/**
* User cake day happens annually, and starts the exact millisecond the user
* signed up, and runs for exactly 24 hours
*
* @param creationDate User created date
* @returns True if cake day! 🍰
*/
export function calculateIsCakeDay(creationDate: Date) {
const oneDayInMilliseconds = 24 * 60 * 60 * 1000;

const currentUTCDate = new Date();
const userCreationUTCDate = new Date(creationDate);

if (
currentUTCDate.getTime() - userCreationUTCDate.getTime() <=
oneDayInMilliseconds
)
// User was just created
return false;

// Set the year of the Cake Day to the current year
userCreationUTCDate.setUTCFullYear(currentUTCDate.getUTCFullYear());

// Check if the current UTC date is within a 24-hour window from the user's creation date
const cakeDayStart = new Date(userCreationUTCDate);
const cakeDayEnd = new Date(
userCreationUTCDate.getTime() + oneDayInMilliseconds,
);

return currentUTCDate >= cakeDayStart && currentUTCDate < cakeDayEnd;
}
2 changes: 1 addition & 1 deletion src/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import "@testing-library/jest-dom/extend-expect";
import "@testing-library/jest-dom";

// Mock matchmedia
window.matchMedia =
Expand Down

0 comments on commit 062df5f

Please sign in to comment.