-
Notifications
You must be signed in to change notification settings - Fork 143
Add useUserAgent #449
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
Open
Tommypop2
wants to merge
8
commits into
solidjs-community:main
Choose a base branch
from
Tommypop2:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add useUserAgent #449
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
19a7e57
Restructured code, to separate different primitives into separate files
Tommypop2 66bc856
Added userAgent to exports
Tommypop2 5f0dd0f
Added docs for createUserAgent
Tommypop2 7a79b8e
Added a JSDoc comment to useUserAgent
Tommypop2 706b8b8
Merge branch 'main' into main
Tommypop2 96764f6
Merge branch 'solidjs-community:main' into main
Tommypop2 96ff599
Merge branch 'solidjs-community:main' into main
Tommypop2 ffe8a97
Add "dev" stuff
Tommypop2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,101 +1,3 @@ | ||
import { createSignal, createEffect, Signal } from "solid-js"; | ||
import { isServer } from "solid-js/web"; | ||
import { parseCookie } from "solid-start"; | ||
import { useRequest } from "solid-start/server"; | ||
|
||
export type MaxAgeOptions = { | ||
/** | ||
* The maximum age of the cookie in seconds. Defaults to 1 year. | ||
*/ | ||
cookieMaxAge?: number; | ||
}; | ||
|
||
export type ServerCookieOptions<T = string> = MaxAgeOptions & { | ||
/** | ||
* A function to deserialize the cookie value to be used as signal value | ||
*/ | ||
deserialize?: (str: string | undefined) => T; | ||
/** | ||
* A function to serialize the signal value to be used as cookie value | ||
*/ | ||
serialize?: (value: T) => string; | ||
}; | ||
|
||
const YEAR = 365 * 24 * 60 * 60; | ||
|
||
/** | ||
* A primitive for creating a cookie that can be accessed isomorphically on the client, or the server | ||
* | ||
* @param name The name of the cookie to be set | ||
* @param options Options for the cookie {@see ServerCookieOptions} | ||
* @return Returns an accessor and setter to manage the user's current theme | ||
*/ | ||
export function createServerCookie<T>( | ||
name: string, | ||
options: ServerCookieOptions<T> & { | ||
deserialize: (str: string | undefined) => T; | ||
serialize: (value: T) => string; | ||
}, | ||
): Signal<T>; | ||
export function createServerCookie( | ||
name: string, | ||
options?: ServerCookieOptions, | ||
): Signal<string | undefined>; | ||
export function createServerCookie<T>( | ||
name: string, | ||
options?: ServerCookieOptions<T | undefined>, | ||
): Signal<T | undefined> { | ||
const { | ||
deserialize = (v: any) => v as T, | ||
serialize = String, | ||
cookieMaxAge = YEAR, | ||
} = options ?? {}; | ||
|
||
const [cookie, setCookie] = createSignal( | ||
deserialize( | ||
parseCookie(isServer ? useRequest().request.headers.get("cookie") ?? "" : document.cookie)[ | ||
name | ||
], | ||
), | ||
); | ||
|
||
createEffect(p => { | ||
const string = serialize(cookie()); | ||
if (p !== string) document.cookie = `${name}=${string};max-age=${cookieMaxAge}`; | ||
return string; | ||
}); | ||
|
||
return [cookie, setCookie]; | ||
} | ||
|
||
export type Theme = "light" | "dark"; | ||
|
||
export type UserThemeOptions = MaxAgeOptions & { | ||
/** | ||
* The default theme to be used if the cookie is not set | ||
*/ | ||
defaultValue?: Theme; | ||
}; | ||
|
||
/** | ||
* Composes {@link createServerCookie} to provide a type safe way to store a theme and access it on the server or client. | ||
* | ||
* @param name The name of the cookie to be set | ||
* @param options Options for the cookie {@link UserThemeOptions} | ||
*/ | ||
export function createUserTheme( | ||
name: string | undefined, | ||
options: UserThemeOptions & { defaultValue: Theme }, | ||
): Signal<Theme>; | ||
export function createUserTheme( | ||
name?: string, | ||
options?: UserThemeOptions, | ||
): Signal<Theme | undefined>; | ||
export function createUserTheme(name = "theme", options?: UserThemeOptions): Signal<any> { | ||
const defaultValue = options?.defaultValue; | ||
return createServerCookie(name, { | ||
...options, | ||
deserialize: str => (str === "light" || str === "dark" ? str : defaultValue), | ||
serialize: String, | ||
}); | ||
} | ||
export * from "./serverCookie"; | ||
export * from "./userTheme"; | ||
export * from "./userAgent"; |
This file contains hidden or 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,69 @@ | ||
import { createSignal, createEffect, Signal } from "solid-js"; | ||
import { isServer } from "solid-js/web"; | ||
import { parseCookie } from "solid-start"; | ||
import { useRequest } from "solid-start/server"; | ||
|
||
export type MaxAgeOptions = { | ||
/** | ||
* The maximum age of the cookie in seconds. Defaults to 1 year. | ||
*/ | ||
cookieMaxAge?: number; | ||
}; | ||
|
||
export type ServerCookieOptions<T = string> = MaxAgeOptions & { | ||
/** | ||
* A function to deserialize the cookie value to be used as signal value | ||
*/ | ||
deserialize?: (str: string | undefined) => T; | ||
/** | ||
* A function to serialize the signal value to be used as cookie value | ||
*/ | ||
serialize?: (value: T) => string; | ||
}; | ||
|
||
const YEAR = 365 * 24 * 60 * 60; | ||
|
||
/** | ||
* A primitive for creating a cookie that can be accessed isomorphically on the client, or the server | ||
* | ||
* @param name The name of the cookie to be set | ||
* @param options Options for the cookie {@see ServerCookieOptions} | ||
* @return Returns an accessor and setter to manage the user's current theme | ||
*/ | ||
export function createServerCookie<T>( | ||
name: string, | ||
options: ServerCookieOptions<T> & { | ||
deserialize: (str: string | undefined) => T; | ||
serialize: (value: T) => string; | ||
}, | ||
): Signal<T>; | ||
export function createServerCookie( | ||
name: string, | ||
options?: ServerCookieOptions, | ||
): Signal<string | undefined>; | ||
export function createServerCookie<T>( | ||
name: string, | ||
options?: ServerCookieOptions<T | undefined>, | ||
): Signal<T | undefined> { | ||
const { | ||
deserialize = (v: any) => v as T, | ||
serialize = String, | ||
cookieMaxAge = YEAR, | ||
} = options ?? {}; | ||
|
||
const [cookie, setCookie] = createSignal( | ||
deserialize( | ||
parseCookie(isServer ? useRequest().request.headers.get("cookie") ?? "" : document.cookie)[ | ||
name | ||
], | ||
), | ||
); | ||
|
||
createEffect(p => { | ||
const string = serialize(cookie()); | ||
if (p !== string) document.cookie = `${name}=${string};max-age=${cookieMaxAge}`; | ||
return string; | ||
}); | ||
|
||
return [cookie, setCookie]; | ||
} |
This file contains hidden or 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,13 @@ | ||
import { useRequest } from "solid-start/server"; | ||
import { isServer } from "solid-js/web"; | ||
/** A primitive that allows for the user agent string to be accessed isomorphically on the client, or on the server | ||
* @return Returns the user agent string, or null | ||
*/ | ||
export function useUserAgent() { | ||
const event = useRequest(); | ||
if (isServer) { | ||
const userAgent = event.request.headers.get("user-agent") ?? null; | ||
return userAgent; | ||
} | ||
return navigator.userAgent; | ||
} |
This file contains hidden or 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,34 @@ | ||
import { Signal } from "solid-js"; | ||
import { MaxAgeOptions, createServerCookie } from "./serverCookie"; | ||
|
||
export type Theme = "light" | "dark"; | ||
|
||
export type UserThemeOptions = MaxAgeOptions & { | ||
/** | ||
* The default theme to be used if the cookie is not set | ||
*/ | ||
defaultValue?: Theme; | ||
}; | ||
|
||
/** | ||
* Composes {@link createServerCookie} to provide a type safe way to store a theme and access it on the server or client. | ||
* | ||
* @param name The name of the cookie to be set | ||
* @param options Options for the cookie {@link UserThemeOptions} | ||
*/ | ||
export function createUserTheme( | ||
name: string | undefined, | ||
options: UserThemeOptions & { defaultValue: Theme }, | ||
): Signal<Theme>; | ||
export function createUserTheme( | ||
name?: string, | ||
options?: UserThemeOptions, | ||
): Signal<Theme | undefined>; | ||
export function createUserTheme(name = "theme", options?: UserThemeOptions): Signal<any> { | ||
const defaultValue = options?.defaultValue; | ||
return createServerCookie(name, { | ||
...options, | ||
deserialize: str => (str === "light" || str === "dark" ? str : defaultValue), | ||
serialize: String, | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.