Skip to content

Commit 66fe65b

Browse files
committed
fix(review): reverse map draft types
1 parent a312c12 commit 66fe65b

5 files changed

Lines changed: 22 additions & 19 deletions

File tree

src/features/review/components/review-list.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ import { getDraftResource } from "../utils/get-draft-resource";
77
import { DraftItem } from "./draft-item";
88

99
export async function ReviewList() {
10-
const data = await fetchDrafts();
11-
const drafts = data.map((draft) => ({
12-
...draft,
13-
resource: getDraftResource(draft),
14-
}));
15-
const resources = new Set(drafts.map((draft) => draft.resource));
10+
const drafts = await fetchDrafts();
11+
const resources = new Set(drafts.map((draft) => getDraftResource(draft)));
1612
const relatedResourcesMap: DraftableResourceRelationMap = typedFromEntries(
1713
await Promise.all(
1814
[...resources].map(async (resource) => [
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Resource } from "@/features/resources";
2+
3+
import type { DraftableResource } from "../types/internal";
4+
5+
export const DRAFT_TYPE_RESOURCES = {
6+
article_draft: Resource.GuideArticles,
7+
organization_draft: Resource.StudentOrganizations,
8+
} as const satisfies Record<string, DraftableResource>;

src/features/review/types/internal.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@ import type {
44
SpecificResourceMetadata,
55
} from "@/features/resources/types";
66
import type { ResourceRelations } from "@/types/components";
7+
import type { Invert } from "@/types/helpers";
78

8-
import type { RESOURCE_DRAFT_TYPES } from "../data/resource-draft-types";
9+
import type { DRAFT_TYPE_RESOURCES } from "../data/draft-type-resources";
910

1011
export type DraftableResource = {
1112
[R in Resource]: SpecificResourceMetadata<R> extends { apiDraftPath: string }
1213
? R
1314
: never;
1415
}[Resource];
1516

17+
type ResourceDraftTypes = Invert<typeof DRAFT_TYPE_RESOURCES>;
1618
export interface ResourceDraft<
1719
T extends DraftableResource = DraftableResource,
1820
> {
19-
resourceType: (typeof RESOURCE_DRAFT_TYPES)[T];
21+
resourceType: ResourceDraftTypes[T];
2022
data: ResourceDataType<T>;
2123
}
2224

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
import { typedEntries } from "@/utils";
2-
3-
import { RESOURCE_DRAFT_TYPES } from "../data/resource-draft-types";
1+
import { DRAFT_TYPE_RESOURCES } from "../data/draft-type-resources";
42
import type { DraftableResource, ResourceDraft } from "../types/internal";
53

64
export const getDraftResource = <R extends DraftableResource>(
75
draft: ResourceDraft<R>,
86
): R => {
9-
const entry = typedEntries(RESOURCE_DRAFT_TYPES).find(
10-
([_, type]) => type === draft.resourceType,
11-
);
12-
if (entry == null) {
13-
throw new Error(`Unknown draft resource type: ${draft.resourceType}`);
14-
}
15-
const [resource] = entry;
16-
return resource as R;
7+
const resource = DRAFT_TYPE_RESOURCES[draft.resourceType];
8+
// TODO: double cast should not be necessary
9+
return resource as DraftableResource as R;
1710
};

src/types/helpers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ export type OptionalPromise<T> = T | Promise<T>;
1717
export type NonNullableValues<T> = {
1818
[K in keyof T]: NonNullable<T[K]>;
1919
};
20+
21+
export type Invert<T extends Record<PropertyKey, PropertyKey>> = {
22+
[K in keyof T as T[K]]: K;
23+
};

0 commit comments

Comments
 (0)