-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add typings to most of the functions
Some runtime logic had to be updated, mostly adding more strict checks to avoid errors
- Loading branch information
Showing
19 changed files
with
193 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,26 @@ | ||
import { useClient, useCapabilities } from 'cozy-client' | ||
|
||
import { useEffect, useMemo, useState } from 'react' | ||
|
||
import { makeSessionAPI } from '/libs/functions/session' | ||
import { useClient, useCapabilities } from 'cozy-client' | ||
|
||
import { makeSessionAPI, SessionApi } from '/libs/functions/session' | ||
|
||
export const useSession = () => { | ||
const [subdomainType, setSubdomainType] = useState() | ||
export const useSession = (): SessionApi => { | ||
const [subdomainType, setSubdomainType] = useState<string>() | ||
const client = useClient() | ||
const { capabilities, fetchStatus } = useCapabilities(client) | ||
|
||
useEffect(() => { | ||
fetchStatus === 'loaded' && | ||
// @ts-expect-error : cozy-client has to be updated | ||
setSubdomainType(capabilities?.flat_subdomains ? 'flat' : 'nested') | ||
}, [capabilities, fetchStatus]) | ||
|
||
return useMemo( | ||
() => makeSessionAPI(client, subdomainType), | ||
// We have to assume that client and subdomainType are defined | ||
// Still, this is old code and we should probably refactor it | ||
// Adding a @TODO flag for now | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
() => makeSessionAPI(client!, subdomainType!), | ||
[client, subdomainType] | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,22 @@ | ||
export const getHostname = nativeEvent => { | ||
import { getErrorMessage } from 'cozy-intent' | ||
|
||
import { devlog } from '/core/tools/env' | ||
|
||
export const getHostname = ( | ||
nativeEvent?: { url?: string | unknown } | unknown | ||
): string | undefined => { | ||
if ( | ||
!nativeEvent || | ||
typeof nativeEvent !== 'object' || | ||
!('url' in nativeEvent) || | ||
typeof nativeEvent.url !== 'string' | ||
) | ||
return | ||
|
||
try { | ||
return new URL(nativeEvent.url).hostname | ||
} catch { | ||
return nativeEvent?.url | ||
} catch (error) { | ||
devlog('getHostname failed, nativeEvent:', nativeEvent, error) | ||
if (getErrorMessage(error).includes('Invalid URL')) return nativeEvent.url | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
export const makeHandlers = handlers => event => | ||
type makeHandlersType = ( | ||
handlers?: Record<string, () => void> | ||
) => (event?: { nativeEvent?: { data?: string | unknown } }) => void | ||
|
||
export const makeHandlers: makeHandlersType = handlers => event => | ||
Object.keys(handlers?.constructor === Object ? handlers : {}).forEach( | ||
handlerName => | ||
event?.nativeEvent?.data?.includes?.(handlerName) && | ||
handlers[handlerName]?.() | ||
handlerName => { | ||
const data = event?.nativeEvent?.data | ||
const isString = typeof data === 'string' | ||
|
||
if (!isString) return | ||
|
||
return data.includes(handlerName) && handlers?.[handlerName]?.() | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.