@team6083/ui 0.0.0-stage → 0.1.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.
Files changed (91) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +211 -2
  3. package/dist/blocks/api-error-message.d.ts +11 -0
  4. package/dist/blocks/api-error-message.d.ts.map +1 -0
  5. package/dist/blocks/api-error-message.js +29 -0
  6. package/dist/blocks/api-error-message.js.map +1 -0
  7. package/dist/blocks/app-commit.d.ts +35 -0
  8. package/dist/blocks/app-commit.d.ts.map +1 -0
  9. package/dist/blocks/app-commit.js +66 -0
  10. package/dist/blocks/app-commit.js.map +1 -0
  11. package/dist/blocks/app-shell.d.ts +99 -0
  12. package/dist/blocks/app-shell.d.ts.map +1 -0
  13. package/dist/blocks/app-shell.js +56 -0
  14. package/dist/blocks/app-shell.js.map +1 -0
  15. package/dist/blocks/config-error-page.d.ts +8 -0
  16. package/dist/blocks/config-error-page.d.ts.map +1 -0
  17. package/dist/blocks/config-error-page.js +21 -0
  18. package/dist/blocks/config-error-page.js.map +1 -0
  19. package/dist/blocks/data-parts.d.ts +57 -0
  20. package/dist/blocks/data-parts.d.ts.map +1 -0
  21. package/dist/blocks/data-parts.js +30 -0
  22. package/dist/blocks/data-parts.js.map +1 -0
  23. package/dist/blocks/data-table.d.ts +56 -0
  24. package/dist/blocks/data-table.d.ts.map +1 -0
  25. package/dist/blocks/data-table.js +93 -0
  26. package/dist/blocks/data-table.js.map +1 -0
  27. package/dist/blocks/member-avatar.d.ts +12 -0
  28. package/dist/blocks/member-avatar.d.ts.map +1 -0
  29. package/dist/blocks/member-avatar.js +14 -0
  30. package/dist/blocks/member-avatar.js.map +1 -0
  31. package/dist/blocks/no-access-page.d.ts +9 -0
  32. package/dist/blocks/no-access-page.d.ts.map +1 -0
  33. package/dist/blocks/no-access-page.js +15 -0
  34. package/dist/blocks/no-access-page.js.map +1 -0
  35. package/dist/blocks/page-parts.d.ts +41 -0
  36. package/dist/blocks/page-parts.d.ts.map +1 -0
  37. package/dist/blocks/page-parts.js +24 -0
  38. package/dist/blocks/page-parts.js.map +1 -0
  39. package/dist/blocks/runtime-config.d.ts +20 -0
  40. package/dist/blocks/runtime-config.d.ts.map +1 -0
  41. package/dist/blocks/runtime-config.js +39 -0
  42. package/dist/blocks/runtime-config.js.map +1 -0
  43. package/dist/blocks/status-page.d.ts +12 -0
  44. package/dist/blocks/status-page.d.ts.map +1 -0
  45. package/dist/blocks/status-page.js +8 -0
  46. package/dist/blocks/status-page.js.map +1 -0
  47. package/dist/chakra/color-mode.d.ts +26 -0
  48. package/dist/chakra/color-mode.d.ts.map +1 -0
  49. package/dist/chakra/color-mode.js +55 -0
  50. package/dist/chakra/color-mode.js.map +1 -0
  51. package/dist/chakra/toaster.d.ts +3 -0
  52. package/dist/chakra/toaster.d.ts.map +1 -0
  53. package/dist/chakra/toaster.js +11 -0
  54. package/dist/chakra/toaster.js.map +1 -0
  55. package/dist/chakra/toggle-tip.d.ts +15 -0
  56. package/dist/chakra/toggle-tip.d.ts.map +1 -0
  57. package/dist/chakra/toggle-tip.js +14 -0
  58. package/dist/chakra/toggle-tip.js.map +1 -0
  59. package/dist/chakra/tooltip.d.ts +12 -0
  60. package/dist/chakra/tooltip.d.ts.map +1 -0
  61. package/dist/chakra/tooltip.js +11 -0
  62. package/dist/chakra/tooltip.js.map +1 -0
  63. package/dist/i18n.d.ts +50 -0
  64. package/dist/i18n.d.ts.map +1 -0
  65. package/dist/i18n.js +107 -0
  66. package/dist/i18n.js.map +1 -0
  67. package/dist/index.d.ts +22 -0
  68. package/dist/index.d.ts.map +1 -0
  69. package/dist/index.js +28 -0
  70. package/dist/index.js.map +1 -0
  71. package/dist/lib/api-error.d.ts +25 -0
  72. package/dist/lib/api-error.d.ts.map +1 -0
  73. package/dist/lib/api-error.js +77 -0
  74. package/dist/lib/api-error.js.map +1 -0
  75. package/dist/lib/commit.d.ts +11 -0
  76. package/dist/lib/commit.d.ts.map +1 -0
  77. package/dist/lib/commit.js +27 -0
  78. package/dist/lib/commit.js.map +1 -0
  79. package/dist/lib/runtime-config.d.ts +33 -0
  80. package/dist/lib/runtime-config.d.ts.map +1 -0
  81. package/dist/lib/runtime-config.js +78 -0
  82. package/dist/lib/runtime-config.js.map +1 -0
  83. package/dist/provider.d.ts +11 -0
  84. package/dist/provider.d.ts.map +1 -0
  85. package/dist/provider.js +11 -0
  86. package/dist/provider.js.map +1 -0
  87. package/dist/theme.d.ts +2 -0
  88. package/dist/theme.d.ts.map +1 -0
  89. package/dist/theme.js +38 -0
  90. package/dist/theme.js.map +1 -0
  91. package/package.json +74 -4
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,212 @@
1
- # Temporary Holding Version
1
+ # @team6083/ui
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The team's Chakra UI v3 theme and the blocks every web app needs under the
4
+ team standard, so each app stops copying the same snippets and writing the
5
+ same pages.
6
+
7
+ | Export | What it is | Standard |
8
+ | --- | --- | --- |
9
+ | `system`, `UiProvider` | The team theme (a `brand` palette) on Chakra's defaults; Chakra + color mode + the package's locale | Frontend: UI 套件 |
10
+ | `Toaster`, `toaster`, `Tooltip`, `ToggleTip`, `InfoTip`, color mode | The Chakra snippets apps used to copy into `components/ui` | |
11
+ | `NoAccessPage`, `isMissingRole` | The one page every app shows on `403 missing_role` | Auth: 使用權角色 |
12
+ | `AppCommitProvider`, `AppOutdatedBanner`, `useAppOutdated`, `useCommitCheckedFetch` | Compares the build's commit with `X-App-Commit`, shows 「系統已更新,請重新載入」 and blocks writes | Frontend: 執行期設定與托管 |
13
+ | `createRuntimeConfig`, `ConfigErrorPage`, `loadRuntimeConfig`, `parseBaseConfig` | Loads `config.json` before anything else and shows the config error page when it fails (SPAs) | Frontend: 執行期設定與托管 |
14
+ | `readApiError`, `ApiError`, `useApiErrorMessage` | Reads the standard error body; localized text for the standard codes | Common: HTTP API 慣例 |
15
+ | `StatusPage` | The layout the pages above share, for an app's own not-found or error pages | |
16
+ | `AppShell`, `AccountMenu`, `LanguageMenu`, `SignOutMenuItem`, `SignInButton`, `BuildInfo`, `ColorModeButton` | The frame every app shares, from OverHours: app name, pages with icons, color mode (system, light, dark), an account menu each app fills with its own items, phone drawer, footer, update banner | [DESIGN.md](DESIGN.md) |
17
+ | `PageHeader`, `PageColumn`, `SectionHeader`, `EmptyState`, `MemberAvatar` | A page's one heading, a page's 5xl column inside a wide app, a section's title with its count and actions, the empty-list message, a person's avatar | [DESIGN.md](DESIGN.md) |
18
+
19
+ | `DataTable`, `FilterBar`, `SelectionBar`, `FactList`, `Totals` | Pages of records to pick from and act on: a table that becomes cards on a phone with row actions in a ⋯ menu, filters behind one button, a bar for acting on the picked rows, a record's facts, amounts that add up | [DESIGN.md](DESIGN.md): Data pages |
20
+
21
+ How apps should look, and what to use these for, is in [DESIGN.md](DESIGN.md).
22
+ OverHours is the reference. Every block is on show, live and with its code, in
23
+ the [demo](https://ui.cmsrobotics.org/).
24
+
25
+ Strings come in `en` and `zh-TW`. The package does not depend on next-intl or
26
+ react-i18next: pass the app's locale to `UiProvider`.
27
+
28
+ ## Install
29
+
30
+ The package is public on GitHub Packages, so any GitHub account can install
31
+ it. The registry still asks every client for a token, even for a public
32
+ package, so each place that runs `npm ci` needs one.
33
+
34
+ 1. In the app, next to `package.json`, add `.npmrc`:
35
+
36
+ ```ini
37
+ @team6083:registry=https://npm.pkg.github.com
38
+ //npm.pkg.github.com/:_authToken=${NODE_AUTH_TOKEN}
39
+ ```
40
+
41
+ 2. In the app's workflows, the built-in `GITHUB_TOKEN` is enough:
42
+
43
+ ```yaml
44
+ permissions:
45
+ contents: read
46
+ packages: read
47
+ # ...
48
+ - uses: actions/setup-node@v7
49
+ with:
50
+ registry-url: https://npm.pkg.github.com
51
+ scope: "@team6083"
52
+ - run: npm ci
53
+ env:
54
+ NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
55
+ ```
56
+
57
+ 3. On your machine, and on hosts that build the app (Cloudflare Pages or
58
+ Workers builds), set `NODE_AUTH_TOKEN` to a classic personal access token
59
+ with `read:packages`, from any GitHub account. With the GitHub CLI:
60
+ `gh auth refresh -h github.com -s read:packages`, then
61
+ `export NODE_AUTH_TOKEN=$(gh auth token)`.
62
+
63
+ 4. Dependabot needs the registry and a Dependabot secret holding such a token;
64
+ see the `registries` block in GitHub's Dependabot docs.
65
+
66
+ Then:
67
+
68
+ ```bash
69
+ npm install @team6083/ui @chakra-ui/react next-themes react-icons
70
+ ```
71
+
72
+ Then delete the app's own copies of `components/ui/provider.tsx`,
73
+ `color-mode.tsx`, `toaster.tsx`, `tooltip.tsx` and `toggle-tip.tsx`, and import
74
+ them from the package.
75
+
76
+ ## Use it in Next.js
77
+
78
+ ```tsx
79
+ // app/providers.tsx
80
+ "use client"
81
+
82
+ import { AppCommitProvider, AppOutdatedBanner, Toaster, UiProvider } from "@team6083/ui"
83
+
84
+ export function Providers({ locale, nonce, children }: { locale: string; nonce?: string; children: React.ReactNode }) {
85
+ return (
86
+ <UiProvider locale={locale} nonce={nonce}>
87
+ {/* Server actions don't expose response headers, so poll an endpoint that sends X-App-Commit. */}
88
+ <AppCommitProvider buildCommit={process.env.NEXT_PUBLIC_COMMIT_SHA} pollUrl="/api/healthz">
89
+ <AppOutdatedBanner />
90
+ {children}
91
+ <Toaster />
92
+ </AppCommitProvider>
93
+ </UiProvider>
94
+ )
95
+ }
96
+ ```
97
+
98
+ Disable save buttons with `useAppOutdated()`. When the server returns
99
+ `missing_role`, render `<NoAccessPage actions={<SignOutButton />} />`.
100
+
101
+ ## Use it in a Vite SPA
102
+
103
+ ```tsx
104
+ // src/config.ts
105
+ import { baseConfigIssues, ConfigError, createRuntimeConfig, type BaseRuntimeConfig } from "@team6083/ui"
106
+
107
+ export type AppConfig = BaseRuntimeConfig & { slackUrl: string }
108
+
109
+ function parseConfig(raw: unknown): AppConfig {
110
+ const issues = baseConfigIssues(raw)
111
+ const slackUrl = (raw as Partial<AppConfig> | null)?.slackUrl
112
+ if (typeof slackUrl !== "string" || slackUrl === "") issues.push("slackUrl is missing or empty")
113
+ if (issues.length > 0) throw new ConfigError(issues)
114
+ return raw as AppConfig
115
+ }
116
+
117
+ export const { RuntimeConfigProvider, useRuntimeConfig } = createRuntimeConfig({
118
+ parse: parseConfig,
119
+ basePath: import.meta.env.BASE_URL,
120
+ })
121
+ ```
122
+
123
+ ```tsx
124
+ // src/main.tsx
125
+ <UiProvider locale={i18n.language}>
126
+ <RuntimeConfigProvider>
127
+ <AppCommitProvider buildCommit={import.meta.env.VITE_COMMIT_SHA}>
128
+ <AppOutdatedBanner />
129
+ <App />
130
+ </AppCommitProvider>
131
+ </RuntimeConfigProvider>
132
+ </UiProvider>
133
+ ```
134
+
135
+ Build the API client on `useCommitCheckedFetch()`: it checks `X-App-Commit` on
136
+ every response and, once the app is outdated, rejects writes with an `ApiError`
137
+ whose code is `app_outdated`. Turn failed responses into errors with
138
+ `readApiError(response)` and show them with `useApiErrorMessage()`.
139
+
140
+ ## Frame
141
+
142
+ `AppShell` is a client component, and `renderLink` is a function, so a Next.js
143
+ server layout cannot pass it directly. Put the frame in a small client file and
144
+ hand it what the server knows (session, server actions):
145
+
146
+ ```tsx
147
+ // app/_components/frame.tsx
148
+ "use client"
149
+
150
+ import NextLink from "next/link"
151
+ import { usePathname } from "next/navigation"
152
+ import { Menu } from "@chakra-ui/react"
153
+ import { AccountMenu, AppShell, BuildInfo, SignOutMenuItem } from "@team6083/ui"
154
+ import { LuLogs, LuUser } from "react-icons/lu"
155
+
156
+ export function Frame({ user, signOut, children }: { user: { name: string; image?: string }; signOut: () => Promise<void>; children: React.ReactNode }) {
157
+ const pathname = usePathname()
158
+ return (
159
+ <AppShell
160
+ appName="OverHours"
161
+ orgName={process.env.NEXT_PUBLIC_ORG_NAME}
162
+ nav={[{ href: "/logs", label: "紀錄", icon: <LuLogs />, active: pathname.startsWith("/logs") }]}
163
+ renderLink={(href, children) => <NextLink href={href}>{children}</NextLink>}
164
+ account={
165
+ <AccountMenu name={user.name} image={user.image}>
166
+ {/* The app arranges its own items; the package offers LanguageMenu and SignOutMenuItem. */}
167
+ <Menu.Item value="profile" asChild>
168
+ <NextLink href="/profile"><LuUser />個人資料</NextLink>
169
+ </Menu.Item>
170
+ <SignOutMenuItem onSelect={() => void signOut()} />
171
+ </AccountMenu>
172
+ }
173
+ buildInfo={<BuildInfo commit={process.env.NEXT_PUBLIC_COMMIT_SHA} builtAt={process.env.NEXT_PUBLIC_BUILD_DATE} />}
174
+ >
175
+ {children}
176
+ </AppShell>
177
+ )
178
+ }
179
+ ```
180
+
181
+ With react-router: `renderLink={(href, children) => <Link to={href}>{children}</Link>}`.
182
+
183
+ ## Theme
184
+
185
+ `brand` is the primary color: use `colorPalette="brand"` for the app's own
186
+ emphasis (today, the current user, primary actions) instead of naming a hue.
187
+ The team has no brand color yet, so it points at Chakra's blue. Every other
188
+ token is Chakra's default until the team decides otherwise.
189
+
190
+ ## Versions
191
+
192
+ Until the first release every version is a prerelease. Each push to `main`
193
+ publishes `<version>-alpha.<n>` under the `next` dist-tag, `<version>` being
194
+ `package.json`'s without its prerelease part and `<n>` the run number, so
195
+ nobody edits version numbers. Apps pin the exact version and update with:
196
+
197
+ ```bash
198
+ npm install --save-exact @team6083/ui@next
199
+ ```
200
+
201
+ A release: set `version` in `package.json`, move the `CHANGELOG.md` entries
202
+ under it, merge, then push a matching tag (`git tag v0.1.0 && git push origin v0.1.0`).
203
+ The release workflow checks, builds and publishes it as `latest`.
204
+
205
+ ## Development
206
+
207
+ See [CONTRIBUTING.md](CONTRIBUTING.md). Configuration is in
208
+ [docs/configuration.md](docs/configuration.md).
209
+
210
+ ## License
211
+
212
+ [Apache-2.0](LICENSE)
@@ -0,0 +1,11 @@
1
+ import { type UiLocale } from "../i18n.js";
2
+ /**
3
+ * The text to show a user for an error. Standard codes get the package's
4
+ * localized text; an app's own codes come from `appMessages` when given.
5
+ * Otherwise English shows the server's fallback message and other locales a
6
+ * generic one, since the server's message is English only.
7
+ */
8
+ export declare function apiErrorMessage(error: unknown, locale: UiLocale, appMessages?: Record<string, string>): string;
9
+ /** apiErrorMessage in the locale given to <UiProvider>. */
10
+ export declare function useApiErrorMessage(appMessages?: Record<string, string>): (error: unknown) => string;
11
+ //# sourceMappingURL=api-error-message.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api-error-message.d.ts","sourceRoot":"","sources":["../../src/blocks/api-error-message.ts"],"names":[],"mappings":"AAIA,OAAO,EAA2B,KAAK,QAAQ,EAAE,MAAM,YAAY,CAAA;AAGnE;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAU9G;AAED,2DAA2D;AAC3D,wBAAgB,kBAAkB,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,MAAM,CAGnG"}
@@ -0,0 +1,29 @@
1
+ "use client";
2
+ import { useCallback } from "react";
3
+ import { useUiLocale, uiMessages } from "../i18n.js";
4
+ import { ApiError } from "../lib/api-error.js";
5
+ /**
6
+ * The text to show a user for an error. Standard codes get the package's
7
+ * localized text; an app's own codes come from `appMessages` when given.
8
+ * Otherwise English shows the server's fallback message and other locales a
9
+ * generic one, since the server's message is English only.
10
+ */
11
+ export function apiErrorMessage(error, locale, appMessages) {
12
+ const m = uiMessages[locale].apiError;
13
+ if (error instanceof TypeError)
14
+ return m.network_error ?? m.fallback;
15
+ if (!(error instanceof ApiError))
16
+ return m.fallback;
17
+ const known = appMessages?.[error.code] ?? m[error.code];
18
+ if (known)
19
+ return known;
20
+ if (locale === "en" && error.message)
21
+ return error.message;
22
+ return m.fallback;
23
+ }
24
+ /** apiErrorMessage in the locale given to <UiProvider>. */
25
+ export function useApiErrorMessage(appMessages) {
26
+ const locale = useUiLocale();
27
+ return useCallback((error) => apiErrorMessage(error, locale, appMessages), [locale, appMessages]);
28
+ }
29
+ //# sourceMappingURL=api-error-message.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api-error-message.js","sourceRoot":"","sources":["../../src/blocks/api-error-message.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,OAAO,EAAE,WAAW,EAAE,MAAM,OAAO,CAAA;AAEnC,OAAO,EAAE,WAAW,EAAE,UAAU,EAAiB,MAAM,YAAY,CAAA;AACnE,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAA;AAE9C;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAC,KAAc,EAAE,MAAgB,EAAE,WAAoC;IACpG,MAAM,CAAC,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAA;IAErC,IAAI,KAAK,YAAY,SAAS;QAAE,OAAO,CAAC,CAAC,aAAa,IAAI,CAAC,CAAC,QAAQ,CAAA;IACpE,IAAI,CAAC,CAAC,KAAK,YAAY,QAAQ,CAAC;QAAE,OAAO,CAAC,CAAC,QAAQ,CAAA;IAEnD,MAAM,KAAK,GAAG,WAAW,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;IACxD,IAAI,KAAK;QAAE,OAAO,KAAK,CAAA;IACvB,IAAI,MAAM,KAAK,IAAI,IAAI,KAAK,CAAC,OAAO;QAAE,OAAO,KAAK,CAAC,OAAO,CAAA;IAC1D,OAAO,CAAC,CAAC,QAAQ,CAAA;AACnB,CAAC;AAED,2DAA2D;AAC3D,MAAM,UAAU,kBAAkB,CAAC,WAAoC;IACrE,MAAM,MAAM,GAAG,WAAW,EAAE,CAAA;IAC5B,OAAO,WAAW,CAAC,CAAC,KAAc,EAAE,EAAE,CAAC,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAAA;AAC5G,CAAC"}
@@ -0,0 +1,35 @@
1
+ import { type ReactNode } from "react";
2
+ export interface AppCommitProviderProps {
3
+ children: ReactNode;
4
+ /** The commit this build came from; leave empty in dev builds to turn the check off. */
5
+ buildCommit: string | null | undefined;
6
+ /**
7
+ * A cheap same-origin endpoint that returns X-App-Commit (e.g. /api/healthz).
8
+ * Checked when the tab regains focus and every `pollIntervalMs`. Needed when
9
+ * the app's writes don't go through fetch (Next.js server actions).
10
+ */
11
+ pollUrl?: string;
12
+ pollIntervalMs?: number;
13
+ /**
14
+ * For apps that already detect a newer build themselves (e.g. in their API
15
+ * client): true shows the banner and turns on useAppOutdated.
16
+ */
17
+ outdated?: boolean;
18
+ }
19
+ /** Tracks whether the server runs a newer build than this page. */
20
+ export declare function AppCommitProvider({ children, buildCommit, pollUrl, pollIntervalMs, outdated: reported }: AppCommitProviderProps): import("react").JSX.Element;
21
+ /** True once the server is on a different build; disable save buttons with it. */
22
+ export declare function useAppOutdated(): boolean;
23
+ /**
24
+ * fetch that checks X-App-Commit on every response and, once the app is
25
+ * outdated, refuses writes (anything but GET/HEAD/OPTIONS) with an ApiError
26
+ * whose code is app_outdated. Use it as the base of the app's API client.
27
+ */
28
+ export declare function useCommitCheckedFetch(): typeof fetch;
29
+ export interface AppOutdatedBannerProps {
30
+ /** Defaults to reloading the page. */
31
+ onReload?: () => void;
32
+ }
33
+ /** Tells the user to reload once the app is outdated; renders nothing before that. */
34
+ export declare function AppOutdatedBanner({ onReload }: AppOutdatedBannerProps): import("react").JSX.Element | null;
35
+ //# sourceMappingURL=app-commit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"app-commit.d.ts","sourceRoot":"","sources":["../../src/blocks/app-commit.tsx"],"names":[],"mappings":"AAGA,OAAO,EAAwE,KAAK,SAAS,EAAE,MAAM,OAAO,CAAA;AAc5G,MAAM,WAAW,sBAAsB;IACrC,QAAQ,EAAE,SAAS,CAAA;IACnB,wFAAwF;IACxF,WAAW,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAA;IACtC;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB;AAED,mEAAmE;AACnE,wBAAgB,iBAAiB,CAAC,EAAE,QAAQ,EAAE,WAAW,EAAE,OAAO,EAAE,cAA2B,EAAE,QAAQ,EAAE,QAAgB,EAAE,EAAE,sBAAsB,+BA8BpJ;AAED,kFAAkF;AAClF,wBAAgB,cAAc,IAAI,OAAO,CAExC;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,IAAI,OAAO,KAAK,CAepD;AAED,MAAM,WAAW,sBAAsB;IACrC,sCAAsC;IACtC,QAAQ,CAAC,EAAE,MAAM,IAAI,CAAA;CACtB;AAED,sFAAsF;AACtF,wBAAgB,iBAAiB,CAAC,EAAE,QAAyC,EAAE,EAAE,sBAAsB,sCAiBtG"}
@@ -0,0 +1,66 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { Alert, Button } from "@chakra-ui/react";
4
+ import { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
5
+ import { useUiMessages } from "../i18n.js";
6
+ import { ApiError } from "../lib/api-error.js";
7
+ import { COMMIT_HEADER, commitsDiffer, isWriteMethod } from "../lib/commit.js";
8
+ const AppCommitContext = createContext({ outdated: false, observe: () => { } });
9
+ /** Tracks whether the server runs a newer build than this page. */
10
+ export function AppCommitProvider({ children, buildCommit, pollUrl, pollIntervalMs = 5 * 60_000, outdated: reported = false }) {
11
+ const [detected, setOutdated] = useState(false);
12
+ const outdated = detected || reported;
13
+ const observe = useCallback((response) => {
14
+ const headers = response instanceof Headers ? response : response.headers;
15
+ if (commitsDiffer(buildCommit, headers.get(COMMIT_HEADER)))
16
+ setOutdated(true);
17
+ }, [buildCommit]);
18
+ useEffect(() => {
19
+ if (!pollUrl || !buildCommit || outdated)
20
+ return;
21
+ const check = () => {
22
+ if (document.visibilityState !== "visible")
23
+ return;
24
+ fetch(pollUrl, { method: "HEAD", cache: "no-store" }).then(observe, () => { });
25
+ };
26
+ check();
27
+ const timer = window.setInterval(check, pollIntervalMs);
28
+ document.addEventListener("visibilitychange", check);
29
+ return () => {
30
+ window.clearInterval(timer);
31
+ document.removeEventListener("visibilitychange", check);
32
+ };
33
+ }, [pollUrl, pollIntervalMs, buildCommit, outdated, observe]);
34
+ const value = useMemo(() => ({ outdated, observe }), [outdated, observe]);
35
+ return _jsx(AppCommitContext.Provider, { value: value, children: children });
36
+ }
37
+ /** True once the server is on a different build; disable save buttons with it. */
38
+ export function useAppOutdated() {
39
+ return useContext(AppCommitContext).outdated;
40
+ }
41
+ /**
42
+ * fetch that checks X-App-Commit on every response and, once the app is
43
+ * outdated, refuses writes (anything but GET/HEAD/OPTIONS) with an ApiError
44
+ * whose code is app_outdated. Use it as the base of the app's API client.
45
+ */
46
+ export function useCommitCheckedFetch() {
47
+ const { outdated, observe } = useContext(AppCommitContext);
48
+ return useCallback(async (input, init) => {
49
+ const method = init?.method ?? (input instanceof Request ? input.method : "GET");
50
+ if (outdated && isWriteMethod(method)) {
51
+ throw new ApiError(0, "app_outdated", "The system has been updated; reload the page");
52
+ }
53
+ const response = await fetch(input, init);
54
+ observe(response);
55
+ return response;
56
+ }, [outdated, observe]);
57
+ }
58
+ /** Tells the user to reload once the app is outdated; renders nothing before that. */
59
+ export function AppOutdatedBanner({ onReload = () => window.location.reload() }) {
60
+ const outdated = useAppOutdated();
61
+ const m = useUiMessages().outdated;
62
+ if (!outdated)
63
+ return null;
64
+ return (_jsxs(Alert.Root, { status: "warning", position: "sticky", top: "0", zIndex: "banner", rounded: "none", "data-testid": "app-outdated-banner", children: [_jsx(Alert.Indicator, {}), _jsxs(Alert.Content, { children: [_jsx(Alert.Title, { children: m.title }), _jsx(Alert.Description, { children: m.description })] }), _jsx(Button, { size: "sm", variant: "solid", colorPalette: "orange", alignSelf: "center", onClick: onReload, children: m.reload })] }));
65
+ }
66
+ //# sourceMappingURL=app-commit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"app-commit.js","sourceRoot":"","sources":["../../src/blocks/app-commit.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAA;;AAEZ,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AAChD,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAkB,MAAM,OAAO,CAAA;AAE5G,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAA;AAC9C,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAQ9E,MAAM,gBAAgB,GAAG,aAAa,CAAwB,EAAE,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,GAAE,CAAC,EAAE,CAAC,CAAA;AAoBrG,mEAAmE;AACnE,MAAM,UAAU,iBAAiB,CAAC,EAAE,QAAQ,EAAE,WAAW,EAAE,OAAO,EAAE,cAAc,GAAG,CAAC,GAAG,MAAM,EAAE,QAAQ,EAAE,QAAQ,GAAG,KAAK,EAA0B;IACnJ,MAAM,CAAC,QAAQ,EAAE,WAAW,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;IAC/C,MAAM,QAAQ,GAAG,QAAQ,IAAI,QAAQ,CAAA;IAErC,MAAM,OAAO,GAAG,WAAW,CACzB,CAAC,QAA4B,EAAE,EAAE;QAC/B,MAAM,OAAO,GAAG,QAAQ,YAAY,OAAO,CAAC,CAAC,CAAC,QAAQ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