Skip to content

Commit

Permalink
refactor: remove useless generics
Browse files Browse the repository at this point in the history
  • Loading branch information
vonovak committed May 30, 2024
1 parent 9118ca0 commit f557f49
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
7 changes: 1 addition & 6 deletions src/fileTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,7 @@ const extensions = Object.freeze({
zip: '.zip .gz',
} as const)

export type PlatformTypes = {
android: typeof mimeTypes
ios: typeof utis
windows: typeof extensions
}
export type SupportedPlatforms = 'ios' | 'android' | 'windows'
export type PlatformTypes = typeof mimeTypes | typeof utis | typeof extensions

export const perPlatformTypes = {
android: mimeTypes,
Expand Down
31 changes: 11 additions & 20 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Platform, ModalPropsIOS } from 'react-native'
import invariant from 'invariant'
import type { PlatformTypes, SupportedPlatforms } from './fileTypes'
import type { PlatformTypes } from './fileTypes'
import { perPlatformTypes } from './fileTypes'
import { NativeDocumentPicker } from './NativeDocumentPicker'

Expand All @@ -21,19 +21,16 @@ export type DirectoryPickerResponse = {

export type TransitionStyle = 'coverVertical' | 'flipHorizontal' | 'crossDissolve' | 'partialCurl'

export type DocumentPickerOptions<OS extends SupportedPlatforms> = {
type?:
| string
| PlatformTypes[OS][keyof PlatformTypes[OS]]
| Array<PlatformTypes[OS][keyof PlatformTypes[OS]] | string>
export type DocumentPickerOptions = {
type?: string | Array<PlatformTypes | string>
mode?: 'import' | 'open'
copyTo?: 'cachesDirectory' | 'documentDirectory'
allowMultiSelection?: boolean
transitionStyle?: TransitionStyle
} & Pick<ModalPropsIOS, 'presentationStyle'>

export async function pickDirectory<OS extends SupportedPlatforms>(
params?: Pick<DocumentPickerOptions<OS>, 'presentationStyle' | 'transitionStyle'>,
export async function pickDirectory(
params?: Pick<DocumentPickerOptions, 'presentationStyle' | 'transitionStyle'>,
): Promise<DirectoryPickerResponse | null> {
if (Platform.OS === 'ios') {
const result = await pick({
Expand All @@ -48,27 +45,23 @@ export async function pickDirectory<OS extends SupportedPlatforms>(
}
}

export function pickSingle<OS extends SupportedPlatforms>(
opts?: DocumentPickerOptions<OS>,
): Promise<DocumentPickerResponse> {
export function pickSingle(opts?: DocumentPickerOptions): Promise<DocumentPickerResponse> {
const options = {
...opts,
allowMultiSelection: false,
}
return pick(options).then((results) => results[0])
}

export function pick<OS extends SupportedPlatforms>(
opts?: DocumentPickerOptions<OS>,
): Promise<DocumentPickerResponse[]> {
export function pick(opts?: DocumentPickerOptions): Promise<DocumentPickerResponse[]> {
const options = {
// must be false to maintain old (v5) behavior
allowMultiSelection: false,
type: [types.allFiles],
...opts,
}

const newOpts: DoPickParams<OS> = {
const newOpts: DoPickParams = {
presentationStyle: 'formSheet',
transitionStyle: 'coverVertical',
...options,
Expand All @@ -78,16 +71,14 @@ export function pick<OS extends SupportedPlatforms>(
return doPick(newOpts)
}

type DoPickParams<OS extends SupportedPlatforms> = DocumentPickerOptions<OS> & {
type: Array<PlatformTypes[OS][keyof PlatformTypes[OS]] | string>
type DoPickParams = DocumentPickerOptions & {
type: Array<PlatformTypes | string>
allowMultiSelection: boolean
presentationStyle: NonNullable<ModalPropsIOS['presentationStyle']>
transitionStyle: TransitionStyle
}

function doPick<OS extends SupportedPlatforms>(
options: DoPickParams<OS>,
): Promise<DocumentPickerResponse[]> {
function doPick(options: DoPickParams): Promise<DocumentPickerResponse[]> {
invariant(
!('filetype' in options),
'A `filetype` option was passed to DocumentPicker.pick, the correct option is `type`',
Expand Down

0 comments on commit f557f49

Please sign in to comment.