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

refactor: remove useless generics #722

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading