Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(grid): sort by creation date #611

Merged
merged 3 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/components/Card/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Card extends React.Component<CardProps, {
tagsExpanded: boolean;
externalUrl: string;
lastUpdated: string | undefined;
created: string | undefined;
}> {
// Theme stuff
// cssURL?: string;
Expand Down Expand Up @@ -83,6 +84,7 @@ class Card extends React.Component<CardProps, {
? `https://github.com/${this.props.item.user}/${this.props.item.repo}`
: "",
lastUpdated: (this.props.item.user && this.props.item.repo) ? this.props.item.lastUpdated : undefined,
created: (this.props.item.user && this.props.item.repo) ? this.props.item.created : undefined,
};
}

Expand Down Expand Up @@ -168,7 +170,7 @@ class Card extends React.Component<CardProps, {
Spicetify.showNotification("There was an error installing extension", true);
return;
}
const { manifest, title, subtitle, authors, user, repo, branch, imageURL, extensionURL, readmeURL, lastUpdated } = this.props.item;
const { manifest, title, subtitle, authors, user, repo, branch, imageURL, extensionURL, readmeURL, lastUpdated, created } = this.props.item;
localStorage.setItem(this.localStorageKey, JSON.stringify({
manifest,
type: this.props.type,
Expand All @@ -183,6 +185,7 @@ class Card extends React.Component<CardProps, {
readmeURL,
stars: this.state.stars,
lastUpdated,
created,
}));

// Add to installed list if not there already
Expand Down Expand Up @@ -241,7 +244,7 @@ class Card extends React.Component<CardProps, {
// Add to localstorage (this stores a copy of all the card props in the localstorage)
// TODO: refactor/clean this up

const { manifest, title, subtitle, authors, user, repo, branch, imageURL, extensionURL, readmeURL, cssURL, schemesURL, include, lastUpdated } = item;
const { manifest, title, subtitle, authors, user, repo, branch, imageURL, extensionURL, readmeURL, cssURL, schemesURL, include, lastUpdated, created } = item;

localStorage.setItem(this.localStorageKey, JSON.stringify({
manifest,
Expand All @@ -265,6 +268,7 @@ class Card extends React.Component<CardProps, {
schemes: parsedSchemes,
activeScheme,
lastUpdated,
created,
}));

// TODO: handle this differently?
Expand Down
4 changes: 3 additions & 1 deletion src/components/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class Grid extends React.Component<

if (repoExtensions && repoExtensions.length) {
extensions.push(...repoExtensions.map((extension) => ({
...extension, lastUpdated: repo.pushed_at,
...extension, lastUpdated: repo.pushed_at, created: repo.created_at,
})));
}
}
Expand Down Expand Up @@ -287,6 +287,7 @@ class Grid extends React.Component<
(theme) => ({
...theme,
lastUpdated: repo.pushed_at,
created: repo.created_at,
}),
));
}
Expand Down Expand Up @@ -330,6 +331,7 @@ class Grid extends React.Component<
apps.push(...repoApps.map((app) => ({
...app,
lastUpdated: repo.pushed_at,
created: repo.created_at,
})));
}
}
Expand Down
23 changes: 22 additions & 1 deletion src/logic/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ export const generateSortOptions = (t: (key: string) => string) => {
{ key: "stars", value: t("grid.sort.stars") },
{ key: "newest", value: t("grid.sort.newest") },
{ key: "oldest", value: t("grid.sort.oldest") },
{ key: "recentlyUpdated", value: t("grid.sort.recentlyUpdated") },
{ key: "mostStale", value: t("grid.sort.mostStale") },
{ key: "a-z", value: t("grid.sort.aToZ") },
{ key: "z-a", value: t("grid.sort.zToA") },
];
Expand Down Expand Up @@ -613,6 +615,19 @@ const compareNames = (a: CardItem | Snippet, b: CardItem | Snippet) => {
return aName.localeCompare(bName);
};

/**
* Compare two card items/snippets by created.
* This is skipped for snippets, since they don't have a created property.
*/
const compareCreated = (a: CardItem | Snippet, b: CardItem | Snippet) => {
// Abort compare if items are missing created
if (a.created === undefined || b.created === undefined) return 0;

const aDate = new Date(a.created);
const bDate = new Date(b.created);
return bDate.getTime() - aDate.getTime();
};

/**
* Compare two card items/snippets by lastUpdated.
* This is skipped for snippets, since they don't have a lastUpdated property.
Expand All @@ -635,9 +650,15 @@ export const sortCardItems = (cardItems: CardItem[] | Snippet[], sortMode: strin
cardItems.sort((a, b) => compareNames(b, a));
break;
case "newest":
cardItems.sort((a, b) => compareUpdated(a, b));
cardItems.sort((a, b) => compareCreated(a, b));
break;
case "oldest":
cardItems.sort((a, b) => compareCreated(b, a));
break;
case "recentlyUpdated":
cardItems.sort((a, b) => compareUpdated(a, b));
break;
case "mostStale":
cardItems.sort((a, b) => compareUpdated(b, a));
break;
case "stars":
Expand Down
2 changes: 2 additions & 0 deletions src/resources/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@
"stars": "Stars",
"newest": "Newest",
"oldest": "Oldest",
"recentlyUpdated": "Recently Updated",
"mostStale": "Most Stale",
"aToZ": "A-Z",
"zToA": "Z-A"
}
Expand Down
5 changes: 3 additions & 2 deletions src/types/marketplace-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export type Snippet = {
schemesURL: undefined;
include: undefined;
lastUpdated: undefined;
created: undefined;
};

// From `fetchExtensionManifest()` and `fetchThemeManifest()`
Expand Down Expand Up @@ -91,8 +92,8 @@ export type CardItem = {
stars: number;
tags: string[];
lastUpdated: string;
created: string;
name: string;
lastUpdated: string;
stargazers_count: number;
// For themes only
cssURL?: string;
Expand Down Expand Up @@ -153,7 +154,7 @@ export type SchemeIni = {
[key: string]: ColourScheme;
};

export type SortMode = "a-z" | "z-a" | "newest" | "oldest" | "stars";
export type SortMode = "a-z" | "z-a" | "newest" | "oldest" | "stars" | "recentlyUpdated" | "mostStale";

export type Config = {
// Fetch the settings and set defaults. Used in Settings.js
Expand Down