create-signet-app 0.8.3 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/base/api/api/app.ts +15 -1
- package/base/api/api/env.ts +4 -0
- package/base/api/api/services.ts +55 -1
- package/package.json +3 -2
- package/parts/auth/api/api/routes/auth.ts +31 -8
- package/parts/mobile/api/api/routes/mobile.ts +71 -0
- package/parts/mobile/api/test/mobile.test.ts +104 -0
- package/parts/mobile/part.ts +11 -0
- package/parts/pwa/web/public/icon-192.png +0 -0
- package/parts/pwa/web/public/icon-512.png +0 -0
- package/parts/pwa/web/public/icon-maskable-512.png +0 -0
- package/parts/pwa/web/public/manifest.webmanifest +11 -1
- package/parts/pwa/web/public/sw.js +8 -1
- package/src/compose.ts +15 -4
- package/src/index.ts +53 -13
- package/src/mobile.ts +58 -0
- package/src/picker.ts +21 -8
- package/template-mobile/README.md +139 -0
- package/template-mobile/_github/workflows/ci.yml +38 -0
- package/template-mobile/_gitignore +1 -0
- package/template-mobile/mobile/_gitignore +16 -0
- package/template-mobile/mobile/app.config.ts +77 -0
- package/template-mobile/mobile/assets/adaptive-icon.png +0 -0
- package/template-mobile/mobile/assets/icon.png +0 -0
- package/template-mobile/mobile/assets/notification-icon.png +0 -0
- package/template-mobile/mobile/assets/splash-icon.png +0 -0
- package/template-mobile/mobile/babel.config.js +4 -0
- package/template-mobile/mobile/biome.json +8 -0
- package/template-mobile/mobile/eas.json +27 -0
- package/template-mobile/mobile/expo-env.d.ts +1 -0
- package/template-mobile/mobile/jest.config.js +9 -0
- package/template-mobile/mobile/jest.setup.js +32 -0
- package/template-mobile/mobile/src/__tests__/auth-gate.test.tsx +40 -0
- package/template-mobile/mobile/src/__tests__/sign-in.test.tsx +41 -0
- package/template-mobile/mobile/src/app/(app)/_layout.tsx +48 -0
- package/template-mobile/mobile/src/app/(app)/devices/[id].tsx +62 -0
- package/template-mobile/mobile/src/app/(app)/devices/_layout.tsx +19 -0
- package/template-mobile/mobile/src/app/(app)/devices/index.tsx +40 -0
- package/template-mobile/mobile/src/app/(app)/index.tsx +69 -0
- package/template-mobile/mobile/src/app/(app)/settings.tsx +116 -0
- package/template-mobile/mobile/src/app/(auth)/_layout.tsx +19 -0
- package/template-mobile/mobile/src/app/(auth)/sign-in.tsx +49 -0
- package/template-mobile/mobile/src/app/(auth)/verify.tsx +62 -0
- package/template-mobile/mobile/src/app/_layout.tsx +95 -0
- package/template-mobile/mobile/src/components/ui.tsx +230 -0
- package/template-mobile/mobile/src/lib/analytics.ts +20 -0
- package/template-mobile/mobile/src/lib/api.ts +49 -0
- package/template-mobile/mobile/src/lib/auth.ts +43 -0
- package/template-mobile/mobile/src/lib/config.ts +14 -0
- package/template-mobile/mobile/src/lib/devices.ts +18 -0
- package/template-mobile/mobile/src/lib/outbox.ts +84 -0
- package/template-mobile/mobile/src/lib/polyfills.ts +6 -0
- package/template-mobile/mobile/src/lib/push.ts +97 -0
- package/template-mobile/mobile/src/lib/session.ts +39 -0
- package/template-mobile/mobile/src/lib/storage.ts +17 -0
- package/template-mobile/mobile/src/lib/theme.tsx +64 -0
- package/template-mobile/mobile/tsconfig.json +11 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createSession,
|
|
3
|
+
type SessionTokens,
|
|
4
|
+
tokenEndpointRefresh,
|
|
5
|
+
} from "@dapplelight/signet-lib/session"
|
|
6
|
+
import { useEffect, useState } from "react"
|
|
7
|
+
import { API_URL } from "./config"
|
|
8
|
+
import { secureStorage } from "./storage"
|
|
9
|
+
|
|
10
|
+
/** The client id the API knows this app by (services.ts, `clientLogin`). */
|
|
11
|
+
export const CLIENT_ID = "mobile"
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The signed-in tokens, kept in the secure store. A stale access token is
|
|
15
|
+
* refreshed at the API's token endpoint before a request; a refused refresh
|
|
16
|
+
* clears the session, and the auth gate in app/_layout.tsx sends you to sign-in.
|
|
17
|
+
*/
|
|
18
|
+
export const session = createSession({
|
|
19
|
+
storage: secureStorage,
|
|
20
|
+
refresh: tokenEndpointRefresh(`${API_URL}/api/mobile/token`, CLIENT_ID),
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
/** The tokens, null when signed out, undefined while the secure store is read. */
|
|
24
|
+
export function useSession(): SessionTokens | null | undefined {
|
|
25
|
+
const [tokens, setTokens] = useState<SessionTokens | null | undefined>(undefined)
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
let live = true
|
|
28
|
+
const off = session.subscribe((t) => live && setTokens(t))
|
|
29
|
+
session.get().then(
|
|
30
|
+
(t) => live && setTokens(t),
|
|
31
|
+
() => live && setTokens(null),
|
|
32
|
+
)
|
|
33
|
+
return () => {
|
|
34
|
+
live = false
|
|
35
|
+
off()
|
|
36
|
+
}
|
|
37
|
+
}, [])
|
|
38
|
+
return tokens
|
|
39
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { StorageAdapter } from "@dapplelight/signet-lib/storage"
|
|
2
|
+
import * as SecureStore from "expo-secure-store"
|
|
3
|
+
import kv from "expo-sqlite/kv-store"
|
|
4
|
+
|
|
5
|
+
/** The Keychain / Keystore, for the session's tokens. Small values only (about 2 KB). */
|
|
6
|
+
export const secureStorage: StorageAdapter = {
|
|
7
|
+
get: (key) => SecureStore.getItemAsync(key),
|
|
8
|
+
set: (key, value) => SecureStore.setItemAsync(key, value),
|
|
9
|
+
remove: (key) => SecureStore.deleteItemAsync(key),
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** A key-value table in SQLite, for everything else: the outbox, preferences. Not encrypted. */
|
|
13
|
+
export const appStorage: StorageAdapter = {
|
|
14
|
+
get: (key) => kv.getItem(key),
|
|
15
|
+
set: (key, value) => kv.setItem(key, value),
|
|
16
|
+
remove: (key) => kv.removeItem(key),
|
|
17
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { nativeTheme, THEME_NAMES, type ThemeName } from "@dapplelight/signet-tokens/native"
|
|
2
|
+
import { createContext, type ReactNode, useContext, useEffect, useMemo, useState } from "react"
|
|
3
|
+
import { useColorScheme } from "react-native"
|
|
4
|
+
import { appStorage } from "./storage"
|
|
5
|
+
|
|
6
|
+
/** Light and dark follow the phone unless the person picks one in Settings. */
|
|
7
|
+
export type Appearance = "system" | "light" | "dark"
|
|
8
|
+
export const APPEARANCES: Appearance[] = ["system", "light", "dark"]
|
|
9
|
+
export const DEFAULT_THEME: ThemeName = "__THEME__"
|
|
10
|
+
|
|
11
|
+
type Prefs = { theme: ThemeName; appearance: Appearance }
|
|
12
|
+
type ThemeValue = ReturnType<typeof nativeTheme> & {
|
|
13
|
+
prefs: Prefs
|
|
14
|
+
dark: boolean
|
|
15
|
+
setPrefs: (next: Partial<Prefs>) => void
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const KEY = "theme"
|
|
19
|
+
const Ctx = createContext<ThemeValue | null>(null)
|
|
20
|
+
|
|
21
|
+
function parse(raw: string | null): Prefs {
|
|
22
|
+
try {
|
|
23
|
+
const p = JSON.parse(raw ?? "{}") as Partial<Prefs>
|
|
24
|
+
return {
|
|
25
|
+
theme: p.theme && THEME_NAMES.includes(p.theme) ? p.theme : DEFAULT_THEME,
|
|
26
|
+
appearance: p.appearance && APPEARANCES.includes(p.appearance) ? p.appearance : "system",
|
|
27
|
+
}
|
|
28
|
+
} catch {
|
|
29
|
+
return { theme: DEFAULT_THEME, appearance: "system" }
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Every colour, radius and size comes from Signet's tokens for the chosen theme and mode. */
|
|
34
|
+
export function ThemeProvider({ children }: { children: ReactNode }) {
|
|
35
|
+
const scheme = useColorScheme()
|
|
36
|
+
const [prefs, setState] = useState<Prefs>({ theme: DEFAULT_THEME, appearance: "system" })
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
appStorage.get(KEY).then(
|
|
39
|
+
(raw) => setState(parse(raw)),
|
|
40
|
+
() => {},
|
|
41
|
+
)
|
|
42
|
+
}, [])
|
|
43
|
+
const value = useMemo<ThemeValue>(() => {
|
|
44
|
+
const mode = prefs.appearance === "system" ? scheme : prefs.appearance
|
|
45
|
+
return {
|
|
46
|
+
...nativeTheme(prefs.theme, mode),
|
|
47
|
+
prefs,
|
|
48
|
+
dark: mode === "dark",
|
|
49
|
+
setPrefs: (next) =>
|
|
50
|
+
setState((p) => {
|
|
51
|
+
const merged = { ...p, ...next }
|
|
52
|
+
void appStorage.set(KEY, JSON.stringify(merged))
|
|
53
|
+
return merged
|
|
54
|
+
}),
|
|
55
|
+
}
|
|
56
|
+
}, [prefs, scheme])
|
|
57
|
+
return <Ctx.Provider value={value}>{children}</Ctx.Provider>
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function useTheme(): ThemeValue {
|
|
61
|
+
const t = useContext(Ctx)
|
|
62
|
+
if (!t) throw new Error("useTheme needs <ThemeProvider> (app/_layout.tsx).")
|
|
63
|
+
return t
|
|
64
|
+
}
|