-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d1f1b08
commit b9e17ae
Showing
11 changed files
with
207 additions
and
45 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { MutableRefObject, RefObject } from "react"; | ||
import type { Writeable } from "zod"; | ||
|
||
/** | ||
* A wonderful workaround for not being allowed to mutate hook props directly according to React Compiler, including with a ref. | ||
*/ | ||
export function updateRef<T>(ref: RefObject<T> | MutableRefObject<T>, value: T) { | ||
(ref as Writeable<typeof ref>).current = value; | ||
} |
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,18 +1,13 @@ | ||
import React from "react"; | ||
import type { Writeable } from "zod"; | ||
import { updateRef } from "./updateRef"; | ||
|
||
export function useMergeRefs<TRefType>(...refs: (React.LegacyRef<TRefType> | React.MutableRefObject<TRefType> | undefined)[]): React.Ref<TRefType> { | ||
return React.useMemo(() => (value: TRefType) => { | ||
for (const ref_ of refs) { | ||
const ref = noOp(ref_); // makes the React Compiler not complain | ||
for (const ref of refs) { | ||
if (!ref) continue; | ||
if (typeof ref === 'function') ref(value); | ||
else if (typeof ref === 'string') throw new Error('Cannot merge string refs; if you are using this, you can implement this functionality yourself.'); | ||
else (ref as Writeable<typeof ref>).current = value; | ||
else updateRef(ref, value); | ||
} | ||
}, [refs]); | ||
} | ||
|
||
function noOp<T>(value: T): T { | ||
return value; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import CookieManager, { type Cookie } from '@react-native-cookies/cookies'; | ||
import type { cookieStore } from 'cookie-store'; | ||
|
||
declare enum CookieSameSite { | ||
strict = "strict", | ||
lax = "lax", | ||
none = "none" | ||
} | ||
|
||
export interface UnifiedCookie { | ||
name: string; | ||
value: string; | ||
path?: string; | ||
domain?: string; | ||
version?: string; | ||
expires?: Date; | ||
secure?: boolean; | ||
httpOnly?: boolean; | ||
sameSite?: CookieSameSite; | ||
} | ||
|
||
export async function setCookie(cookie: UnifiedCookie, supplementaryCanonicalHost: string): Promise<boolean> { | ||
const augmentedCookie = Object.assign(cookie, { | ||
expires: cookie.expires?.toUTCString(), | ||
}); | ||
|
||
return await CookieManager.set(supplementaryCanonicalHost, augmentedCookie); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { cookieStore } from 'cookie-store'; | ||
import type { Cookie } from '@react-native-cookies/cookies'; | ||
import type { UnifiedCookie } from './setCookie'; | ||
if (typeof window !== 'undefined' && !window.cookieStore) window.cookieStore = (await import('cookie-store')).cookieStore; | ||
|
||
export async function setCookie(cookie: UnifiedCookie, supplementaryCanonicalHost: string): Promise<true> { | ||
const augmentedCookie = Object.assign(cookie, { | ||
domain: supplementaryCanonicalHost, | ||
expires: cookie.expires ?? null, | ||
}); | ||
|
||
console.log('setCookie', augmentedCookie); | ||
|
||
await window.cookieStore.set(augmentedCookie); | ||
return true; | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export const STOCKEDHOME_CSRF_COOKIE_HEADER_NAME = 'stockedhome-csrf-token'; |
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