File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -7,12 +7,8 @@ import { getDraftResource } from "../utils/get-draft-resource";
77import { DraftItem } from "./draft-item" ;
88
99export 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 ) => [
Original file line number Diff line number Diff line change 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 > ;
Original file line number Diff line number Diff line change @@ -4,19 +4,21 @@ import type {
44 SpecificResourceMetadata ,
55} from "@/features/resources/types" ;
66import 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
1011export 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 > ;
1618export 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
Original file line number Diff line number Diff line change 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" ;
42import type { DraftableResource , ResourceDraft } from "../types/internal" ;
53
64export 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} ;
Original file line number Diff line number Diff line change @@ -17,3 +17,7 @@ export type OptionalPromise<T> = T | Promise<T>;
1717export 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+ } ;
You can’t perform that action at this time.
0 commit comments