/* INCLUDE: "common.jsx" */ const nearDevGovGigsContractAccountId = props.nearDevGovGigsContractAccountId || (context.widgetSrc ?? "devgovgigs.near").split("/", 1)[0]; const nearDevGovGigsWidgetsAccountId = props.nearDevGovGigsWidgetsAccountId || (context.widgetSrc ?? "devgovgigs.near").split("/", 1)[0]; function widget(widgetName, widgetProps, key) { widgetProps = { ...widgetProps, nearDevGovGigsContractAccountId: props.nearDevGovGigsContractAccountId, nearDevGovGigsWidgetsAccountId: props.nearDevGovGigsWidgetsAccountId, referral: props.referral, }; return ( <Widget src={`${nearDevGovGigsWidgetsAccountId}/widget/gigs-board.${widgetName}`} props={widgetProps} key={key} /> ); } function href(widgetName, linkProps) { linkProps = { ...linkProps }; if (props.nearDevGovGigsContractAccountId) { linkProps.nearDevGovGigsContractAccountId = props.nearDevGovGigsContractAccountId; } if (props.nearDevGovGigsWidgetsAccountId) { linkProps.nearDevGovGigsWidgetsAccountId = props.nearDevGovGigsWidgetsAccountId; } if (props.referral) { linkProps.referral = props.referral; } const linkPropsQuery = Object.entries(linkProps) .filter(([_key, nullable]) => (nullable ?? null) !== null) .map(([key, value]) => `${key}=${value}`) .join("&"); return `/#/${nearDevGovGigsWidgetsAccountId}/widget/gigs-board.pages.${widgetName}${ linkPropsQuery ? "?" : "" }${linkPropsQuery}`; } /* END_INCLUDE: "common.jsx" */ /* INCLUDE: "core/lib/gui/form" */ const defaultFieldUpdate = ({ input, lastKnownValue, params: { arrayDelimiter }, }) => { switch (typeof input) { case "boolean": return input; case "object": return Array.isArray(input) && typeof lastKnownValue === "string" ? input.join(arrayDelimiter ?? ",") : input; case "string": return Array.isArray(lastKnownValue) ? input.split(arrayDelimiter ?? ",").map((string) => string.trim()) : input; default: { if ((input ?? null) === null) { switch (typeof lastKnownValue) { case "boolean": return !lastKnownValue; default: return lastKnownValue; } } else return input; } } }; const useForm = ({ initialValues, stateKey, uninitialized }) => { const initialFormState = { hasUnsubmittedChanges: false, values: initialValues ?? {}, }; const formState = state[stateKey] ?? null, isSynced = Struct.isEqual(formState?.values ?? {}, initialFormState.values); const formReset = () => State.update((lastKnownComponentState) => ({ ...lastKnownComponentState, [stateKey]: initialFormState, hasUnsubmittedChanges: false, })); const formUpdate = ({ path, via: customFieldUpdate, ...params }) => (fieldInput) => { const updatedValues = Struct.deepFieldUpdate( formState?.values ?? {}, { input: fieldInput?.target?.value ?? fieldInput, params, path, via: typeof customFieldUpdate === "function" ? customFieldUpdate : defaultFieldUpdate, } ); State.update((lastKnownComponentState) => ({ ...lastKnownComponentState, [stateKey]: { hasUnsubmittedChanges: !Struct.isEqual( updatedValues, initialFormState.values ), values: updatedValues, }, })); }; if ( !uninitialized && (formState === null || (!formState.hasUnsubmittedChanges && !isSynced)) ) { formReset(); } return { ...(formState ?? initialFormState), isSynced, reset: formReset, stateKey, update: formUpdate, }; }; /* END_INCLUDE: "core/lib/gui/form" */ /* INCLUDE: "core/lib/gui/attractable" */ const AttractableDiv = styled.div` box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075) !important; transition: box-shadow 0.6s; &:hover { box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15) !important; } `; const AttractableLink = styled.a` box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075) !important; transition: box-shadow 0.6s; &:hover { box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15) !important; } `; const AttractableImage = styled.img` box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075) !important; transition: box-shadow 0.6s; &:hover { box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15) !important; } `; /* END_INCLUDE: "core/lib/gui/attractable" */ /* INCLUDE: "core/lib/uuid" */ const uuid = () => [Date.now().toString(16)] .concat( Array.from( { length: 4 }, () => Math.floor(Math.random() * 0xffffffff) & 0xffffffff ).map((value) => value.toString(16)) ) .join("-"); const withUUIDIndex = (data) => { const id = uuid(); return Object.fromEntries([[id, { ...data, id }]]); }; /* END_INCLUDE: "core/lib/uuid" */ /* INCLUDE: "core/lib/struct" */ const Struct = { deepFieldUpdate: ( node, { input, params, path: [nextNodeKey, ...remainingPath], via: toFieldValue } ) => ({ ...node, [nextNodeKey]: remainingPath.length > 0 ? Struct.deepFieldUpdate( Struct.typeMatch(node[nextNodeKey]) || Array.isArray(node[nextNodeKey]) ? node[nextNodeKey] : { ...((node[nextNodeKey] ?? null) !== null ? { __archivedLeaf__: node[nextNodeKey] } : {}), }, { input, path: remainingPath, via: toFieldValue } ) : toFieldValue({ input, lastKnownValue: node[nextNodeKey], params, }), }), isEqual: (input1, input2) => Struct.typeMatch(input1) && Struct.typeMatch(input2) ? JSON.stringify(Struct.toOrdered(input1)) === JSON.stringify(Struct.toOrdered(input2)) : false, toOrdered: (input) => Object.keys(input) .sort() .reduce((output, key) => ({ ...output, [key]: input[key] }), {}), pick: (object, subsetKeys) => Object.fromEntries( Object.entries(object ?? {}).filter(([key, _]) => subsetKeys.includes(key) ) ), typeMatch: (input) => input !== null && typeof input === "object" && !Array.isArray(input), }; /* END_INCLUDE: "core/lib/struct" */ /* INCLUDE: "core/adapter/dev-hub" */ const devHubAccountId = props.nearDevGovGigsContractAccountId || (context.widgetSrc ?? "devgovgigs.near").split("/", 1)[0]; const DevHub = { get_root_members: () => Near.view(devHubAccountId, "get_root_members") ?? null, has_moderator: ({ account_id }) => Near.view(devHubAccountId, "has_moderator", { account_id }) ?? null, create_community: ({ inputs }) => Near.call(devHubAccountId, "create_community", { inputs }), get_community: ({ handle }) => Near.view(devHubAccountId, "get_community", { handle }) ?? null, get_account_community_permissions: ({ account_id, community_handle }) => Near.view(devHubAccountId, "get_account_community_permissions", { account_id, community_handle, }) ?? null, update_community: ({ handle, community }) => Near.call(devHubAccountId, "update_community", { handle, community }), delete_community: ({ handle }) => Near.call(devHubAccountId, "delete_community", { handle }), update_community_board: ({ handle, board }) => Near.call(devHubAccountId, "update_community_board", { handle, board }), update_community_github: ({ handle, github }) => Near.call(devHubAccountId, "update_community_github", { handle, github }), get_access_control_info: () => Near.view(devHubAccountId, "get_access_control_info") ?? null, get_all_authors: () => Near.view(devHubAccountId, "get_all_authors") ?? null, get_all_communities_metadata: () => Near.view(devHubAccountId, "get_all_communities_metadata") ?? null, get_all_labels: () => Near.view(devHubAccountId, "get_all_labels") ?? null, get_post: ({ post_id }) => Near.view(devHubAccountId, "get_post", { post_id }) ?? null, get_posts_by_author: ({ author }) => Near.view(devHubAccountId, "get_posts_by_author", { author }) ?? null, get_posts_by_label: ({ label }) => Near.view(nearDevGovGigsContractAccountId, "get_posts_by_label", { label, }) ?? null, useQuery: (name, params) => { const initialState = { data: null, error: null, isLoading: true }; const cacheState = useCache( () => Near.asyncView(devHubAccountId, ["get", name].join("_"), params ?? {}) .then((response) => ({ ...initialState, data: response ?? null, isLoading: false, })) .catch((error) => ({ ...initialState, error: props?.error ?? error, isLoading: false, })), JSON.stringify({ name, params }), { subscribe: true } ); return cacheState === null ? initialState : cacheState; }, }; /* END_INCLUDE: "core/adapter/dev-hub" */ const EditorSettings = { maxColumnsNumber: 20, }; const CompactContainer = styled.div` width: fit-content !important; max-width: 100%; `; const BoardConfigDefaults = { id: uuid(), kind: "github-view", columns: {}, dataTypesIncluded: { Issue: false, PullRequest: true }, description: "", repoURL: "", ticketState: "all", title: "", }; const GithubKanbanViewConfigurator = ({ communityHandle, link, permissions, }) => { State.init({ editingMode: "form", isActive: false, }); const community = DevHub.useQuery("community", { handle: communityHandle }); const boards = ((community?.data?.github ?? null) === null ? {} : JSON.parse(community.data.github) )?.kanbanBoards ?? {}; const board = Object.values(boards)[0] ?? {}; const errors = { noBoards: Object.keys(boards).length === 0, noCommunity: !community.isLoading && community.data === null, }; const form = useForm({ initialValues: board, stateKey: "board", uninitialized: errors.noBoards, }); const formToggle = (forcedState) => State.update((lastKnownState) => ({ ...lastKnownState, isActive: forcedState ?? !lastKnownState.isActive, })); const onEditingModeChange = ({ target: { value } }) => State.update((lastKnownState) => ({ ...lastKnownState, editingMode: value, })); const newViewInit = () => State.update((lastKnownState) => ({ ...lastKnownState, board: { hasUnsubmittedChanges: false, values: BoardConfigDefaults }, isActive: true, })); const columnsCreateNew = ({ lastKnownValue }) => Object.keys(lastKnownValue).length < EditorSettings.maxColumnsNumber ? { ...(lastKnownValue ?? {}), ...withUUIDIndex({ description: "", labelSearchTerms: [], title: "New column", }), } : lastKnownValue; const columnsDeleteById = (id) => ({ lastKnownValue }) => Object.fromEntries( Object.entries(lastKnownValue).filter(([columnId]) => columnId !== id) ); const onSubmit = () => DevHub.update_community_github({ handle: communityHandle, github: JSON.stringify({ kanbanBoards: { ...boards, [form.values.id]: { kind: "github-view", ...form.values }, }, }), }); const formElement = Object.keys(form.values).length > 0 ? ( <> <div className="d-flex gap-3 flex-column flex-lg-row"> {widget( "components.molecule.text-input", { className: "flex-shrink-0", key: `${form.values.id}-title`, label: "Title", onChange: form.update({ path: ["title"] }), placeholder: "NEAR Protocol NEPs", value: form.values.title, }, `${form.values.id}-title` )} {widget("components.molecule.text-input", { className: "w-100", key: `${form.values.id}-repoURL`, label: "GitHub repository URL", onChange: form.update({ path: ["repoURL"] }), placeholder: "https://github.com/example-org/example-repo", value: form.values.repoURL, })} </div> <div className="d-flex gap-3 flex-column flex-lg-row"> <CompactContainer className="d-flex gap-3 flex-column justify-content-start p-2"> <span className="d-inline-flex gap-2" id={`${form.values.id}-dataTypesIncluded`} > <i className="bi bi-ticket-fill" /> <span>Ticket type</span> </span> {Object.entries(form.values.dataTypesIncluded ?? {}).map( ([typeName, enabled]) => widget( "components.atom.toggle", { active: enabled, className: "w-100", key: typeName, label: typeName, onSwitch: form.update({ path: ["dataTypesIncluded", typeName], }), }, typeName ) )} </CompactContainer> <CompactContainer className="d-flex gap-3 flex-column justify-content-start p-2"> <span className="d-inline-flex gap-2" id={`${form.values.id}-dataTypesIncluded`} > <i class="bi bi-cone-striped" /> <span>Ticket state</span> </span> {widget("components.molecule.button-switch", { currentValue: form.values.ticketState, key: "ticketState", onChange: form.update({ path: ["ticketState"] }), options: [ { label: "All", value: "all" }, { label: "Open", value: "open" }, { label: "Closed", value: "closed" }, ], title: "Editing mode selection", })} </CompactContainer> {widget("components.molecule.text-input", { className: "w-100", inputProps: { className: "h-75" }, key: `${form.values.id}-description`, label: "Description", multiline: true, onChange: form.update({ path: ["description"] }), placeholder: "Latest NEAR Enhancement Proposals by status.", value: form.values.description, })} </div> <div className="d-flex align-items-center justify-content-between"> <span className="d-inline-flex gap-2 m-0"> <i className="bi bi-list-task" /> <span>Columns ( max. {EditorSettings.maxColumnsNumber} )</span> </span> </div> <div className="d-flex flex-column align-items-center gap-3"> {Object.values(form.values.columns ?? {}).map( ({ id, description, labelSearchTerms, title }) => ( <div className="d-flex gap-3 border border-secondary rounded-4 p-3 w-100" key={id} > <div className="d-flex flex-column gap-1 w-100"> {widget("components.molecule.text-input", { className: "flex-grow-1", key: `${form.values.id}-column-${id}-title`, label: "Title", onChange: form.update({ path: ["columns", id, "title"] }), placeholder: "👀 Review", value: title, })} {widget("components.molecule.text-input", { className: "flex-grow-1", key: `${form.values.id}-column-${id}-description`, label: "Description", onChange: form.update({ path: ["columns", id, "description"], }), placeholder: "NEPs that need a review by Subject Matter Experts.", value: description, })} {widget("components.molecule.text-input", { format: "comma-separated", key: `${form.values.id}-column-${title}-labelSearchTerms`, label: `Search terms for all the labels MUST be presented in included tickets`, onChange: form.update({ path: ["columns", id, "labelSearchTerms"], }), placeholder: "WG-, draft, review, proposal, ...", value: labelSearchTerms.join(", "), })} </div> <div className="d-flex flex-column gap-3 border-start p-3 pe-0" style={{ marginTop: -16, marginBottom: -16 }} > <button className="btn btn-outline-danger shadow" onClick={form.update({ path: ["columns"], via: columnsDeleteById(id), })} title="Delete column" > <i className="bi bi-trash-fill" /> </button> </div> </div> ) )} </div> </> ) : null; return community.data === null ? ( <div> {(community.isLoading && "Loading...") || (errors.noCommunity && `Community with handle ${communityHandle} not found.`)} </div> ) : ( <div className="d-flex flex-column gap-4"> {state.isActive && Object.keys(form.values).length > 0 ? ( <AttractableDiv className="d-flex flex-column gap-3 p-3 w-100 rounded-4"> <div className="d-flex align-items-center justify-content-between gap-3"> <h5 className="h5 d-inline-flex gap-2 m-0"> <i className="bi bi-gear-wide-connected" /> <span>GitHub board configuration</span> </h5> {widget("components.molecule.button-switch", { currentValue: state.editingMode, isHidden: true, key: "editingMode", onChange: onEditingModeChange, options: [ { label: "Form", value: "form" }, { label: "JSON", value: "JSON" }, ], title: "Editing mode selection", })} </div> {state.editingMode === "form" ? ( formElement ) : ( <div className="d-flex flex-column flex-grow-1 border-0 bg-transparent w-100"> <textarea className="form-control" disabled rows="12" type="text" value={JSON.stringify(form.values ?? {}, null, "\t")} /> </div> )} <div className="d-flex align-items-center justify-content-end gap-3"> <button className="btn shadow btn-outline-secondary d-inline-flex gap-2 me-auto" disabled={ Object.keys(form.values.columns).length >= EditorSettings.maxColumnsNumber } onClick={form.update({ path: ["columns"], via: columnsCreateNew, })} > <i className="bi bi-plus-lg" /> <span>New column</span> </button> <button className="btn btn-outline-danger border-0 d-inline-flex gap-2 align-items-center" onClick={() => formToggle(false)} style={{ width: "fit-content" }} > <span>Cancel</span> </button> <button disabled={!form.hasUnsubmittedChanges} className="btn shadow btn-success d-inline-flex gap-2 align-items-center" onClick={onSubmit} style={{ width: "fit-content" }} > <i className="bi bi-cloud-arrow-up-fill" /> <span>Save</span> </button> </div> </AttractableDiv> ) : null} {Object.keys(form.values).length > 0 ? ( widget("entity.workspace.github-view", { ...form.values, isUnderConfiguration: state.isActive, link, onConfigureClick: () => formToggle(true), permissions, }) ) : ( <div className="d-flex flex-column align-items-center justify-content-center gap-4" style={{ height: 384 }} > <h5 className="h5 d-inline-flex gap-2 m-0"> This community doesn't have a GitHub board </h5> {widget("components.molecule.button", { icon: { kind: "bootstrap-icon", variant: "bi-github" }, isHidden: !permissions.can_configure, label: "Create GitHub board", onClick: newViewInit, })} </div> )} </div> ); }; return GithubKanbanViewConfigurator(props);